1 /* 2 * Copyright (c) 2011, 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 @summary Verify the proper number of ComponentEvents are delivered 27 @summary com.apple.junit.java.awt.Event; 28 @library ../../../regtesthelpers 29 @build VisibilityValidator 30 @run junit ContainerAddedTest 31 */ 32 33 import junit.framework.*; 34 35 import java.awt.*; 36 import java.awt.event.AWTEventListener; 37 38 import test.java.awt.regtesthelpers.VisibilityValidator; 39 40 public class ContainerAddedTest extends TestCase { 41 static final int LOOP = 100; 42 43 class ContainerEventCounter { 44 static final int TIMEOUT = 5000; // Wait up to five seconds 45 private int counter = 0; 46 private AWTEventListener listener = null; 47 48 public ContainerEventCounter() { 49 listener = new AWTEventListener() { 50 public void eventDispatched( AWTEvent e ) { 51 // System.out.println("AWTEvent: " + e); 52 doEvent(e); 53 } 54 }; 55 Toolkit.getDefaultToolkit().addAWTEventListener( listener, AWTEvent.CONTAINER_EVENT_MASK ); 56 } 57 58 public void dispose() { 59 Toolkit.getDefaultToolkit().removeAWTEventListener( listener ); 60 } 61 62 synchronized public boolean requireEvents( int total ) { 63 long endtime = System.currentTimeMillis() + TIMEOUT; 64 try { 65 while (counter < total) { 66 if (System.currentTimeMillis() < endtime) { 67 wait( TIMEOUT ); 68 } 69 else { 70 break; 71 } 72 } 73 } 74 catch (InterruptedException ix) { 75 } 76 if (counter != total) { 77 System.out.println("### ERROR: expected " + total + " events but received " + counter + " events."); 78 } 79 return (counter == total); 80 } 81 82 synchronized void doEvent(AWTEvent e) { 83 counter++; 84 notify(); 85 } 86 } 87 88 public void testContainerEvents01() throws Exception { 89 Frame frame = null; 90 ContainerEventCounter collector = null; 91 try { 92 // Thread.currentThread().setName( "ContainerAddedTest Thread" ); 93 94 // Bring up a test frame 95 frame = new Frame( "ContainerAddedTest " ); 96 frame.setLayout( new FlowLayout() ); 97 frame.setSize( 300, 400 ); 98 VisibilityValidator.setVisibleAndConfirm(frame); 99 100 // Add components and count the events 101 collector = new ContainerEventCounter(); 102 assertTrue( "Nothing added yet, no container events expected", collector.requireEvents( 0 ) ); 103 for (int i = 0; i < LOOP; i++) { 104 frame.add( new Label( "-" + i + "-" ) ); 105 } 106 assertTrue( LOOP + " events expected", collector.requireEvents( LOOP ) ); 107 108 // ### todo -- add a test which collects events with eventListener 109 110 /* 111 // Add these tests after Greg's fix is done 112 113 assertFalse("The frame should not be valid yet", frame.isValid()); 114 115 Thread.sleep(500); // <-- should be smarter than just waiting .5 seconds here... 116 */ 117 118 frame.validate(); 119 assertTrue( "The frame should be valid now", frame.isValid() ); 120 } 121 finally { 122 if (frame != null) { 123 frame.setVisible( false ); 124 frame.dispose( ); 125 } 126 127 if (collector != null) { 128 collector.dispose(); 129 } 130 131 } 132 } 133 134 public static Test suite() { 135 return new TestSuite( ContainerAddedTest.class); 136 } 137 138 public static void main (String[] args) throws RuntimeException { 139 TestResult tr = junit.textui.TestRunner.run(suite()); 140 if((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 141 throw new RuntimeException("### Unexpected JUnit errors or failures."); 142 } 143 } 144 }