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