1 /* 2 test 3 @bug 6190768 6190778 4 @summary Tests that triggering events on AWT list by pressing CTRL + HOME, CTRL + END, PG-UP, PG-DOWN similar Motif behavior 5 @author Dmitry.Cherepanov@SUN.COM area=awt.list 6 @library ../../../../lib/testlibrary 7 @build jdk.testlibrary.OSInfo 8 @run applet KeyEventsTest.html 9 */ 10 11 /** 12 * KeyEventsTest.html 13 * 14 * summary: 15 */ 16 17 import java.applet.Applet; 18 import java.awt.*; 19 import java.awt.event.*; 20 import java.util.Set; 21 import java.lang.reflect.*; 22 23 import jdk.testlibrary.OSInfo; 24 25 public class KeyEventsTest extends Applet implements ItemListener, FocusListener, KeyListener 26 { 27 TestState currentState; 28 final Object LOCK = new Object(); 29 final int ACTION_TIMEOUT = 500; 30 31 List single = new List(3, false); 32 List multiple = new List(3, true); 33 34 Panel p1 = new Panel (); 35 Panel p2 = new Panel (); 36 37 public void init() 38 { 39 setLayout (new BorderLayout ()); 40 41 single.add("0"); 42 single.add("1"); 43 single.add("2"); 44 single.add("3"); 45 single.add("4"); 46 single.add("5"); 47 single.add("6"); 48 single.add("7"); 49 single.add("8"); 50 51 multiple.add("0"); 52 multiple.add("1"); 53 multiple.add("2"); 54 multiple.add("3"); 55 multiple.add("4"); 56 multiple.add("5"); 57 multiple.add("6"); 58 multiple.add("7"); 59 multiple.add("8"); 60 61 single.addKeyListener(this); 62 single.addItemListener(this); 63 single.addFocusListener(this); 64 p1.add(single); 65 add("North", p1); 66 67 multiple.addKeyListener(this); 68 multiple.addItemListener(this); 69 multiple.addFocusListener(this); 70 p2.add(multiple); 71 add("South", p2); 72 73 }//End init() 74 75 public void start () 76 { 77 78 try{ 79 setSize (200,200); 80 setVisible(true); 81 validate(); 82 83 main(null); 84 85 } catch (Exception e) { 86 e.printStackTrace(); 87 throw new RuntimeException("The test failed."); 88 } 89 90 }// start() 91 92 private void main(String[] args) 93 throws InterruptedException, InvocationTargetException { 94 95 doTest(); 96 97 System.out.println("Test passed."); 98 } 99 100 public void itemStateChanged (ItemEvent ie) { 101 System.out.println("itemStateChanged-"+ie); 102 this.currentState.setAction(true); 103 } 104 105 public void focusGained(FocusEvent e){ 106 107 synchronized (LOCK) { 108 LOCK.notifyAll(); 109 } 110 111 } 112 113 public void focusLost(FocusEvent e){ 114 } 115 116 public void keyPressed(KeyEvent e){ 117 System.out.println("keyPressed-"+e); 118 } 119 120 public void keyReleased(KeyEvent e){ 121 System.out.println("keyReleased-"+e); 122 } 123 124 public void keyTyped(KeyEvent e){ 125 System.out.println("keyTyped-"+e); 126 } 127 128 private void test(TestState currentState) 129 throws InterruptedException, InvocationTargetException { 130 131 synchronized (LOCK) { 132 133 this.currentState = currentState; 134 System.out.println(this.currentState); 135 136 List list; 137 if (currentState.getMultiple()){ 138 list = multiple; 139 }else{ 140 list = single; 141 } 142 143 Robot r; 144 try { 145 r = new Robot(); 146 } catch(AWTException e) { 147 throw new RuntimeException(e.getMessage()); 148 } 149 150 r.delay(10); 151 Point loc = this.getLocationOnScreen(); 152 153 r.mouseMove(loc.x+10, loc.y+10); 154 r.mousePress(InputEvent.BUTTON1_MASK); 155 r.delay(10); 156 r.mouseRelease(InputEvent.BUTTON1_MASK); 157 r.delay(10); 158 159 list.requestFocusInWindow(); 160 LOCK.wait(ACTION_TIMEOUT); 161 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list){ 162 throw new RuntimeException("Test failed - list isn't focus owner."); 163 } 164 165 list.deselect(0); 166 list.deselect(1); 167 list.deselect(2); 168 list.deselect(3); 169 list.deselect(4); 170 list.deselect(5); 171 list.deselect(6); 172 list.deselect(7); 173 list.deselect(8); 174 175 int selectIndex = 0; 176 int visibleIndex = 0; 177 178 if (currentState.getScrollMoved()){ 179 180 if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP || 181 currentState.getKeyID() == KeyEvent.VK_HOME){ 182 selectIndex = 8; 183 visibleIndex = 8; 184 }else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN || 185 currentState.getKeyID() == KeyEvent.VK_END){ 186 selectIndex = 0; 187 visibleIndex = 0; 188 } 189 190 }else{ 191 192 if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP || 193 currentState.getKeyID() == KeyEvent.VK_HOME){ 194 195 if (currentState.getSelectedMoved()){ 196 selectIndex = 1; 197 visibleIndex = 0; 198 }else{ 199 selectIndex = 0; 200 visibleIndex = 0; 201 } 202 203 }else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN || 204 currentState.getKeyID() == KeyEvent.VK_END){ 205 206 if (currentState.getSelectedMoved()){ 207 selectIndex = 7; 208 visibleIndex = 8; 209 }else{ 210 selectIndex = 8; 211 visibleIndex = 8; 212 } 213 214 } 215 216 } 217 218 list.select(selectIndex); 219 list.makeVisible(visibleIndex); 220 221 r.delay(10); 222 223 if (currentState.getKeyID() == KeyEvent.VK_HOME || 224 currentState.getKeyID() == KeyEvent.VK_END){ 225 r.keyPress(KeyEvent.VK_CONTROL); 226 } 227 228 r.delay(10); 229 r.keyPress(currentState.getKeyID()); 230 r.delay(10); 231 r.keyRelease(currentState.getKeyID()); 232 r.delay(10); 233 234 if (currentState.getKeyID() == KeyEvent.VK_HOME || 235 currentState.getKeyID() == KeyEvent.VK_END){ 236 r.keyRelease(KeyEvent.VK_CONTROL); 237 } 238 239 r.waitForIdle(); 240 r.delay(200); 241 242 if (currentState.getTemplate() != currentState.getAction()) 243 throw new RuntimeException("Test failed."); 244 245 } 246 247 } 248 249 private void doTest() 250 throws InterruptedException, InvocationTargetException { 251 252 boolean isWin = false; 253 if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) { 254 isWin = true; 255 }else if(OSInfo.getOSType() == OSInfo.OSType.MACOSX) { 256 System.out.println("Not for OS X"); 257 return; 258 } 259 260 System.out.println("multiple? selectedMoved? ?scrollMoved keyID? template? action?"); 261 test(new TestState(false, false, false, KeyEvent.VK_PAGE_UP, isWin?false:false)); 262 // SelectedMoved (false) != ScrollMoved (true) for single list not emulated 263 test(new TestState(false, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false)); 264 test(new TestState(false, true, true, KeyEvent.VK_PAGE_UP, isWin?true:true)); 265 test(new TestState(true, false, false, KeyEvent.VK_PAGE_UP, isWin?true:false)); 266 test(new TestState(true, false, true, KeyEvent.VK_PAGE_UP, isWin?true:false)); 267 test(new TestState(true, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false)); 268 test(new TestState(true, true, true, KeyEvent.VK_PAGE_UP, isWin?true:false)); 269 270 test(new TestState(false, false, false, KeyEvent.VK_PAGE_DOWN, isWin?false:false)); 271 test(new TestState(false, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false)); 272 test(new TestState(false, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:true)); 273 test(new TestState(true, false, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false)); 274 test(new TestState(true, false, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false)); 275 test(new TestState(true, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false)); 276 test(new TestState(true, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false)); 277 278 test(new TestState(false, false, false, KeyEvent.VK_HOME, isWin?false:true)); 279 test(new TestState(false, true, false, KeyEvent.VK_HOME, isWin?true:true)); 280 test(new TestState(false, true, true, KeyEvent.VK_HOME, isWin?true:true)); 281 test(new TestState(true, false, false, KeyEvent.VK_HOME, isWin?true:false)); 282 test(new TestState(true, false, true, KeyEvent.VK_HOME, isWin?true:false)); 283 test(new TestState(true, true, false, KeyEvent.VK_HOME, isWin?true:false)); 284 test(new TestState(true, true, true, KeyEvent.VK_HOME, isWin?true:false)); 285 286 test(new TestState(false, false, false, KeyEvent.VK_END, isWin?false:true)); 287 test(new TestState(false, true, false, KeyEvent.VK_END, isWin?true:true)); 288 test(new TestState(false, true, true, KeyEvent.VK_END, isWin?true:true)); 289 test(new TestState(true, false, false, KeyEvent.VK_END, isWin?true:false)); 290 test(new TestState(true, false, true, KeyEvent.VK_END, isWin?true:false)); 291 test(new TestState(true, true, false, KeyEvent.VK_END, isWin?true:false)); 292 test(new TestState(true, true, true, KeyEvent.VK_END, isWin?true:false)); 293 294 } 295 }// class KeyEventsTest 296 297 class TestState{ 298 299 private boolean multiple; 300 // after key pressing selected item moved 301 private final boolean selectedMoved; 302 // after key pressing scroll moved 303 private final boolean scrollMoved; 304 private final int keyID; 305 private final boolean template; 306 private boolean action; 307 308 public TestState(boolean multiple, boolean selectedMoved, boolean scrollMoved, int keyID, boolean template){ 309 this.multiple = multiple; 310 this.selectedMoved = selectedMoved; 311 this.scrollMoved = scrollMoved; 312 this.keyID = keyID; 313 this.template = template; 314 this.action = false; 315 } 316 317 public boolean getMultiple(){ 318 return multiple; 319 } 320 public boolean getSelectedMoved(){ 321 return selectedMoved; 322 } 323 324 public boolean getScrollMoved(){ 325 return scrollMoved; 326 } 327 328 public int getKeyID(){ 329 return keyID; 330 } 331 332 public boolean getTemplate(){ 333 return template; 334 } 335 336 public boolean getAction(){ 337 return action; 338 } 339 340 public void setAction(boolean action){ 341 this.action = action; 342 } 343 344 public String toString(){ 345 return multiple + "," + selectedMoved + "," + scrollMoved + "," + keyID + "," + template + "," + action; 346 } 347 }// TestState