--- /dev/null 2014-02-04 15:17:31.000000000 +0400 +++ new/test/java/awt/event/AWTEvent/Container/ContainerAddedTest.java 2014-02-04 15:17:31.447052600 +0400 @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +import java.awt.AWTEvent; +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.Label; +import java.awt.Toolkit; +import java.awt.event.AWTEventListener; + +/** + * @test + * @summary Verify the proper number of ComponentEvents are delivered + * @library ../../../regtesthelpers + * @build VisibilityValidator + * @run main ContainerAddedTest + */ +public final class ContainerAddedTest { + static final int LOOP = 100; + + class ContainerEventCounter { + static final int TIMEOUT = 5000; // Wait up to five seconds + private int counter = 0; + private AWTEventListener listener = null; + + ContainerEventCounter() { + listener = e -> { + //System.out.println("AWTEvent: " + e); + doEvent(e); + }; + Toolkit.getDefaultToolkit().addAWTEventListener(listener, + AWTEvent.CONTAINER_EVENT_MASK); + } + + public void reset() { + counter = 0; + } + + public void dispose() { + Toolkit.getDefaultToolkit().removeAWTEventListener(listener); + } + + synchronized public boolean requireEvents(int total) { + long endtime = System.currentTimeMillis() + TIMEOUT; + try { + while (counter < total) { + if (System.currentTimeMillis() < endtime) { + wait(TIMEOUT); + } else { + break; + } + } + } catch (InterruptedException ix) { + } + if (counter != total) { + System.out.println( + "### ERROR: expected " + total + " events but received " + counter + " events."); + } + return (counter == total); + } + + synchronized void doEvent(AWTEvent e) { + if (e.getSource() instanceof Frame) { + counter++; + notify(); + } + } + } + + public void testContainerEvents01() throws Exception { + Frame frame = null; + ContainerEventCounter collector = null; + try { + // Thread.currentThread().setName( "ContainerAddedTest Thread" ); + + // Bring up a test frame + frame = new Frame("ContainerAddedTest "); + frame.setLayout(new FlowLayout()); + frame.setSize(300, 400); + VisibilityValidator.setVisibleAndConfirm(frame); + + // Add components and count the events + collector = new ContainerEventCounter(); + if (!collector.requireEvents(0)) { + throw new RuntimeException( + "Nothing added yet, no container events expected"); + } + for (int i = 0; i < LOOP; i++) { + frame.add(new Label("-" + i + "-")); + } + + if (!collector.requireEvents(LOOP)) { + throw new RuntimeException(LOOP + " events expected"); + } + + frame.validate(); + if (!frame.isValid()) { + throw new RuntimeException("The frame should be valid now"); + } + collector.reset(); + frame.removeAll(); + + if (!collector.requireEvents(LOOP)) { + throw new RuntimeException(LOOP + " events expected"); + } + + if (frame.isValid()) { + throw new RuntimeException("The frame should not be valid yet"); + } + frame.validate(); + if (!frame.isValid()) { + throw new RuntimeException("The frame should be valid now"); + } + + } finally { + if (frame != null) { + frame.setVisible(false); + frame.dispose(); + } + + if (collector != null) { + collector.dispose(); + } + + } + } + + public static void main(String[] args) throws Exception { + ContainerAddedTest test = new ContainerAddedTest(); + test.testContainerEvents01(); + } +}