1 /* 2 * Copyright (c) 2013, 2016, 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 @key headful 27 @bug 8015500 28 @summary DisposeAction multiplies the WINDOW_CLOSED event. 29 @author jlm@joseluismartin.info 30 @run main WindowClosedEventOnDispose 31 */ 32 33 34 import java.awt.Toolkit; 35 import java.awt.event.WindowAdapter; 36 import java.awt.event.WindowEvent; 37 38 import javax.swing.JDialog; 39 import javax.swing.JFrame; 40 import javax.swing.SwingUtilities; 41 42 /** 43 * WindowClosedEventOnDispose.java 44 * Summary: tests that Window don't multiplies the WINDOW_CLOSED event 45 * on dispose. 46 * Test fails if fire more events that expected; 47 */ 48 public class WindowClosedEventOnDispose { 49 50 private static int N_LOOPS = 5; 51 private static int N_DIALOGS = 2; 52 53 public static void main(String args[]) throws Exception { 54 tesWithFrame(); 55 testWithoutFrame(); 56 testHidenChildDispose(); 57 testHidenWindowDispose(); 58 } 59 60 /** 61 * Test WINDOW_CLOSED event received by a dialog 62 * that have a owner window. 63 * @throws Exception 64 */ 65 public static void tesWithFrame() throws Exception { 66 doTest(true); 67 } 68 69 /** 70 * Test WINDOW_CLOSED event received by a dialog 71 * that don't have a owner window. 72 * @throws Exception 73 */ 74 public static void testWithoutFrame() throws Exception { 75 System.out.println("Run without owner Frame"); 76 doTest(false); 77 } 78 79 /** 80 * Test if a dialog that has never been shown fire 81 * the WINDOW_CLOSED event on parent dispose(). 82 * @throws Exception 83 */ 84 public static void testHidenChildDispose() throws Exception { 85 JFrame f = new JFrame(); 86 JDialog dlg = new JDialog(f); 87 Listener l = new Listener(); 88 dlg.addWindowListener(l); 89 f.dispose(); 90 waitEvents(); 91 92 assertEquals(0, l.getCount()); 93 } 94 95 /** 96 * Test if a dialog fire the WINDOW_CLOSED event 97 * on parent dispose(). 98 * @throws Exception 99 */ 100 public static void testVisibleChildParentDispose() throws Exception { 101 JFrame f = new JFrame(); 102 JDialog dlg = new JDialog(f); 103 Listener l = new Listener(); 104 dlg.addWindowListener(l); 105 dlg.setVisible(true); 106 f.dispose(); 107 waitEvents(); 108 109 assertEquals(1, l.getCount()); 110 } 111 112 /** 113 * Test if a Window that has never been shown fire the 114 * WINDOW_CLOSED event on dispose() 115 */ 116 public static void testHidenWindowDispose() throws Exception { 117 JFrame f = new JFrame(); 118 Listener l = new Listener(); 119 f.addWindowListener(l); 120 f.dispose(); 121 waitEvents(); 122 123 assertEquals(0, l.getCount()); 124 } 125 126 /** 127 * Test if a JDialog receive the correct number 128 * of WINDOW_CLOSED_EVENT 129 * @param useFrame true if use a owner frame 130 * @throws Exception 131 */ 132 private static void doTest(final boolean useFrame) throws Exception { 133 final Listener l = new Listener(); 134 final JFrame f = new JFrame(); 135 136 for (int i = 0; i < N_LOOPS; i++) { 137 138 SwingUtilities.invokeLater(new Runnable() { 139 140 public void run() { 141 JDialog[] dialogs = new JDialog[N_DIALOGS]; 142 for (int i = 0; i < N_DIALOGS; i++) { 143 if (useFrame) { 144 dialogs[i]= new JDialog(f); 145 } 146 else { 147 dialogs[i] = new JDialog(); 148 } 149 150 dialogs[i].addWindowListener(l); 151 dialogs[i].setVisible(true); 152 } 153 154 // Dispose all 155 for (JDialog d : dialogs) 156 d.dispose(); 157 158 f.dispose(); 159 } 160 }); 161 } 162 163 waitEvents(); 164 165 assertEquals(N_DIALOGS * N_LOOPS, l.getCount()); 166 } 167 168 private static void waitEvents() throws InterruptedException { 169 // Wait until events are dispatched 170 while (Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent() != null) 171 Thread.sleep(100); 172 } 173 174 /** 175 * @param expected the expected value 176 * @param real the real value 177 */ 178 private static void assertEquals(int expected, int real) throws Exception { 179 if (expected != real) { 180 throw new Exception("Expected events: " + expected + " Received Events: " + real); 181 } 182 } 183 184 } 185 186 /** 187 * Listener to count events 188 */ 189 class Listener extends WindowAdapter { 190 191 private volatile int count = 0; 192 193 public void windowClosed(WindowEvent e) { 194 count++; 195 } 196 197 public int getCount() { 198 return count; 199 } 200 201 public void setCount(int count) { 202 this.count = count; 203 } 204 }