001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.harness.util;
011:
012: import java.lang.reflect.Method;
013:
014: import junit.framework.Assert;
015: import junit.framework.TestCase;
016:
017: import org.eclipse.jface.action.ActionContributionItem;
018: import org.eclipse.jface.action.IAction;
019: import org.eclipse.jface.action.IContributionItem;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.MenuManager;
022: import org.eclipse.jface.action.SubContributionItem;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.internal.WorkbenchWindow;
025:
026: /**
027: * <code>ActionUtil</code> contains methods to run actions
028: * in the workbench.
029: */
030: public class ActionUtil {
031:
032: /**
033: * Runs an action contribution.
034: *
035: * @param test the current test case
036: * @param item an action contribution item
037: */
038: public static void runAction(TestCase test, IContributionItem item) {
039: Assert.assertTrue(item instanceof ActionContributionItem);
040: ((ActionContributionItem) item).getAction().run();
041: }
042:
043: /**
044: * Runs the first action found in a menu manager with a
045: * particular label.
046: *
047: * @param test the current test case
048: * @param mgr the containing menu manager
049: * @param label the action label
050: */
051: public static void runActionWithLabel(TestCase test,
052: IMenuManager mgr, String label) {
053: IContributionItem[] items = mgr.getItems();
054: for (int nX = 0; nX < items.length; nX++) {
055: IContributionItem item = items[nX];
056: if (item instanceof SubContributionItem)
057: item = ((SubContributionItem) item).getInnerItem();
058: if (item instanceof ActionContributionItem) {
059: IAction action = ((ActionContributionItem) item)
060: .getAction();
061: if (label.equals(action.getText())) {
062: action.run();
063: return;
064: }
065: }
066: }
067: Assert.fail("Unable to find action: " + label);
068: }
069:
070: /**
071: * Runs the first action found in a window with a
072: * particular label.
073: *
074: * @param test the current test case
075: * @param win the containing window
076: * @param label the action label
077: */
078: public static void runActionWithLabel(TestCase test,
079: IWorkbenchWindow win, String label) {
080: WorkbenchWindow realWin = (WorkbenchWindow) win;
081: IMenuManager mgr = realWin.getMenuManager();
082: runActionWithLabel(test, mgr, label);
083: }
084:
085: /**
086: * Runs an action identified by an id path in a
087: * menu manager.
088: *
089: * @param test the current test case
090: * @param mgr the containing menu manager
091: * @param label the action label
092: */
093: public static void runActionUsingPath(TestCase test,
094: IMenuManager mgr, String idPath) {
095: IContributionItem item = mgr.findUsingPath(idPath);
096: Assert.assertNotNull(item);
097: runAction(test, item);
098: }
099:
100: /**
101: * Runs an action identified by an id path in a
102: * window.
103: *
104: * @param test the current test case
105: * @param win the containing window
106: * @param label the action label
107: */
108: public static void runActionUsingPath(TestCase test,
109: IWorkbenchWindow win, String idPath) {
110: WorkbenchWindow realWin = (WorkbenchWindow) win;
111: IMenuManager mgr = realWin.getMenuManager();
112: runActionUsingPath(test, mgr, idPath);
113: }
114:
115: /**
116: * Returns the first action found in a menu manager with a
117: * particular label.
118: *
119: * @param mgr the containing menu manager
120: * @param label the action label
121: * @return the first action with the label, or <code>null</code>
122: * if it is not found.
123: */
124: public static IAction getActionWithLabel(IMenuManager mgr,
125: String label) {
126: IContributionItem[] items = mgr.getItems();
127: for (int nX = 0; nX < items.length; nX++) {
128: IContributionItem item = items[nX];
129: if (item instanceof SubContributionItem)
130: item = ((SubContributionItem) item).getInnerItem();
131: if (item instanceof ActionContributionItem) {
132: IAction action = ((ActionContributionItem) item)
133: .getAction();
134: if (label.equals(action.getText())) {
135: return action;
136: }
137: }
138: }
139: return null;
140: }
141:
142: /**
143: * Fire the "handleAboutToShow" method in a menu manager.
144: * This triggers the same behavior as when a user opens a menu.
145: * The menu to be populated with actions and those
146: * actions to be enacted in SWT widgets.
147: *
148: * @param mgr the menu manager to open
149: */
150: public static void fireAboutToShow(MenuManager mgr)
151: throws Throwable {
152: Class clazz = mgr.getClass();
153: Method method = clazz.getDeclaredMethod("handleAboutToShow",
154: new Class[0]);
155: method.setAccessible(true);
156: method.invoke(mgr, new Object[0]);
157: }
158: }
|