1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.DisplayMode;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsDevice;
  27 import java.util.ArrayList;
  28 import java.util.Random;
  29 
  30 import static java.awt.DisplayMode.REFRESH_RATE_UNKNOWN;
  31 
  32 /**
  33  * @test
  34  * @bug 6430607
  35  * @summary Test that we throw an exception for incorrect display modes
  36  * @author Dmitri.Trembovetski@Sun.COM area=FullScreen
  37  * @run main/othervm NonExistentDisplayModeTest
  38  * @run main/othervm -Dsun.java2d.noddraw=true NonExistentDisplayModeTest
  39  * @run main/othervm -Dsun.java2d.opengl=true NonExistentDisplayModeTest
  40  */
  41 public class NonExistentDisplayModeTest {
  42 
  43     public static void main(String[] args) {
  44         new NonExistentDisplayModeTest().start();
  45     }
  46 
  47     private void start() {
  48         Frame f = new Frame("Testing, please wait..");
  49         f.pack();
  50         GraphicsDevice gd = f.getGraphicsConfiguration().getDevice();
  51         if (!gd.isFullScreenSupported()) {
  52             System.out.println("Exclusive FS mode not supported, test passed.");
  53             f.dispose();
  54             return;
  55         }
  56 
  57         gd.setFullScreenWindow(f);
  58         if (!gd.isDisplayChangeSupported()) {
  59             System.out.println("DisplayMode change not supported, test passed.");
  60             f.dispose();
  61             return;
  62         }
  63 
  64         DisplayMode dms[] = gd.getDisplayModes();
  65         ArrayList<DisplayMode> dmList = new ArrayList<DisplayMode>(dms.length);
  66         for (DisplayMode dm : dms) {
  67             dmList.add(dm);
  68         }
  69 
  70         ArrayList<DisplayMode> nonExistentDms = createNonExistentDMList(dmList);
  71 
  72         for (DisplayMode dm : nonExistentDms) {
  73             boolean exThrown = false;
  74             try {
  75                 System.out.printf("Testing mode: (%4dx%4d) depth=%3d rate=%d\n",
  76                                   dm.getWidth(), dm.getHeight(),
  77                                   dm.getBitDepth(), dm.getRefreshRate());
  78                 gd.setDisplayMode(dm);
  79             } catch (IllegalArgumentException e) {
  80                 exThrown = true;
  81             }
  82             if (!exThrown) {
  83                 gd.setFullScreenWindow(null);
  84                 f.dispose();
  85                 throw new
  86                     RuntimeException("Failed: No exception thrown for dm "+dm);
  87             }
  88         }
  89         gd.setFullScreenWindow(null);
  90         f.dispose();
  91         System.out.println("Test passed.");
  92     }
  93 
  94     private static final Random rnd = new Random();
  95     private ArrayList<DisplayMode>
  96         createNonExistentDMList(ArrayList<DisplayMode> dmList)
  97     {
  98         ArrayList<DisplayMode> newList =
  99             new ArrayList<DisplayMode>(dmList.size());
 100         // vary one parameter at a time
 101         int param = 0;
 102         for (DisplayMode dm : dmList) {
 103             param = ++param % 3;
 104             switch (param) {
 105                 case 0: {
 106                     DisplayMode newDM = deriveSize(dm);
 107                     if (!dmList.contains(newDM)) {
 108                         newList.add(newDM);
 109                     }
 110                     break;
 111                 }
 112                 case 1: {
 113                     DisplayMode newDM = deriveDepth(dm);
 114                     if (!dmList.contains(newDM)) {
 115                         newList.add(newDM);
 116                     }
 117                     break;
 118                 }
 119                 case 2: {
 120                     if (dm.getRefreshRate() != REFRESH_RATE_UNKNOWN) {
 121                         DisplayMode newDM = deriveRR(dm);
 122                         if (!dmList.contains(newDM)) {
 123                             newList.add(newDM);
 124                         }
 125                     }
 126                     break;
 127                 }
 128             }
 129         }
 130         return newList;
 131     }
 132 
 133     private static DisplayMode deriveSize(DisplayMode dm) {
 134         int w = dm.getWidth() / 7;
 135         int h = dm.getHeight() / 3;
 136         return new DisplayMode(w, h, dm.getBitDepth(), dm.getRefreshRate());
 137     }
 138     private static DisplayMode deriveRR(DisplayMode dm) {
 139         return new DisplayMode(dm.getWidth(), dm.getHeight(),
 140                                dm.getBitDepth(), 777);
 141     }
 142     private static DisplayMode deriveDepth(DisplayMode dm) {
 143         int depth;
 144         if (dm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) {
 145             depth = 77;
 146         } else {
 147             depth = DisplayMode.BIT_DEPTH_MULTI;
 148         }
 149         return new DisplayMode(dm.getWidth(), dm.getHeight(),
 150                                depth, dm.getRefreshRate());
 151     }
 152 }