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.Dimension;
  26 import java.awt.GridLayout;
  27 import java.awt.Point;
  28 import java.awt.Robot;
  29 import java.awt.event.AdjustmentEvent;
  30 import java.awt.event.AdjustmentListener;
  31 import javax.swing.JFrame;
  32 import javax.swing.JPanel;
  33 import javax.swing.JScrollPane;
  34 import javax.swing.SwingUtilities;
  35 
  36 /**
  37  * AWT/Swing overlapping test for {@link javax.swing.JScrollPane } component.
  38  * <p>See base class for test info.
  39  */
  40 /*
  41 @test
  42 @summary Overlapping test for javax.swing.JScrollPane
  43 @author sergey.grinev@oracle.com: area=awt.mixing
  44 @run main JScrollPaneOverlapping
  45  */
  46 public class JScrollPaneOverlapping extends OverlappingTestBase {
  47 
  48 //    {testEmbeddedFrame = true;}
  49 
  50     private boolean horizontalClicked = false;
  51     private boolean verticalClicked = false;
  52     private Point hLoc;
  53     private Point vLoc;
  54 
  55     private JFrame f;
  56     private JPanel p;
  57     private JScrollPane scrollPane;
  58 
  59     protected void prepareControls() {
  60 
  61         f = new JFrame("JScrollPane");
  62         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63         f.setSize(120, 120);
  64 
  65         p = new JPanel(new GridLayout(0, 1));
  66 
  67         scrollPane = new JScrollPane(p);
  68         scrollPane.setPreferredSize(new Dimension(300,300));
  69         scrollPane.setHorizontalScrollBarPolicy(
  70                 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  71         scrollPane.setVerticalScrollBarPolicy(
  72                 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  73 
  74         scrollPane.getHorizontalScrollBar().setValue(1);
  75         scrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
  76 
  77             public void adjustmentValueChanged(AdjustmentEvent e) {
  78                 horizontalClicked = true;
  79             }
  80         });
  81 
  82         scrollPane.getVerticalScrollBar().setValue(1);
  83         scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
  84 
  85             public void adjustmentValueChanged(AdjustmentEvent e) {
  86                 verticalClicked = true;
  87             }
  88         });
  89 
  90         f.getContentPane().add(scrollPane);
  91         f.setVisible(true);
  92         propagateAWTControls(p);
  93 //        JButton b = new JButton("Space extender");
  94 //        b.setPreferredSize(new Dimension(150,150));
  95 //        p.add( b );
  96 
  97         //b.requestFocus(); // to change the look of AWT component, especially Choice
  98     }
  99 
 100     @Override
 101     protected boolean performTest() {
 102         // run robot
 103         Robot robot = Util.createRobot();
 104         robot.setAutoDelay(ROBOT_DELAY);
 105 
 106         try {
 107             SwingUtilities.invokeAndWait(new Runnable() {
 108                 public void run() {
 109                     hLoc = scrollPane.getHorizontalScrollBar().getLocationOnScreen();
 110                     vLoc = scrollPane.getVerticalScrollBar().getLocationOnScreen();
 111                 }
 112             });
 113         } catch (Exception e) {
 114         }
 115         hLoc.translate(2, 2);
 116         vLoc.translate(2, 2);
 117 
 118         clickAndBlink(robot, hLoc, false);
 119         clickAndBlink(robot, vLoc, false);
 120 
 121         return horizontalClicked && verticalClicked;
 122     }
 123 
 124     // this strange plumbing stuff is required due to "Standard Test Machinery" in base class
 125     public static void main(String args[]) throws InterruptedException {
 126         instance = new JScrollPaneOverlapping();
 127         OverlappingTestBase.doMain(args);
 128     }
 129 }