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  @library ../../regtesthelpers
  29  @build RobotUtilities
  30  @build Waypoint
  31  @run main AddOrder01
  32  */
  33 
  34 // classes necessary for this test
  35 import java.awt.*;
  36 import java.awt.event.ActionEvent;
  37 import java.awt.event.ActionListener;
  38 import test.java.awt.regtesthelpers.RobotUtilities;
  39 import test.java.awt.regtesthelpers.Waypoint;
  40 
  41 public class AddOrder01 {
  42 
  43     static volatile String result = null;
  44     static Waypoint waypoint = null;
  45 
  46     protected static void setUp() throws Exception {
  47         if (waypoint == null) {
  48             waypoint = new Waypoint();
  49         }
  50         else {
  51             waypoint.reset();
  52         }
  53     }
  54 
  55     public static void testAddOrder() throws Exception {
  56         doZOrder(false);
  57     }
  58 
  59     public static void testSetVisibleOrder() throws Exception {
  60         doZOrder(true);
  61     }
  62 
  63     private static void doZOrder(boolean shouldDoVisible) throws RuntimeException  {
  64         Frame frame = null;
  65         Panel panel;
  66         Dimension dimension;
  67 
  68         Button topButton;
  69         Button bottomButton;
  70         Button topBottomButton;
  71         Button topTopButton;
  72 
  73         // Final order should be (top to bottom) topTopButton, topButton, topBottomButton, panel, bottomButton
  74 
  75         try {
  76             frame = new Frame("Add Order Test");
  77 
  78             frame.setLayout(new FlowLayout());
  79             frame.setBounds(400, 100, 400, 150);
  80 
  81             panel = new Panel();
  82             panel.setBackground(Color.yellow);
  83             panel.setPreferredSize(new Dimension(400,150));
  84             frame.add(panel);
  85 
  86             frame.setVisible(true);
  87 
  88 
  89             // Test 1 - this should be under the panel
  90             result = null;
  91             bottomButton =    new Button("  Bottom Button  ");
  92             bottomButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { result = "bottomButton"; } });
  93             frame.add(bottomButton);
  94             bottomButton.setSize(bottomButton.getPreferredSize());
  95             bottomButton.setLocation(150, 35);
  96             if (shouldDoVisible) bottomButton.setVisible(false);
  97             if (shouldDoVisible) bottomButton.setVisible(true);
  98             RobotUtilities.click(bottomButton);
  99             pause(500); // hard to use waypoint for negative test, so just wait...
 100             if (result != null) {
 101                 throw new RuntimeException("bottomButton got clicked when it should NOT have!");
 102             }
 103 
 104             // Test 2 - this should be on top of the panel
 105             result = null;
 106             topButton =       new Button("   Top Button    ");
 107             topButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { result = "topButton";  waypoint.clear(); } });
 108             frame.add(topButton, 0);
 109             topButton.setSize(topButton.getPreferredSize());
 110             topButton.setLocation(150, 35);
 111             if (shouldDoVisible) topButton.setVisible(false);
 112             if (shouldDoVisible) topButton.setVisible(true);
 113             pause(300); // allow some time for events to propagate
 114             waypoint.reset();
 115             RobotUtilities.click(topButton);
 116             String expectTopMessage = "topButton did NOT get clicked when it should have; waypoint timed out.  Check double-click speed in control panel is not too slow. ";
 117             waypoint.requireClear(expectTopMessage); // wait for the waypoint to clear
 118             if (!("topButton".equals(result))) {
 119                 throw new RuntimeException(expectTopMessage);
 120             }
 121 
 122             // Test 3 - this should be between the panel and the topButton
 123             result = null;
 124             topBottomButton = new Button("Top Bottom Button");
 125             topBottomButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { result = "topBottomButton"; } });
 126             frame.add(topBottomButton, 1);
 127             topBottomButton.setSize(topBottomButton.getPreferredSize());
 128             topBottomButton.setLocation(150, 35);
 129             if (shouldDoVisible) topBottomButton.setVisible(false);
 130             if (shouldDoVisible) topBottomButton.setVisible(true);
 131             RobotUtilities.click(topBottomButton);
 132             pause(500);  // hard to use waypoint for negative test, so just wait...
 133             if ("topBottomButton".equals(result)) {
 134                 throw new RuntimeException("bottomButton got clicked when it should NOT have!");
 135             }
 136 
 137             // Test 4 - this should be on top of everything
 138             result = null;
 139             topTopButton =    new Button(" Top Top Button  ");
 140             topTopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { result = "topTopButton"; waypoint.clear(); } });
 141             frame.add(topTopButton, 0);
 142             topTopButton.setSize(topTopButton.getPreferredSize());
 143             topTopButton.setLocation(150, 35);
 144             if (shouldDoVisible) topTopButton.setVisible(false);
 145             if (shouldDoVisible) topTopButton.setVisible(true);
 146             pause(300); // allow some time for events to propagate
 147             waypoint.reset();
 148             RobotUtilities.click(topTopButton);
 149             String expectTopTopMessage = "topTopButton did NOT get clicked when it should have; waypoint timed out.  Check double-click speed in control panel is not too slow. ";            waypoint.requireClear(expectTopMessage); // wait for the waypoint to clear
 150             waypoint.requireClear(expectTopTopMessage); // wait for the waypoint to clear
 151             if (!("topTopButton".equals(result))) {
 152                 throw new RuntimeException(expectTopMessage);
 153             }
 154         }
 155         finally {
 156             if (frame != null) {
 157                 frame.setVisible(false);
 158                 frame.dispose();
 159             }
 160         }
 161     }
 162 
 163     public static void pause( int duration ) {
 164         try {
 165             Thread.sleep(duration);
 166         } catch(Throwable t) {}
 167     }
 168 
 169     public static void main (String[] args) throws RuntimeException {
 170         try {
 171             setUp();
 172             testAddOrder();
 173             setUp();
 174             testSetVisibleOrder();
 175         }
 176         catch (Exception e) {
 177             throw new RuntimeException(e.getMessage());
 178         }
 179     }
 180 }
 181 
 182