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 
  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 
  40 /**
  41  * AWT/Swing overlapping test for {@link javax.swing.JMenuBar } and {@link javax.swing.JSeparator} components.
  42  * <p>This test creates menu bar and test if heavyweight component is drawn correctly then menu dropdown is shown.
  43  * <p>See base class for test info.
  44  */
  45 /*
  46 @test
  47 @summary Overlapping test for javax.swing.JScrollPane
  48 @author sergey.grinev@oracle.com: area=awt.mixing


  49 @run main JMenuBarOverlapping
  50  */
  51 public class JMenuBarOverlapping extends OverlappingTestBase {
  52 
  53     {testEmbeddedFrame = true;}
  54 
  55     private boolean lwClicked = false;
  56     private boolean spClicked = false;
  57     private Point loc;
  58     private Point loc2;
  59     private Point sepLoc;
  60     private JFrame frame;
  61     private JMenuBar menuBar;
  62     JSeparator separator;
  63 
  64     protected void prepareControls() {
  65         frame = new JFrame("Mixing : Dropdown Overlapping test");
  66         frame.setLayout(new GridLayout(0,1));
  67         frame.setSize(200, 200);
  68         frame.setVisible(true);
  69 
  70         menuBar = new JMenuBar();
  71         JMenu menu = new JMenu("Test Menu");
  72         ActionListener menuListener = new ActionListener() {
  73 
  74             public void actionPerformed(ActionEvent event) {
  75                 lwClicked = true;
  76             }
  77         };
  78 
  79         JMenuItem item;
  80         menu.add(item = new JMenuItem("first"));
  81         item.addActionListener(menuListener);
  82         separator = new JSeparator();
  83         separator.addMouseListener(new MouseAdapter() {
  84 
  85             @Override
  86             public void mouseClicked(MouseEvent e) {
  87                 spClicked = true;
  88             }
  89         });
  90         menu.add(separator);
  91 
  92         for (int i = 0; i < petStrings.length; i++) {
  93             menu.add(item = new JMenuItem(petStrings[i]));
  94             item.addActionListener(menuListener);
  95         }
  96         menuBar.add(menu);
  97         frame.setJMenuBar(menuBar);
  98 
  99         propagateAWTControls(frame);
 100         frame.setVisible(true);
 101     }
 102 
 103     @Override
 104     protected boolean performTest() {
 105         try {
 106             SwingUtilities.invokeAndWait(new Runnable() {
 107                 public void run() {
 108                     loc = menuBar.getLocationOnScreen();
 109                     loc2 = frame.getContentPane().getLocationOnScreen();
 110                 }
 111             });
 112         } catch (Exception e) {
 113         }
 114         // run robot
 115         Robot robot = Util.createRobot();
 116         robot.setAutoDelay(ROBOT_DELAY);
 117 
 118         loc2.translate(75, 75);
 119         pixelPreCheck(robot, loc2, currentAwtControl);
 120 
 121         loc.translate(3, 3);
 122         clickAndBlink(robot, loc, false);
 123 
 124         clickAndBlink(robot, loc2, false);
 125 
 126         clickAndBlink(robot, loc, false);
 127         try {
 128             SwingUtilities.invokeAndWait(new Runnable() {
 129                 public void run() {
 130                     sepLoc = separator.getLocationOnScreen();
 131                 }
 132             });
 133         } catch (Exception e) {
 134             e.printStackTrace();
 135             System.exit(1);
 136         }
 137         sepLoc.translate(20, 1);
 138         clickAndBlink(robot, sepLoc, false);
 139 
 140         clickAndBlink(robot, loc, false); // close menu before running next step
 141         return lwClicked && spClicked;
 142     }
 143 
 144     // this strange plumbing stuff is required due to "Standard Test Machinery" in base class
 145     public static void main(String args[]) throws InterruptedException {
 146         instance = new JMenuBarOverlapping();
 147         OverlappingTestBase.doMain(args);
 148     }
 149 }
--- EOF ---