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