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