|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.tests.commands; |
| 13 |
|
| 14 |
import java.lang.reflect.Method; |
| 15 |
|
| 16 |
import org.eclipse.core.commands.AbstractHandler; |
| 17 |
import org.eclipse.core.commands.ExecutionEvent; |
| 18 |
import org.eclipse.core.commands.ExecutionException; |
| 19 |
import org.eclipse.jface.action.Action; |
| 20 |
import org.eclipse.jface.action.IAction; |
| 21 |
import org.eclipse.swt.widgets.Event; |
| 22 |
import org.eclipse.ui.IWorkbenchWindow; |
| 23 |
import org.eclipse.ui.PlatformUI; |
| 24 |
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; |
| 25 |
import org.eclipse.ui.commands.ICommand; |
| 26 |
import org.eclipse.ui.handlers.IHandlerActivation; |
| 27 |
import org.eclipse.ui.handlers.IHandlerService; |
| 28 |
import org.eclipse.ui.internal.WorkbenchWindow; |
| 29 |
import org.eclipse.ui.internal.actions.CommandAction; |
| 30 |
import org.eclipse.ui.tests.harness.util.UITestCase; |
| 31 |
|
| 32 |
/** |
| 33 |
* |
| 34 |
* @since 3.5 |
| 35 |
* |
| 36 |
*/ |
| 37 |
public class Bug370119Test extends UITestCase { |
| 38 |
// you can find these commands in org.eclipse.ui.tests/plugin.xml |
| 39 |
private static final String PREFIX = "tests.commands.global."; |
| 40 |
private static final String CMD_ID = PREFIX + "Command1"; |
| 41 |
private static final String ACT1_ID = PREFIX + "Action1"; |
| 42 |
private static final String ACT2_ID = PREFIX + "Action2"; |
| 43 |
|
| 44 |
private IHandlerService handlerService; |
| 45 |
TestCommandAction testCommandAction; |
| 46 |
TestCommandActionHandler testCommandActionHandler; |
| 47 |
IHandlerActivation testCommandActionActivation; |
| 48 |
TestAction testAction; |
| 49 |
|
| 50 |
|
| 51 |
interface ITestAction { |
| 52 |
void clear(); |
| 53 |
boolean isExecuted(); |
| 54 |
} |
| 55 |
|
| 56 |
private static class TestCommandAction extends CommandAction implements |
| 57 |
IWorkbenchAction, ITestAction { |
| 58 |
private boolean executed = false; |
| 59 |
|
| 60 |
public TestCommandAction(String commandId, |
| 61 |
IWorkbenchWindow window) { |
| 62 |
super(window, commandId); |
| 63 |
setActionDefinitionId(commandId); |
| 64 |
} |
| 65 |
|
| 66 |
public void run() { |
| 67 |
runWithEvent(null); |
| 68 |
} |
| 69 |
|
| 70 |
public void runWithEvent(Event event) { |
| 71 |
executed = true; |
| 72 |
} |
| 73 |
|
| 74 |
public void clear() { |
| 75 |
executed = false; |
| 76 |
} |
| 77 |
|
| 78 |
public boolean isExecuted() { |
| 79 |
return executed; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private static class TestAction extends Action implements |
| 84 |
IWorkbenchAction, ITestAction { |
| 85 |
private boolean executed = false; |
| 86 |
|
| 87 |
public TestAction(String commandId) { |
| 88 |
super(commandId); |
| 89 |
setActionDefinitionId(commandId); |
| 90 |
} |
| 91 |
|
| 92 |
public void dispose() { |
| 93 |
} |
| 94 |
|
| 95 |
public void run() { |
| 96 |
runWithEvent(null); |
| 97 |
} |
| 98 |
|
| 99 |
public void runWithEvent(Event event) { |
| 100 |
executed = true; |
| 101 |
} |
| 102 |
|
| 103 |
public void clear() { |
| 104 |
executed = false; |
| 105 |
} |
| 106 |
|
| 107 |
public boolean isExecuted() { |
| 108 |
return executed; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* Creates an action to be used along with ActionHandler instance in Global Action Handlers mapping |
| 114 |
*/ |
| 115 |
public TestAction createTestAction(String commandID, String actionID) { |
| 116 |
TestAction action = new TestAction(commandID); |
| 117 |
action.setId(actionID); |
| 118 |
action.setText(actionID); |
| 119 |
return action; |
| 120 |
} |
| 121 |
|
| 122 |
/* |
| 123 |
* Creates an action to be used with IActionCommandMappingService mapping |
| 124 |
*/ |
| 125 |
public TestCommandAction createTestCommandAction(IWorkbenchWindow window, String commandID, String actionID) { |
| 126 |
TestCommandAction action = new TestCommandAction(commandID, window); |
| 127 |
action.setId(actionID); |
| 128 |
action.setText(actionID); |
| 129 |
return action; |
| 130 |
} |
| 131 |
|
| 132 |
private static class TestCommandActionHandler extends AbstractHandler { |
| 133 |
TestCommandAction commandAction; |
| 134 |
/** |
| 135 |
* |
| 136 |
*/ |
| 137 |
public TestCommandActionHandler(TestCommandAction commandAction) { |
| 138 |
this.commandAction = commandAction; |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
* (non-Javadoc) |
| 143 |
* |
| 144 |
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
| 145 |
*/ |
| 146 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
| 147 |
commandAction.run(); |
| 148 |
return null; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/* |
| 154 |
* (non-Javadoc) |
| 155 |
* |
| 156 |
* @see org.eclipse.ui.tests.harness.util.UITestCase#doSetUp() |
| 157 |
*/ |
| 158 |
protected void doSetUp() throws Exception { |
| 159 |
super.doSetUp(); |
| 160 |
handlerService = (IHandlerService) fWorkbench |
| 161 |
.getService(IHandlerService.class); |
| 162 |
|
| 163 |
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 164 |
if (window != null) { |
| 165 |
testCommandAction = createTestCommandAction(window, CMD_ID, ACT1_ID); |
| 166 |
testCommandActionHandler = new TestCommandActionHandler(testCommandAction); |
| 167 |
testCommandActionActivation = handlerService.activateHandler(CMD_ID, testCommandActionHandler); |
| 168 |
testAction = createTestAction(CMD_ID, ACT2_ID); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* (non-Javadoc) |
| 174 |
* |
| 175 |
* @see org.eclipse.ui.tests.harness.util.UITestCase#doTearDown() |
| 176 |
*/ |
| 177 |
protected void doTearDown() throws Exception { |
| 178 |
if (testCommandActionActivation != null) { |
| 179 |
handlerService.deactivateHandler(testCommandActionActivation); |
| 180 |
testCommandActionActivation = null; |
| 181 |
} |
| 182 |
|
| 183 |
super.doTearDown(); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @param testName |
| 188 |
*/ |
| 189 |
public Bug370119Test(String testName) { |
| 190 |
super(testName); |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
* Invokes the method void registerGlobalAction(IAction globalAction) on WorkbenchWindow instance |
| 195 |
* |
| 196 |
* @param action The action to be registered |
| 197 |
* @param window The instance of WorkbenchWindow |
| 198 |
*/ |
| 199 |
private void invokeRegisterGlobalAction(IAction action, WorkbenchWindow window) { |
| 200 |
try { |
| 201 |
Method m = WorkbenchWindow.class.getDeclaredMethod("registerGlobalAction", new Class[] {IAction.class}); //$NON-NLS-1$ |
| 202 |
m.setAccessible(true); |
| 203 |
m.invoke(window, new Object[] {action}); |
| 204 |
} catch (Exception e) { |
| 205 |
fail("An error occured while invoking registerGlobalAction() method on WorkbenchWindow instance!", e); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/* |
| 210 |
* Test performs the following checks: |
| 211 |
* - Registers the Action within the WorkbenchWindow (ActionHandler will be created and registered) |
| 212 |
* - Asserts that the Action is registered and can be executed |
| 213 |
* - Registers the CommandAction within the WorkbenchWindow (no ActionHandler is to be created, the old one is to be removed) |
| 214 |
* - Asserts that the CommandAction is registered and can be executed as well as the old ActionHandler is removed and isn't used anymore |
| 215 |
*/ |
| 216 |
|
| 217 |
public void testActionRegistration() throws Exception { |
| 218 |
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 219 |
assertNotNull("Workbench Window instance doesn't exist!", window); |
| 220 |
assertTrue("Workbench Window is not instance of WorkbenchWindow", (window instanceof |
| 221 |
WorkbenchWindow)); |
| 222 |
WorkbenchWindow wWindow = (WorkbenchWindow)window; |
| 223 |
|
| 224 |
// Register Action (ActionHandler instance will be created for the Action) |
| 225 |
invokeRegisterGlobalAction(testAction, wWindow); |
| 226 |
|
| 227 |
// Make sure that testAction can be executed |
| 228 |
ICommand cmd = fWorkbench.getCommandSupport().getCommandManager().getCommand(CMD_ID); |
| 229 |
assertNotNull("Test Command not found for ID: " + CMD_ID, cmd); |
| 230 |
|
| 231 |
cmd.execute(null); |
| 232 |
assertTrue("Test Action is not executed but should be!", testAction.isExecuted()); |
| 233 |
assertFalse("Test Command Action is executed but shouldn't be!", testCommandAction.isExecuted()); |
| 234 |
|
| 235 |
testAction.clear(); // Clear execution flag |
| 236 |
testCommandAction.clear(); // Clear execution flag |
| 237 |
|
| 238 |
// Register Command Action (ActionHandler instance will not be created) |
| 239 |
invokeRegisterGlobalAction(testCommandAction, wWindow); |
| 240 |
// Make sure that testAction can be executed |
| 241 |
cmd = fWorkbench.getCommandSupport().getCommandManager().getCommand(CMD_ID); |
| 242 |
assertNotNull("Test Command not found for ID: " + CMD_ID, cmd); |
| 243 |
|
| 244 |
cmd.execute(null); |
| 245 |
assertTrue("Test Command Action is not executed but should be!", testCommandAction.isExecuted()); |
| 246 |
assertFalse("Test Action is executed but shouldn't be!", testAction.isExecuted()); |
| 247 |
} |
| 248 |
} |