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