1 /*
   2  * Copyright (c) 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 com.sun.awt.AWTUtilities;
  26 import java.awt.Frame;
  27 import java.awt.Panel;
  28 import java.awt.Point;
  29 import java.awt.Rectangle;
  30 import java.awt.Robot;
  31 import java.awt.event.InputEvent;
  32 import java.awt.event.MouseAdapter;
  33 import java.awt.event.MouseEvent;
  34 import javax.swing.JButton;
  35 import javax.swing.SwingUtilities;
  36 
  37 /**
  38  * AWT/Swing overlapping test for opaque Swing components.
  39  * <p>This test verify if AWT components are drawn correctly under opaque components.
  40  * <p>See <a href="https://bugs.openjdk.java.net/browse/JDK-6776743">JDK-6776743</a> for details
  41  * <p>See base class for test info.
  42  */
  43 /*
  44 @test
  45 @bug 6776743
  46 @summary Opaque overlapping test for each AWT component
  47 @run main OpaqueOverlapping
  48  */
  49 public class OpaqueOverlapping extends OverlappingTestBase {
  50 
  51     {
  52         useClickValidation = false;
  53         failMessage = "Opacity test mismatchs";
  54         
  55         // CR 6994264 (Choice autohides dropdown on Solaris 10)
  56         skipClassNames = new String[] { "Choice" };
  57     }
  58     private String testSeq;
  59     private final static String checkSeq = "010000101";
  60     private Point heavyLoc;
  61     private JButton light;
  62     private Frame frame = null;
  63 
  64     protected void prepareControls() {
  65         testSeq = "";
  66         // Create components
  67         if(frame != null) {
  68             frame.setVisible(false);
  69         }
  70         frame = new Frame("OpaqueOverlapping mixing test");
  71         final Panel panel = new Panel();
  72         panel.setLayout(null);
  73 
  74         propagateAWTControls(panel);
  75 
  76         // Overlap the buttons
  77         currentAwtControl.setBounds(30, 30, 200, 200);
  78 
  79         light = new JButton("  LW Button  ");
  80         light.setBounds(10, 10, 50, 50);
  81 
  82         // Put the components into the frame
  83         panel.add(light);
  84         frame.add(panel);
  85         frame.setBounds(50, 50, 400, 400);
  86         frame.setVisible(true);
  87 
  88         currentAwtControl.addMouseListener(new MouseAdapter() {
  89             @Override
  90             public void mouseClicked(MouseEvent e) {
  91                 panel.setComponentZOrder(light, 0);
  92                 frame.validate();
  93                 testSeq = testSeq + "0";
  94             }
  95         });
  96         light.addActionListener(new java.awt.event.ActionListener() {
  97             public void actionPerformed(java.awt.event.ActionEvent e) {
  98                 panel.setComponentZOrder(currentAwtControl, 0);
  99                 frame.validate();
 100                 testSeq = testSeq + "1";
 101             }
 102         });
 103     }
 104 
 105     @Override
 106     protected boolean performTest() {
 107         try {
 108             SwingUtilities.invokeAndWait(new Runnable() {
 109                 public void run() {
 110                     heavyLoc = currentAwtControl.getLocationOnScreen();
 111                 }
 112             });
 113         } catch (Exception e) {
 114         }        
 115         Robot robot = Util.createRobot();
 116         robot.setAutoDelay(ROBOT_DELAY);
 117 
 118         Util.waitForIdle(robot);
 119 
 120         // Move the mouse pointer to the position where both
 121         //    components overlap
 122         robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);
 123 
 124         // Now perform the click at this point for 9 times
 125         // In the middle of the process toggle the opaque
 126         // flag value.
 127         for (int i = 0; i < 9; ++i) {
 128             if (i == 3) {
 129                 AWTUtilities.setComponentMixingCutoutShape(light,
 130                         new Rectangle());
 131             }
 132             if (i == 6) {
 133                 AWTUtilities.setComponentMixingCutoutShape(light,
 134                         null);
 135             }
 136 
 137             robot.mousePress(InputEvent.BUTTON1_MASK);
 138             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 139             Util.waitForIdle(robot);
 140 
 141             if (currentAwtControl.getClass() == java.awt.Choice.class && i != 1 && i != 6 && i != 8) {
 142                 // due to the fact that Choice doesn't get mouseClicked event if its dropdown is shown
 143                 robot.mousePress(InputEvent.BUTTON1_MASK);
 144                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 145                 Util.waitForIdle(robot);
 146             }
 147         }
 148 
 149         Util.waitForIdle(robot);
 150 
 151         boolean result = testSeq.equals(checkSeq);
 152         if (!result) {
 153             System.err.println("Expected: " + checkSeq);
 154             System.err.println("Observed: " + testSeq);
 155         }
 156         return result;
 157     }
 158 
 159     // this strange plumbing stuff is required due to "Standard Test Machinery" in base class
 160     public static void main(String args[]) throws InterruptedException {
 161         instance = new OpaqueOverlapping();
 162         OverlappingTestBase.doMain(args);
 163     }
 164 }