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