|
| 1 | +package org.eclipse.ui.tests.menus; |
| 2 | + |
| 3 | +import static org.eclipse.ui.tests.harness.util.UITestUtil.openTestWindow; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.eclipse.core.resources.IProject; |
| 10 | +import org.eclipse.core.runtime.CoreException; |
| 11 | +import org.eclipse.jface.action.ContributionItem; |
| 12 | +import org.eclipse.jface.action.IContributionItem; |
| 13 | +import org.eclipse.jface.viewers.ISelectionProvider; |
| 14 | +import org.eclipse.jface.viewers.StructuredSelection; |
| 15 | +import org.eclipse.swt.widgets.Menu; |
| 16 | +import org.eclipse.swt.widgets.MenuItem; |
| 17 | +import org.eclipse.ui.IPageLayout; |
| 18 | +import org.eclipse.ui.IViewPart; |
| 19 | +import org.eclipse.ui.IWorkbenchPage; |
| 20 | +import org.eclipse.ui.IWorkbenchWindow; |
| 21 | +import org.eclipse.ui.actions.ContributionItemFactory; |
| 22 | +import org.eclipse.ui.ide.IDE; |
| 23 | +import org.eclipse.ui.internal.ide.handlers.ShowInSystemExplorerHandler; |
| 24 | +import org.eclipse.ui.tests.harness.util.CloseTestWindowsExtension; |
| 25 | +import org.eclipse.ui.tests.harness.util.EditorTestHelper; |
| 26 | +import org.eclipse.ui.tests.harness.util.FileUtil; |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 31 | + |
| 32 | +@ExtendWith(CloseTestWindowsExtension.class) |
| 33 | +public class ShowInMenuTest { |
| 34 | + |
| 35 | + private static final String TEST_ITEM_ID = "org.eclipse.ui.tests.menus.showInMenuTestItem"; |
| 36 | + |
| 37 | + private IWorkbenchWindow window; |
| 38 | + private IWorkbenchPage page; |
| 39 | + private List<IProject> projects; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + public final void setUp() throws Exception { |
| 43 | + window = openTestWindow(IDE.RESOURCE_PERSPECTIVE_ID); |
| 44 | + page = window.getActivePage(); |
| 45 | + projects = List.of(FileUtil.createProject("a"), FileUtil.createProject("b")); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + public final void tearDown() throws Exception { |
| 50 | + for (IProject project : projects) { |
| 51 | + FileUtil.deleteProject(project); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testVisibleWhenEvaluation() throws CoreException { |
| 57 | + IViewPart viewPart = page.showView(IPageLayout.ID_PROJECT_EXPLORER); |
| 58 | + ISelectionProvider selectionProvider = viewPart.getSite().getSelectionProvider(); |
| 59 | + IWorkbenchWindow workbenchWindow = EditorTestHelper.getActiveWorkbenchWindow(); |
| 60 | + |
| 61 | + // Test item should be visible when selecting a single project |
| 62 | + selectionProvider.setSelection(new StructuredSelection(projects.get(0))); |
| 63 | + Menu menu = computeShowInMenu(workbenchWindow); |
| 64 | + |
| 65 | + assertNotNull(getContributionItemById(menu, TEST_ITEM_ID)); |
| 66 | + |
| 67 | + // Test item should be hidden when selecting multiple projects |
| 68 | + selectionProvider.setSelection(new StructuredSelection(projects)); |
| 69 | + menu = computeShowInMenu(workbenchWindow); |
| 70 | + |
| 71 | + assertNull(getContributionItemById(menu, TEST_ITEM_ID)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testVisibleWhenEvaluationForShowInSystemExplorer() throws CoreException { |
| 76 | + IViewPart viewPart = page.showView(IPageLayout.ID_PROJECT_EXPLORER); |
| 77 | + ISelectionProvider selectionProvider = viewPart.getSite().getSelectionProvider(); |
| 78 | + IWorkbenchWindow workbenchWindow = EditorTestHelper.getActiveWorkbenchWindow(); |
| 79 | + |
| 80 | + // "Show in System Explorer" should be visible when selecting a single project |
| 81 | + selectionProvider.setSelection(new StructuredSelection(projects.get(0))); |
| 82 | + Menu menu = computeShowInMenu(workbenchWindow); |
| 83 | + |
| 84 | + assertNotNull(getContributionItemById(menu, ShowInSystemExplorerHandler.ID)); |
| 85 | + |
| 86 | + // "Show in System Explorer" should be hidden when selecting multiple projects |
| 87 | + selectionProvider.setSelection(new StructuredSelection(projects)); |
| 88 | + menu = computeShowInMenu(workbenchWindow); |
| 89 | + |
| 90 | + assertNull(getContributionItemById(menu, ShowInSystemExplorerHandler.ID)); |
| 91 | + } |
| 92 | + |
| 93 | + private static Menu computeShowInMenu(IWorkbenchWindow workbenchWindow) { |
| 94 | + IContributionItem contributionItem = ContributionItemFactory.VIEWS_SHOW_IN.create(workbenchWindow); |
| 95 | + Menu menu = new Menu(workbenchWindow.getShell()); |
| 96 | + contributionItem.fill(menu, 0); |
| 97 | + return menu; |
| 98 | + } |
| 99 | + |
| 100 | + private static ContributionItem getContributionItemById(Menu menu, String id) { |
| 101 | + for (MenuItem item : menu.getItems()) { |
| 102 | + if (item.getData() instanceof ContributionItem contributionItem && contributionItem.getId().equals(id)) { |
| 103 | + return contributionItem; |
| 104 | + } |
| 105 | + } |
| 106 | + return null; |
| 107 | + } |
| 108 | +} |
0 commit comments