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