1 /******************************************************************************* 2 * Copyright (c) 2000, 2013 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.ui.tests.api; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IMarker; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.ui.IEditorInput; 21 import org.eclipse.ui.IEditorPart; 22 import org.eclipse.ui.IPageLayout; 23 import org.eclipse.ui.IPerspectiveDescriptor; 24 import org.eclipse.ui.IViewPart; 25 import org.eclipse.ui.IWorkbenchPage; 26 import org.eclipse.ui.IWorkbenchWindow; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.ide.IDE; 29 import org.eclipse.ui.part.FileEditorInput; 30 import org.eclipse.ui.tests.harness.util.ArrayUtil; 31 import org.eclipse.ui.tests.harness.util.CallHistory; 32 import org.eclipse.ui.tests.harness.util.EmptyPerspective; 33 import org.eclipse.ui.tests.harness.util.FileUtil; 34 import org.eclipse.ui.tests.harness.util.UITestCase; 35 import org.junit.Ignore; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.junit.runners.JUnit4; 39 40 @RunWith(JUnit4.class) 41 public class IDeprecatedWorkbenchPageTest extends UITestCase { 42 43 private IWorkbenchPage fActivePage; 44 45 private IWorkbenchWindow fWin; 46 47 private IProject proj; 48 IDeprecatedWorkbenchPageTest()49 public IDeprecatedWorkbenchPageTest() { 50 super(IDeprecatedWorkbenchPageTest.class.getSimpleName()); 51 } 52 53 @Override doSetUp()54 protected void doSetUp() throws Exception { 55 super.doSetUp(); 56 fWin = openTestWindow(); 57 fActivePage = fWin.getActivePage(); 58 } 59 60 @Override doTearDown()61 protected void doTearDown() throws Exception { 62 super.doTearDown(); 63 if (proj != null) { 64 FileUtil.deleteProject(proj); 65 proj = null; 66 } 67 } 68 69 /** 70 * tests both of the following: setEditorAreaVisible() isEditorAreaVisible() 71 */ 72 @Test testGet_SetEditorAreaVisible()73 public void testGet_SetEditorAreaVisible() throws Throwable { 74 fActivePage.setEditorAreaVisible(true); 75 assertTrue(fActivePage.isEditorAreaVisible() == true); 76 77 fActivePage.setEditorAreaVisible(false); 78 assertTrue(fActivePage.isEditorAreaVisible() == false); 79 } 80 81 @Test testGetPerspective()82 public void testGetPerspective() throws Throwable { 83 assertNotNull(fActivePage.getPerspective()); 84 85 IWorkbenchPage page = fWin.openPage(EmptyPerspective.PERSP_ID, 86 getPageInput()); 87 assertEquals(EmptyPerspective.PERSP_ID, page.getPerspective().getId()); 88 } 89 90 @Test testSetPerspective()91 public void testSetPerspective() throws Throwable { 92 IPerspectiveDescriptor per = PlatformUI.getWorkbench() 93 .getPerspectiveRegistry().findPerspectiveWithId( 94 EmptyPerspective.PERSP_ID); 95 fActivePage.setPerspective(per); 96 assertEquals(per, fActivePage.getPerspective()); 97 } 98 99 @Test testGetLabel()100 public void testGetLabel() { 101 assertNotNull(fActivePage.getLabel()); 102 } 103 104 @Test testGetInput()105 public void testGetInput() throws Throwable { 106 IAdaptable input = getPageInput(); 107 IWorkbenchPage page = fWin.openPage(input); 108 assertEquals(input, page.getInput()); 109 } 110 111 @Test testActivate()112 public void testActivate() throws Throwable { 113 MockViewPart part = (MockViewPart) fActivePage 114 .showView(MockViewPart.ID); 115 MockViewPart part2 = (MockViewPart) fActivePage 116 .showView(MockViewPart.ID2); 117 118 MockPartListener listener = new MockPartListener(); 119 fActivePage.addPartListener(listener); 120 fActivePage.activate(part); 121 122 CallHistory callTrace; 123 124 callTrace = part2.getCallHistory(); 125 callTrace.clear(); 126 fActivePage.activate(part2); 127 assertTrue(callTrace.contains("setFocus")); 128 assertTrue(listener.getCallHistory().contains("partActivated")); 129 130 callTrace = part.getCallHistory(); 131 callTrace.clear(); 132 fActivePage.activate(part); 133 assertTrue(callTrace.contains("setFocus")); 134 assertTrue(listener.getCallHistory().contains("partActivated")); 135 } 136 137 @Test testBringToTop()138 public void testBringToTop() throws Throwable { 139 proj = FileUtil.createProject("testOpenEditor"); 140 IEditorPart part = IDE.openEditor(fActivePage, FileUtil.createFile( 141 "a.mock1", proj), true); 142 IEditorPart part2 = IDE.openEditor(fActivePage, FileUtil.createFile( 143 "b.mock1", proj), true); 144 145 MockPartListener listener = new MockPartListener(); 146 fActivePage.addPartListener(listener); 147 CallHistory callTrace = listener.getCallHistory(); 148 149 // at this point, part2 is active 150 fActivePage.bringToTop(part); 151 assertEquals(callTrace.contains("partBroughtToTop"), true); 152 153 callTrace.clear(); 154 fActivePage.bringToTop(part2); 155 assertEquals(callTrace.contains("partBroughtToTop"), true); 156 } 157 158 @Test testGetWorkbenchWindow()159 public void testGetWorkbenchWindow() { 160 /* 161 * Commented out because until test case can be updated to work with new 162 * window/page/perspective implementation 163 * 164 * assertEquals(fActivePage.getWorkbenchWindow(), fWin); IWorkbenchPage 165 * page = openTestPage(fWin); assertEquals(page.getWorkbenchWindow(), 166 * fWin); 167 */ 168 } 169 170 @Test testShowView()171 public void testShowView() throws Throwable { 172 /* 173 * javadoc: Shows a view in this page and give it focus 174 */ 175 MockViewPart view = (MockViewPart) fActivePage 176 .showView(MockViewPart.ID); 177 assertNotNull(view); 178 assertTrue(view.getCallHistory().verifyOrder( 179 new String[] { "init", "createPartControl", "setFocus" })); 180 181 fActivePage.showView(MockViewPart.ID2); 182 183 /* 184 * javadoc: If the view is already visible, it is given focus 185 */ 186 CallHistory callTrace = view.getCallHistory(); 187 callTrace.clear(); 188 assertEquals(fActivePage.showView(MockViewPart.ID), view); 189 assertEquals(callTrace.contains("setFocus"), true); 190 } 191 192 /** 193 * openEditor(IFile input) 194 */ 195 @Test testOpenEditor()196 public void testOpenEditor() throws Throwable { 197 proj = FileUtil.createProject("testOpenEditor"); 198 199 /* 200 * javadoc: 1. The workbench editor registry is consulted to determine 201 * if an editor extension has been registered for the file type. If so, 202 * an instance of the editor extension is opened on the file 203 */ 204 IFile file = FileUtil.createFile("test.mock1", proj); 205 IEditorPart editor = IDE.openEditor(fActivePage, file, true); 206 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 207 assertEquals(fActivePage.getActiveEditor(), editor); 208 assertEquals(editor.getSite().getId(), fWorkbench.getEditorRegistry() 209 .getDefaultEditor(file.getName()).getId()); 210 211 /* 212 * javadoc: 2. Next, the native operating system will be consulted to 213 * determine if a native editor exists for the file type. If so, a new 214 * process is started and the native editor is opened on the file. 215 */ 216 // can not be tested 217 /* 218 * javadoc: 3. If all else fails the file will be opened in a default 219 * text editor. 220 */ 221 file = FileUtil.createFile("a.null and void", proj); 222 editor = IDE.openEditor(fActivePage, file, true); 223 if (editor != null) {//Editor may be external 224 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), 225 true); 226 assertEquals(fActivePage.getActiveEditor(), editor); 227 assertEquals(editor.getSite().getId(), 228 "org.eclipse.ui.DefaultTextEditor"); 229 230 // open another editor to take the focus away from the first editor 231 IDE.openEditor(fActivePage, 232 FileUtil.createFile("test.mock2", proj), true); 233 234 /* 235 * javadoc: If this page already has an editor open on the target 236 * object that editor is activated 237 */ 238 // open the editor second time. 239 assertEquals(editor, IDE.openEditor(fActivePage, file, true)); 240 assertEquals(editor, fActivePage.getActiveEditor()); 241 } 242 } 243 244 /** 245 * openEditor(IFile input, String editorID) 246 */ 247 @Test testOpenEditor2()248 public void testOpenEditor2() throws Throwable { 249 proj = FileUtil.createProject("testOpenEditor"); 250 final IFile file = FileUtil.createFile("asfasdasdf", proj); 251 final String id = MockEditorPart.ID1; 252 253 /* 254 * javadoc: The editor type is determined by mapping editorId to an 255 * editor extension registered with the workbench. 256 */ 257 IEditorPart editor = fActivePage.openEditor(new FileEditorInput(file), 258 id); 259 assertEquals(editor.getSite().getId(), id); 260 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 261 assertEquals(fActivePage.getActiveEditor(), editor); 262 263 // open another editor to take the focus away from the first editor 264 IDE.openEditor(fActivePage, FileUtil.createFile("test.mock2", proj), 265 true); 266 267 /* 268 * javadoc: If this page already has an editor open on the target object 269 * that editor is activated 270 */ 271 // open the first editor second time. 272 assertEquals(fActivePage.openEditor(new FileEditorInput(file), id), 273 editor); 274 assertEquals(fActivePage.getActiveEditor(), editor); 275 } 276 277 /** 278 * openEditor(IEditorInput input,String editorId) 279 */ 280 @Test testOpenEditor3()281 public void testOpenEditor3() throws Throwable { 282 proj = FileUtil.createProject("testOpenEditor"); 283 final String id = MockEditorPart.ID1; 284 IEditorInput input = new FileEditorInput(FileUtil.createFile( 285 "test.mock1", proj)); 286 287 /* 288 * javadoc: The editor type is determined by mapping editorId to an 289 * editor extension registered with the workbench 290 */ 291 IEditorPart editor = fActivePage.openEditor(input, id); 292 assertEquals(editor.getEditorInput(), input); 293 assertEquals(editor.getSite().getId(), id); 294 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 295 assertEquals(fActivePage.getActiveEditor(), editor); 296 297 // open another editor to take the focus away from the first editor 298 IDE.openEditor(fActivePage, FileUtil.createFile("test.mock2", proj), 299 true); 300 301 /* 302 * javadoc: If this page already has an editor open on the target object 303 * that editor is activated 304 */ 305 // open the first editor second time. 306 assertEquals(fActivePage.openEditor(input, id), editor); 307 assertEquals(fActivePage.getActiveEditor(), editor); 308 } 309 310 /** 311 * openEditor(IEditorInput input, String editorId, boolean activate) 312 */ 313 @Test testOpenEditor4()314 public void testOpenEditor4() throws Throwable { 315 proj = FileUtil.createProject("testOpenEditor"); 316 final String id = MockEditorPart.ID1; 317 IEditorInput input = new FileEditorInput(FileUtil.createFile( 318 "test.mock1", proj)); 319 MockPartListener listener = new MockPartListener(); 320 fActivePage.addPartListener(listener); 321 CallHistory callTrace = listener.getCallHistory(); 322 323 /* 324 * javadoc: The editor type is determined by mapping editorId to an 325 * editor extension registered with the workbench. javadoc: If activate == 326 * true the editor will be activated 327 */ 328 // open an editor with activation 329 IEditorPart editor = fActivePage.openEditor(input, id, true); 330 assertEquals(editor.getEditorInput(), input); 331 assertEquals(editor.getSite().getId(), id); 332 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 333 assertEquals(fActivePage.getActiveEditor(), editor); 334 assertEquals(callTrace.contains("partActivated"), true); 335 336 // we need another editor so that the editor under test can receive 337 // events. 338 // otherwise, events will be ignored. 339 IEditorPart extra = IDE.openEditor(fActivePage, FileUtil.createFile( 340 "aaaaa", proj), true); 341 342 // close the first editor after the second has opened; necessary for 343 // test to work with fix to PR 7743 344 fActivePage.closeEditor(editor, false); 345 346 // Activate something in a different stack, or the editor will end up 347 // activated regardless of 348 // the activate flag. 349 fActivePage.showView(IPageLayout.ID_PROBLEM_VIEW, null, 350 IWorkbenchPage.VIEW_ACTIVATE); 351 // open an editor without activation 352 callTrace.clear(); 353 editor = fActivePage.openEditor(input, id, false); 354 assertEquals(editor.getEditorInput(), input); 355 assertEquals(editor.getSite().getId(), id); 356 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 357 assertEquals(callTrace.contains("partActivated"), false); 358 assertEquals(callTrace.contains("partBroughtToTop"), true); 359 360 fActivePage.activate(extra); 361 362 /* 363 * javadoc: If this page already has an editor open on the target object 364 * that editor is brought to the front 365 */ 366 // Activate something in a different stack, or the editor will end up 367 // activated regardless of 368 // the activate flag. 369 fActivePage.showView(IPageLayout.ID_PROBLEM_VIEW, null, 370 IWorkbenchPage.VIEW_ACTIVATE); 371 // open the editor under test second time without activation 372 callTrace.clear(); 373 assertEquals(fActivePage.openEditor(input, id, false), editor); 374 assertEquals(callTrace.contains("partBroughtToTop"), true); 375 assertEquals(callTrace.contains("partActivated"), false); 376 377 // activate the other editor 378 fActivePage.activate(extra); 379 380 /* 381 * javadoc: If activate == true the editor will be activated 382 */ 383 // open the editor under test second time with activation 384 callTrace.clear(); 385 assertEquals(fActivePage.openEditor(input, id, true), editor); 386 assertEquals(callTrace.contains("partBroughtToTop"), true); 387 assertEquals(callTrace.contains("partActivated"), true); 388 } 389 390 /** 391 * openEditor(IMarker marker) 392 */ 393 @Test testOpenEditor5()394 public void testOpenEditor5() throws Throwable { 395 proj = FileUtil.createProject("testOpenEditor"); 396 IMarker marker = FileUtil.createFile("aa.mock2", proj).createMarker( 397 IMarker.TASK); 398 CallHistory callTrace; 399 400 /* 401 * javadoc: the cursor and selection state of the editor is then updated 402 * from information recorded in the marker. 403 */ 404 // open the registered editor for the marker resource 405 IEditorPart editor = IDE.openEditor(fActivePage, marker, true); 406 callTrace = ((MockEditorPart) editor).getCallHistory(); 407 assertEquals(editor.getSite().getId(), MockEditorPart.ID2); 408 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 409 assertEquals(fActivePage.getActiveEditor(), editor); 410 assertEquals(callTrace.contains("gotoMarker"), true); 411 fActivePage.closeEditor(editor, false); 412 413 /* 414 * javadoc: If the marker contains an EDITOR_ID_ATTR attribute the 415 * attribute value will be used to determine the editor type to be 416 * opened 417 */ 418 marker.setAttribute(IWorkbenchPage.EDITOR_ID_ATTR, MockEditorPart.ID1); 419 editor = IDE.openEditor(fActivePage, marker, true); 420 callTrace = ((MockEditorPart) editor).getCallHistory(); 421 assertEquals(editor.getSite().getId(), MockEditorPart.ID1); 422 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 423 assertEquals(fActivePage.getActiveEditor(), editor); 424 assertEquals(callTrace.contains("gotoMarker"), true); 425 // do not close the editor this time 426 427 /* 428 * javdoc: If this page already has an editor open on the target object 429 * that editor is activated 430 */ 431 callTrace.clear(); 432 assertEquals(IDE.openEditor(fActivePage, marker, true), editor); 433 assertEquals(fActivePage.getActiveEditor(), editor); 434 assertEquals(callTrace.contains("gotoMarker"), true); 435 fActivePage.closeEditor(editor, false); 436 } 437 438 /** 439 * openEditor(IMarker marker, boolean activate) 440 */ 441 @Test testOpenEditor6()442 public void testOpenEditor6() throws Throwable { 443 proj = FileUtil.createProject("testOpenEditor"); 444 IMarker marker = FileUtil.createFile("aa.mock2", proj).createMarker( 445 IMarker.TASK); 446 MockPartListener listener = new MockPartListener(); 447 fActivePage.addPartListener(listener); 448 CallHistory listenerCall = listener.getCallHistory(); 449 CallHistory editorCall; 450 451 // we need another editor so that the editor under test can receive 452 // events. 453 // otherwise, events will be ignored. 454 IEditorPart extra = IDE.openEditor(fActivePage, FileUtil.createFile( 455 "aaaaa", proj), true); 456 457 /* 458 * javadoc: If activate == true the editor will be activated 459 */ 460 // open the registered editor for the marker resource with activation 461 IEditorPart editor = IDE.openEditor(fActivePage, marker, true); 462 editorCall = ((MockEditorPart) editor).getCallHistory(); 463 assertEquals(editor.getSite().getId(), MockEditorPart.ID2); 464 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 465 assertEquals(fActivePage.getActiveEditor(), editor); 466 467 /* 468 * javadoc: the cursor and selection state of the editor is then updated 469 * from information recorded in the marker. 470 */ 471 assertEquals(editorCall.contains("gotoMarker"), true); 472 fActivePage.closeEditor(editor, false); 473 474 fActivePage.activate(extra); 475 476 // Activate something in a different stack, or the editor will end up 477 // activated regardless of 478 // the activate flag. 479 fActivePage.showView(IPageLayout.ID_PROBLEM_VIEW, null, 480 IWorkbenchPage.VIEW_ACTIVATE); 481 482 // open the registered editor for the marker resource without activation 483 listenerCall.clear(); 484 editor = IDE.openEditor(fActivePage, marker, false); 485 editorCall = ((MockEditorPart) editor).getCallHistory(); 486 assertEquals(editor.getSite().getId(), MockEditorPart.ID2); 487 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 488 assertEquals(listenerCall.contains("partBroughtToTop"), true); 489 assertEquals(listenerCall.contains("partActivated"), false); 490 assertEquals(editorCall.contains("gotoMarker"), true); 491 fActivePage.closeEditor(editor, false); 492 493 /* 494 * javadoc: If the marker contains an EDITOR_ID_ATTR attribute the 495 * attribute value will be used to determine the editor type to be 496 * opened 497 */ 498 String id = MockEditorPart.ID1; 499 marker.setAttribute(IWorkbenchPage.EDITOR_ID_ATTR, id); 500 501 // open an editor with activation 502 listenerCall.clear(); 503 504 editor = IDE.openEditor(fActivePage, marker, true); 505 editorCall = ((MockEditorPart) editor).getCallHistory(); 506 assertEquals(editor.getSite().getId(), id); 507 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 508 assertEquals(fActivePage.getActiveEditor(), editor); 509 assertEquals(editorCall.contains("gotoMarker"), true); 510 fActivePage.closeEditor(editor, false); 511 512 fActivePage.activate(extra); 513 514 // Activate something in a different stack, or the editor will end up 515 // activated regardless of 516 // the activate flag. 517 fActivePage.showView(IPageLayout.ID_PROBLEM_VIEW, null, 518 IWorkbenchPage.VIEW_ACTIVATE); 519 520 // open an editor without activation 521 listenerCall.clear(); 522 editor = IDE.openEditor(fActivePage, marker, false); 523 editorCall = ((MockEditorPart) editor).getCallHistory(); 524 assertEquals(editor.getSite().getId(), id); 525 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editor), true); 526 assertEquals(editorCall.contains("gotoMarker"), true); 527 assertEquals(listenerCall.contains("partActivated"), false); 528 assertEquals(listenerCall.contains("partBroughtToTop"), true); 529 // do not close the editor this time 530 531 fActivePage.activate(extra); 532 533 /* 534 * javadoc: If this page already has an editor open on the target object 535 * that editor is brought to front 536 */ 537 // Activate something in a different stack, or the editor will end up 538 // activated regardless of 539 // the activate flag. 540 fActivePage.showView(IPageLayout.ID_PROBLEM_VIEW, null, 541 IWorkbenchPage.VIEW_ACTIVATE); 542 // open the editor second time without activation 543 listenerCall.clear(); 544 assertEquals(IDE.openEditor(fActivePage, marker, false), editor); 545 assertEquals(listenerCall.contains("partBroughtToTop"), true); 546 assertEquals(listenerCall.contains("partActivated"), false); 547 548 fActivePage.activate(extra); 549 550 /* 551 * javdoc: If activate == true the editor will be activated 552 */ 553 // open the editor second time with activation 554 listenerCall.clear(); 555 assertEquals(IDE.openEditor(fActivePage, marker, true), editor); 556 assertEquals(editorCall.contains("gotoMarker"), true); 557 assertEquals(listenerCall.contains("partBroughtToTop"), true); 558 assertEquals(listenerCall.contains("partActivated"), true); 559 } 560 561 @Test testFindView()562 public void testFindView() throws Throwable { 563 String id = MockViewPart.ID3; 564 // id of valid, but not open view 565 assertNull(fActivePage.findView(id)); 566 567 IViewPart view = fActivePage.showView(id); 568 assertEquals(fActivePage.findView(id), view); 569 570 // close view 571 fActivePage.hideView(view); 572 assertNull(fActivePage.findView(id)); 573 } 574 575 @Test testGetViews()576 public void testGetViews() throws Throwable { 577 int totalBefore = fActivePage.getViews().length; 578 579 IViewPart view = fActivePage.showView(MockViewPart.ID2); 580 assertEquals(ArrayUtil.contains(fActivePage.getViews(), view), true); 581 assertEquals(fActivePage.getViews().length, totalBefore + 1); 582 583 fActivePage.hideView(view); 584 assertEquals(ArrayUtil.contains(fActivePage.getViews(), view), false); 585 assertEquals(fActivePage.getViews().length, totalBefore); 586 } 587 588 @Test testHideView()589 public void testHideView() throws Throwable { 590 IViewPart view = fActivePage.showView(MockViewPart.ID3); 591 592 fActivePage.hideView(view); 593 CallHistory callTrace = ((MockViewPart) view).getCallHistory(); 594 assertTrue(callTrace.contains("dispose")); 595 } 596 597 @Test 598 @Ignore XXXtestClose()599 public void XXXtestClose() throws Throwable { 600 IWorkbenchPage page = openTestPage(fWin); 601 602 proj = FileUtil.createProject("testOpenEditor"); 603 final IFile file = FileUtil.createFile("aaa.mock1", proj); 604 IEditorPart editor = IDE.openEditor(page, file, true); 605 CallHistory callTrace = ((MockEditorPart) editor).getCallHistory(); 606 callTrace.clear(); 607 608 /* 609 * javadoc: If the page has open editors with unsaved content and save 610 * is true, the user will be given the opportunity to save them 611 */ 612 assertEquals(page.close(), true); 613 assertEquals(callTrace 614 .verifyOrder(new String[] { "isDirty", "dispose" }), true); 615 assertEquals(fWin.getActivePage(), fActivePage); 616 } 617 618 @Test testCloseEditor()619 public void testCloseEditor() throws Throwable { 620 proj = FileUtil.createProject("testOpenEditor"); 621 final IFile file = FileUtil.createFile("test.mock1", proj); 622 IEditorPart editor; 623 CallHistory callTrace; 624 MockEditorPart mock; 625 626 /* 627 * javadoc: Parameters: save - true to save the editor contents if 628 * required (recommended) 629 */ 630 // create a clean editor that needs to be saved on closing 631 editor = IDE.openEditor(fActivePage, file, true); 632 mock = (MockEditorPart) editor; 633 mock.setSaveNeeded(true); 634 callTrace = mock.getCallHistory(); 635 callTrace.clear(); 636 // close the editor with save confirmation 637 assertEquals(fActivePage.closeEditor(editor, true), true); 638 assertEquals(callTrace 639 .verifyOrder(new String[] { "isDirty", "dispose" }), true); 640 641 /* 642 * javadoc: If the editor has unsaved content and save is true, the user 643 * will be given the opportunity to save it. 644 */ 645 // can't be tested 646 /* 647 * javadoc: Parameters: save - false to discard any unsaved changes 648 */ 649 // create a dirty editor 650 editor = IDE.openEditor(fActivePage, file, true); 651 mock = (MockEditorPart) editor; 652 mock.setDirty(true); 653 mock.setSaveNeeded(true); 654 callTrace = mock.getCallHistory(); 655 callTrace.clear(); 656 // close the editor and discard changes 657 assertEquals(fActivePage.closeEditor(editor, false), true); 658 assertEquals(callTrace.contains("isSaveOnCloseNeeded"), false); 659 /* 660 * It is possible that some action may query the isDirty value of the 661 * editor to update its enabled state. There is nothing wrong in doing 662 * that, so do not test for no isDirty call here. 663 * 664 * assertEquals(callTrace.contains( "isDirty"), false); 665 */ 666 assertEquals(callTrace.contains("doSave"), false); 667 assertEquals(callTrace.contains("dispose"), true); 668 } 669 670 @Test testCloseAllEditors()671 public void testCloseAllEditors() throws Throwable { 672 int total = 5; 673 final IFile[] files = new IFile[total]; 674 IEditorPart[] editors = new IEditorPart[total]; 675 CallHistory[] callTraces = new CallHistory[total]; 676 MockEditorPart[] mocks = new MockEditorPart[total]; 677 678 proj = FileUtil.createProject("testOpenEditor"); 679 for (int i = 0; i < total; i++) { 680 files[i] = FileUtil.createFile(i + ".mock2", proj); 681 } 682 683 /* 684 * javadoc: If the page has open editors with unsaved content and save 685 * is true, the user will be given the opportunity to save them. 686 */ 687 // close all clean editors with confirmation 688 for (int i = 0; i < total; i++) { 689 editors[i] = IDE.openEditor(fActivePage, files[i], true); 690 callTraces[i] = ((MockEditorPart) editors[i]).getCallHistory(); 691 } 692 assertEquals(fActivePage.closeAllEditors(true), true); 693 for (int i = 0; i < total; i++) { 694 assertEquals(callTraces[i].contains("isDirty"), true); 695 assertEquals(callTraces[i].contains("doSave"), false); 696 callTraces[i].clear(); 697 } 698 699 // close all dirty editors with confirmation 700 // can't be tested 701 702 // close all dirty editors discarding them 703 for (int i = 0; i < total; i++) { 704 editors[i] = IDE.openEditor(fActivePage, files[i], true); 705 mocks[i] = (MockEditorPart) editors[i]; 706 mocks[i].setDirty(true); 707 callTraces[i] = mocks[i].getCallHistory(); 708 } 709 assertEquals(fActivePage.closeAllEditors(false), true); 710 for (int i = 0; i < total; i++) { 711 assertEquals(callTraces[i].contains("doSave"), false); 712 } 713 } 714 715 @Test testSaveEditor()716 public void testSaveEditor() throws Throwable { 717 proj = FileUtil.createProject("testOpenEditor"); 718 final IFile file = FileUtil.createFile("test.mock1", proj); 719 IEditorPart editor; 720 CallHistory callTrace; 721 MockEditorPart mock; 722 723 // create a clean editor 724 editor = IDE.openEditor(fActivePage, file, true); 725 mock = (MockEditorPart) editor; 726 callTrace = mock.getCallHistory(); 727 callTrace.clear(); 728 729 /* 730 * javadoc: Saves the contents of the given editor if dirty. If not, 731 * this method returns without effect 732 */ 733 // save the clean editor with confirmation 734 assertEquals(fActivePage.saveEditor(editor, true), true); 735 assertEquals(callTrace.contains("isDirty"), true); 736 assertEquals(callTrace.contains("doSave"), false); 737 738 /* 739 * javadoc: If confirm is true the user is prompted to confirm the 740 * command. 741 */ 742 // can't be tested 743 /* 744 * javadoc: Otherwise, the save happens without prompt. 745 */ 746 // save the clean editor without confirmation 747 assertEquals(fActivePage.saveEditor(editor, false), true); 748 assertEquals(callTrace.contains("isDirty"), true); 749 assertEquals(callTrace.contains("doSave"), false); 750 751 // save the dirty editor without confirmation 752 mock.setDirty(true); 753 callTrace.clear(); 754 assertEquals(fActivePage.saveEditor(editor, false), true); 755 assertEquals(callTrace 756 .verifyOrder(new String[] { "isDirty", "doSave" }), true); 757 } 758 759 @Test 760 @Ignore XXXtestSaveAllEditors()761 public void XXXtestSaveAllEditors() throws Throwable { 762 int total = 3; 763 764 final IFile[] files = new IFile[total]; 765 IEditorPart[] editors = new IEditorPart[total]; 766 CallHistory[] callTraces = new CallHistory[total]; 767 MockEditorPart[] mocks = new MockEditorPart[total]; 768 769 proj = FileUtil.createProject("testOpenEditor"); 770 for (int i = 0; i < total; i++) { 771 files[i] = FileUtil.createFile(i + ".mock2", proj); 772 editors[i] = IDE.openEditor(fActivePage, files[i], true); 773 mocks[i] = (MockEditorPart) editors[i]; 774 callTraces[i] = mocks[i].getCallHistory(); 775 } 776 777 /* 778 * javadoc: If there are no dirty editors this method returns without 779 * effect. javadoc: If confirm is true the user is prompted to confirm 780 * the command 781 */ 782 // save all clean editors with confirmation 783 assertEquals(fActivePage.saveAllEditors(true), true); 784 for (int i = 0; i < total; i++) { 785 assertEquals(callTraces[i].contains("isDirty"), true); 786 assertEquals(callTraces[i].contains("doSave"), false); 787 callTraces[i].clear(); 788 } 789 790 // save all dirty editors with confirmation can't be tested 791 792 /* 793 * javadoc: Parameters: confirm - false to save unsaved changes without 794 * asking 795 */ 796 // save all clean editors without confirmation 797 assertEquals(fActivePage.saveAllEditors(false), true); 798 for (int i = 0; i < total; i++) { 799 assertEquals(callTraces[i].contains("isDirty"), true); 800 assertEquals(callTraces[i].contains("doSave"), false); 801 callTraces[i].clear(); 802 } 803 804 // save all dirty editors without confirmation 805 for (int i = 0; i < total; i++) { 806 mocks[i].setDirty(true); 807 } 808 assertEquals(fActivePage.saveAllEditors(false), true); 809 for (int i = 0; i < total; i++) { 810 assertEquals(callTraces[i].verifyOrder(new String[] { "isDirty", 811 "doSave" }), true); 812 } 813 } 814 815 @Test testGetEditors()816 public void testGetEditors() throws Throwable { 817 proj = FileUtil.createProject("testOpenEditor"); 818 int totalBefore = fActivePage.getEditors().length; 819 int num = 3; 820 IEditorPart[] editors = new IEditorPart[num]; 821 822 for (int i = 0; i < num; i++) { 823 editors[i] = IDE.openEditor(fActivePage, FileUtil.createFile(i 824 + ".mock2", proj), true); 825 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), 826 editors[i]), true); 827 } 828 assertEquals(fActivePage.getEditors().length, totalBefore + num); 829 830 fActivePage.closeEditor(editors[0], false); 831 assertEquals(ArrayUtil.contains(fActivePage.getEditors(), editors[0]), 832 false); 833 assertEquals(fActivePage.getEditors().length, totalBefore + num - 1); 834 835 fActivePage.closeAllEditors(false); 836 assertEquals(fActivePage.getEditors().length, 0); 837 } 838 839 @Test 840 @Ignore XXXtestShowActionSet()841 public void XXXtestShowActionSet() { 842 String id = MockActionDelegate.ACTION_SET_ID; 843 844 // int totalBefore = facade.getActionSetCount(fActivePage); 845 // FIXME: No implementation 846 fail("facade.getActionSetCount() had no implementation"); 847 848 fActivePage.showActionSet(id); 849 850 // facade.assertActionSetId(fActivePage, id, true); 851 852 // check that the method does not add an invalid action set to itself 853 id = IConstants.FakeID; 854 fActivePage.showActionSet(id); 855 856 // facade.assertActionSetId(fActivePage, id, false); 857 // FIXME: No implementation 858 fail("facade.assertActionSetId() had no implementation"); 859 860 // assertEquals(facade.getActionSetCount(fActivePage), totalBefore + 1); 861 } 862 863 @Test 864 @Ignore XXXtestHideActionSet()865 public void XXXtestHideActionSet() { 866 // int totalBefore = facade.getActionSetCount(fActivePage); 867 // FIXME: No implementation 868 869 fail("facade.getActionSetCount() had no implementation"); 870 871 String id = MockWorkbenchWindowActionDelegate.SET_ID; 872 fActivePage.showActionSet(id); 873 // assertEquals(facade.getActionSetCount(fActivePage), totalBefore + 1); 874 875 fActivePage.hideActionSet(id); 876 // assertEquals(facade.getActionSetCount(fActivePage), totalBefore); 877 878 // facade.assertActionSetId(fActivePage, id, false); 879 880 fail("facade.assertActionSetId() had no implementation"); 881 } 882 } 883