1 19 20 package org.netbeans.test.editor.suites.keybindings; 21 22 import java.awt.event.InputEvent ; 23 import java.awt.event.KeyEvent ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import javax.swing.ListModel ; 27 import junit.textui.TestRunner; 28 import lib.EditorTestCase; 29 import lib.EditorTestCase.ValueResolver; 30 import org.netbeans.jellytools.Bundle; 31 import org.netbeans.jellytools.EditorOperator; 32 import org.netbeans.jellytools.HelpOperator; 33 import org.netbeans.jellytools.JellyTestCase; 34 import org.netbeans.jellytools.ProjectsTabOperator; 35 import org.netbeans.jellytools.actions.OpenAction; 36 import org.netbeans.jellytools.modules.editor.AddShortcutDialog; 37 import org.netbeans.jellytools.modules.editor.CreateNewProfileDialog; 38 import org.netbeans.jellytools.modules.editor.KeyMapOperator; 39 import org.netbeans.jellytools.nodes.Node; 40 import org.netbeans.jemmy.EventTool; 41 import org.netbeans.jemmy.operators.JListOperator; 42 import org.netbeans.jemmy.operators.JTreeOperator; 43 import org.netbeans.junit.NbTestSuite; 44 45 46 50 public class KeyMapTest extends JellyTestCase{ 51 52 private static final String testFile = "Test"; 54 public static final String SRC_PACKAGES_PATH = 55 Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle", 56 "NAME_src.dir"); 57 58 private static final String testPackage = "keymap"; 60 private static final String NEW_PROFILE = "DupProfile2"; 61 62 63 private EditorOperator editor; 64 67 public KeyMapTest(String name) { 68 super(name); 69 } 70 71 private EditorOperator openFile() { 72 Node pn = new ProjectsTabOperator().getProjectRootNode("editor_test"); 73 pn.select(); 74 Node n = new Node(pn, SRC_PACKAGES_PATH + "|" + testPackage + "|" + testFile); 76 n.select(); 77 new OpenAction().perform(); 78 new EventTool().waitNoEvent(500); 79 return new EditorOperator("Test"); 80 } 81 82 @Override 83 protected void setUp() throws Exception { 84 super.setUp(); 85 editor=openFile(); 86 System.out.println("Starting: "+getName()); 87 } 88 89 public String getHumanReadablePath(String path) { 90 int pos = path.lastIndexOf("|"); 91 String prefix = path.substring(0, pos+1); 92 String action = path.substring(pos+1); 93 int pos2 = action.lastIndexOf(":"); 94 if(pos2<0) action = "<unknown action>"; 95 else action = action.substring(pos2+1); 96 return prefix+action; 97 } 98 99 private void dump(String path, JTreeOperator tree,KeyMapOperator kmo) { 100 Node n = new Node(tree, path); 101 if(path.length()!=0) n.select(); 102 String [] s = n.getChildren(); 103 if(s.length==0) { 104 JListOperator shortcuts = kmo.shortcuts(); 105 ListModel model = shortcuts.getModel(); 106 if(model.getSize()>0) { 107 getRef().println(getHumanReadablePath(path)); 108 } 109 for (int i = 0; i < model.getSize(); i++) { 110 getRef().println(" "+model.getElementAt(i)); 111 } 112 } else { 113 for (int i = 0; i < s.length; i++) { 114 String item = s[i]; 115 if(item.equals("Other")) continue; if(path.length()!=0) dump(path+"|"+item,tree,kmo); 117 else dump(item,tree,kmo); 118 } 119 n.collapse(); 120 } 121 122 } 123 124 public void testAllKeyMapNetbeans() throws IOException { 125 KeyMapOperator kmo = null; 126 boolean closed = true; 127 try { 128 kmo = KeyMapOperator.invoke(); 129 closed = false; 130 kmo.selectProfile("NetBeans"); 131 JTreeOperator tree =kmo.actions(); 132 dump("",tree,kmo); 133 kmo.ok().push(); 134 closed = true; 135 assertFile(new File (getWorkDir(),getName()+".ref"), getGoldenFile(), new File (getWorkDir(),getName()+".diff")); 136 } finally { 137 if(!closed) kmo.cancel().push(); 138 editor.close(false); 139 } 140 } 141 142 143 public void testAllKeyMapEmacs() throws IOException { 144 KeyMapOperator kmo = null; 145 boolean closed = true; 146 try { 147 kmo = KeyMapOperator.invoke(); 148 closed = false; 149 kmo.selectProfile("Emacs"); 150 JTreeOperator tree =kmo.actions(); 151 dump("",tree,kmo); 152 kmo.ok().push(); 153 closed = true; 154 assertFile(new File (getWorkDir(),getName()+".ref"), getGoldenFile(), new File (getWorkDir(),getName()+".diff")); 155 } finally { 156 if(!closed) kmo.cancel().push(); 157 editor.close(false); 158 } 159 } 160 161 public void testAllKeyMapEclipse() throws IOException { 162 KeyMapOperator kmo = null; 163 boolean closed = true; 164 try { 165 kmo = KeyMapOperator.invoke(); 166 closed = false; 167 kmo.selectProfile("Eclipse"); 168 JTreeOperator tree =kmo.actions(); 169 dump("",tree,kmo); 170 kmo.ok().push(); 171 closed = true; 172 assertFile(new File (getWorkDir(),getName()+".ref"), getGoldenFile(), new File (getWorkDir(),getName()+".diff")); 173 } finally { 174 if(!closed) kmo.cancel().push(); 175 editor.close(false); 176 } 177 } 178 179 public void testAddShortcut() { 180 KeyMapOperator kmo = null; 181 boolean closed = true; 182 try { 183 kmo = KeyMapOperator.invoke(); 184 closed = false; 185 kmo.selectProfile("NetBeans"); 186 kmo.selectAction("Other|select-line"); 187 checkListContents(kmo.shortcuts(), new Object []{}); 189 kmo.add().push(); 190 AddShortcutDialog asd = new AddShortcutDialog(); 191 asd.txtJTextField().pushKey(KeyEvent.VK_L, InputEvent.ALT_DOWN_MASK); 192 asd.btOK().push(); 193 checkListContents(kmo.shortcuts(), "Alt+L"); 194 kmo.ok().push(); 195 closed = true; 196 new EventTool().waitNoEvent(500); 197 editor.setCaretPosition(7, 1); 198 editor.pushKey(KeyEvent.VK_L, InputEvent.ALT_DOWN_MASK); 199 new EventTool().waitNoEvent(100); 200 String text = editor.txtEditorPane().getSelectedText(); 201 assertEquals(" System.out.println(\"Hello\");",text); 202 } finally { 203 if(!closed && kmo!=null) kmo.cancel().push(); 204 editor.close(false); 205 } 206 207 } 208 209 public void testRemoveShortcut() { 210 KeyMapOperator kmo = null; 211 boolean closed = true; 212 try { 213 kmo = KeyMapOperator.invoke(); 214 closed = false; 215 kmo.selectProfile("NetBeans"); 216 kmo.selectAction("Other|selection-end-line"); 217 checkListContents(kmo.shortcuts(), "Shift+END"); 218 kmo.shortcuts().selectItem(0); 219 kmo.remove().push(); 220 checkListContents(kmo.shortcuts(), new Object []{}); 221 kmo.ok().push(); 222 closed = true; 223 new EventTool().waitNoEvent(500); 224 editor.setCaretPosition(7, 1); 225 editor.pushKey(KeyEvent.VK_END, InputEvent.SHIFT_DOWN_MASK); 226 new EventTool().waitNoEvent(100); 227 String text = editor.txtEditorPane().getSelectedText(); 228 assertEquals(null,text); 229 } finally { 230 if(!closed && kmo!=null) kmo.cancel().push(); 231 editor.close(false); 232 } 233 } 234 235 public void testAddDuplicate() { 236 KeyMapOperator kmo = null; 237 boolean closed = true; 238 try { 239 kmo = KeyMapOperator.invoke(); 240 closed = false; 241 kmo.selectProfile("NetBeans"); 242 kmo.selectAction("Other|selection-end-word"); 243 kmo.add().push(); 244 AddShortcutDialog asd = new AddShortcutDialog(); 245 asd.txtJTextField().pushKey(KeyEvent.VK_UP, InputEvent.SHIFT_DOWN_MASK); 246 assertEquals("Shortcut already assigned to Extend Selection Up Action.",asd.lblConflict().getText()); 247 asd.btOK().push(); 248 checkListContents(kmo.shortcuts(), "Shift+UP"); 249 kmo.selectAction("Other|selection-up"); 250 checkListContents(kmo.shortcuts(), new Object []{}); 251 kmo.ok().push(); 252 closed = true; 253 new EventTool().waitNoEvent(500); 254 editor.setCaretPosition(7, 9); 255 editor.pushKey(KeyEvent.VK_UP, InputEvent.SHIFT_DOWN_MASK); 256 new EventTool().waitNoEvent(100); 257 String text = editor.txtEditorPane().getSelectedText(); 258 assertEquals("System",text); 259 } finally { 260 if(!closed && kmo!=null) kmo.cancel().push(); 261 editor.close(false); 262 } 263 264 } 265 266 public void testCancelAdding() { 267 KeyMapOperator kmo = null; 268 boolean closed = true; 269 try { 270 kmo = KeyMapOperator.invoke(); 271 closed = false; 272 kmo.selectProfile("NetBeans"); 273 kmo.selectAction("Other|remove-selection"); 274 checkListContents(kmo.shortcuts(), new Object []{}); 275 kmo.add().push(); 276 AddShortcutDialog asd = new AddShortcutDialog(); 277 asd.txtJTextField().pushKey(KeyEvent.VK_R, InputEvent.ALT_DOWN_MASK); 278 asd.btCancel().push(); 279 checkListContents(kmo.shortcuts(), new Object []{}); 280 kmo.ok().push(); 281 closed = true; 282 new EventTool().waitNoEvent(500); 283 editor.setCaretPosition(7, 9); 284 new EventTool().waitNoEvent(100); 285 editor.txtEditorPane().setSelectionStart(1); 286 editor.txtEditorPane().setSelectionEnd(8); 287 editor.pushKey(KeyEvent.VK_R, InputEvent.ALT_DOWN_MASK); 288 String text = editor.txtEditorPane().getSelectedText(); 289 assertEquals("package",text); 290 } finally { 291 if(!closed && kmo!=null) kmo.cancel().push(); 292 editor.close(false); 293 } 294 295 } 296 297 public void testCancelOptions() { 298 KeyMapOperator kmo = null; 299 boolean closed = true; 300 try { 301 kmo = KeyMapOperator.invoke(); 302 closed = false; 303 kmo.selectProfile("NetBeans"); 304 kmo.selectAction("Other|selection-line-first-column"); 305 checkListContents(kmo.shortcuts(), new Object []{}); 306 kmo.add().push(); 307 AddShortcutDialog asd = new AddShortcutDialog(); 308 asd.txtJTextField().pushKey(KeyEvent.VK_Z, InputEvent.ALT_DOWN_MASK); 309 asd.btOK().push(); 310 checkListContents(kmo.shortcuts(), "Alt+Z"); 311 kmo.cancel().push(); 312 closed = true; 313 new EventTool().waitNoEvent(500); 314 editor.setCaretPosition(7, 9); 315 editor.pushKey(KeyEvent.VK_Z, InputEvent.ALT_DOWN_MASK); 316 new EventTool().waitNoEvent(100); 317 String text = editor.txtEditorPane().getSelectedText(); 318 assertEquals(null,text); 319 } finally { 320 if(!closed && kmo!=null) kmo.cancel().push(); 321 editor.close(false); 322 } 323 } 324 325 public void testAddShortCutDialog() { 326 KeyMapOperator kmo = null; 327 boolean closedKMO = true; 328 AddShortcutDialog asd = null; 329 boolean closedASD = true; 330 try { 331 kmo = KeyMapOperator.invoke(); 332 closedKMO = false; 333 kmo.selectProfile("NetBeans"); 334 kmo.selectAction("Other|selection-line-first-column"); 335 checkListContents(kmo.shortcuts(), new Object []{}); 336 kmo.add().push(); 337 closedASD = false; 338 asd = new AddShortcutDialog(); 339 asd.txtJTextField().pushKey(KeyEvent.VK_Z, InputEvent.ALT_DOWN_MASK); assertEquals("Alt+Z",asd.txtJTextField().getText()); 341 342 asd.btClear().push(); assertEquals("",asd.txtJTextField().getText()); 344 345 asd.txtJTextField().requestFocus(); 346 asd.txtJTextField().pushKey(KeyEvent.VK_X,InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK ); 347 assertEquals("Alt+Shift+X",asd.txtJTextField().getText()); 348 349 asd.btTab().push(); 350 assertEquals("Alt+Shift+X TAB",asd.txtJTextField().getText()); 351 352 asd.txtJTextField().requestFocus(); 353 asd.txtJTextField().pushKey(KeyEvent.VK_ESCAPE); 354 assertEquals("Alt+Shift+X TAB ESCAPE",asd.txtJTextField().getText()); 355 356 asd.btClear().push(); asd.txtJTextField().requestFocus(); 358 asd.txtJTextField().pushKey(KeyEvent.VK_BACK_SPACE); 359 assertEquals("BACK_SPACE",asd.txtJTextField().getText()); 360 361 asd.txtJTextField().pushKey(KeyEvent.VK_BACK_SPACE); 362 assertEquals("",asd.txtJTextField().getText()); 363 364 asd.btCancel().push(); 365 closedASD = true; 366 kmo.cancel().push(); 367 closedKMO = true; 368 } finally { 369 if(!closedASD && asd!=null) asd.btCancel().push(); 370 if(!closedKMO && kmo!=null) kmo.cancel().push(); 371 editor.close(false); 372 } 373 374 } 375 376 public void testHelp() { 377 KeyMapOperator kmo = null; 378 boolean closed = true; 379 try { 380 kmo = KeyMapOperator.invoke(); 381 closed = false; 382 kmo.help().push(); 383 HelpOperator help = new HelpOperator(); 384 385 boolean ok = help.getContentText().contains("Options Window: Keymap"); 386 assertTrue("Wrong help page opened",ok); 387 help.close(); 388 } finally { 389 if(!closed && kmo!=null) kmo.cancel().push(); 390 editor.close(false); 391 } 392 393 394 } 395 396 public void testProfileSwitch() { 397 KeyMapOperator kmo = null; 398 boolean closed = true; 399 try { 400 kmo = KeyMapOperator.invoke(); 401 closed = false; 402 kmo.selectProfile("Eclipse"); 403 kmo.selectAction("Other|remove-line"); 404 checkListContents(kmo.shortcuts(), "Ctrl+D"); 405 kmo.ok().push(); 406 closed = true; 407 new EventTool().waitNoEvent(500); 408 editor.setCaretPosition(7, 1); 409 editor.pushKey(KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK); 410 new EventTool().waitNoEvent(100); 411 String text = editor.getText(); 412 assertEquals("\n" + 413 "package keymap;\n" + 414 "\n" + 415 "public class Test {\n" + 416 " \n" + 417 " public Test() {\n" + 418 " }\n" + 419 " \n" + 420 "}\n",text); 421 kmo = KeyMapOperator.invoke(); 422 closed = false; 423 assertEquals(kmo.profile().getSelectedItem(),"Eclipse"); 424 kmo.ok().push(); 425 closed = true; 426 } finally { 427 if(!closed && kmo!=null) kmo.cancel().push(); 428 editor.close(false); 429 } 430 } 431 432 public void testProfileDouble() { 433 KeyMapOperator kmo = null; 434 boolean closed = true; 435 try { 436 kmo = KeyMapOperator.invoke(); 437 closed = false; 438 kmo.selectProfile("NetBeans"); 440 int originalSize = kmo.profile().getModel().getSize(); 441 kmo.duplicate().push(); 442 CreateNewProfileDialog cnpd = new CreateNewProfileDialog(); 443 cnpd.txtProfileName().clearText(); 444 cnpd.txtProfileName().typeText(NEW_PROFILE); 445 cnpd.btOK().push(); 446 new EventTool().waitNoEvent(100); 447 assertEquals(kmo.profile().getSelectedItem(),NEW_PROFILE); 448 kmo.selectAction("Other|toggle-case-identifier-begin"); 450 checkListContents(kmo.shortcuts(), new Object []{}); 451 kmo.add().push(); 452 AddShortcutDialog asd = new AddShortcutDialog(); 453 asd.txtJTextField().pushKey(KeyEvent.VK_C, InputEvent.ALT_DOWN_MASK); 454 asd.btOK().push(); 455 checkListContents(kmo.shortcuts(), "Alt+C"); 456 kmo.ok().push(); 457 new EventTool().waitNoEvent(1000); 458 closed = true; 459 editor.setCaretPosition(7, 12); 461 editor.pushKey(KeyEvent.VK_C, InputEvent.ALT_DOWN_MASK); 462 ValueResolver res = new ValueResolver() { 463 public Object getValue() { 464 editor.setCaretPosition(7, 12); 465 editor.pushKey(KeyEvent.VK_C, InputEvent.ALT_DOWN_MASK); 466 return editor.getText().contains("system.out.println"); 467 } 468 }; 469 waitMaxMilisForValue(10000, res, Boolean.TRUE); 470 assertTrue("Action not performed",editor.getText().contains("system.out.println")); 471 kmo = KeyMapOperator.invoke(); 472 closed = false; 473 assertEquals(kmo.profile().getSelectedItem(),NEW_PROFILE); 475 kmo.selectProfile("NetBeans"); 476 kmo.selectAction("Other|toggle-case-identifier-begin"); 477 checkListContents(kmo.shortcuts(), new Object []{}); 478 kmo.ok().push(); 479 new EventTool().waitNoEvent(500); 480 closed = true; 481 editor.setCaretPosition(7, 12); 483 editor.pushKey(KeyEvent.VK_C, InputEvent.ALT_DOWN_MASK); 484 waitMaxMilisForValue(5000, res, Boolean.FALSE); 485 assertTrue("Action performed",editor.getText().contains("system.out.println")); 486 kmo = KeyMapOperator.invoke(); 488 closed = false; 489 assertEquals("NetBeans",kmo.profile().getSelectedItem()); 490 kmo.selectProfile(NEW_PROFILE); 491 kmo.delete().push(); 492 assertEquals("Wrong number of profiles",originalSize,kmo.profile().getModel().getSize()); 493 kmo.ok().push(); 494 new EventTool().waitNoEvent(500); 495 closed = true; 496 } finally { 497 if(!closed && kmo!=null) kmo.cancel().push(); 498 editor.close(false); 499 } 500 } 501 502 public void testProfileRestore() { 503 KeyMapOperator kmo = null; 504 boolean closed = true; 505 try { 506 kmo = KeyMapOperator.invoke(); 507 closed = false; 508 kmo.selectProfile("NetBeans"); 510 kmo.selectAction("Other|selection-last-non-white"); 511 checkListContents(kmo.shortcuts(), new Object []{}); 512 kmo.add().push(); 513 new EventTool().waitNoEvent(100); 514 AddShortcutDialog asd = new AddShortcutDialog(); 515 asd.txtJTextField().pushKey(KeyEvent.VK_Q, InputEvent.ALT_DOWN_MASK); 516 asd.btOK().push(); 517 checkListContents(kmo.shortcuts(), "Alt+Q"); 518 kmo.selectAction("Other|caret-begin-line"); 520 checkListContents(kmo.shortcuts(), "HOME"); 521 kmo.shortcuts().setSelectedIndex(0); 522 kmo.remove().push(); 523 checkListContents(kmo.shortcuts(), new Object []{}); 524 kmo.ok().push(); 525 closed = true; 526 new EventTool().waitNoEvent(500); 527 editor.setCaretPosition(7, 12); 529 editor.pushKey(KeyEvent.VK_Q, InputEvent.ALT_DOWN_MASK); 530 new EventTool().waitNoEvent(100); 531 String text = editor.txtEditorPane().getSelectedText(); 532 assertEquals("tem.out.println(\"Hello\")",text); 533 int caretPositionOriginal = editor.txtEditorPane().getCaretPosition(); 534 editor.pushKey(KeyEvent.VK_HOME); 535 int caretPosition = editor.txtEditorPane().getCaretPosition(); 536 assertEquals("Caret was moved", caretPositionOriginal,caretPosition); 537 System.out.println("Caret position:"+caretPosition); 538 kmo = KeyMapOperator.invoke(); 539 closed = false; 540 kmo.restore().push(); 541 kmo.selectAction("Other|selection-last-non-white"); 542 checkListContents(kmo.shortcuts(), new Object []{}); 543 kmo.selectAction("Other|caret-begin-line"); 544 checkListContents(kmo.shortcuts(), "HOME"); 545 kmo.ok().push(); 546 closed = true; 547 new EventTool().waitNoEvent(500); 548 editor.setCaretPosition(7, 9); 549 caretPositionOriginal = editor.txtEditorPane().getCaretPosition(); 550 editor.txtEditorPane().setSelectionStart(-1); 551 editor.txtEditorPane().setSelectionEnd(-1); 552 editor.setCaretPosition(7, 12); 553 editor.pushKey(KeyEvent.VK_Q, InputEvent.ALT_DOWN_MASK); 554 new EventTool().waitNoEvent(100); 555 text = editor.txtEditorPane().getSelectedText(); 556 assertEquals(null,text); 557 editor.pushKey(KeyEvent.VK_HOME); 558 caretPosition = editor.txtEditorPane().getCaretPosition(); 559 assertEquals("Caret was not moved", caretPositionOriginal,caretPosition); 560 System.out.println("Caret position:"+caretPosition); 561 } finally { 562 if(!closed && kmo!=null) kmo.cancel().push(); 563 editor.close(false); 564 } 565 } 566 567 protected boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object requiredValue){ 568 int time = (int) maxMiliSeconds / 100; 569 while (time > 0) { 570 Object resolvedValue = resolver.getValue(); 571 if (requiredValue == null && resolvedValue == null){ 572 return true; 573 } 574 if (requiredValue != null && requiredValue.equals(resolvedValue)){ 575 return true; 576 } 577 try { 578 Thread.currentThread().sleep(100); 579 } catch (InterruptedException ex) { 580 time=0; 581 } 582 time--; 583 } 584 return false; 585 } 586 587 588 private void checkListContents(JListOperator oper,Object ... items) { 589 ListModel model = oper.getModel(); 590 assertEquals("List does not contains expected number of items", items.length, model.getSize()); 591 for (int i = 0; i < items.length; i++) { 592 Object object = items[i]; 593 assertEquals(items[i],model.getElementAt(i)); 594 } 595 } 596 597 public static void main(String [] args) { 598 TestRunner.run(new NbTestSuite(KeyMapTest.class)); 599 } 600 601 } 602 | Popular Tags |