1 /* 2 * Copyright (c) 2018, 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.*; 25 import java.lang.reflect.Method; 26 import java.util.concurrent.CountDownLatch; 27 28 import javax.swing.JButton; 29 import javax.swing.JFrame; 30 31 import sun.awt.DisplayChangedListener; 32 import sun.awt.SunToolkit; 33 34 /** 35 * @test 36 * @key headful 37 * @bug 8207070 38 * @modules java.desktop/sun.java2d 39 * java.desktop/sun.awt 40 */ 41 public final class DisplayChangesException { 42 43 private static boolean fail; 44 private static CountDownLatch go = new CountDownLatch(1); 45 46 static final class TestThread extends Thread { 47 48 private JFrame frame; 49 50 private TestThread(ThreadGroup tg, String threadName) { 51 super(tg, threadName); 52 } 53 54 public void run() { 55 try { 56 test(); 57 } catch (final Exception e) { 58 throw new RuntimeException(e); 59 } 60 } 61 62 private void test() throws Exception { 63 SunToolkit.createNewAppContext(); 64 EventQueue.invokeAndWait(() -> { 65 frame = new JFrame(); 66 final JButton b = new JButton(); 67 b.addPropertyChangeListener(evt -> { 68 if (!SunToolkit.isDispatchThreadForAppContext(b)) { 69 System.err.println("Wrong thread:" + currentThread()); 70 fail = true; 71 } 72 }); 73 frame.add(b); 74 frame.setSize(100, 100); 75 frame.setLocationRelativeTo(null); 76 frame.pack(); 77 }); 78 go.await(); 79 EventQueue.invokeAndWait(() -> { 80 frame.dispose(); 81 }); 82 } 83 } 84 85 public static void main(final String[] args) throws Exception { 86 ThreadGroup tg0 = new ThreadGroup("ThreadGroup0"); 87 ThreadGroup tg1 = new ThreadGroup("ThreadGroup1"); 88 89 TestThread t0 = new TestThread(tg0, "TestThread 0"); 90 TestThread t1 = new TestThread(tg1, "TestThread 1"); 91 92 t0.start(); 93 t1.start(); 94 Thread.sleep(1500); // Cannot use Robot.waitForIdle 95 testToolkit(); 96 Thread.sleep(1500); 97 testGE(); 98 Thread.sleep(1500); 99 go.countDown(); 100 101 if (fail) { 102 throw new RuntimeException(); 103 } 104 } 105 106 private static void testGE() { 107 Object ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 108 if (!(ge instanceof DisplayChangedListener)) { 109 return; 110 } 111 ((DisplayChangedListener) ge).displayChanged(); 112 } 113 114 private static void testToolkit() { 115 final Class toolkit; 116 try { 117 toolkit = Class.forName("sun.awt.windows.WToolkit"); 118 } catch (final ClassNotFoundException ignored) { 119 return; 120 } 121 try { 122 final Method displayChanged = toolkit.getMethod("displayChanged"); 123 displayChanged.invoke(Toolkit.getDefaultToolkit()); 124 } catch (final Exception e) { 125 e.printStackTrace(); 126 fail = true; 127 } 128 } 129 } 130