1 /*
   2  * Copyright (c) 2004, 2018, 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   @test
  26   @key headful
  27   @bug 5044150
  28   @summary Tests that pupup doesn't popdown if no space to display under
  29   @library /test/lib
  30   @build jdk.test.lib.Platform
  31   @run main PopupPosTest
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 
  37 import jdk.test.lib.Platform;
  38 
  39 public class PopupPosTest {
  40 
  41     public static void main(final String[] args) {
  42         if (Platform.isOSX()) {
  43             // On OS X, popup isn't under the mouse
  44             return;
  45         }
  46         Frame frame = new TestFrame();
  47     }
  48 }
  49 
  50 class TestFrame extends Frame implements ItemListener {
  51     Robot robot;
  52     Toolkit tk = Toolkit.getDefaultToolkit();
  53     Choice choice = new Choice();
  54     boolean indexChanged = false;
  55     final static int INITIAL_ITEM = 99;
  56     volatile boolean stateChanged;
  57 
  58     public TestFrame() {
  59         for (int i = 0; i < 100; i++) {
  60              choice.addItem("Item Item Item " + i);
  61         }
  62         choice.addItemListener(this);
  63 
  64         choice.select(INITIAL_ITEM);
  65         choice.setFont(new Font("Courier", Font.BOLD + Font.ITALIC, 100));
  66 
  67         add(choice, BorderLayout.CENTER);
  68         Dimension screen = tk.getScreenSize();
  69         setSize(screen.width - 10, screen.height - 70);
  70         setVisible(true);
  71         toFront();
  72         try {
  73             robot = new Robot();
  74             robot.setAutoDelay(50);
  75             robot.waitForIdle();
  76             // fix for 6175418. When we take "choice.getHeight()/2"
  77             // divider 2 is not sufficiently big to hit into the
  78             // small box Choice. We should use bigger divider to get
  79             // smaller value choice.getHeight()/i. 4 is sufficient.
  80             Point pt = choice.getLocationOnScreen();
  81             // click on 1/4 of Choice's height
  82             mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,
  83                               pt.y + choice.getHeight()/4);
  84 
  85             // click on center of Choice's height
  86             mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,
  87                               pt.y + choice.getHeight()/2);
  88 
  89             // click on 3/4 of Choice's height
  90             mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,
  91                               pt.y + choice.getHeight()*3/4);
  92             // testing that ItemEvent doesn't generated on a simple
  93             // mouse click when the dropdown appears under mouse : 6425067
  94             stateChanged = false;
  95             openChoice();
  96             closeChoice();
  97         } catch (Throwable e) {
  98             throw new RuntimeException("The test was not completed.\n\n" + e);
  99         }
 100 
 101         if (!indexChanged){
 102             throw new RuntimeException("Test failed. Another item wasn't selected.");
 103         }
 104 
 105         if(stateChanged){
 106             throw new RuntimeException("Test failed. ItemEvent was generated on a simple mouse click when the dropdown appears under mouse");
 107         }
 108     }// start()
 109 
 110     public void itemStateChanged(ItemEvent ie) {
 111         System.out.println("choice.stateChanged = "+ ie);
 112         stateChanged = true;
 113     }
 114 
 115     public void mouseMoveAndPressOnChoice(int x, int y){
 116         openChoice();
 117         robot.mouseMove(x, y);
 118         robot.mousePress(InputEvent.BUTTON1_MASK);
 119         robot.delay(30);
 120         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 121         robot.waitForIdle();
 122         //should close choice after each test stage
 123         closeChoice();
 124         checkSelectedIndex();
 125     }
 126 
 127     public void openChoice(){
 128         Point pt = choice.getLocationOnScreen();
 129         robot.mouseMove(pt.x + choice.getWidth() - choice.getHeight()/4,
 130                         pt.y + choice.getHeight()/2);
 131         robot.mousePress(InputEvent.BUTTON1_MASK);
 132         robot.delay(30);
 133         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 134         robot.waitForIdle();
 135     }
 136     public void closeChoice(){
 137         robot.keyPress(KeyEvent.VK_ESCAPE);
 138         robot.keyRelease(KeyEvent.VK_ESCAPE);
 139         robot.waitForIdle();
 140     }
 141 
 142     public void checkSelectedIndex(){
 143         if (choice.getSelectedIndex() != INITIAL_ITEM) {
 144             System.out.println("choice.getSelectedIndex = "+ choice.getSelectedIndex());
 145             indexChanged = true;
 146         }
 147     }
 148 }// class TestFrame