Path: blob/master/test/jdk/java/awt/GraphicsDevice/DisplayModes/CycleDMImage.java
41154 views
/*1* Copyright (c) 2003, 2020, 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*/2223import java.awt.Color;24import java.awt.Component;25import java.awt.DisplayMode;26import java.awt.Frame;27import java.awt.Graphics;28import java.awt.GraphicsConfiguration;29import java.awt.GraphicsDevice;30import java.awt.GraphicsEnvironment;31import java.awt.Image;32import java.awt.Rectangle;33import java.awt.Robot;34import java.awt.Transparency;35import java.awt.Window;36import java.awt.event.KeyEvent;37import java.awt.event.KeyListener;38import java.awt.image.BufferedImage;39import java.util.ArrayList;4041/**42* @test43* @bug 4836241 6364134 8232200 825213344* @key headful45* @summary verify that images are restored correctly after display mode46* switches and that no other rendering or crash problems occur47* @run main/timeout=500 CycleDMImage48*/49public class CycleDMImage extends Component implements Runnable, KeyListener {50/**51* This test cycles through all available display modes, waiting after52* each call to setDisplayMode() to ensure that the new one is active53* before proceeding on to the next one. The Component is filled with54* a green background color and then compatible images of all 355* Transparency types are copied to the screen. The results of these56* operations are checked (using Robot) and the test fails if any of the57* rendering is wrong in any of the DisplayModes. The purpose of this58* test is to ensure that display mode switches do not cause problems59* with image restoration (or other rendering operations).60*/61boolean painted = false;62boolean earlyExit = false;63Image rImage = null, wImage = null, bImage = null;64int imgSize = 10;65static Robot robot = null;66volatile static boolean done = false;67static String errorMessage = null;6869public synchronized void paint(Graphics g) {70if (!painted) {71painted = true;72(new Thread(this)).start();73}74if (rImage == null) {75GraphicsConfiguration gc = getGraphicsConfiguration();76rImage = gc.createCompatibleImage(imgSize, imgSize);77wImage = gc.createCompatibleImage(imgSize, imgSize,78Transparency.BITMASK);79bImage = gc.createCompatibleImage(imgSize, imgSize,80Transparency.TRANSLUCENT);81Graphics imgGraphics = rImage.getGraphics();82imgGraphics.setColor(Color.red);83imgGraphics.fillRect(0, 0, imgSize, imgSize);84imgGraphics = wImage.getGraphics();85imgGraphics.setColor(Color.white);86imgGraphics.fillRect(0, 0, imgSize, imgSize);87imgGraphics = bImage.getGraphics();88imgGraphics.setColor(Color.blue);89imgGraphics.fillRect(0, 0, imgSize, imgSize);90}91g.setColor(Color.green);92g.fillRect(0, 0, getWidth(), getHeight());93g.drawImage(rImage, 0, 0, this);94g.drawImage(wImage, imgSize, 0, this);95g.drawImage(bImage, imgSize*2, 0, this);96g.drawImage(rImage, 0, getHeight()-imgSize, null);97g.drawImage(rImage, getWidth()-imgSize, getHeight()-imgSize, null);98g.drawImage(rImage, getWidth()-imgSize, 0, null);99}100101static void delay(long ms) {102try {103Thread.sleep(ms);104} catch (Exception e) {}105}106107public boolean checkResult(DisplayMode dm) {108Rectangle bounds = getGraphicsConfiguration().getBounds();109int pixels[] = new int[imgSize * 4];110BufferedImage clientPixels =111robot.createScreenCapture(new Rectangle(bounds.x, bounds.y,112imgSize*4, 1));113clientPixels.getRGB(0, 0, imgSize * 4, 1, pixels, 0, getWidth());114// Now check the results. We expect: imgSize blocks of r/w/b/g115int colors[] = {0xffff0000, 0xffffffff, 0xff0000ff, 0xff00ff00};116for (int color = 0; color < 4; ++color) {117for (int i = 0; i < imgSize; ++i) {118int pixelIndex = imgSize * color + i;119if (pixels[pixelIndex] != colors[color]) {120errorMessage = "\n DisplayMode(" +121dm.getWidth() + " x " +122dm.getHeight() + " x " +123dm.getBitDepth() + "bpp x " +124dm.getRefreshRate() +125")\n Pixel " + i +126": Expected " +127Integer.toHexString(colors[color]) +128", got " +129Integer.toHexString(pixels[pixelIndex]) +130" at " + i;131return false;132}133}134}135return true;136}137138boolean displayModesEqual(DisplayMode dm1, DisplayMode dm2) {139if (dm1.equals(dm2)) {140return true;141}142// not enough - check whether the modes are equal except for143// refreshRate, if either mode has REFRESH_RATE_UNKNOWN144// value for this parameter145if (dm1.getWidth() != dm2.getWidth() ||146dm1.getHeight() != dm2.getHeight() ||147dm1.getBitDepth() != dm2.getBitDepth())148{149// core parameters must match150return false;151}152// Now we know that w1 == w2, h1 == h2, and d1 == d2; must be the153// case that the refresh rates do not match.154// Is either one REFRESH_RATE_UNKNOWN?155if (dm1.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN ||156dm2.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN)157{158return true;159}160return false;161}162163public void run() {164GraphicsDevice gd = getGraphicsConfiguration().getDevice();165gd.setFullScreenWindow((Window) getParent());166// First, delay a bit just to let the fullscreen window167// settle down before switching display modes168robot.waitForIdle();169delay(1000);170171if (!gd.isDisplayChangeSupported()) {172System.err.println("Display change is not supported,"+173" the test is considered passed.");174finished();175return;176}177178// We are really only interested in unique w/h/d resolutions179// and it would be nice to skip the myriad of refresh rate180// varations, so let us construct a subset that contains181// only those DisplayModes with unique w/h/d values182// Also, due to a current bug (4837228), we should skip the183// 24-bit depths since attempting to create bitmask-transparent184// ddraw images can cause the test to crash (we should change this185// test to include that depth when the bug is fixed).186ArrayList<DisplayMode> dmSubset = new ArrayList<>();187for (final DisplayMode dm : gd.getDisplayModes()) {188boolean skip = false;189for (final DisplayMode dmUnique : dmSubset) {190int bitDepth = dm.getBitDepth();191int width = dm.getWidth();192int height = dm.getHeight();193if (bitDepth == 24 || width <= 800 || height <= 600 ||194(dmUnique.getWidth() == width &&195dmUnique.getHeight() == height &&196dmUnique.getBitDepth() == bitDepth)) {197skip = true;198break;199}200}201if (!skip) {202dmSubset.add(dm);203}204}205206// Now, cycle through the display modes one-by-one. For207// each new display mode, delay until we detect that the208// new mode == the current mode. Then delay an additional209// second (to allow any repaints to occur)210211for (DisplayMode newDM : dmSubset) {212gd.setDisplayMode(newDM);213while (!displayModesEqual(newDM, gd.getDisplayMode())) {214delay(100);215}216// Delay another few seconds after the new display mode is active217delay(4000);218219// Check the rendering results220if (!checkResult(newDM)) {221finished();222return;223}224225// Escape out if requested by the user226if (earlyExit) {227System.out.println("Exiting test early, by request");228System.exit(0);229}230}231232// Done with test; if we got here, we passed233System.out.println("Passed");234finished();235}236237public static void finished() {238synchronized (CycleDMImage.class) {239done = true;240CycleDMImage.class.notify();241}242}243244/**245* KeyListener methods; these provide a way for a user to escape out of246* a potentially lengthy test.247*/248249public void keyTyped(KeyEvent e) {250}251252public void keyPressed(KeyEvent e) {253if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {254earlyExit = true;255}256}257258public void keyReleased(KeyEvent e) {259}260261public static void main(String args[]) throws Exception {262robot = new Robot();263GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();264for (final GraphicsDevice gd: ge.getScreenDevices()) {265if (!gd.isFullScreenSupported()) {266System.err.println("FullScreen mode is not supported,"+267" the test is considered passed.");268continue;269}270done = false;271DisplayMode currentDM = gd.getDisplayMode();272Frame frame = new Frame(gd.getDefaultConfiguration());273try {274frame.setSize(400, 400);275frame.setUndecorated(true);276CycleDMImage comp = new CycleDMImage();277frame.addKeyListener(comp);278frame.add(comp);279frame.setVisible(true);280// Sleep awaiting frame disposal281synchronized (CycleDMImage.class) {282while (!done) {283try {284CycleDMImage.class.wait(100);285} catch (InterruptedException e) {286}287}288}289} finally {290gd.setDisplayMode(currentDM);291gd.setFullScreenWindow(null);292frame.dispose();293}294if (errorMessage != null) {295throw new RuntimeException(errorMessage);296}297// delay a bit just to let the fullscreen window disposing complete298// before switching to next display299delay(10000);300}301}302}303304305