Path: blob/master/test/jdk/java/awt/FullScreen/MultimonFullscreenTest/MultimonFullscreenTest.java
41153 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 5041219 5101561 5035272 5096011 5101712 5098624 819861326* @summary Here are a few assertions worth verification:27* - the fullscreen window is positioned at 0,028* - the fs window appears on the correct screen29* - if the exclusive FS mode is supported, no other widndow should30* overlap the fs window (including the taskbar).31* You could, however, alt+tab out of a fullscreen window, or at least32* minimize it (if you've entered the fs mode with a Window, you'll need33* to minimize the owner frame).34* Note that there may be issues with FS exclusive mode with ddraw and35* multiple fullscreen windows (one per device).36* - if display mode is supported that it did change37* - that the original display mode is restored once38* the ws window is disposed39* All of the above should work with and w/o DirectDraw40* (-Dsun.java2d.noddraw=true) on windows, and w/ and w/o opengl on X1141* (-Dsun.java2d.opengl=True).42* @run main/manual/othervm -Dsun.java2d.pmoffscreen=true MultimonFullscreenTest43* @run main/manual/othervm -Dsun.java2d.pmoffscreen=false MultimonFullscreenTest44* @run main/manual/othervm -Dsun.java2d.d3d=True MultimonFullscreenTest45* @run main/manual/othervm -Dsun.java2d.noddraw=true MultimonFullscreenTest46* @run main/manual/othervm MultimonFullscreenTest47*/4849import java.awt.Button;50import java.awt.Checkbox;51import java.awt.CheckboxGroup;52import java.awt.Color;53import java.awt.Component;54import java.awt.Dialog;55import java.awt.DisplayMode;56import java.awt.Font;57import java.awt.Frame;58import java.awt.Graphics;59import java.awt.GraphicsConfiguration;60import java.awt.GraphicsDevice;61import java.awt.GraphicsEnvironment;62import java.awt.GridLayout;63import java.awt.Panel;64import java.awt.Rectangle;65import java.awt.Window;66import java.awt.event.ActionEvent;67import java.awt.event.ActionListener;68import java.awt.event.ItemEvent;69import java.awt.event.ItemListener;70import java.awt.event.MouseAdapter;71import java.awt.event.MouseEvent;72import java.awt.event.WindowAdapter;73import java.awt.event.WindowEvent;74import java.awt.image.BufferStrategy;75import java.util.HashMap;76import java.util.Random;7778/**79*/8081public class MultimonFullscreenTest extends Frame implements ActionListener {82GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().83getDefaultScreenDevice();84GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment().85getScreenDevices();86HashMap<Button, GraphicsDevice> deviceMap;8788private static boolean dmChange = false;89static boolean setNullOnDispose = false;90static boolean useFSFrame = true;91static boolean useFSWindow = false;92static boolean useFSDialog = false;93static boolean useBS = false;94static boolean runRenderLoop = false;95static boolean addHWChildren = false;96static volatile boolean done = true;9798public MultimonFullscreenTest(String title) {99super(title);100addWindowListener(new WindowAdapter() {101public void windowClosing(WindowEvent e) {102System.exit(0);103}104});105Panel p = new Panel();106deviceMap = new HashMap<Button, GraphicsDevice>(gd.length);107int num = 0;108for (GraphicsDevice dev : gd) {109Button b;110if (dev == defDev) {111b = new Button("Primary screen: " + num);112System.out.println("Primary Dev : " + dev + " Bounds: " +113dev.getDefaultConfiguration().getBounds());114} else {115b = new Button("Secondary screen " + num);116System.out.println("Secondary Dev : " + dev + " Bounds: " +117dev.getDefaultConfiguration().getBounds());118}119b.addActionListener(this);120p.add(b);121deviceMap.put(b, dev);122num++;123}124add("South", p);125Panel p1 = new Panel();126p1.setLayout(new GridLayout(2,0));127Checkbox cb = new Checkbox("Change DM on entering FS");128cb.addItemListener(new ItemListener() {129public void itemStateChanged(ItemEvent e) {130dmChange = ((Checkbox)e.getSource()).getState();131}132});133p1.add(cb);134// cb = new Checkbox("Exit FS on window dispose");135// cb.addItemListener(new ItemListener() {136// public void itemStateChanged(ItemEvent e) {137// setNullOnDispose = ((Checkbox)e.getSource()).getState();138// }139// });140// p1.add(cb);141CheckboxGroup cbg = new CheckboxGroup();142cb = new Checkbox("Use Frame to enter FS", cbg, true);143cb.addItemListener(new ItemListener() {144public void itemStateChanged(ItemEvent e) {145useFSFrame = true;146useFSWindow = false;147useFSDialog = false;148}149});150p1.add(cb);151cb = new Checkbox("Use Window to enter FS", cbg, false);152cb.addItemListener(new ItemListener() {153public void itemStateChanged(ItemEvent e) {154useFSFrame = false;155useFSWindow = true;156useFSDialog = false;157}158});159p1.add(cb);160cb = new Checkbox("Use Dialog to enter FS", cbg, false);161cb.addItemListener(new ItemListener() {162public void itemStateChanged(ItemEvent e) {163useFSFrame = false;164useFSWindow = false;165useFSDialog = true;166}167});168p1.add(cb);169cb = new Checkbox("Run render loop");170cb.addItemListener(new ItemListener() {171public void itemStateChanged(ItemEvent e) {172runRenderLoop = ((Checkbox)e.getSource()).getState();173}174});175p1.add(cb);176cb = new Checkbox("Use BufferStrategy in render loop");177cb.addItemListener(new ItemListener() {178public void itemStateChanged(ItemEvent e) {179useBS = ((Checkbox)e.getSource()).getState();180}181});182p1.add(cb);183cb = new Checkbox("Add Children to FS window");184cb.addItemListener(new ItemListener() {185public void itemStateChanged(ItemEvent e) {186addHWChildren = ((Checkbox)e.getSource()).getState();187}188});189p1.add(cb);190add("North", p1);191192pack();193setVisible(true);194}195196Font f = new Font("Dialog", Font.BOLD, 24);197Random rnd = new Random();198public void renderDimensions(Graphics g, Rectangle rectWndBounds,199GraphicsConfiguration gc) {200g.setColor(new Color(rnd.nextInt(0xffffff)));201g.fillRect(0, 0, rectWndBounds.width, rectWndBounds.height);202203g.setColor(new Color(rnd.nextInt(0xffffff)));204Rectangle rectStrBounds;205206g.setFont(f);207208rectStrBounds = g.getFontMetrics().209getStringBounds(rectWndBounds.toString(), g).getBounds();210rectStrBounds.height += 30;211g.drawString(rectWndBounds.toString(), 50, rectStrBounds.height);212int oldHeight = rectStrBounds.height;213String isFSupported = "Exclusive Fullscreen mode supported: " +214gc.getDevice().isFullScreenSupported();215rectStrBounds = g.getFontMetrics().216getStringBounds(isFSupported, g).getBounds();217rectStrBounds.height += (10 + oldHeight);218g.drawString(isFSupported, 50, rectStrBounds.height);219220oldHeight = rectStrBounds.height;221String isDMChangeSupported = "Display Mode Change supported: " +222gc.getDevice().isDisplayChangeSupported();223rectStrBounds = g.getFontMetrics().224getStringBounds(isDMChangeSupported, g).getBounds();225rectStrBounds.height += (10 + oldHeight);226g.drawString(isDMChangeSupported, 50, rectStrBounds.height);227228oldHeight = rectStrBounds.height;229String usingBS = "Using BufferStrategy: " + useBS;230rectStrBounds = g.getFontMetrics().231getStringBounds(usingBS, g).getBounds();232rectStrBounds.height += (10 + oldHeight);233g.drawString(usingBS, 50, rectStrBounds.height);234235final String m_strQuitMsg = "Double-click to dispose FullScreen Window";236rectStrBounds = g.getFontMetrics().237getStringBounds(m_strQuitMsg, g).getBounds();238g.drawString(m_strQuitMsg,239(rectWndBounds.width - rectStrBounds.width) / 2,240(rectWndBounds.height - rectStrBounds.height) / 2);241242243}244245public void actionPerformed(ActionEvent ae) {246GraphicsDevice dev = deviceMap.get(ae.getSource());247System.err.println("Setting FS on device:"+dev);248final Window fsWindow;249250if (useFSWindow) {251fsWindow = new Window(this, dev.getDefaultConfiguration()) {252public void paint(Graphics g) {253renderDimensions(g, getBounds(),254this.getGraphicsConfiguration());255}256};257} else if (useFSDialog) {258fsWindow = new Dialog((Frame)null, "FS Dialog on device "+dev, false,259dev.getDefaultConfiguration());260fsWindow.add(new Component() {261public void paint(Graphics g) {262renderDimensions(g, getBounds(),263this.getGraphicsConfiguration());264}265});266} else {267fsWindow = new Frame("FS Frame on device "+dev,268dev.getDefaultConfiguration())269{270public void paint(Graphics g) {271renderDimensions(g, getBounds(),272this.getGraphicsConfiguration());273}274};275if (addHWChildren) {276fsWindow.add("South", new Panel() {277public void paint(Graphics g) {278g.setColor(Color.red);279g.fillRect(0, 0, getWidth(), getHeight());280}281});282fsWindow.add("North", new Button("Button, sucka!"));283}284}285fsWindow.addMouseListener(new MouseAdapter() {286public void mouseClicked(MouseEvent e) {287if (e.getClickCount() > 1) {288done = true;289fsWindow.dispose();290}291}292});293294fsWindow.addWindowListener(new WindowHandler());295dev.setFullScreenWindow(fsWindow);296if (dmChange && dev.isDisplayChangeSupported()) {297DisplayMode dms[] = dev.getDisplayModes();298DisplayMode myDM = null;299for (DisplayMode dm : dms) {300if (dm.getWidth() == 800 && dm.getHeight() == 600 &&301(dm.getBitDepth() >= 16 ||302dm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) &&303(dm.getRefreshRate() >= 60 ||304dm.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN))305{306myDM = dm;307break;308}309}310if (myDM != null) {311System.err.println("Setting Display Mode: "+312myDM.getWidth() + "x" + myDM.getHeight() + "x" +313myDM.getBitDepth() + "@" + myDM.getRefreshRate() +314"Hz on device" + dev);315dev.setDisplayMode(myDM);316} else {317System.err.println("Can't find suitable display mode.");318}319}320done = false;321if (runRenderLoop) {322Thread updateThread = new Thread(new Runnable() {323public void run() {324BufferStrategy bs = null;325if (useBS) {326fsWindow.createBufferStrategy(2);327bs = fsWindow.getBufferStrategy();328}329while (!done) {330if (useBS) {331Graphics g = bs.getDrawGraphics();332renderDimensions(g, fsWindow.getBounds(),333fsWindow.getGraphicsConfiguration());334bs.show();335} else {336fsWindow.repaint();337}338try {339Thread.sleep(1000);340} catch (InterruptedException e) {}341}342if (useBS) {343bs.dispose();344}345}346});347updateThread.start();348}349}350351public static void main(String args[]) {352for (String s : args) {353if (s.equalsIgnoreCase("-dm")) {354System.err.println("Do Display Change after entering FS mode");355dmChange = true;356} else if (s.equalsIgnoreCase("-usewindow")) {357System.err.println("Using Window to enter FS mode");358useFSWindow = true;359} else if (s.equalsIgnoreCase("-setnull")) {360System.err.println("Setting null FS window on dispose");361setNullOnDispose = true;362} else {363System.err.println("Usage: MultimonFullscreenTest " +364"[-dm][-usewindow][-setnull]");365}366367}368MultimonFullscreenTest fs =369new MultimonFullscreenTest("Test Full Screen");370}371class WindowHandler extends WindowAdapter {372public void windowClosing(WindowEvent we) {373done = true;374Window w = (Window)we.getSource();375if (setNullOnDispose) {376w.getGraphicsConfiguration().getDevice().setFullScreenWindow(null);377}378w.dispose();379}380}381}382383384