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 
  25 import java.awt.Color;
  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.ActionEvent;
  31 import java.awt.event.ActionListener;
  32 import java.awt.event.InputEvent;
  33 import javax.swing.JButton;
  34 import javax.swing.JFrame;
  35 import javax.swing.JPanel;
  36 import javax.swing.JScrollPane;
  37 import javax.swing.JSplitPane;
  38 import javax.swing.SwingUtilities;
  39 import test.java.awt.regtesthelpers.Util;
  40 
  41 /**
  42  * AWT/Swing overlapping test for {@link javax.swing.JSplitPane } component.
  43  * <p>This test puts heavyweight and lightweight components into different
  44  * panels and test if splitter image and components itself are drawn correctly.
  45  * <p>See base class for test info.
  46  */
  47 /*
  48  * @test
  49  * @key headful
  50  * @bug 6986109
  51  * @summary Overlapping test for javax.swing.JSplitPane
  52  * @author sergey.grinev@oracle.com: area=awt.mixing
  53  * @library /java/awt/patchlib  ../../regtesthelpers
  54  * @modules java.desktop/sun.awt
  55  *          java.desktop/java.awt.peer
  56  * @build java.desktop/java.awt.Helper
  57  * @build Util
  58  * @run main JSplitPaneOverlapping
  59  */
  60 public class JSplitPaneOverlapping extends OverlappingTestBase {
  61 
  62     private boolean clicked = false;
  63     private Point splitterLoc;
  64     private JScrollPane sp1;
  65     private JScrollPane sp2;
  66 
  67     protected void prepareControls() {
  68         JFrame frame = new JFrame("SplitPane Mixing");
  69         JPanel p = new JPanel(new GridLayout());
  70         p.setPreferredSize(new Dimension(500, 500));
  71         propagateAWTControls(p);
  72         sp1 = new JScrollPane(p);
  73 
  74         JButton button = new JButton("JButton");
  75         button.setBackground(Color.RED);
  76         button.addActionListener(new ActionListener() {
  77 
  78             public void actionPerformed(ActionEvent e) {
  79                 clicked = true;
  80             }
  81         });
  82         sp2 = new JScrollPane(button);
  83 
  84         JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp1, sp2);
  85         splitPane.setOneTouchExpandable(false);
  86         splitPane.setDividerLocation(150);
  87 
  88         splitPane.setPreferredSize(new Dimension(400, 200));
  89 
  90         frame.getContentPane().add(splitPane);
  91         frame.pack();
  92         frame.setVisible(true);
  93     }
  94 
  95     private static final boolean ignoreFail = false;
  96 
  97     @Override
  98     protected boolean performTest() {
  99         try {
 100             SwingUtilities.invokeAndWait(new Runnable() {
 101                 public void run() {
 102                     splitterLoc = sp2.getLocationOnScreen();
 103                     Point leftLoc = sp1.getLocationOnScreen();
 104                     leftLoc.translate(sp1.getWidth(), 0);
 105                     splitterLoc.translate(-(splitterLoc.x - leftLoc.x) / 2, 30);
 106                 }
 107             });
 108         } catch (Exception e) {
 109             e.printStackTrace();
 110             throw new RuntimeException("Where is splitter?");
 111         }
 112         // run robot
 113         Robot robot = Util.createRobot();
 114         robot.setAutoDelay(ROBOT_DELAY);
 115 
 116         robot.mouseMove(splitterLoc.x, splitterLoc.y);
 117         Util.waitForIdle(robot);
 118 
 119         robot.mousePress(InputEvent.BUTTON1_MASK);
 120         robot.mouseMove(splitterLoc.x - 50, splitterLoc.y);
 121         Color c = robot.getPixelColor(splitterLoc.x - 50, splitterLoc.y);
 122         System.out.println("Actual: "+c+", (not) expected: "+AWT_VERIFY_COLOR+" at "+(splitterLoc.x - 50)+", "+ splitterLoc.y);
 123         if (!ignoreFail && c.equals(AWT_VERIFY_COLOR)) {
 124             fail("The JSplitPane drag-n-drop image did not pass pixel color check and is overlapped");
 125         }
 126         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 127 
 128         clickAndBlink(robot, splitterLoc);
 129 
 130         return clicked;
 131     }
 132 
 133     // this strange plumbing stuff is required due to "Standard Test Machinery" in base class
 134     public static void main(String args[]) throws InterruptedException {
 135         instance = new JSplitPaneOverlapping();
 136         OverlappingTestBase.doMain(args);
 137     }
 138 }