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 import java.awt.BorderLayout;
  25 import java.awt.Color;
  26 import java.awt.Container;
  27 import java.awt.Point;
  28 import java.awt.Robot;
  29 import java.awt.event.InputEvent;
  30 import java.awt.event.MouseAdapter;
  31 import java.awt.event.MouseEvent;
  32 import javax.swing.JFrame;
  33 import javax.swing.JInternalFrame;
  34 import javax.swing.SwingUtilities;
  35 import test.java.awt.regtesthelpers.Util;
  36 
  37 /**
  38  * AWT/Swing overlapping test with JInternalFrame being moved in GlassPane.
  39  * See <a href="https://bugs.openjdk.java.net/browse/JDK-6637655">JDK-6637655</a> and
  40  * <a href="https://bugs.openjdk.java.net/browse/JDK-6981919">JDK-6981919</a>.
  41  * <p>See base class for details.
  42  */
  43 /*
  44 @test
  45 @bug 6637655 6981919
  46 @summary Overlapping test for javax.swing.JScrollPane
  47 @author sergey.grinev@oracle.com: area=awt.mixing
  48 @library ../../regtesthelpers
  49 @modules java.desktop/sun.awt
  50          java.desktop/java.awt.peer
  51 @build Util
  52 @run main JGlassPaneMoveOverlapping
  53  */
  54 public class JGlassPaneMoveOverlapping extends OverlappingTestBase {
  55 
  56     private boolean lwClicked = true;
  57     private volatile Point lLoc;
  58     private volatile Point lLoc2;
  59 
  60     private JInternalFrame internalFrame;
  61     private JFrame frame = null;
  62     private volatile Point frameLoc;
  63 
  64     protected boolean performTest() {
  65 
  66         try {
  67             SwingUtilities.invokeAndWait(new Runnable() {
  68                 public void run() {
  69                     lLoc = internalFrame.getContentPane().getLocationOnScreen();
  70                     lLoc2 = lLoc.getLocation();
  71                     lLoc2.translate(0, internalFrame.getHeight());
  72                     frameLoc = frame.getLocationOnScreen();
  73                     frameLoc.translate(frame.getWidth()/2, 3);
  74                 }
  75             });
  76         } catch (Exception e) {
  77             e.printStackTrace();
  78             throw new RuntimeException("Where is internal frame?");
  79         }
  80 
  81         // run robot
  82         Robot robot = Util.createRobot();
  83         robot.setAutoDelay(ROBOT_DELAY);
  84 
  85         // force focus on JFrame (jtreg workaround)
  86         robot.mouseMove(frameLoc.x, frameLoc.y);
  87         Util.waitForIdle(robot);
  88         robot.mousePress(InputEvent.BUTTON1_MASK);
  89         robot.delay(50);
  90         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  91         Util.waitForIdle(robot);
  92 
  93 
  94         //slow move
  95         robot.mouseMove(lLoc.x +  25, lLoc.y - 5);
  96         robot.mousePress(InputEvent.BUTTON1_MASK);
  97         robot.delay(100);
  98         robot.mouseMove(lLoc.x +  45, lLoc.y - 5);
  99         robot.delay(100);
 100         robot.mouseMove(lLoc.x +  internalWidth - 75, lLoc.y - 5);
 101         robot.delay(100);
 102         robot.mouseMove(lLoc.x +  internalWidth - 55, lLoc.y - 5);
 103         robot.delay(100);
 104         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 105 
 106         Color c2 = robot.getPixelColor(lLoc.x +  internalWidth + 15, lLoc.y - 5);
 107         if (c2.equals(AWT_BACKGROUND_COLOR)) {
 108             error("Foreground frame is not drawn properly");
 109         }
 110         Color c = robot.getPixelColor(lLoc.x +  internalWidth - 95, lLoc.y + 5);
 111         robot.mouseMove(lLoc.x +  internalWidth - 95, lLoc.y + 5);
 112         System.out.println("color: " + c + " " + AWT_BACKGROUND_COLOR);
 113         if (!c.equals(AWT_BACKGROUND_COLOR) && currentAwtControl.getClass() != java.awt.Scrollbar.class) {
 114             error("Background AWT component is not drawn properly");
 115         }
 116 
 117         return true;
 118     }
 119 
 120     // {debugClassName = "Choice";}
 121 
 122     private static void error(String st) {
 123         //System.out.println(st);
 124         fail(st);
 125     }
 126 
 127     private static final int internalWidth = 200;
 128 
 129     @Override
 130     protected void prepareControls() {
 131         if(frame != null) {
 132             frame.setVisible(false);
 133         }
 134         frame = new JFrame("Glass Pane children test");
 135         frame.setLayout(null);
 136 
 137         Container contentPane = frame.getContentPane();
 138         contentPane.setLayout(new BorderLayout());
 139         super.propagateAWTControls(contentPane);
 140 
 141         Container glassPane = (Container) frame.getRootPane().getGlassPane();
 142         glassPane.setVisible(true);
 143         glassPane.setLayout(null);
 144 
 145         internalFrame = new JInternalFrame("Internal Frame", true);
 146         internalFrame.setBounds(50, 0, internalWidth, 100);
 147         internalFrame.setVisible(true);
 148         glassPane.add(internalFrame);
 149 
 150         internalFrame.addMouseListener(new MouseAdapter() {
 151 
 152             @Override
 153             public void mouseClicked(MouseEvent e) {
 154                 lwClicked = true;
 155             }
 156         });
 157 
 158         frame.setSize(400, 180);
 159         frame.setLocationRelativeTo(null);
 160         frame.setVisible(true);
 161     }
 162 
 163     // this strange plumbing stuff is required due to "Standard Test Machinery" in base class
 164     public static void main(String args[]) throws InterruptedException {
 165         if (System.getProperty("os.name").toLowerCase().contains("os x")) {
 166             System.out.println("Aqua L&F ignores setting color to component. Test passes on Mac OS X.");
 167             return;
 168         }
 169         instance = new JGlassPaneMoveOverlapping();
 170         OverlappingTestBase.doMain(args);
 171     }
 172 }