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.Container;
  25 import java.awt.Point;
  26 import java.awt.event.MouseAdapter;
  27 import java.awt.event.MouseEvent;
  28 import java.awt.event.InputEvent;
  29 import java.lang.reflect.InvocationTargetException;
  30 import javax.swing.JFrame;
  31 import javax.swing.SpringLayout;
  32 import javax.swing.SwingUtilities;
  33 import test.java.awt.regtesthelpers.Util;
  34 
  35 /**
  36  * Base class for testing overlapping of Swing and AWT component put into GlassPane.
  37  * Validates drawing and event delivery at the components intersection.
  38  * <p> See {@link OverlappingTestBase} for usage
  39  *
  40  * @author Sergey Grinev
  41  */
  42 public abstract class GlassPaneOverlappingTestBase extends SimpleOverlappingTestBase {
  43 
  44     /**
  45      * If true components is additionally tested to be correctly drawn after resize.
  46      */
  47     protected boolean testResize = true;
  48     private JFrame f = null;
  49     private volatile Point ancestorLoc;
  50 
  51     /**
  52      * Setups GlassPane with lightweight component returned by {@link SimpleOverlappingTestBase#getSwingComponent() }
  53      * Called by base class.
  54      */
  55     @Override
  56     protected void prepareControls() {
  57         wasLWClicked = false;
  58 
  59         if(f != null) {
  60             f.setVisible(false);
  61         }
  62         f = new JFrame("Mixing : GlassPane Overlapping test");
  63         f.setLayout(new SpringLayout());
  64         f.setSize(200, 200);
  65 
  66         propagateAWTControls(f);
  67 
  68         f.getGlassPane().setVisible(true);
  69         Container glassPane = (Container) f.getGlassPane();
  70         glassPane.setLayout(null);
  71 
  72         testedComponent = getSwingComponent();
  73         if (useDefaultClickValidation) {
  74             testedComponent.addMouseListener(new MouseAdapter() {
  75 
  76                 @Override
  77                 public void mouseClicked(MouseEvent e) {
  78                     //System.err.println("lw mouse clicked");
  79                     wasLWClicked = true;
  80                 }
  81             });
  82         }
  83         testedComponent.setBounds(0, 0, testedComponent.getPreferredSize().width, testedComponent.getPreferredSize().height);
  84         glassPane.add(testedComponent);
  85 
  86         f.setVisible(true);
  87     }
  88 
  89     public GlassPaneOverlappingTestBase() {
  90         super();
  91     }
  92 
  93     public GlassPaneOverlappingTestBase(boolean defaultClickValidation) {
  94         super(defaultClickValidation);
  95     }
  96 
  97     /**
  98      * Run test by {@link OverlappingTestBase#clickAndBlink(java.awt.Robot, java.awt.Point) } validation for current lightweight component.
  99      * <p>Also resize component and repeat validation in the resized area.
 100      * <p>Called by base class.
 101      * @return true if test passed
 102      * @see GlassPaneOverlappingTestBase#testResize
 103      */
 104     @Override
 105     protected boolean performTest() {
 106         if (!super.performTest()) {
 107             return false;
 108         }
 109         if (testResize) {
 110             wasLWClicked = false;
 111             try {
 112                 SwingUtilities.invokeAndWait(new Runnable() {
 113 
 114                     public void run() {
 115                         testedComponent.setBounds(0, 0, testedComponent.getPreferredSize().width, testedComponent.getPreferredSize().height + 20);
 116                         ancestorLoc = f.getLocationOnScreen();
 117                     }
 118                 });
 119             } catch (InterruptedException ex) {
 120                 fail(ex.getMessage());
 121             } catch (InvocationTargetException ex) {
 122                 fail(ex.getMessage());
 123             }
 124             Point lLoc = testedComponent.getLocationOnScreen();
 125             lLoc.translate(1, testedComponent.getPreferredSize().height + 1);
 126 
 127             /* this is a workaround for certain jtreg(?) focus issue:
 128                tests fail starting after failing mixing tests but always pass alone.
 129              */
 130             Util.waitForIdle(robot);
 131             ancestorLoc.translate(f.getWidth()/2-15, 2);
 132             robot.mouseMove(ancestorLoc.x, ancestorLoc.y);
 133             Util.waitForIdle(robot);
 134             robot.mousePress(InputEvent.BUTTON1_MASK);
 135             robot.delay(50);
 136             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 137             Util.waitForIdle(robot);
 138 
 139             clickAndBlink(robot, lLoc);
 140             return wasLWClicked;
 141         } else {
 142             return true;
 143         }
 144     }
 145 }