1 /*
   2  * Copyright (c) 2014, 2015, 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 
  25 import java.awt.Color;
  26 import java.awt.GridLayout;
  27 import java.awt.Point;
  28 import java.awt.Robot;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.ActionListener;
  31 import java.awt.event.MouseAdapter;
  32 import java.awt.event.MouseEvent;
  33 import javax.swing.JFrame;
  34 import javax.swing.JMenu;
  35 import javax.swing.JMenuBar;
  36 import javax.swing.JMenuItem;
  37 import javax.swing.JSeparator;
  38 import javax.swing.SwingUtilities;
  39 import test.java.awt.regtesthelpers.Util;
  40 
  41 /**
  42  * AWT/Swing overlapping test for {@link javax.swing.JMenuBar } and {@link javax.swing.JSeparator} components.
  43  * <p>This test creates menu bar and test if heavyweight component is drawn correctly then menu dropdown is shown.
  44  * <p>See base class for test info.
  45  */
  46 /*
  47 @test
  48 @summary Overlapping test for javax.swing.JScrollPane
  49 @author sergey.grinev@oracle.com: area=awt.mixing
  50 @library ../../regtesthelpers
  51 @modules java.desktop/sun.awt
  52 @build Util
  53 @run main JMenuBarOverlapping
  54  */
  55 public class JMenuBarOverlapping extends OverlappingTestBase {
  56 
  57     {testEmbeddedFrame = true;}
  58 
  59     private boolean lwClicked = false;
  60     private boolean spClicked = false;
  61     private Point loc;
  62     private Point loc2;
  63     private Point sepLoc;
  64     private JFrame frame;
  65     private JMenuBar menuBar;
  66     JSeparator separator;
  67 
  68     protected void prepareControls() {
  69         frame = new JFrame("Mixing : Dropdown Overlapping test");
  70         frame.setLayout(new GridLayout(0,1));
  71         frame.setSize(200, 200);
  72         frame.setVisible(true);
  73 
  74         menuBar = new JMenuBar();
  75         JMenu menu = new JMenu("Test Menu");
  76         ActionListener menuListener = new ActionListener() {
  77 
  78             public void actionPerformed(ActionEvent event) {
  79                 lwClicked = true;
  80             }
  81         };
  82 
  83         JMenuItem item;
  84         menu.add(item = new JMenuItem("first"));
  85         item.addActionListener(menuListener);
  86         separator = new JSeparator();
  87         separator.addMouseListener(new MouseAdapter() {
  88 
  89             @Override
  90             public void mouseClicked(MouseEvent e) {
  91                 spClicked = true;
  92             }
  93         });
  94         menu.add(separator);
  95 
  96         for (int i = 0; i < petStrings.length; i++) {
  97             menu.add(item = new JMenuItem(petStrings[i]));
  98             item.addActionListener(menuListener);
  99         }
 100         menuBar.add(menu);
 101         frame.setJMenuBar(menuBar);
 102 
 103         propagateAWTControls(frame);
 104         frame.setVisible(true);
 105     }
 106 
 107     @Override
 108     protected boolean performTest() {
 109         try {
 110             SwingUtilities.invokeAndWait(new Runnable() {
 111                 public void run() {
 112                     loc = menuBar.getLocationOnScreen();
 113                     loc2 = frame.getContentPane().getLocationOnScreen();
 114                 }
 115             });
 116         } catch (Exception e) {
 117         }
 118         // run robot
 119         Robot robot = Util.createRobot();
 120         robot.setAutoDelay(ROBOT_DELAY);
 121 
 122         loc2.translate(75, 75);
 123         pixelPreCheck(robot, loc2, currentAwtControl);
 124 
 125         loc.translate(3, 3);
 126         clickAndBlink(robot, loc, false);
 127 
 128         clickAndBlink(robot, loc2, false);
 129 
 130         clickAndBlink(robot, loc, false);
 131         try {
 132             SwingUtilities.invokeAndWait(new Runnable() {
 133                 public void run() {
 134                     sepLoc = separator.getLocationOnScreen();
 135                 }
 136             });
 137         } catch (Exception e) {
 138             e.printStackTrace();
 139             throw new RuntimeException("Where is separator?");
 140         }
 141         sepLoc.translate(20, 1);
 142         clickAndBlink(robot, sepLoc, false);
 143 
 144         clickAndBlink(robot, loc, false); // close menu before running next step
 145         return lwClicked && spClicked;
 146     }
 147 
 148     // this strange plumbing stuff is required due to "Standard Test Machinery" in base class
 149     public static void main(String args[]) throws InterruptedException {
 150         instance = new JMenuBarOverlapping();
 151         OverlappingTestBase.doMain(args);
 152     }
 153 }