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 import java.awt.*;
  25 import java.awt.event.*;
  26 import javax.swing.*;
  27 
  28 /**
  29  * Base class for testing overlapping of Swing and AWT component put into one frame.
  30  * Validates drawing and event delivery at the components intersection.
  31  * <p> See base class for usage
  32  *
  33  * @author Sergey Grinev
  34 */
  35 public abstract class SimpleOverlappingTestBase extends OverlappingTestBase {
  36 
  37     {
  38         testEmbeddedFrame = true;
  39     }
  40 
  41     /**
  42      * Event delivery validation. If set to true (default) tested lightweight component will be provided
  43      * with mouse listener which should be called in order for test to pass.
  44      */
  45     protected final boolean useDefaultClickValidation;
  46 
  47     /**
  48      * Constructor which sets {@link SimpleOverlappingTestBase#useDefaultClickValidation }
  49      * @param defaultClickValidation
  50      */
  51     protected SimpleOverlappingTestBase(boolean defaultClickValidation) {
  52         super();
  53         this.useDefaultClickValidation = defaultClickValidation;
  54     }
  55 
  56     public SimpleOverlappingTestBase() {
  57         this(true);
  58     }
  59 
  60     //overridables
  61     /**
  62      * Successors override this method providing swing component for testing
  63      * @return swing component to test
  64      */
  65     protected abstract JComponent getSwingComponent();
  66 
  67     /**
  68      * For tests debugging. Please, ignore.
  69      */
  70     protected static final boolean debug = false;
  71 
  72     /**
  73      * Should be set to true if test isn't using {@link SimpleOverlappingTestBase#useDefaultClickValidation }
  74      */
  75     protected volatile boolean wasLWClicked = false;
  76 
  77     /**
  78      * Current tested lightweight component
  79      * @see SimpleOverlappingTestBase#getSwingComponent()
  80      */
  81     protected JComponent testedComponent;
  82 
  83     /**
  84      * Setups simple frame with lightweight component returned by {@link SimpleOverlappingTestBase#getSwingComponent() }
  85      * Called by base class.
  86      */
  87     protected void prepareControls() {
  88         wasLWClicked = false;
  89 
  90         final JFrame f = new JFrame("Mixing : Simple Overlapping test");
  91         f.setLayout(new SpringLayout());
  92         f.setSize(200, 200);
  93 
  94         testedComponent = getSwingComponent();
  95 
  96         if (useDefaultClickValidation) {
  97             testedComponent.addMouseListener(new MouseAdapter() {
  98 
  99                 @Override
 100                 public void mouseClicked(MouseEvent e) {
 101                     wasLWClicked = true;
 102                     f.setVisible(false);
 103                 }
 104             });
 105         }
 106 
 107         if (!debug) {
 108             f.add(testedComponent);
 109         } else {
 110             System.err.println("Warning: DEBUG MODE");
 111         }
 112 
 113         propagateAWTControls(f);
 114 
 115         f.setVisible(true);
 116     }
 117 
 118     /**
 119      * AWT Robot instance. Shouldn't be used in most cases.
 120      */
 121     protected Robot robot;
 122 
 123     /**
 124      * Run test by {@link OverlappingTestBase#clickAndBlink(java.awt.Robot, java.awt.Point) } validation for current lightweight component.
 125      * <p>Called by base class.
 126      * @return true if test passed
 127      */
 128     protected boolean performTest() {
 129         testedComponent.requestFocus();
 130 
 131         // run robot
 132         robot = Util.createRobot();
 133         robot.setAutoDelay(20);
 134 
 135         // get coord
 136         Point lLoc = !debug ? testedComponent.getLocationOnScreen() : new Point(70, 30);
 137         Util.waitForIdle(null);
 138         /* this is a workaround for certain jtreg(?) focus issue:
 139            tests fail starting after failing mixing tests but always pass alone.
 140          */
 141         JFrame ancestor = (JFrame)(testedComponent.getTopLevelAncestor());
 142         if( ancestor != null ) {
 143             Point ancestorLoc = ancestor.getLocationOnScreen();
 144             ancestorLoc.translate(ancestor.getWidth()/2-15, 2);
 145             robot.mouseMove(ancestorLoc.x, ancestorLoc.y);
 146             Util.waitForIdle(robot);
 147             robot.mousePress(InputEvent.BUTTON1_MASK);
 148             robot.delay(50);
 149             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 150             Util.waitForIdle(robot);
 151         }
 152 
 153         clickAndBlink(robot, lLoc);
 154         Util.waitForIdle(null);
 155 
 156         return wasLWClicked;
 157     }
 158 
 159 }
 160