|
1 | 1 | /* |
2 | | - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | /* |
25 | | - test |
26 | | - @bug 6252982 |
27 | | - @summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit |
28 | | - @author andrei.dmitriev : area=awt.choice |
29 | | - @run applet ChoiceKeyEventReaction.html |
30 | | -*/ |
31 | | - |
32 | | -import java.applet.Applet; |
33 | | -import java.awt.*; |
34 | | -import java.awt.event.*; |
35 | | -import test.java.awt.regtesthelpers.Util; |
36 | | - |
37 | | -public class ChoiceKeyEventReaction extends Applet |
| 25 | + * @test |
| 26 | + * @bug 6252982 |
| 27 | + * @key headful |
| 28 | + * @summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit |
| 29 | + * @author andrei.dmitriev : area=awt.choice |
| 30 | + * @run main ChoiceKeyEventReaction |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.Robot; |
| 34 | +import java.awt.Choice; |
| 35 | +import java.awt.Point; |
| 36 | +import java.awt.Toolkit; |
| 37 | +import java.awt.TextField; |
| 38 | +import java.awt.FlowLayout; |
| 39 | +import java.awt.event.InputEvent; |
| 40 | +import java.awt.event.KeyEvent; |
| 41 | +import java.awt.event.ItemEvent; |
| 42 | +import java.awt.event.KeyAdapter; |
| 43 | +import java.awt.event.ItemListener; |
| 44 | +import java.awt.Frame; |
| 45 | + |
| 46 | +public class ChoiceKeyEventReaction |
38 | 47 | { |
39 | | - Robot robot; |
40 | | - Choice choice1 = new Choice(); |
41 | | - Point pt; |
42 | | - TextField tf = new TextField("Hi"); |
| 48 | + private static Robot robot; |
| 49 | + private static Choice choice1 = new Choice(); |
| 50 | + private static Point pt; |
| 51 | + private static TextField tf = new TextField("Hi"); |
| 52 | + private static boolean keyTypedOnTextField = false; |
| 53 | + private static boolean itemChanged = false; |
| 54 | + private static Frame frame; |
| 55 | + private static String toolkit; |
| 56 | + |
| 57 | + public static void main(String[] args) { |
| 58 | + createAndShowGUI(); |
| 59 | + |
| 60 | + try { |
| 61 | + robot = new Robot(); |
| 62 | + robot.setAutoDelay(100); |
43 | 63 |
|
44 | | - boolean keyTypedOnTextField = false; |
45 | | - boolean itemChanged = false; |
46 | | - String toolkit; |
| 64 | + moveFocusToTextField(); |
| 65 | + testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP); |
| 66 | + } catch (Exception e) { |
| 67 | + throw new RuntimeException("Test failed. Exception thrown: "+e); |
| 68 | + } finally { |
| 69 | + if (frame != null) { |
| 70 | + frame.dispose(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
47 | 74 |
|
48 | | - public void init() |
49 | | - { |
| 75 | + private static void createAndShowGUI() { |
| 76 | + frame = new Frame(); |
50 | 77 | toolkit = Toolkit.getDefaultToolkit().getClass().getName(); |
51 | 78 | System.out.println("Current toolkit is :" +toolkit); |
52 | 79 | for (int i = 1; i<20; i++){ |
53 | 80 | choice1.add("item-0"+i); |
54 | 81 | } |
55 | | - tf.addKeyListener(new KeyAdapter(){ |
56 | | - public void keyPressed(KeyEvent ke) { |
57 | | - keyTypedOnTextField = true; |
58 | | - System.out.println(ke); |
59 | | - } |
60 | | - }); |
61 | 82 |
|
| 83 | + tf.addKeyListener(new KeyAdapter(){ |
| 84 | + public void keyPressed(KeyEvent ke) { |
| 85 | + keyTypedOnTextField = true; |
| 86 | + System.out.println(ke); |
| 87 | + } |
| 88 | + }); |
62 | 89 |
|
63 | 90 | choice1.addItemListener(new ItemListener() { |
64 | | - public void itemStateChanged(ItemEvent e) { |
65 | | - itemChanged = true; |
66 | | - System.out.println(e); |
67 | | - } |
68 | | - }); |
69 | | - |
| 91 | + public void itemStateChanged(ItemEvent e) { |
| 92 | + itemChanged = true; |
| 93 | + System.out.println(e); |
| 94 | + } |
| 95 | + }); |
70 | 96 | choice1.setFocusable(false); |
71 | | - add(tf); |
72 | | - add(choice1); |
73 | | - setLayout (new FlowLayout()); |
74 | | - }//End init() |
75 | | - |
76 | | - public void start () |
77 | | - { |
78 | | - setSize (200,200); |
79 | | - setVisible(true); |
80 | | - validate(); |
81 | | - try{ |
82 | | - robot = new Robot(); |
83 | | - Util.waitForIdle(robot); |
84 | | - moveFocusToTextField(); |
85 | | - testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP); |
86 | | - } catch (Throwable e) { |
87 | | - throw new RuntimeException("Test failed. Exception thrown: "+e); |
88 | | - } |
89 | | - }// start() |
90 | 97 |
|
91 | | - public void testKeyOnChoice(int button, int key){ |
| 98 | + frame.add(tf); |
| 99 | + frame.add(choice1); |
| 100 | + frame.setLayout (new FlowLayout()); |
| 101 | + frame.setSize (200,200); |
| 102 | + frame.setVisible(true); |
| 103 | + } |
| 104 | + |
| 105 | + private static void testKeyOnChoice(int button, int key) { |
92 | 106 | pt = choice1.getLocationOnScreen(); |
93 | 107 | robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2); |
94 | | - Util.waitForIdle(robot); |
| 108 | + |
95 | 109 | robot.mousePress(button); |
96 | | - robot.delay(10); |
97 | 110 | robot.mouseRelease(button); |
98 | | - Util.waitForIdle(robot); |
99 | 111 |
|
100 | 112 | robot.keyPress(key); |
101 | 113 | robot.keyRelease(key); |
102 | 114 |
|
103 | | - Util.waitForIdle(robot); |
104 | | - |
105 | 115 | System.out.println("keyTypedOnTextField = "+keyTypedOnTextField +": itemChanged = " + itemChanged); |
106 | | - |
107 | | - if (itemChanged){ |
108 | | - throw new RuntimeException("Test failed. ItemChanged event occur on Choice."); |
109 | | - } |
110 | | - |
111 | | - // We may just write |
112 | | - // if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;} |
113 | | - // but must report differently in these cases so put two separate if statements for simplicity. |
114 | | - if (toolkit.equals("sun.awt.windows.WToolkit") && |
115 | | - !keyTypedOnTextField) |
116 | | - { |
117 | | - throw new RuntimeException("Test failed. (Win32) KeyEvent wasn't addressed to TextField. "); |
| 116 | + if (itemChanged) { |
| 117 | + throw new RuntimeException("Test failed. ItemChanged event occur on Choice."); |
118 | 118 | } |
119 | 119 |
|
120 | | - if (!toolkit.equals("sun.awt.windows.WToolkit") && |
121 | | - keyTypedOnTextField) |
122 | | - { |
123 | | - throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField."); |
| 120 | + // We may just write |
| 121 | + // if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;} |
| 122 | + // but must report differently in these cases so put two separate if statements for simplicity. |
| 123 | + if (toolkit.equals("sun.awt.windows.WToolkit") && |
| 124 | + !keyTypedOnTextField) { |
| 125 | + throw new RuntimeException("Test failed. (Win32) KeyEvent wasn't addressed to TextField. "); |
| 126 | + } |
| 127 | + |
| 128 | + if (!toolkit.equals("sun.awt.windows.WToolkit") && |
| 129 | + keyTypedOnTextField) { |
| 130 | + throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField."); |
124 | 131 | } |
125 | 132 |
|
126 | 133 | System.out.println("Test passed. Unfocusable Choice doesn't react on keys."); |
| 134 | + |
127 | 135 | //close opened choice |
128 | 136 | robot.keyPress(KeyEvent.VK_ESCAPE); |
129 | 137 | robot.keyRelease(KeyEvent.VK_ESCAPE); |
130 | | - Util.waitForIdle(robot); |
131 | 138 | } |
132 | 139 |
|
133 | | - public void moveFocusToTextField(){ |
| 140 | + public static void moveFocusToTextField() { |
134 | 141 | pt = tf.getLocationOnScreen(); |
135 | 142 | robot.mouseMove(pt.x + tf.getWidth()/2, pt.y + tf.getHeight()/2); |
136 | | - Util.waitForIdle(robot); |
| 143 | + |
137 | 144 | robot.mousePress(InputEvent.BUTTON1_MASK); |
138 | | - robot.delay(10); |
139 | 145 | robot.mouseRelease(InputEvent.BUTTON1_MASK); |
140 | | - Util.waitForIdle(robot); |
141 | 146 | } |
142 | | -}//:~ |
| 147 | +} |
0 commit comments