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 Zorder test
  27  @summary com.apple.junit.java.awt.Container
  28  @run main ValidateTest
  29  */
  30 
  31 // classes necessary for this test
  32 
  33 import java.awt.BorderLayout;
  34 import java.awt.Button;
  35 import java.awt.Color;
  36 import java.awt.FlowLayout;
  37 import java.awt.Frame;
  38 import java.awt.Panel;
  39 import java.awt.event.ComponentEvent;
  40 import java.awt.event.ComponentAdapter;
  41 
  42 public class ValidateTest {
  43     private static Frame frame = null;
  44     public static boolean wasMoved = false;
  45     public static boolean wasResized = false;
  46 
  47     public static void testValidation() throws Exception {
  48         try {
  49             // Bring up a test frame
  50             frame = new Frame( "Test Frame");
  51 
  52             frame.setLayout(new BorderLayout());
  53             frame.setLocation(75, 75);
  54             frame.setSize(300, 200);
  55 
  56             Panel  pan = new Panel(new FlowLayout());
  57             pan.setBackground(new Color(255, 0, 0));
  58             frame.add("Center", pan);
  59 
  60             frame.setVisible(true);
  61 
  62             Button button = new Button("A Button");
  63             pan.add(button);
  64             pan.validate();
  65 
  66             Thread.sleep(1000);
  67 
  68             // For some reason, the validation behavior only kicks in _after_ the first component is added
  69             Button button2 = new Button("A Button");
  70             button2.addComponentListener(new ComponentAdapter() {
  71                 public void componentMoved(ComponentEvent e) {
  72                     wasMoved = true;
  73                 }
  74                 public void componentResized(ComponentEvent e) {
  75                     wasResized = true;
  76                 }
  77             });
  78             pan.add(button2);
  79             pan.validate();
  80 
  81             Thread.sleep(1000);
  82 
  83             if (!wasResized) {
  84                 System.out.println("Component wasn't resized.");
  85                 throw new RuntimeException("Component wasn't resized.");
  86             }
  87             if (!wasMoved) {
  88                 throw new RuntimeException("Component wasn't moved.");
  89             }
  90         }
  91         finally {
  92             if (frame != null) {
  93                 frame.setVisible(false);
  94                 frame.dispose();
  95                 frame = null;
  96             }
  97         }
  98     }
  99 
 100     public static void main (String[] args) throws RuntimeException {
 101         try {
 102             testValidation();
 103         }
 104         catch (Exception e) {
 105             throw new RuntimeException(e.getMessage());
 106         }
 107     }
 108 }
 109