Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 90e285240f103b26b04e256180d9ee06c28a8ddc (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*******************************************************************************
 * Copyright (c) 2017 Fabio Zadrozny and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Fabio Zadrozny - initial API and implementation - http://eclip.se/8519
 *******************************************************************************/
package org.eclipse.e4.ui.macros.tests;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.eclipse.e4.core.macros.CancelMacroRecordingException;
import org.eclipse.e4.core.macros.EMacroService;
import org.eclipse.e4.core.macros.IMacroInstruction;
import org.eclipse.e4.core.macros.IMacroInstructionFactory;
import org.eclipse.e4.core.macros.IMacroPlaybackContext;
import org.eclipse.e4.core.macros.IMacroStateListener;
import org.eclipse.e4.core.macros.internal.MacroManager;
import org.eclipse.e4.core.macros.internal.MacroManager.PathAndTime;
import org.eclipse.e4.core.macros.internal.MacroServiceImplementation;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class MacroTest {

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();

	private static class PlaybackContext implements IMacroPlaybackContext {

		public StringBuffer buffer = new StringBuffer();

		public void recordPlayback(String name) {
			if (buffer.length() > 0) {
				buffer.append("\n");
			}
			buffer.append(name);
		}


		private final Map<Object, Object> ctx = new HashMap<>();

		@Override
		public Object get(String key) {
			return ctx.get(key);
		}

		@Override
		public void set(String key, Object value) {
			ctx.put(key, value);
		}
	}

	private static class DummyMacroInstruction implements IMacroInstruction {

		private String fName;

		public DummyMacroInstruction(String name) {
			this.fName = name;
		}

		@Override
		public void execute(IMacroPlaybackContext macroPlaybackContext) {
			((PlaybackContext) macroPlaybackContext).recordPlayback(this.fName);
		}

		@Override
		public String getId() {
			return "dummy";
		}

		@Override
		public String toString() {
			return "Dummy macro instruction";
		}

		@Override
		public Map<String, String> toMap() {
			HashMap<String, String> map = new HashMap<>();
			map.put("dummyKey", "dummyValue");
			map.put("name", this.fName);
			return map;
		}

	}

	@Test
	public void testRecordingState() throws Exception {
		Map<String, IMacroInstructionFactory> macroInstructionIdToFactory = makeMacroInstructionIdToFactory();
		MacroServiceImplementation macroService = new MacroServiceImplementation(null, null);
		Field field = macroService.getClass().getDeclaredField("fMacroInstructionIdToFactory");
		field.setAccessible(true);
		field.set(macroService, macroInstructionIdToFactory);
		File root = folder.getRoot();
		macroService.getMacroManager().setMacrosDirectories(root);
		final StringBuilder buf = new StringBuilder();
		macroService.getMacroManager().addMacroStateListener(new IMacroStateListener() {

			@Override
			public void macroStateChanged(EMacroService macroService, StateChange stateChange) {
				buf.append("rec: ").append(macroService.isRecording()).append(" play: ")
						.append(macroService.isPlayingBack()).append("\n");
			}
		});

		Assert.assertFalse(macroService.isRecording());
		Assert.assertEquals("", buf.toString());

		macroService.toggleMacroRecord();
		Assert.assertEquals("rec: true play: false\n", buf.toString());
		buf.setLength(0);
		Assert.assertTrue(macroService.isRecording());

		// Add something, otherwise no macro will be recorded
		macroService.addMacroInstruction(new DummyMacroInstruction("test"));

		macroService.toggleMacroRecord();
		Assert.assertEquals("rec: false play: false\n", buf.toString());
		buf.setLength(0);
		Assert.assertFalse(macroService.isRecording());

		PlaybackContext playbackContext = new PlaybackContext();
		macroService.getMacroManager().playbackLastMacro(macroService, playbackContext, macroInstructionIdToFactory);
		Assert.assertEquals("rec: false play: true\n"
				+ "rec: false play: false\n", buf.toString());
		buf.setLength(0);

		macroService.toggleMacroRecord();
		macroService.getMacroManager().playbackLastMacro(macroService, playbackContext, macroInstructionIdToFactory);
		macroService.toggleMacroRecord();
		Assert.assertEquals(
				"rec: true play: false\n"
				+ "rec: true play: true\n"
				+ "rec: true play: false\n"
				+ "rec: false play: false\n",
				buf.toString());
	}

	@Test
	public void testAddMacroInstructions() throws Exception {
		MacroManager macroManager = new MacroManager();
		Map<String, IMacroInstructionFactory> macroInstructionIdToFactory = makeMacroInstructionIdToFactory();
		PlaybackContext playbackContext = new PlaybackContext();
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
		Assert.assertTrue(macroManager.isRecording());
		macroManager.addMacroInstruction(new DummyMacroInstruction("macro1"));
		macroManager.addMacroInstruction(new DummyMacroInstruction("macro2"));
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
		Assert.assertFalse(macroManager.isRecording());

		macroManager.playbackLastMacro(null, playbackContext, macroInstructionIdToFactory);
		Assert.assertEquals("macro1\nmacro2", playbackContext.buffer.toString());
	}

	@Test
	public void testAddMacroInstructionPriority() throws Exception {
		MacroManager macroManager = new MacroManager();
		Map<String, IMacroInstructionFactory> macroInstructionIdToFactory = makeMacroInstructionIdToFactory();
		PlaybackContext playbackContext = new PlaybackContext();
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
		Assert.assertTrue(macroManager.isRecording());
		Object ev = new Integer(1);
		macroManager.addMacroInstruction(new DummyMacroInstruction("macro1"), ev, 2);
		Assert.assertEquals(1, macroManager.getLenOfMacroBeingRecorded());
		macroManager.addMacroInstruction(new DummyMacroInstruction("macro2"), ev, 1); // will not replace it
		Assert.assertEquals(1, macroManager.getLenOfMacroBeingRecorded());
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
		Assert.assertFalse(macroManager.isRecording());

		macroManager.playbackLastMacro(null, playbackContext, macroInstructionIdToFactory);
		Assert.assertEquals("macro1", playbackContext.buffer.toString());
	}

	@Test
	public void testAddMacroInstructionPriority1() throws Exception {
		MacroManager macroManager = new MacroManager();
		Map<String, IMacroInstructionFactory> macroInstructionIdToFactory = makeMacroInstructionIdToFactory();
		PlaybackContext playbackContext = new PlaybackContext();
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
		Assert.assertTrue(macroManager.isRecording());
		Object ev = new Integer(1);
		macroManager.addMacroInstruction(new DummyMacroInstruction("macro1"), ev, 1);
		Assert.assertEquals(1, macroManager.getLenOfMacroBeingRecorded());
		macroManager.addMacroInstruction(new DummyMacroInstruction("macro2"), ev, 2); // will replace it
		Assert.assertEquals(1, macroManager.getLenOfMacroBeingRecorded());
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
		Assert.assertFalse(macroManager.isRecording());

		macroManager.playbackLastMacro(null, playbackContext, macroInstructionIdToFactory);
		Assert.assertEquals("macro2", playbackContext.buffer.toString());
	}

	@Test
	public void testMacroManagerSaveRestore() throws Exception {
		File root = folder.getRoot();
		MacroManager macroManager = new MacroManager(root);
		Map<String, IMacroInstructionFactory> macroInstructionIdToFactory = makeMacroInstructionIdToFactory();
		createMacroWithOneDummyMacroInstruction(macroManager, macroInstructionIdToFactory);
		String[] macroNames = listTemporaryMacros(root);
		Assert.assertEquals(1, macroNames.length);

		// Create a new macroManager (to force getting from the disk).
		macroManager = new MacroManager(root);
		PlaybackContext playbackContext = new PlaybackContext();
		macroManager.playbackLastMacro(null, playbackContext, macroInstructionIdToFactory);
		Assert.assertEquals("macro1", playbackContext.buffer.toString());
	}

	@Test
	public void testMacroManagerMaxNumberOfMacros() throws Exception {
		File root = folder.getRoot();
		MacroManager macroManager = new MacroManager(root);
		macroManager.setMaxNumberOfTemporaryMacros(2);
		Map<String, IMacroInstructionFactory> macroInstructionIdToFactory = makeMacroInstructionIdToFactory();

		createMacroWithOneDummyMacroInstruction(macroManager, macroInstructionIdToFactory);

		String[] macroNames1 = listTemporaryMacros(root);
		Assert.assertEquals(macroNames1.length, 1);

		// Sleep to make sure that the time of the file will be different
		// (otherwise, if it runs too fast, the same time could be applied to
		// the first, second and last file, in which case we may remove the
		// wrong one).
		sleepABit();
		createMacroWithOneDummyMacroInstruction(macroManager, macroInstructionIdToFactory);

		String[] macroNames2 = listTemporaryMacros(root);
		Assert.assertEquals(2, macroNames2.length);
		HashSet<Long> times = new HashSet<>();
		List<PathAndTime> macrosPathAndTime = macroManager.listTemporaryMacrosPathAndTime(root);
		for (PathAndTime pathAndTime : macrosPathAndTime) {
			times.add(pathAndTime.fLastModified);
		}
		Assert.assertEquals(2, times.size());

		// Now, creating a new one removes the old one
		sleepABit();
		createMacroWithOneDummyMacroInstruction(macroManager, macroInstructionIdToFactory);
		macrosPathAndTime = macroManager.listTemporaryMacrosPathAndTime(root);

		String[] macroNames3 = listTemporaryMacros(root);
		Assert.assertEquals(2, macroNames3.length);

		for (PathAndTime pathAndTime : macrosPathAndTime) {
			times.add(pathAndTime.fLastModified);
		}

		Assert.assertEquals(3, times.size());

		assertNotContains(Arrays.asList(macroNames3), macroNames1[0]);
	}

	private void assertNotContains(Collection<String> list, String name) {
		if (list.contains(name)) {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("Did not expect: ").append(name).append("\nTo be in:\n");
			for (String string : list) {
				stringBuffer.append(string).append("\n");
			}
			Assert.fail(stringBuffer.toString());
		}

	}

	private void sleepABit() {
		synchronized (this) {
			try {
				this.wait(1000); // Sleep a full second to make sure that we have a new timestamp
									// on files to make test less brittle (not all systems have
									// a good precision).
			} catch (InterruptedException e) {
				// Ignore in this case
			}
		}

	}

	protected String[] listTemporaryMacros(File root) {
		String[] files = root.list(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".js") && name.startsWith("temp_macro");
			}
		});
		return files;
	}

	protected void createMacroWithOneDummyMacroInstruction(MacroManager macroManager,
			Map<String, IMacroInstructionFactory> macroInstructionIdToFactory) throws CancelMacroRecordingException {
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
		macroManager.addMacroInstruction(new DummyMacroInstruction("macro1"));
		macroManager.toggleMacroRecord(null, macroInstructionIdToFactory);
	}

	private Map<String, IMacroInstructionFactory> makeMacroInstructionIdToFactory() {
		Map<String, IMacroInstructionFactory> macroInstructionIdToFactory = new HashMap<>();
		macroInstructionIdToFactory.put("dummy", new IMacroInstructionFactory() {

			@Override
			public IMacroInstruction create(Map<String, String> stringMap) {
				if (stringMap.size() != 2) {
					throw new AssertionError("Expected map size to be 2. Found: " + stringMap.size());
				}
				if (!stringMap.get("dummyKey").equals("dummyValue")) {
					throw new AssertionError("Did not find dummyKey->dummyValue mapping.");
				}
				if (stringMap.get("name") == null) {
					throw new AssertionError("Expected name to be defined.");
				}

				return new DummyMacroInstruction(stringMap.get("name"));
			}
		});
		return macroInstructionIdToFactory;
	}

}

Back to the top