Skip to content

Commit

Permalink
Create ResourceMgmtActionProviderTests.java
Browse files Browse the repository at this point in the history
Refresh option not available for some resources that are not closed projects #2538

Change made since the fix to #1438 unintentionally removed the "refresh" contextual menu for resources that are not projects. 

With this change the "refresh" contextual menu is shown if ANY navigator selection is either (A) an open project, or (B) a non-project resource. Put another way: the 'refresh' item is NOT shown if ALL selections are closed projects.

Fixes #2538
  • Loading branch information
DaveCarpeneto authored Dec 3, 2024
1 parent ea04dc7 commit 8bdef7e
Showing 1 changed file with 221 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/*******************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.ui.tests.navigator.resources;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.internal.navigator.NavigatorContentService;
import org.eclipse.ui.internal.navigator.extensions.CommonActionExtensionSite;
import org.eclipse.ui.internal.navigator.resources.actions.ResourceMgmtActionProvider;
import org.eclipse.ui.navigator.CommonViewerSiteFactory;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonMenuConstants;
import org.eclipse.ui.tests.navigator.NavigatorTestBase;
import org.junit.Before;
import org.junit.Test;

public final class ResourceMgmtActionProviderTests extends NavigatorTestBase {

private IMenuManager manager;

public ResourceMgmtActionProviderTests() {
_navigatorInstanceId = TEST_VIEWER;
}

@Override
@Before
public void setUp() {
super.setUp();
manager = new MenuManager();
manager.add(new GroupMarker(ICommonMenuConstants.GROUP_BUILD));
}

/**
* Test for 'no selection' condition - no menu items should be included
*/
@Test
public void testFillContextMenu_noSelection() {
ResourceMgmtActionProvider provider = provider((IResource[]) null);
provider.fillContextMenu(manager);
checkMenuHasCorrectContributions(false, false, false, false, false);
}

/**
* Test for 'folder' condition - only 'refresh' should be included
*/
@Test
public void testFillContextMenu_folderSelection() {

IFolder justAFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path("some/folder"));
ResourceMgmtActionProvider provider = provider(justAFolder);
provider.fillContextMenu(manager);
checkMenuHasCorrectContributions(false, true, false, false, false);
}

/**
* Test for 'closed project' - only 'open project' should be included
*/
@Test
public void testFillContextMenu_closedProjectSelection() {
IProject closedProj = ResourcesPlugin.getWorkspace().getRoot().getProject("closedProj");
ResourceMgmtActionProvider provider = provider(closedProj);
provider.fillContextMenu(manager);
checkMenuHasCorrectContributions(false, false, true, false, false);
}

/**
* Test for 'open project' that doesn't have a builder attached - all but
* 'build' & 'open project' should be enabled
*/
@Test
public void testFillContextMenu_openProjectNoBuilderSelection() {
IProject openProj = ResourcesPlugin.getWorkspace().getRoot().getProject("Test");
boolean autoBuildInitialState = ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding();

try {
if (!autoBuildInitialState) {
// we want to enable auto-building for this test to guarantee that the 'build'
// menu option isn't shown
IWorkspaceDescription wsd = ResourcesPlugin.getWorkspace().getDescription();
wsd.setAutoBuilding(true);
ResourcesPlugin.getWorkspace().setDescription(wsd);
}
openProj.open(null);
} catch (CoreException e) {
fail(e.getClass().getSimpleName() + " thrown: " + e.getLocalizedMessage());
}
ResourceMgmtActionProvider provider = provider(openProj);
provider.fillContextMenu(manager);
checkMenuHasCorrectContributions(false, true, false, true, true);

if (!autoBuildInitialState) {
// clean-up: reset autobuild since we changed it
try {
IWorkspaceDescription wsd = ResourcesPlugin.getWorkspace().getDescription();
wsd.setAutoBuilding(false);
ResourcesPlugin.getWorkspace().setDescription(wsd);
} catch (CoreException e) {
fail(e.getClass().getSimpleName() + " thrown: " + e.getLocalizedMessage());
}
}
}

/**
* Test for 'open project' that doesn't have a builder attached - only 'open
* project' should be disabled
*/
@Test
public void testFillContextMenu_openProjectWithBuilderSelection() {
IProject openProj = ResourcesPlugin.getWorkspace().getRoot().getProject("Test");
IWorkspaceDescription wsd = ResourcesPlugin.getWorkspace().getDescription();
boolean autobuildInitialState = wsd.isAutoBuilding();
boolean hasNoInitialBuildCommands = false;
IProjectDescription desc = null;
try {
if (autobuildInitialState) {
wsd.setAutoBuilding(false);
ResourcesPlugin.getWorkspace().setDescription(wsd);
}
openProj.open(null);
desc = openProj.getDescription();
if (desc.getBuildSpec().length == 0) {
hasNoInitialBuildCommands = true;
ICommand cmd = desc.newCommand();
desc.setBuildSpec(new ICommand[] { cmd });
openProj.setDescription(desc, null);
}
} catch (CoreException e) {
fail(e.getClass().getSimpleName() + " thrown: " + e.getLocalizedMessage());
}
ResourceMgmtActionProvider provider = provider(openProj);
provider.fillContextMenu(manager);
checkMenuHasCorrectContributions(true, true, false, true, true);
try {
// clean-up where needed: reset autobuild if we changed it & remove
// the build config if we added it
if (autobuildInitialState) {
wsd.setAutoBuilding(true);
ResourcesPlugin.getWorkspace().setDescription(wsd);
}
if (desc != null && hasNoInitialBuildCommands) {
desc.setBuildSpec(new ICommand[0]);
openProj.setDescription(desc, null);
}
} catch (CoreException e) {
fail(e.getClass().getSimpleName() + " thrown: " + e.getLocalizedMessage());
}
}

/*
* Return a provider, given the selected navigator items
*/
private ResourceMgmtActionProvider provider(IResource... selectedElements) {
ICommonActionExtensionSite cfg = new CommonActionExtensionSite("NA", "NA",
CommonViewerSiteFactory.createCommonViewerSite(_commonNavigator.getViewSite()),
(NavigatorContentService) _contentService, _viewer);
ResourceMgmtActionProvider provider = new ResourceMgmtActionProvider();
StructuredSelection selection = null;
if (selectedElements != null && selectedElements.length > 0) {
selection = new StructuredSelection(selectedElements);
} else {
selection = new StructuredSelection();
}
provider.setContext(new ActionContext(selection));
provider.init(cfg);
return provider;
}

/*
* Check the expected menu items (passed in) against what the menu actually has
*/
private void checkMenuHasCorrectContributions(boolean... actions) {
if (actions.length != 5) { // there's 5 menus we check for
fail(String.format("Incorrect number of menu items being checked : %d", actions.length));
}
int index = 0;
for (String thisAction : new String[] { "org.eclipse.ui.BuildAction", "org.eclipse.ui.RefreshAction",
"org.eclipse.ui.OpenResourceAction", "org.eclipse.ui.CloseResourceAction",
"org.eclipse.ui.CloseUnrelatedProjectsAction" }) {
assertTrue(String.format("Unexpected menu membership for %s (%b)", thisAction, !actions[index]),
actions[index] == menuHasContribution(thisAction));
index++;
}
}

/*
* Check the menu for the named entry
*/
private boolean menuHasContribution(String contribution) {
for (IContributionItem thisItem : manager.getItems()) {
if (thisItem.getId().equals(contribution)) {
return true;
}
}
return false;
}

}

0 comments on commit 8bdef7e

Please sign in to comment.