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