(original) (raw)

package info.joseluismartin.ui; import java.awt.Toolkit; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.SwingUtilities; import junit.framework.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; /** * Test that JDialogs multiplies the WINDOW_CLOSED event. * * @author Jose Luis Martin. */ @RunWith(BlockJUnit4ClassRunner.class) public class DialogTest { private static int N_LOOPS = 10; private static int N_DIALOGS = 2; @Test public void TesWithFrame() throws Exception { System.out.println("Run with owner Frame"); doTest(true); } @Test public void testWithoutFrame() throws Exception { System.out.println("Run without owner Frame"); doTest(false); } private void doTest(final boolean useFrame) throws InterruptedException { final Listener l = new Listener(); final JFrame f = new JFrame(); for (int i = 0; i < N_LOOPS; i++) { SwingUtilities.invokeLater(new Runnable() { public void run() { JDialog[] dialogs = new JDialog[N_DIALOGS]; for (int i = 0; i < N_DIALOGS; i++) { if (useFrame) { dialogs[i]= new JDialog(f); } else { dialogs[i] = new JDialog(); } dialogs[i].addWindowListener(l); } // Dipose all for (JDialog d : dialogs) d.dispose(); f.dispose(); } }); } // Wait until events are dispatched while (Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent() != null) Thread.sleep(100); System.out.println("Expected events: " + N_DIALOGS * N_LOOPS); System.out.println("Received events: " + l.getCount()); Assert.assertEquals(N_DIALOGS * N_LOOPS, l.getCount()); } } class Listener extends WindowAdapter { private int count = 0; public void windowClosed(WindowEvent e) { count++; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }