Path: blob/master/test/jdk/java/awt/FullScreen/NoResizeEventOnDMChangeTest/NoResizeEventOnDMChangeTest.java
66645 views
/*1* Copyright (c) 2007, 2021, 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*/22/*23* @test24* @key headful25* @bug 664641126* @summary Tests that full screen window and its children receive resize27event when display mode changes28* @run main/othervm NoResizeEventOnDMChangeTest29* @run main/othervm -Dsun.java2d.d3d=false NoResizeEventOnDMChangeTest30*/3132import java.awt.Canvas;33import java.awt.Color;34import java.awt.Component;35import java.awt.DisplayMode;36import java.awt.EventQueue;37import java.awt.Frame;38import java.awt.Graphics;39import java.awt.GraphicsDevice;40import java.awt.GraphicsEnvironment;41import java.awt.Window;42import java.awt.event.ComponentAdapter;43import java.awt.event.ComponentEvent;44import java.awt.event.WindowAdapter;45import java.awt.event.WindowEvent;4647public class NoResizeEventOnDMChangeTest {48public static void main(String[] args) {49final GraphicsDevice gd = GraphicsEnvironment.50getLocalGraphicsEnvironment().getDefaultScreenDevice();5152if (!gd.isFullScreenSupported()) {53System.out.println("Full screen not supported, test passed");54return;55}5657DisplayMode dm = gd.getDisplayMode();58final DisplayMode dms[] = new DisplayMode[2];59for (DisplayMode dm1 : gd.getDisplayModes()) {60if (dm1.getWidth() != dm.getWidth() ||61dm1.getHeight() != dm.getHeight())62{63dms[0] = dm1;64break;65}66}67if (dms[0] == null) {68System.out.println("Test Passed: all DMs have same dimensions");69return;70}71dms[1] = dm;7273Frame f = new Frame() {74@Override75public void paint(Graphics g) {76g.setColor(Color.red);77g.fillRect(0, 0, getWidth(), getHeight());78g.setColor(Color.green);79g.drawRect(0, 0, getWidth()-1, getHeight()-1);80}81};82f.setUndecorated(true);83testFSWindow(gd, dms, f);8485Window w = new Window(f) {86@Override87public void paint(Graphics g) {88g.setColor(Color.magenta);89g.fillRect(0, 0, getWidth(), getHeight());90g.setColor(Color.cyan);91g.drawRect(0, 0, getWidth()-1, getHeight()-1);92}93};94testFSWindow(gd, dms, w);95System.out.println("Test Passed.");96}9798private static void testFSWindow(final GraphicsDevice gd,99final DisplayMode dms[],100final Window fsWin)101{102System.out.println("Testing FS window: "+fsWin);103Component c = new Canvas() {104@Override105public void paint(Graphics g) {106g.setColor(Color.blue);107g.fillRect(0, 0, getWidth(), getHeight());108g.setColor(Color.magenta);109g.drawRect(0, 0, getWidth()-1, getHeight()-1);110g.setColor(Color.red);111g.drawString("FS Window : " + fsWin, 50, 50);112DisplayMode dm =113getGraphicsConfiguration().getDevice().getDisplayMode();114g.drawString("Display Mode: " +115dm.getWidth() + "x" + dm.getHeight(), 50, 75);116}117};118fsWin.add("Center", c);119fsWin.addWindowListener(new WindowAdapter() {120@Override121public void windowClosing(WindowEvent e) {122fsWin.dispose();123if (fsWin.getOwner() != null) {124fsWin.getOwner().dispose();125}126}127});128129try {130EventQueue.invokeAndWait(new Runnable() {131public void run() {132gd.setFullScreenWindow(fsWin);133}134});135} catch (Exception ex) {}136137sleep(1000);138139final ResizeEventChecker r1 = new ResizeEventChecker();140final ResizeEventChecker r2 = new ResizeEventChecker();141142if (gd.isDisplayChangeSupported()) {143fsWin.addComponentListener(r1);144c.addComponentListener(r2);145for (final DisplayMode dm1 : dms) {146try {147EventQueue.invokeAndWait(new Runnable() {148public void run() {149System.err.printf("----------- Setting DM %dx%d:\n",150dm1.getWidth(), dm1.getHeight());151try {152Frame f = fsWin instanceof Frame ? (Frame) fsWin : (Frame) fsWin.getOwner();153DisplayMode oldMode = f.getGraphicsConfiguration().getDevice().getDisplayMode();154gd.setDisplayMode(dm1);155sleep(2000);156// Check if setting new display mode actually results in frame being157// placed onto display with different resolution.158DisplayMode newMode = f.getGraphicsConfiguration().getDevice().getDisplayMode();159if (oldMode.getWidth() != newMode.getWidth()160|| oldMode.getHeight() != newMode.getHeight()) {161r1.incDmChanges();162r2.incDmChanges();163} else {164System.out.println("Skipping this iteration. Details:");165System.out.println("Requested device = " + gd);166System.out.println("Actual device = " + f.getGraphicsConfiguration().getDevice());167}168} catch (IllegalArgumentException iae) {}169}170});171} catch (Exception ex) {}172for (int i = 0; i < 3; i++) {173fsWin.repaint();174sleep(1000);175}176}177fsWin.removeComponentListener(r1);178c.removeComponentListener(r2);179}180181try {182EventQueue.invokeAndWait(new Runnable() {183public void run() {184gd.setFullScreenWindow(null);185fsWin.dispose();186if (fsWin.getOwner() != null) {187fsWin.getOwner().dispose();188}189}190});191} catch (Exception ex) {}192193System.out.printf("FS Window: resizes=%d, dm changes=%d\n",194r1.getResizes(), r1.getDmChanges());195System.out.printf("Component: resizes=%d, dm changes=%d\n",196r2.getResizes(), r2.getDmChanges());197if (r1.getResizes() < r1.getDmChanges()) {198throw new RuntimeException("FS Window didn't receive all resizes!");199}200if (r2.getResizes() < r2.getDmChanges()) {201throw new RuntimeException("Component didn't receive all resizes!");202}203}204205static void sleep(long ms) {206long targetTime = System.currentTimeMillis() + ms;207do {208try {209Thread.sleep(targetTime - System.currentTimeMillis());210} catch (InterruptedException ex) {}211} while (System.currentTimeMillis() < targetTime);212}213214static class ResizeEventChecker extends ComponentAdapter {215int dmChanges;216int resizes;217218@Override219public synchronized void componentResized(ComponentEvent e) {220System.out.println("Received resize event for "+e.getSource());221resizes++;222}223public synchronized int getResizes() {224return resizes;225}226public synchronized void incDmChanges() {227dmChanges++;228}229public synchronized int getDmChanges() {230return dmChanges;231}232}233}234235236