Skip to content

Commit

Permalink
Stop needlessly pass TestCase instance in harness
Browse files Browse the repository at this point in the history
Convert the tests to plain JUnit4 while at them too.
  • Loading branch information
akurtakov committed Oct 28, 2024
1 parent 9e0cb2a commit ec2d9bd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@
import org.eclipse.ui.internal.WorkbenchWindow;
import org.junit.Assert;

import junit.framework.TestCase;

/**
* <code>ActionUtil</code> contains methods to run actions
* in the workbench.
* <code>ActionUtil</code> contains methods to run actions in the workbench.
*/
public class ActionUtil {

/**
* Runs an action contribution.
*
* @param test the current test case
* @param item an action contribution item
*/
public static void runAction(TestCase test, IContributionItem item) {
public static void runAction(IContributionItem item) {
Assert.assertTrue(item instanceof ActionContributionItem);
((ActionContributionItem) item).getAction().run();
}
Expand All @@ -46,18 +42,16 @@ public static void runAction(TestCase test, IContributionItem item) {
* Runs the first action found in a menu manager with a
* particular label.
*
* @param test the current test case
* @param mgr the containing menu manager
* @param label the action label
*/
public static void runActionWithLabel(TestCase test, IMenuManager mgr,
String label) {
public static void runActionWithLabel(IMenuManager mgr, String label) {
IContributionItem[] items = mgr.getItems();
for (IContributionItem item : items) {
if (item instanceof SubContributionItem)
item = ((SubContributionItem) item).getInnerItem();
if (item instanceof ActionContributionItem) {
IAction action = ((ActionContributionItem) item).getAction();
if (item instanceof SubContributionItem subItem)
item = subItem.getInnerItem();
if (item instanceof ActionContributionItem actionContribItem) {
IAction action = actionContribItem.getAction();
if (label.equals(action.getText())) {
action.run();
return;
Expand All @@ -71,43 +65,37 @@ public static void runActionWithLabel(TestCase test, IMenuManager mgr,
* Runs the first action found in a window with a
* particular label.
*
* @param test the current test case
* @param win the containing window
* @param label the action label
*/
public static void runActionWithLabel(TestCase test, IWorkbenchWindow win,
String label) {
public static void runActionWithLabel(IWorkbenchWindow win, String label) {
WorkbenchWindow realWin = (WorkbenchWindow) win;
IMenuManager mgr = realWin.getMenuBarManager();
runActionWithLabel(test, mgr, label);
runActionWithLabel(mgr, label);
}

/**
* Runs an action identified by an id path in a
* menu manager.
*
* @param test the current test case
* @param mgr the containing menu manager
*/
public static void runActionUsingPath(TestCase test, IMenuManager mgr,
String idPath) {
public static void runActionUsingPath(IMenuManager mgr, String idPath) {
IContributionItem item = mgr.findUsingPath(idPath);
Assert.assertNotNull(item);
runAction(test, item);
runAction(item);
}

/**
* Runs an action identified by an id path in a
* window.
*
* @param test the current test case
* @param win the containing window
*/
public static void runActionUsingPath(TestCase test, IWorkbenchWindow win,
String idPath) {
public static void runActionUsingPath(IWorkbenchWindow win, String idPath) {
WorkbenchWindow realWin = (WorkbenchWindow) win;
IMenuManager mgr = realWin.getMenuBarManager();
runActionUsingPath(test, mgr, idPath);
runActionUsingPath(mgr, idPath);
}

/**
Expand All @@ -122,10 +110,10 @@ public static void runActionUsingPath(TestCase test, IWorkbenchWindow win,
public static IAction getActionWithLabel(IMenuManager mgr, String label) {
IContributionItem[] items = mgr.getItems();
for (IContributionItem item : items) {
if (item instanceof SubContributionItem)
item = ((SubContributionItem) item).getInnerItem();
if (item instanceof ActionContributionItem) {
IAction action = ((ActionContributionItem) item).getAction();
if (item instanceof SubContributionItem subItem)
item = subItem.getInnerItem();
if (item instanceof ActionContributionItem actionContribItem) {
IAction action = actionContribItem.getAction();
if (label.equals(action.getText())) {
return action;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
*******************************************************************************/
package org.eclipse.ui.tests.api;

import static org.eclipse.ui.tests.harness.util.UITestCase.openTestWindow;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.jface.action.Action;
Expand All @@ -28,20 +35,22 @@
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.internal.handlers.IActionCommandMappingService;
import org.eclipse.ui.tests.harness.util.ActionUtil;
import org.eclipse.ui.tests.harness.util.UITestCase;
import org.eclipse.ui.tests.harness.util.CloseTestWindowsRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Test the lifecycle of an action delegate.
*/
@RunWith(JUnit4.class)
public class IActionBarsTest extends UITestCase {
public class IActionBarsTest {

private IWorkbenchWindow fWindow;

protected IWorkbenchWindow fWindow;
private IWorkbenchPage fPage;

protected IWorkbenchPage fPage;
@Rule
public final CloseTestWindowsRule closeTestWindows = new CloseTestWindowsRule();

private static class MockAction extends Action {
public boolean hasRun = false;
Expand All @@ -56,16 +65,9 @@ public void run() {
}
}

/**
* Constructor for IActionDelegateTest
*/
public IActionBarsTest() {
super(IActionBarsTest.class.getSimpleName());
}

@Override
protected void doSetUp() throws Exception {
super.doSetUp();
@Before
public void doSetUp() throws Exception {
fWindow = openTestWindow();
fPage = fWindow.getActivePage();
}
Expand Down Expand Up @@ -168,7 +170,7 @@ public void testSetGlobalActionHandler() throws Throwable {
runMatchingCommand(fWindow, ActionFactory.CUT.getId());
runMatchingCommand(fWindow, ActionFactory.UNDO.getId());

ActionUtil.runActionUsingPath(this, fWindow,
ActionUtil.runActionUsingPath(fWindow,
IWorkbenchActionConstants.M_FILE + '/' + IWorkbenchActionConstants.QUIT);
assertTrue(cut.hasRun);
assertTrue(!copy.hasRun);
Expand All @@ -187,7 +189,7 @@ public void testSetGlobalActionHandler() throws Throwable {
cut.hasRun = copy.hasRun = undo.hasRun = quit.hasRun = false;
runMatchingCommand(fWindow, ActionFactory.CUT.getId());
runMatchingCommand(fWindow, ActionFactory.UNDO.getId());
ActionUtil.runActionUsingPath(this, fWindow,
ActionUtil.runActionUsingPath(fWindow,
IWorkbenchActionConstants.M_FILE + '/' + IWorkbenchActionConstants.QUIT);
assertTrue(!cut.hasRun);
assertTrue(!copy.hasRun);
Expand All @@ -200,7 +202,7 @@ public void testSetGlobalActionHandler() throws Throwable {
cut.hasRun = copy.hasRun = undo.hasRun = quit.hasRun = false;
runMatchingCommand(fWindow, ActionFactory.CUT.getId());
runMatchingCommand(fWindow, ActionFactory.UNDO.getId());
ActionUtil.runActionUsingPath(this, fWindow,
ActionUtil.runActionUsingPath(fWindow,
IWorkbenchActionConstants.M_FILE + '/' + IWorkbenchActionConstants.QUIT);
assertTrue(cut.hasRun);
assertTrue(!copy.hasRun);
Expand All @@ -219,7 +221,7 @@ private void runMatchingCommand(IWorkbenchWindow window, String actionId) {
// this is not a failure, just a condition to be checked by
// the test
} catch (Exception e) {
fail("Failed to run " + commandId, e);
fail("Failed to run " + commandId);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@
*******************************************************************************/
package org.eclipse.ui.tests.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.tests.harness.util.CloseTestWindowsRule;
import org.eclipse.ui.tests.harness.util.UITestCase;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
* Test the lifecycle of an action delegate.
*/
public abstract class IActionDelegateTest extends UITestCase {
public abstract class IActionDelegateTest {

protected IWorkbenchWindow fWindow;

protected IWorkbenchPage fPage;

/**
* Constructor for IActionDelegateTest
*/
public IActionDelegateTest(String testName) {
super(testName);
}
@Rule
public final CloseTestWindowsRule closeTestWindows = new CloseTestWindowsRule();

@Override
protected void doSetUp() throws Exception {
super.doSetUp();
fWindow = openTestWindow();
@Before
public void doSetUp() throws Exception {
fWindow = UITestCase.openTestWindow();
fPage = fWindow.getActivePage();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*******************************************************************************/
package org.eclipse.ui.tests.api;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.IMenuManager;
Expand All @@ -21,26 +24,16 @@
import org.eclipse.ui.tests.harness.util.ActionUtil;
import org.eclipse.ui.tests.harness.util.FileUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests the lifecycle for an editor action delegate.
*/
@RunWith(JUnit4.class)
public class IEditorActionDelegateTest extends IActionDelegateTest {

public static String EDITOR_ID = "org.eclipse.ui.tests.api.IEditorActionDelegateTest";

private MockEditorPart editor;

/**
* Constructor for IWorkbenchWindowActionDelegateTest
*/
public IEditorActionDelegateTest() {
super(IEditorActionDelegateTest.class.getSimpleName());
}

@Test
public void testSetActiveEditor() throws Throwable {
// When an action delegate is run the
Expand Down Expand Up @@ -69,7 +62,7 @@ protected void runAction(Object widget) throws Throwable {
MockEditorActionBarContributor contributor = (MockEditorActionBarContributor) editor
.getEditorSite().getActionBarContributor();
IMenuManager mgr = contributor.getActionBars().getMenuManager();
ActionUtil.runActionWithLabel(this, mgr, "Mock Action");
ActionUtil.runActionWithLabel(mgr, "Mock Action");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,20 @@
*******************************************************************************/
package org.eclipse.ui.tests.api;

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

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.ui.tests.harness.util.ActionUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests the lifecycle for a view action delegate.
*/
@RunWith(JUnit4.class)
public class IViewActionDelegateTest extends IActionDelegateTest {

public static String TEST_VIEW_ID = "org.eclipse.ui.tests.api.IViewActionDelegateTest";

/**
* Constructor for IWorkbenchWindowActionDelegateTest
*/
public IViewActionDelegateTest() {
super(IViewActionDelegateTest.class.getSimpleName());
}

@Test
public void testInit() throws Throwable {
// When an action delegate is run the
Expand All @@ -59,7 +52,7 @@ protected Object createActionWidget() throws Throwable {
protected void runAction(Object widget) throws Throwable {
MockViewPart view = (MockViewPart) widget;
IMenuManager mgr = view.getViewSite().getActionBars().getMenuManager();
ActionUtil.runActionWithLabel(this, mgr, "Mock Action");
ActionUtil.runActionWithLabel(mgr, "Mock Action");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,24 @@
*******************************************************************************/
package org.eclipse.ui.tests.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import org.eclipse.ui.IActionDelegate2;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.tests.harness.util.ActionUtil;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests the lifecycle for a window action delegate.
*/
@RunWith(JUnit4.class)
public class IWorkbenchWindowActionDelegateTest extends IActionDelegateTest {

/**
* Constructor for IWorkbenchWindowActionDelegateTest
*/
public IWorkbenchWindowActionDelegateTest() {
super(IWorkbenchWindowActionDelegateTest.class.getSimpleName());
}

@Test
@Override
public void testRun() throws Throwable {
Expand Down Expand Up @@ -123,7 +118,7 @@ protected Object createActionWidget() throws Throwable {

@Override
protected void runAction(Object widget) throws Throwable {
ActionUtil.runActionWithLabel(this, fWindow, "Mock Action");
ActionUtil.runActionWithLabel(fWindow, "Mock Action");
}

@Override
Expand Down

0 comments on commit ec2d9bd

Please sign in to comment.