Path: blob/master/test/jdk/java/awt/FullScreen/NonExistentDisplayModeTest/NonExistentDisplayModeTest.java
41153 views
/*1* Copyright (c) 2006, 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*/2223import java.awt.DisplayMode;24import java.awt.Frame;25import java.awt.GraphicsDevice;26import java.util.ArrayList;27import java.util.Random;2829import static java.awt.DisplayMode.REFRESH_RATE_UNKNOWN;3031/**32* @test33* @key headful34* @bug 6430607 819861335* @summary Test that we throw an exception for incorrect display modes36* @author [email protected] area=FullScreen37* @run main/othervm NonExistentDisplayModeTest38* @run main/othervm -Dsun.java2d.noddraw=true NonExistentDisplayModeTest39*/40public class NonExistentDisplayModeTest {4142public static void main(String[] args) {43new NonExistentDisplayModeTest().start();44}4546private void start() {47Frame f = new Frame("Testing, please wait..");48f.pack();49GraphicsDevice gd = f.getGraphicsConfiguration().getDevice();50if (!gd.isFullScreenSupported()) {51System.out.println("Exclusive FS mode not supported, test passed.");52f.dispose();53return;54}5556gd.setFullScreenWindow(f);57if (!gd.isDisplayChangeSupported()) {58System.out.println("DisplayMode change not supported, test passed.");59f.dispose();60return;61}6263DisplayMode dms[] = gd.getDisplayModes();64ArrayList<DisplayMode> dmList = new ArrayList<DisplayMode>(dms.length);65for (DisplayMode dm : dms) {66dmList.add(dm);67}6869ArrayList<DisplayMode> nonExistentDms = createNonExistentDMList(dmList);7071for (DisplayMode dm : nonExistentDms) {72boolean exThrown = false;73try {74System.out.printf("Testing mode: (%4dx%4d) depth=%3d rate=%d\n",75dm.getWidth(), dm.getHeight(),76dm.getBitDepth(), dm.getRefreshRate());77gd.setDisplayMode(dm);78} catch (IllegalArgumentException e) {79exThrown = true;80}81if (!exThrown) {82gd.setFullScreenWindow(null);83f.dispose();84throw new85RuntimeException("Failed: No exception thrown for dm "+dm);86}87}88gd.setFullScreenWindow(null);89f.dispose();90System.out.println("Test passed.");91}9293private static final Random rnd = new Random();94private ArrayList<DisplayMode>95createNonExistentDMList(ArrayList<DisplayMode> dmList)96{97ArrayList<DisplayMode> newList =98new ArrayList<DisplayMode>(dmList.size());99// vary one parameter at a time100int param = 0;101for (DisplayMode dm : dmList) {102param = ++param % 3;103switch (param) {104case 0: {105DisplayMode newDM = deriveSize(dm);106if (!dmList.contains(newDM)) {107newList.add(newDM);108}109break;110}111case 1: {112DisplayMode newDM = deriveDepth(dm);113if (!dmList.contains(newDM)) {114newList.add(newDM);115}116break;117}118case 2: {119if (dm.getRefreshRate() != REFRESH_RATE_UNKNOWN) {120DisplayMode newDM = deriveRR(dm);121if (!dmList.contains(newDM)) {122newList.add(newDM);123}124}125break;126}127}128}129return newList;130}131132private static DisplayMode deriveSize(DisplayMode dm) {133int w = dm.getWidth() / 7;134int h = dm.getHeight() / 3;135return new DisplayMode(w, h, dm.getBitDepth(), dm.getRefreshRate());136}137private static DisplayMode deriveRR(DisplayMode dm) {138return new DisplayMode(dm.getWidth(), dm.getHeight(),139dm.getBitDepth(), 777);140}141private static DisplayMode deriveDepth(DisplayMode dm) {142int depth;143if (dm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) {144depth = 77;145} else {146depth = DisplayMode.BIT_DEPTH_MULTI;147}148return new DisplayMode(dm.getWidth(), dm.getHeight(),149depth, dm.getRefreshRate());150}151}152153154