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