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 /test/lib 30 @build jdk.test.lib.Platform 31 @run applet PopupPosTest.html 32 */ 33 34 import java.applet.Applet; 35 import java.awt.*; 36 import java.awt.event.*; 37 38 import jdk.test.lib.Platform; 39 40 public class PopupPosTest extends Applet 41 { 42 public void start () 43 { 44 if (Platform.isOSX()) { 45 // On OS X, popup isn't under the mouse 46 return; 47 } 48 Frame frame = new TestFrame(); 49 } 50 } 51 52 class 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 | 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 } |