1 19 20 package gui.windowsystem; 21 22 import java.io.File ; 23 import java.io.PrintStream ; 24 import java.io.PrintWriter ; 25 import java.io.FileOutputStream ; 26 27 import javax.swing.JMenuBar ; 28 import javax.swing.JPopupMenu ; 29 30 import org.netbeans.jellytools.JellyTestCase; 31 import org.netbeans.jellytools.MainWindowOperator; 32 import org.netbeans.jellytools.NbDialogOperator; 33 import org.netbeans.jellytools.OptionsOperator; 34 import org.netbeans.jellytools.ProjectsTabOperator; 35 36 import org.netbeans.jellytools.actions.DeleteAction; 37 import org.netbeans.jellytools.actions.RenameAction; 38 39 import org.netbeans.jellytools.nodes.Node; 40 import org.netbeans.jellytools.nodes.SourcePackagesNode; 41 42 import org.netbeans.jemmy.EventTool; 43 import org.netbeans.jemmy.JemmyException; 44 import org.netbeans.jemmy.JemmyProperties; 45 import org.netbeans.jemmy.TestOut; 46 import org.netbeans.jemmy.operators.JPopupMenuOperator; 47 import org.netbeans.jemmy.operators.JTextFieldOperator; 48 import org.netbeans.jemmy.operators.JTreeOperator; 49 50 import org.netbeans.junit.NbTestSuite; 51 52 53 54 58 public class MainMenu extends JellyTestCase { 59 60 protected PrintStream err; 61 protected PrintStream log; 62 63 private static ProjectsTabOperator projects ; 64 65 private static final String coreBundle = "org.netbeans.core.Bundle"; 66 private static final String openideActionsBundle = "org.openide.actions.Bundle"; 67 68 private static final String testProject_name="SampleProject"; 69 70 private static final String ideConfiguration = fromBundle(coreBundle, "UI/Services/IDEConfiguration"); private static final String lookAndFeel = fromBundle(coreBundle, "UI/Services/IDEConfiguration/LookAndFeel"); private static final String menuBar = fromBundle(coreBundle, "Menu"); private static final String toolbars = fromBundle(coreBundle, "Toolbars"); 75 private static String lookAndFeelNodeOptions = ideConfiguration + "|" + lookAndFeel; 76 private static String menuBarNodeOptionsPath = lookAndFeelNodeOptions + "|" + menuBar; 77 private static String toolbarsNodeOptions = lookAndFeelNodeOptions + "|" + toolbars; 78 private static String separator = ""; 80 private static String versioning = ""; private static String versioningMenuBarOptionsNodePath = menuBarNodeOptionsPath + "|" + versioning; 82 83 private static final String MOVE_UP = "Move Up"; 84 private static final String MOVE_DOWN = "Move Down"; 85 private static final String SEPARATOR = ""; 87 private JMenuBar mainMenu; 88 89 private static OptionsOperator options = null; 90 91 public MainMenu(java.lang.String testName) { 92 super(testName); 93 } 94 95 public static NbTestSuite suite() { 96 97 NbTestSuite suite = new NbTestSuite(); 98 99 suite.addTest(new MainMenu("testMainMenuMnemonicsCollision")); 100 suite.addTest(new MainMenu("testMainMenuShortCutCollision")); 101 102 suite.addTest(new MainMenu("testMenuItemsWhenProjectSelected")); 103 suite.addTest(new MainMenu("testMenuItemsWhenSourcePackagesSelected")); 104 suite.addTest(new MainMenu("testMenuItemsWhenPackageSelected")); 105 suite.addTest(new MainMenu("testMenuItemsWhenJavaSelected")); 106 suite.addTest(new MainMenu("testMenuItemsWhenFormSelected")); 107 108 suite.addTest(new MainMenu("testPopupMenuItemsWhenProjectSelected")); 109 suite.addTest(new MainMenu("testPopupMenuItemsWhenSourcePackagesSelected")); 110 suite.addTest(new MainMenu("testPopupMenuItemsWhenPackageSelected")); 111 suite.addTest(new MainMenu("testPopupMenuItemsWhenJavaSelected")); 112 suite.addTest(new MainMenu("testPopupMenuItemsWhenFormSelected")); 113 130 return suite; 131 } 132 133 public void setUp() { 134 err = getLog(); 136 log = getRef(); 137 138 try { 139 JemmyProperties.getProperties().setOutput(new TestOut(null, new PrintWriter (err, true), new PrintWriter (err, true), null)); 141 mainMenu = MainWindowOperator.getDefault().getJMenuBar(); 142 projects = new ProjectsTabOperator(); 143 }catch(Exception exc) { 144 failTest(exc, "Fail setUp() - maybe MainFrame hasn't menubar"); 145 } 146 147 } 148 149 public void testMainMenuMnemonicsCollision() { 150 String collisions = MenuChecker.checkMnemonicCollision(); 151 assertFalse(collisions, collisions.length()>0); 152 } 153 154 155 public void testMainMenuShortCutCollision() { 156 String collisions = MenuChecker.checkShortCutCollision(); 157 assertFalse(collisions, collisions.length()>0); 158 } 159 160 161 public void testMenuItemsWhenProjectSelected() { 162 selectNodeAndCheckMainMenu(projects.getProjectRootNode(testProject_name)); 163 } 164 165 public void testMenuItemsWhenSourcePackagesSelected() { 166 selectNodeAndCheckMainMenu(new SourcePackagesNode(testProject_name)); 167 } 168 169 public void testMenuItemsWhenPackageSelected() { 170 selectNodeAndCheckMainMenu(new Node(new SourcePackagesNode(testProject_name), "sampleproject")); 171 } 172 173 public void testMenuItemsWhenJavaSelected() { 174 selectNodeAndCheckMainMenu(new Node(new SourcePackagesNode(testProject_name), "sampleproject|JavaFile.java")); 175 } 176 177 public void testMenuItemsWhenFormSelected() { 178 selectNodeAndCheckMainMenu(new Node(new SourcePackagesNode(testProject_name), "sampleproject|FormFile.java")); 179 } 180 181 182 public void testPopupMenuItemsWhenProjectSelected() { 183 selectNodeAndCheckPopupMenu(projects.getProjectRootNode(testProject_name)); 184 } 185 186 public void testPopupMenuItemsWhenSourcePackagesSelected() { 187 selectNodeAndCheckPopupMenu(new SourcePackagesNode(testProject_name)); 188 } 189 190 public void testPopupMenuItemsWhenPackageSelected() { 191 selectNodeAndCheckPopupMenu(new Node(new SourcePackagesNode(testProject_name), "sampleproject")); 192 } 193 194 public void testPopupMenuItemsWhenJavaSelected() { 195 selectNodeAndCheckPopupMenu(new Node(new SourcePackagesNode(testProject_name), "sampleproject|JavaFile.java")); 196 } 197 198 public void testPopupMenuItemsWhenFormSelected() { 199 selectNodeAndCheckPopupMenu(new Node(new SourcePackagesNode(testProject_name), "sampleproject|FormFile.java")); 200 } 201 202 203 public void testAddingNewSeparator(){ 204 new EventTool().waitNoEvent(10); 205 206 try { 207 openOptions(); 208 Node versioningMenuBarOptionsNode = new Node(options.treeTable().tree(), versioningMenuBarOptionsNodePath); 209 210 versioningMenuBarOptionsNode.performPopupAction(fromBundle(openideActionsBundle, "New")+"|"+separator); 211 new EventTool().waitNoEvent(1000); 212 213 options.selectOption(versioningMenuBarOptionsNodePath+"|"+SEPARATOR); 215 } catch(Exception exc) { 216 failTest(exc, "Exception: "+exc.getMessage()+" arises."); 217 } 218 } 219 220 public void testMoveUpDownSeparator() { 221 new EventTool().waitNoEvent(10); 222 String initialState, finalState; 223 224 try { 225 openOptions(); 226 227 String fileNodePath = menuBarNodeOptionsPath + "|" + fromBundleNotTrimmed(coreBundle, "Menu/File"); 228 229 initialState = printChildrenInTreePath(options.treeTable().tree(), fileNodePath, "|"); 230 231 makeLog(initialState, "Initial State"); 232 233 Node moveNode = new Node(options.treeTable().tree(), fileNodePath + "|" + SEPARATOR); 234 235 moveNode.performPopupAction(MOVE_DOWN); 236 new EventTool().waitNoEvent(200); 237 238 moveNode.performPopupAction(MOVE_UP); 239 new EventTool().waitNoEvent(200); 240 241 moveNode.performPopupAction(MOVE_DOWN); 242 new EventTool().waitNoEvent(200); 243 244 moveNode.performPopupAction(MOVE_DOWN); 245 new EventTool().waitNoEvent(1000); 246 247 moveNode.performPopupAction(MOVE_UP); 248 new EventTool().waitNoEvent(200); 249 250 moveNode.performPopupAction(MOVE_UP); 251 new EventTool().waitNoEvent(200); 252 253 finalState = printChildrenInTreePath(options.treeTable().tree(), fileNodePath, "|"); 254 makeLog(finalState, "Final State"); 255 256 if(initialState.compareTo(finalState)!=0){ 257 throw new JemmyException("Initial State of menu {"+fileNodePath+"} isn't the same after change order items operations"); 258 } 259 260 } catch(Exception exc) { 261 failTest(exc, "Exception: "+exc.getMessage()+" arises."); 262 } 263 264 } 265 266 public void testDeletingSeparator(){ 267 String confirmDeletionTitle = fromBundle("org.openide.explorer.Bundle", "MSG_ConfirmDeleteObjectTitle"); deleteNodeInOptions(SEPARATOR, versioningMenuBarOptionsNodePath, confirmDeletionTitle); 269 } 270 271 272 public void testAddNewMenu(){ 273 new EventTool().waitNoEvent(10); 274 275 try { 276 openOptions(); 277 278 Node menuBarOptionsNode = new Node(options.treeTable().tree(), menuBarNodeOptionsPath); 279 280 menuBarOptionsNode.performPopupActionNoBlock("Add Menu"); 282 new NbDialogOperator(fromBundle(coreBundle, "CTL_newMenuDialog")).ok(); 283 new EventTool().waitNoEvent(200); 284 285 options.selectOption(menuBarNodeOptionsPath + "|" + fromBundle(coreBundle, "CTL_newMenu")); 287 } catch(Exception exc) { 288 failTest(exc, "Exception: "+exc.getMessage()+" arises."); 289 } 290 291 } 292 293 public void testChangeOrderOfMenu(){ 294 new EventTool().waitNoEvent(100); 295 296 try { 297 File ref = new File (this.getWorkDir(), "InfoAboutMenu.check"); 298 File golden = new File (this.getWorkDir(), "InfoAboutMenu.gold"); 299 300 openOptions(); 301 302 String moveNodePath = menuBarNodeOptionsPath + "|" + fromBundleNotTrimmed(coreBundle, "Menu/File"); 303 Node moveNode = new Node(options.treeTable().tree(), moveNodePath); 304 305 new PrintStream (new FileOutputStream (golden)).println(printChildrenInTreePath(options.treeTable().tree(), menuBarNodeOptionsPath,"|")); 306 err.println("\n ======= BEFORE : \n " + printChildrenInTreePath(options.treeTable().tree(), menuBarNodeOptionsPath,"|") + " \n ============="); 307 308 moveNode.performPopupAction(MOVE_DOWN); 309 new EventTool().waitNoEvent(200); 310 311 moveNode.performPopupAction(MOVE_UP); 312 new EventTool().waitNoEvent(200); 313 314 moveNode.performPopupAction(MOVE_DOWN); 315 new EventTool().waitNoEvent(200); 316 317 moveNode.performPopupAction(MOVE_UP); 318 new EventTool().waitNoEvent(200); 319 320 new PrintStream (new FileOutputStream (ref)).println(printChildrenInTreePath(options.treeTable().tree(), menuBarNodeOptionsPath,"|")); 321 err.println("\n ======= AFTER : \n " + printChildrenInTreePath(options.treeTable().tree(), menuBarNodeOptionsPath,"|") + " \n ============="); 322 323 assertFile("Menu before and after isn't the same and it should be",ref, golden, this.getWorkDir()); 324 325 328 330 } catch(Exception exc) { 331 failTest(exc, "Exception: "+exc.getMessage()+" arises."); 332 } 333 334 } 335 336 337 338 public void testDeleteMenuItem() { 339 String file = fromBundleNotTrimmed(coreBundle, "Menu/File"); 340 String confirmDeletionTitle = fromBundle("org.openide.explorer.Bundle", "MSG_ConfirmDeleteObjectTitle"); deleteNodeInOptions("Pa&ge Setup", menuBarNodeOptionsPath + "|" + file, confirmDeletionTitle); } 343 344 345 public void testDeleteMenu() { 346 String confirmDeletionTitle = fromBundle("org.openide.explorer.Bundle", "MSG_ConfirmDeleteObjectTitle"); String menuXX = fromBundle(coreBundle, "CTL_newMenuName"); deleteNodeInOptions(menuXX, menuBarNodeOptionsPath, confirmDeletionTitle); 349 } 350 351 352 public void testRenameMenu() { 353 String build = fromBundleNotTrimmed(coreBundle, "Menu/Build"); renameNodeInOptions(menuBarNodeOptionsPath, build, "TestMenu"); 355 } 356 357 public void testCreateNewToolbar() { 358 new EventTool().waitNoEvent(100); 359 360 try { 361 openOptions(); 362 363 new Node(options.treeTable().tree(), toolbarsNodeOptions).performPopupActionNoBlock("Add Toolbar"); 365 new NbDialogOperator("New Toolbar").ok(); new EventTool().waitNoEvent(200); 367 368 options.selectOption(toolbarsNodeOptions + "|Toolbar"); 370 } catch(Exception exc) { 371 failTest(exc, "Exception: "+exc.getMessage()+" arises."); 372 } 373 } 374 375 public void testRenameToolbar() { 376 String build = fromBundleNotTrimmed(coreBundle, "Toolbars/Build"); renameNodeInOptions(toolbarsNodeOptions, build, "TestBuild"); 378 } 379 380 381 private void renameNodeInOptions(String parentNodeName, String oldNodeName, String newNodeName) { 382 new EventTool().waitNoEvent(100); 383 384 try { 385 openOptions(); 386 387 Node renamedNode = new Node(options.treeTable().tree(), parentNodeName + "|" + oldNodeName); 388 new RenameAction().performPopup(renamedNode); 389 390 String rename = fromBundle(openideActionsBundle, "CTL_RenameTitle"); NbDialogOperator renameDialog = new NbDialogOperator(rename); 392 JTextFieldOperator dialogTextField = new JTextFieldOperator(renameDialog,0); 393 dialogTextField.setText(newNodeName); 394 String realNewName = dialogTextField.getText(); 395 renameDialog.ok(); 396 397 log("=========================== New name is {" + realNewName + "}."); 398 399 new EventTool().waitNoEvent(300); 400 401 options.selectOption(parentNodeName + "|" + realNewName); 402 403 } catch(Exception exc) { 404 failTest(exc, "Exception: "+exc.getMessage()+" arises."); 405 } 406 } 407 408 409 private void deleteNodeInOptions(String nodeName, String parentNode, String confirmationDialogTitle) { 410 boolean ariseJemmyException = false; 411 String deleteNodePath = parentNode + "|" + nodeName; 412 413 try { 414 openOptions(); 415 416 Node deleteNode = new Node(options.treeTable().tree(), deleteNodePath); 417 new DeleteAction().performPopup(deleteNode); 418 419 closeConfirmDialog(confirmationDialogTitle); 420 421 try { 423 options.selectOption(deleteNodePath); 424 } catch(JemmyException ex) { ariseJemmyException = true; 426 } 427 428 if(!ariseJemmyException) 429 fail("Nonexpected node {" + nodeName + "} is under node {" + deleteNode + "}."); 430 431 } catch(Exception exc) { 432 failTest(exc, "Exception: "+exc.getMessage()+" arises."); 433 } 434 435 } 436 437 438 protected void selectNodeAndCheckMainMenu(Node nodeToSelect) { 439 err.println("=========== try to select node ["+nodeToSelect.getPath()+"]."); 440 441 nodeToSelect.select(); 442 443 new EventTool().waitNoEvent(100); 444 MenuChecker.visitMenuBar(mainMenu); 445 446 String file = fromBundle(coreBundle, "Menu/File"); String edit = fromBundle(coreBundle, "Menu/Edit"); String build = fromBundle(coreBundle, "Menu/Build"); 450 MenuChecker.printMenuBarStructure(mainMenu, log, file+", "+edit+", "+build, true, true); 451 452 compareReferenceFiles(); 453 } 454 455 protected void selectNodeAndCheckPopupMenu(Node nodeToSelect) { 456 err.println("=========== try to select node ["+nodeToSelect.getPath()+"]."); 457 458 nodeToSelect.select(); 459 460 JPopupMenuOperator popupOperator = nodeToSelect.callPopup(); 461 new EventTool().waitNoEvent(500); 462 JPopupMenu popup = (JPopupMenu )popupOperator.getSource(); 463 464 String tools = fromBundle(coreBundle, "Menu/Tools"); MenuChecker.printPopupMenuStructure(popup, log, tools, true, true); 466 468 if(popupOperator!=null) 469 popupOperator.pressKey(java.awt.event.KeyEvent.VK_ESCAPE); 470 471 473 compareReferenceFiles(); 474 } 475 476 protected String printChildrenInTreePath(JTreeOperator operator, String path, String delim) { 477 StringBuffer buffer = new StringBuffer (""); 478 479 operator.expandPath(operator.findPath(path, delim)); 480 javax.swing.tree.TreePath children[] = operator.getChildPaths(operator.findPath(path, delim)); 481 482 for(int i=0;i<children.length;i++) { 483 buffer.append("\n"+children[i].toString()); 484 } 485 486 return buffer.toString(); 487 } 488 489 490 private void closeConfirmDialog(String title) { 491 new NbDialogOperator(title).yes(); 492 new EventTool().waitNoEvent(1000); 493 } 494 495 496 private void makeLog(String logString, String logTitle) { 497 log("---------- " + logTitle + " >>>>>>>"); 498 log(logString); 499 log(">>>>>>>>>> " + logTitle + " -------"); 500 } 501 502 506 private void failTest(Exception exc, String message) { 507 try{ 508 getWorkDir(); 509 org.netbeans.jemmy.util.PNGEncoder.captureScreen(getWorkDirPath()+System.getProperty("file.separator")+"IDEscreenshot.png"); 510 }catch(Exception ioexc){ 511 log("Impossible make IDE screenshot!!! \n" + ioexc.toString()); 512 } 513 514 err.println("################################"); 515 exc.printStackTrace(err); 516 err.println("################################"); 517 fail(message); 518 } 519 520 545 565 566 private static void openOptions(){ 567 if (options == null) { 568 options = OptionsOperator.invoke(); 569 new EventTool().waitNoEvent(500); 570 } 571 572 } 573 574 private static String fromBundle(String bundle, String key) { 575 return org.netbeans.jellytools.Bundle.getStringTrimmed(bundle, key); 576 } 577 578 private static String fromBundleNotTrimmed(String bundle, String key) { 579 return org.netbeans.jellytools.Bundle.getString(bundle, key); 580 } 581 582 public static void main(java.lang.String [] args) { 583 junit.textui.TestRunner.run(suite()); 584 } 585 } 586 | Popular Tags |