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