Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6b66146ad292300a3af103fd6eb13dd59d1c7f7b (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
/*******************************************************************************
 * Copyright (c) 2004, 2009 Tasktop Technologies 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:
 *     Brian de Alwis - initial API and implementation
 *     Tasktop Technologies - improvements
 *******************************************************************************/

package org.eclipse.mylyn.monitor.tests;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import junit.framework.TestCase;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.window.WindowManager;
import org.eclipse.mylyn.internal.monitor.ui.IMonitoredWindow;
import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin;
import org.eclipse.mylyn.internal.monitor.usage.InteractionEventLogger;
import org.eclipse.mylyn.internal.monitor.usage.UiUsageMonitorPlugin;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.internal.IWorkbenchConstants;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.ui.internal.WorkbenchWindow;

/**
 * @author Brian de Alwis
 * @author Mik Kersten
 * @author Shawn Minto
 */
public class MultiWindowMonitorTest extends TestCase {

	private class ContextAwareWorkbenchWindow extends WorkbenchWindow implements IMonitoredWindow {

		private boolean monitored = true;

		public ContextAwareWorkbenchWindow(int number, boolean monitored) {
			super(number);
			this.monitored = monitored;
		}

		public boolean isMonitored() {
			return monitored;
		}

		public boolean isPerspectiveManaged() {
			return false;
		}
	}

	private class MockWorkbench {
		private final Workbench wBench;

		private WindowManager parentManager = null;

		private MockWorkbench() {
			wBench = Workbench.getInstance();

			Field wManagerField;
			try {
				wManagerField = Workbench.class.getDeclaredField("windowManager");
				wManagerField.setAccessible(true);
				parentManager = (WindowManager) wManagerField.get(wBench);

			} catch (Exception e) {
			}
		}

		private int getNewWindowNumber() {
			Window[] windows = parentManager.getWindows();
			int count = windows.length;

			boolean checkArray[] = new boolean[count];
			for (int nX = 0; nX < count; nX++) {
				if (windows[nX] instanceof WorkbenchWindow) {
					WorkbenchWindow ww = (WorkbenchWindow) windows[nX];
					int index = ww.getNumber() - 1;
					if (index >= 0 && index < count) {
						checkArray[index] = true;
					}
				}
			}

			for (int index = 0; index < count; index++) {
				if (!checkArray[index]) {
					return index + 1;
				}
			}
			return count + 1;
		}

		private ContextAwareWorkbenchWindow newWorkbenchWindow(boolean isMonitored) {
			return new ContextAwareWorkbenchWindow(getNewWindowNumber(), isMonitored);
		}

		public ContextAwareWorkbenchWindow restoreState(IMemento memento, boolean isMonitored) {

			ContextAwareWorkbenchWindow newWindow = newWorkbenchWindow(isMonitored);
			newWindow.create();

			parentManager.add(newWindow);

			boolean opened = false;

			try {
				newWindow.restoreState(memento, null);
				newWindow.open();
				opened = true;
			} finally {
				if (!opened) {
					newWindow.close();
				}
			}

			return newWindow;
		}
	}

	private final InteractionEventLogger logger = UiUsageMonitorPlugin.getDefault().getInteractionLogger();

	private MockSelectionMonitor selectionMonitor;

	private IWorkbenchWindow window1;

	private IWorkbenchWindow window2;

	private IWorkbenchWindow window3;

	private IWorkbenchWindow window4;

	private boolean monitoringWasEnabled;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		selectionMonitor = new MockSelectionMonitor();
		monitoringWasEnabled = UiUsageMonitorPlugin.getDefault().isMonitoringEnabled();
		UiUsageMonitorPlugin.getDefault().stopMonitoring();

		// make sure the MonitorUiPlugin is fully initialized
		while (PlatformUI.getWorkbench().getDisplay().readAndDispatch()) {
		}

		window1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window1));
		window2 = duplicateWindow(window1);
		assertNotNull(window2);
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window2));
		window3 = createContextAwareWindow(true, window1);
		assertNotNull(window3);
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window3));
		window4 = createContextAwareWindow(false, window1);
		assertNotNull(window4);
		assertFalse(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window4));
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		window2.close();
		window3.close();
		window4.close();
		if (monitoringWasEnabled) {
			UiUsageMonitorPlugin.getDefault().startMonitoring();
		}
		if (selectionMonitor != null) {
			selectionMonitor.dispose();
		}
	}

	protected void generateSelection(IWorkbenchWindow w) {
		selectionMonitor.selectionChanged(w.getActivePage().getActivePart(), new StructuredSelection("yo"));
	}

	public void testMultipleWindows() throws IOException {
		File monitorFile = UiUsageMonitorPlugin.getDefault().getMonitorLogFile();
		logger.clearInteractionHistory();
		assertEquals(0, logger.getHistoryFromFile(monitorFile).size());

		generateSelection(window1);
		assertEquals(0, logger.getHistoryFromFile(monitorFile).size());

		UiUsageMonitorPlugin.getDefault().startMonitoring();
		generateSelection(window1);
		generateSelection(window2);
		generateSelection(window3);
		generateSelection(window4);
		assertEquals(3, logger.getHistoryFromFile(monitorFile).size());
	}

	protected IWorkbenchWindow duplicateWindow(IWorkbenchWindow window) {
		WorkbenchWindow w = (WorkbenchWindow) window;
		XMLMemento memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WINDOW);
		IStatus status = w.saveState(memento);
		if (!status.isOK()) {
			fail("failed to duplicate window: " + status);
		}
		return restoreWorkbenchWindow((Workbench) w.getWorkbench(), memento);
	}

	private IWorkbenchWindow createContextAwareWindow(boolean monitored, IWorkbenchWindow window) {
		WorkbenchWindow w = (WorkbenchWindow) window;
		XMLMemento memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WINDOW);
		IStatus status = w.saveState(memento);
		if (!status.isOK()) {
			fail("failed to duplicate window: " + status);
		}
		return new MockWorkbench().restoreState(memento, monitored);
	}

	protected IWorkbenchWindow restoreWorkbenchWindow(Workbench workbench, IMemento memento) {
		return (IWorkbenchWindow) invokeMethod(workbench, "restoreWorkbenchWindow", new Class[] { IMemento.class },
				new Object[] { memento });
	}

	protected Object invokeMethod(Object instance, String methodName, Class<?> argTypes[], Object arguments[]) {
		Class<?> clas = instance.getClass();
		try {
			Method method = clas.getDeclaredMethod(methodName, argTypes);
			method.setAccessible(true);
			return method.invoke(instance, arguments);
		} catch (Exception ex) {
			fail("exception during reflective invocation of " + clas.getName() + "." + methodName + ": " + ex);
			return null;
		}
	}

}

Back to the top