1 /*
   2  * Copyright (c) 2003, 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   @test
  25   @bug 4800638
  26   @summary Tests that Choice does not lock the Desktop
  27   @run main GrabLockTest
  28 */
  29 import java.awt.*;
  30 import java.awt.event.*;
  31 
  32 public class GrabLockTest 
  33 {
  34     public static void main (String args[])
  35     {
  36         Frame frame = new TestFrame();
  37     }
  38 }
  39 
  40 class TestFrame extends Frame implements MouseListener {
  41     public TestFrame() {
  42         Choice choice = new Choice();
  43         choice.addItem("Fist Item");
  44         choice.addItem("Second Item");
  45         add(choice,BorderLayout.NORTH);
  46         Panel panel = new Panel();
  47         panel.addMouseListener(this);
  48         panel.setBackground(Color.RED);
  49         add(panel);
  50         setSize(200, 200);
  51         setVisible(true);
  52         toFront();
  53 
  54         try {
  55             Robot robot = new Robot();
  56             robot.setAutoWaitForIdle(true);
  57             robot.setAutoDelay(50);
  58 
  59             robot.waitForIdle();
  60 
  61             Point pt = choice.getLocationOnScreen();
  62             robot.mouseMove(pt.x + choice.getWidth() - choice.getHeight()/2,
  63                 pt.y + choice.getHeight()/2);
  64             robot.mousePress(InputEvent.BUTTON1_MASK);
  65             robot.waitForIdle();
  66             robot.mouseMove(pt.x + choice.getWidth()/2,
  67                 pt.y + choice.getHeight()*2);
  68             robot.waitForIdle();
  69             robot.mousePress(InputEvent.BUTTON2_MASK);
  70             robot.waitForIdle();
  71             Point pt1 = panel.getLocationOnScreen();
  72             robot.mouseMove(pt1.x + panel.getWidth()/2,
  73                 pt1.y + panel.getHeight()/2);
  74             robot.waitForIdle();
  75             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  76             robot.mouseRelease(InputEvent.BUTTON2_MASK);
  77 
  78             robot.waitForIdle();
  79 
  80             robot.mousePress(InputEvent.BUTTON1_MASK);
  81             robot.delay(30);
  82             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  83             robot.waitForIdle();
  84             if (nPressed == 0) {
  85                 robot.keyPress(KeyEvent.VK_ESCAPE);
  86                 robot.keyRelease(KeyEvent.VK_ESCAPE);
  87                 throw new RuntimeException("GrabLockTest failed." + nPressed);
  88             }
  89         } catch (Exception e) {
  90             throw new RuntimeException("The test was not completed.\n\n" + e);
  91         }
  92 
  93     }
  94 
  95     public int nPressed = 0;
  96 
  97     public void mouseClicked(MouseEvent e) {
  98     }
  99 
 100     public void mousePressed(MouseEvent e) {
 101         nPressed++;
 102         System.out.println("Pressed!");
 103     }
 104 
 105     public void mouseReleased(MouseEvent e) {
 106     }
 107 
 108     public void mouseEntered(MouseEvent e) {}
 109     public void mouseExited(MouseEvent e) {}
 110 }// class TestFrame