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