Path: blob/master/test/jdk/java/awt/Mixing/AWT_Mixing/SimpleOverlappingTestBase.java
41152 views
/*1* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.event.*;25import javax.swing.*;26import test.java.awt.regtesthelpers.Util;2728/**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 usage32*33* @author Sergey Grinev34*/35public abstract class SimpleOverlappingTestBase extends OverlappingTestBase {3637{38testEmbeddedFrame = true;39}4041/**42* Event delivery validation. If set to true (default) tested lightweight component will be provided43* with mouse listener which should be called in order for test to pass.44*/45protected final boolean useDefaultClickValidation;4647/**48* Constructor which sets {@link SimpleOverlappingTestBase#useDefaultClickValidation }49* @param defaultClickValidation50*/51protected SimpleOverlappingTestBase(boolean defaultClickValidation) {52super();53this.useDefaultClickValidation = defaultClickValidation;54}5556public SimpleOverlappingTestBase() {57this(true);58}5960//overridables61/**62* Successors override this method providing swing component for testing63* @return swing component to test64*/65protected abstract JComponent getSwingComponent();6667/**68* For tests debugging. Please, ignore.69*/70protected static final boolean debug = false;7172/**73* Should be set to true if test isn't using {@link SimpleOverlappingTestBase#useDefaultClickValidation }74*/75protected volatile boolean wasLWClicked = false;7677/**78* Current tested lightweight component79* @see SimpleOverlappingTestBase#getSwingComponent()80*/81protected JComponent testedComponent;8283/**84* Setups simple frame with lightweight component returned by {@link SimpleOverlappingTestBase#getSwingComponent() }85* Called by base class.86*/87protected void prepareControls() {88wasLWClicked = false;8990final JFrame f = new JFrame("Mixing : Simple Overlapping test");91f.setLayout(new SpringLayout());92f.setSize(200, 200);9394testedComponent = getSwingComponent();9596if (useDefaultClickValidation) {97testedComponent.addMouseListener(new MouseAdapter() {9899@Override100public void mouseClicked(MouseEvent e) {101wasLWClicked = true;102f.setVisible(false);103}104});105}106107if (!debug) {108f.add(testedComponent);109} else {110System.err.println("Warning: DEBUG MODE");111}112113propagateAWTControls(f);114115f.setVisible(true);116}117118/**119* AWT Robot instance. Shouldn't be used in most cases.120*/121protected Robot robot;122123/**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 passed127*/128protected boolean performTest() {129testedComponent.requestFocus();130131// run robot132robot = Util.createRobot();133robot.setAutoDelay(20);134135// get coord136Point lLoc = !debug ? testedComponent.getLocationOnScreen() : new Point(70, 30);137Util.waitForIdle(robot);138/* this is a workaround for certain jtreg(?) focus issue:139tests fail starting after failing mixing tests but always pass alone.140*/141JFrame ancestor = (JFrame)(testedComponent.getTopLevelAncestor());142if( ancestor != null ) {143Point ancestorLoc = ancestor.getLocationOnScreen();144ancestorLoc.translate(isOel7() ? 5 :145ancestor.getWidth() / 2 - 15, 2);146robot.mouseMove(ancestorLoc.x, ancestorLoc.y);147Util.waitForIdle(robot);148robot.mousePress(InputEvent.BUTTON1_MASK);149robot.delay(50);150robot.mouseRelease(InputEvent.BUTTON1_MASK);151Util.waitForIdle(robot);152}153154clickAndBlink(robot, lLoc);155Util.waitForIdle(robot);156157return wasLWClicked;158}159160public boolean isOel7() {161return System.getProperty("os.name").toLowerCase()162.contains("linux") && System.getProperty("os.version")163.toLowerCase().contains("el7");164}165166}167168169