Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f3e17fe95009db7017d8ca33cbac254aef8ce23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*******************************************************************************
 * Copyright (c) 2012, 2017 IBM Corporation and others.
 *
 * 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.ui.tests.commands;

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

import java.util.ArrayList;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IExecutionListener;
import org.eclipse.core.commands.IExecutionListenerWithChecks;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.IHandlerService;
import org.junit.Test;

/**
 * @since 3.103
 *
 */
public class CommandExecutionTest {
	static class Pair {
		public Pair(String a, Object b) {
			key = a;
			result = b;
		}

		String key;
		Object result;

		@Override
		public String toString() {
			return "(" + key + ",\n\t" + result + ")";
		}
	}

	private static class EL implements IExecutionListenerWithChecks {
		ArrayList<Pair> methods = new ArrayList<>();
		IWorkbenchWindow wbw;

		@Override
		public void preExecute(String commandId, ExecutionEvent event) {
			methods.add(new Pair("preExecute", event));
			// ensure HandlerUtil has proper access. See bug 412681.
			wbw = HandlerUtil.getActiveWorkbenchWindow(event);
		}

		@Override
		public void postExecuteSuccess(String commandId, Object returnValue) {
			methods.add(new Pair("postExecuteSuccess", returnValue));
		}

		@Override
		public void postExecuteFailure(String commandId, ExecutionException exception) {
			methods.add(new Pair("postExecuteFailure", exception));
		}

		@Override
		public void notHandled(String commandId, NotHandledException exception) {
			methods.add(new Pair("notHandled", exception));
		}

		@Override
		public void notEnabled(String commandId, NotEnabledException exception) {
			methods.add(new Pair("notEnabled", exception));
		}

		@Override
		public void notDefined(String commandId, NotDefinedException exception) {
			methods.add(new Pair("notDefined", exception));
		}
	}

	private void compare(String[] calls, ArrayList<Pair> methods) {
		for (int i = 0; i < calls.length && i < methods.size(); i++) {
			assertEquals("call " + i, calls[i], methods.get(i).key);
		}
		assertEquals(calls.length, methods.size());
	}

	@Test
	public void testCommandServiceExecute() throws Exception {
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		cmdService.addExecutionListener(listener);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand(IWorkbenchCommandConstants.FILE_CLOSE_OTHERS, null);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "notEnabled" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	@Test
	public void testCommandExecute() throws Exception {
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		cmdService.addExecutionListener(listener);
		final Command cmd = cmdService.getCommand(IWorkbenchCommandConstants.FILE_CLOSE_OTHERS);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		final ExecutionEvent event = handlerService.createExecutionEvent(cmd, null);
		try {
			cmd.executeWithChecks(event);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "notEnabled" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	/**
	 * Verify that {@link IExecutionListener#preExecute(String, ExecutionEvent)} has
	 * received an event compatible with {@link HandlerUtil} methods.
	 *
	 * @param listener
	 */
	private void verifyHandlerUtilAccessDuringPreExecute(EL listener) {
		assertNotNull("HandlerUtil.getActiveWorkbenchWindow() returned null during ICommandListener.preExecute().",
				listener.wbw);
	}

	@Test
	public void testCommandListenerExecute() throws Exception {
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		final Command cmd = cmdService.getCommand(IWorkbenchCommandConstants.FILE_CLOSE_OTHERS);
		cmd.addExecutionListener(listener);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		final ExecutionEvent event = handlerService.createExecutionEvent(cmd, null);
		try {
			cmd.executeWithChecks(event);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "notEnabled" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	@Test
	public void testCommandServiceExecuteRefresh() throws Exception {
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		cmdService.addExecutionListener(listener);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand(IWorkbenchCommandConstants.FILE_REFRESH, null);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "notHandled" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	@Test
	public void testCommandExecuteRefresh() throws Exception {
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		cmdService.addExecutionListener(listener);
		final Command cmd = cmdService.getCommand(IWorkbenchCommandConstants.FILE_REFRESH);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		final ExecutionEvent event = handlerService.createExecutionEvent(cmd, null);
		try {
			cmd.executeWithChecks(event);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "notHandled" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	@Test
	public void testCommandListenerExecuteRefresh() throws Exception {
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		final Command cmd = cmdService.getCommand(IWorkbenchCommandConstants.FILE_REFRESH);
		cmd.addExecutionListener(listener);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		final ExecutionEvent event = handlerService.createExecutionEvent(cmd, null);
		try {
			cmd.executeWithChecks(event);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "notHandled" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	@Test
	public void testCommandServiceExecuteClosePart() throws Exception {
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IPageLayout.ID_PROGRESS_VIEW);
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		cmdService.addExecutionListener(listener);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand(IWorkbenchCommandConstants.WINDOW_CLOSE_PART, null);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "postExecuteSuccess" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	@Test
	public void testCommandExecuteClosePart() throws Exception {
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IPageLayout.ID_PROGRESS_VIEW);
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		cmdService.addExecutionListener(listener);
		final Command cmd = cmdService.getCommand(IWorkbenchCommandConstants.WINDOW_CLOSE_PART);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		final ExecutionEvent event = handlerService.createExecutionEvent(cmd, null);
		try {
			cmd.executeWithChecks(event);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "postExecuteSuccess" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}

	@Test
	public void testCommandListenerExecuteClosePart() throws Exception {
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IPageLayout.ID_PROGRESS_VIEW);
		EL listener = new EL();
		ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class);
		final Command cmd = cmdService.getCommand(IWorkbenchCommandConstants.WINDOW_CLOSE_PART);
		cmd.addExecutionListener(listener);
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
		final ExecutionEvent event = handlerService.createExecutionEvent(cmd, null);
		try {
			cmd.executeWithChecks(event);
		} catch (Exception e) {
			// do nothing
		}
		System.out.println(listener.methods);
		String[] calls = { "preExecute", "postExecuteSuccess" };
		compare(calls, listener.methods);
		verifyHandlerUtilAccessDuringPreExecute(listener);
	}
}

Back to the top