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