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