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