1 /*
   2  * Copyright (c) 2006, 2007, 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 /**
  25  * @test
  26  * @bug 6358034 6568560
  27  * @key headful
  28  * @summary Tests that no exception is thrown when display mode is changed
  29  *          externally
  30  * @compile UninitializedDisplayModeChangeTest.java DisplayModeChanger.java
  31  * @run main/othervm UninitializedDisplayModeChangeTest
  32  * @run main/othervm -Dsun.java2d.opengl=true UninitializedDisplayModeChangeTest
  33  */
  34 
  35 import java.awt.EventQueue;
  36 import java.awt.Toolkit;
  37 import java.io.BufferedReader;
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.io.InputStream;
  41 import java.io.InputStreamReader;
  42 import java.lang.reflect.InvocationTargetException;
  43 
  44 public class UninitializedDisplayModeChangeTest {
  45     public static volatile boolean failed = false;
  46     public static void main(String[] args) {
  47         Toolkit.getDefaultToolkit();
  48         try {
  49             EventQueue.invokeAndWait(new Runnable() {
  50                 public void run() {
  51                     Thread.currentThread().setDefaultUncaughtExceptionHandler(
  52                         new Thread.UncaughtExceptionHandler() {
  53                             public void uncaughtException(Thread t,
  54                                                           Throwable e)
  55                             {
  56                                 System.err.println("Exception Detected:");
  57                                 e.printStackTrace();
  58                                 failed = true;
  59                             }
  60                         }
  61                     );
  62                 }
  63             });
  64         } catch (InterruptedException ex) {
  65             ex.printStackTrace();
  66         } catch (InvocationTargetException ex) {
  67             ex.printStackTrace();
  68         }
  69 
  70         Process childProc;
  71         String classPath = System.getProperty("java.class.path" , ".");
  72         String cmd = new String(System.getProperty("java.home") +
  73                 File.separator +
  74                 "bin" +
  75                 File.separator +
  76                 "java -cp " + classPath +
  77                 " DisplayModeChanger");
  78 
  79         System.out.println("Launching the display mode changer process");
  80         System.out.println("cmd="+cmd);
  81         try {
  82             childProc = Runtime.getRuntime().exec(cmd);
  83             StreamProcessor err =
  84                 new StreamProcessor("stderr", childProc.getErrorStream());
  85             StreamProcessor out =
  86                 new StreamProcessor("stdout", childProc.getInputStream());
  87             err.start();
  88             out.start();
  89 
  90             childProc.waitFor();
  91         } catch (Exception e) {
  92             failed = true;
  93             e.printStackTrace();
  94         }
  95 
  96         if (failed) {
  97             throw new RuntimeException("Test Failed: exception detected");
  98         }
  99         System.out.println("Test Passed.");
 100     }
 101 
 102     static class StreamProcessor extends Thread {
 103         InputStream is;
 104         String inputType;
 105         StreamProcessor(String inputType, InputStream is) {
 106             this.inputType = inputType;
 107             this.is = is;
 108         }
 109         public void run() {
 110             try {
 111                 InputStreamReader isr = new InputStreamReader(is);
 112                 BufferedReader br = new BufferedReader(isr);
 113                 String line = null;
 114                 while ( (line = br.readLine()) != null) {
 115                     System.out.println("Display Changer "+inputType+
 116                                        " output > " + line);
 117                 }
 118             } catch (IOException ioe) {
 119                 ioe.printStackTrace();
 120             }
 121         }
 122     }
 123 }