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 
  25 import java.awt.BorderLayout;
  26 import java.awt.Dimension;
  27 import java.awt.GridLayout;
  28 import java.awt.Point;
  29 import java.awt.Robot;
  30 import java.awt.event.InputEvent;
  31 import java.awt.event.MouseAdapter;
  32 import java.awt.event.MouseEvent;
  33 import javax.swing.BorderFactory;
  34 import javax.swing.JButton;
  35 import javax.swing.JComponent;
  36 import javax.swing.JFrame;
  37 import javax.swing.JPanel;
  38 import javax.swing.JScrollPane;
  39 import javax.swing.SwingUtilities;
  40 
  41 /**
  42  * AWT/Swing overlapping test for viewport
  43  * <p>This test verify if AWT components are drawn correctly being partially shown through viewport
  44  * <p>See <a href="http://monaco.sfbay.sun.com/detail.jsf?cr=6778882">CR6778882</a> for details
  45  * <p>See base class for test info.
  46  */
  47 /*
  48 @test
  49 @bug 6778882
  50 @summary Viewport overlapping test for each AWT component
  51 @author sergey.grinev@oracle.com: area=awt.mixing
  52 @run main ViewportOverlapping
  53  */
  54 public class ViewportOverlapping extends OverlappingTestBase {
  55 
  56     private volatile int frameClicked;
  57     private Point hLoc;
  58     private Point vLoc;
  59     private Point testLoc;
  60     private Point resizeLoc;
  61 
  62     private JFrame f;
  63     private JPanel p;
  64     private JButton b;
  65     private JScrollPane scrollPane;
  66 
  67     protected void prepareControls() {
  68         p = new JPanel(new GridLayout(0, 1));
  69         propagateAWTControls(p);
  70         b = new JButton("Space extender");
  71         p.add(b);
  72         p.setPreferredSize(new Dimension(500, 500));
  73         scrollPane = new JScrollPane(p);
  74 
  75         f = new JFrame();
  76         f.addMouseListener(new MouseAdapter() {
  77 
  78             @Override
  79             public void mouseClicked(MouseEvent e) {
  80                 frameClicked++;
  81             }
  82         });
  83         f.getContentPane().add(scrollPane, BorderLayout.CENTER);
  84         ((JComponent) f.getContentPane()).setBorder(
  85                 BorderFactory.createEmptyBorder(50, 50, 50, 50));
  86         f.setSize(400, 400);
  87         f.setLocationRelativeTo(null);
  88         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  89         f.setVisible(true);
  90     }
  91 
  92     @Override
  93     protected boolean performTest() {
  94         try {
  95             SwingUtilities.invokeAndWait(new Runnable() {
  96                 public void run() {
  97                     // prepare test data
  98                     frameClicked = 0;
  99 
 100                     b.requestFocus();
 101 
 102                     scrollPane.getHorizontalScrollBar().setUnitIncrement(40);
 103                     scrollPane.getVerticalScrollBar().setUnitIncrement(40);
 104 
 105                     hLoc = scrollPane.getHorizontalScrollBar().getLocationOnScreen();
 106                     hLoc.translate(scrollPane.getHorizontalScrollBar().getWidth() - 3, 3);
 107                     vLoc = scrollPane.getVerticalScrollBar().getLocationOnScreen();
 108                     vLoc.translate(3, scrollPane.getVerticalScrollBar().getHeight() - 3);
 109 
 110                     testLoc = p.getLocationOnScreen();
 111                     testLoc.translate(-3, -3);
 112 
 113                     resizeLoc = f.getLocationOnScreen();
 114                     resizeLoc.translate(f.getWidth() - 1, f.getHeight() - 1);
 115                 }
 116             });
 117         } catch (Exception e) {
 118             e.printStackTrace();
 119             System.exit(1);
 120         }
 121         // run robot
 122         Robot robot = Util.createRobot();
 123         robot.setAutoDelay(ROBOT_DELAY);
 124 
 125         robot.mouseMove(hLoc.x, hLoc.y);
 126         robot.mousePress(InputEvent.BUTTON1_MASK);
 127         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 128         Util.waitForIdle(robot);
 129 
 130         robot.mouseMove(vLoc.x, vLoc.y);
 131         robot.mousePress(InputEvent.BUTTON1_MASK);
 132         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 133         Util.waitForIdle(robot);
 134 
 135         clickAndBlink(robot, testLoc, false);
 136         robot.mouseMove(resizeLoc.x, resizeLoc.y);
 137         robot.mousePress(InputEvent.BUTTON1_MASK);
 138         robot.mouseMove(resizeLoc.x + 5, resizeLoc.y + 5);
 139         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 140         Util.waitForIdle(robot);
 141 
 142         clickAndBlink(robot, testLoc, false);
 143         return frameClicked == 2;
 144     }
 145 
 146     // this strange plumbing stuff is required due to "Standard Test Machinery" in base class
 147     public static void main(String args[]) throws InterruptedException {
 148         instance = new ViewportOverlapping();
 149         OverlappingTestBase.doMain(args);
 150     }
 151 }