|
Lines 1-240
Link Here
|
| 1 |
/******************************************************************************* |
1 |
///******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2009 Tasktop Technologies and others. |
2 |
// * Copyright (c) 2004, 2009 Tasktop Technologies and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
// * All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
// * are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
// * which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
6 |
// * http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
7 |
// * |
| 8 |
* Contributors: |
8 |
// * Contributors: |
| 9 |
* Brian de Alwis - initial API and implementation |
9 |
// * Brian de Alwis - initial API and implementation |
| 10 |
* Tasktop Technologies - improvements |
10 |
// * Tasktop Technologies - improvements |
| 11 |
*******************************************************************************/ |
11 |
// *******************************************************************************/ |
| 12 |
|
12 |
// |
| 13 |
package org.eclipse.mylyn.monitor.tests; |
13 |
//package org.eclipse.mylyn.monitor.tests; |
| 14 |
|
14 |
// |
| 15 |
import java.io.File; |
15 |
//import java.io.File; |
| 16 |
import java.io.IOException; |
16 |
//import java.io.IOException; |
| 17 |
import java.lang.reflect.Field; |
17 |
//import java.lang.reflect.Field; |
| 18 |
import java.lang.reflect.Method; |
18 |
//import java.lang.reflect.Method; |
| 19 |
|
19 |
// |
| 20 |
import junit.framework.TestCase; |
20 |
//import junit.framework.TestCase; |
| 21 |
|
21 |
// |
| 22 |
import org.eclipse.core.runtime.IStatus; |
22 |
//import org.eclipse.core.runtime.IStatus; |
| 23 |
import org.eclipse.jface.viewers.StructuredSelection; |
23 |
//import org.eclipse.jface.viewers.StructuredSelection; |
| 24 |
import org.eclipse.jface.window.Window; |
24 |
//import org.eclipse.jface.window.Window; |
| 25 |
import org.eclipse.jface.window.WindowManager; |
25 |
//import org.eclipse.jface.window.WindowManager; |
| 26 |
import org.eclipse.mylyn.internal.monitor.ui.IMonitoredWindow; |
26 |
//import org.eclipse.mylyn.internal.monitor.ui.IMonitoredWindow; |
| 27 |
import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin; |
27 |
//import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin; |
| 28 |
import org.eclipse.mylyn.internal.monitor.usage.InteractionEventLogger; |
28 |
//import org.eclipse.mylyn.internal.monitor.usage.InteractionEventLogger; |
| 29 |
import org.eclipse.mylyn.internal.monitor.usage.UiUsageMonitorPlugin; |
29 |
//import org.eclipse.mylyn.internal.monitor.usage.UiUsageMonitorPlugin; |
| 30 |
import org.eclipse.ui.IMemento; |
30 |
//import org.eclipse.ui.IMemento; |
| 31 |
import org.eclipse.ui.IWorkbenchWindow; |
31 |
//import org.eclipse.ui.IWorkbenchWindow; |
| 32 |
import org.eclipse.ui.PlatformUI; |
32 |
//import org.eclipse.ui.PlatformUI; |
| 33 |
import org.eclipse.ui.XMLMemento; |
33 |
//import org.eclipse.ui.XMLMemento; |
| 34 |
import org.eclipse.ui.internal.IWorkbenchConstants; |
34 |
//import org.eclipse.ui.internal.IWorkbenchConstants; |
| 35 |
import org.eclipse.ui.internal.Workbench; |
35 |
//import org.eclipse.ui.internal.Workbench; |
| 36 |
import org.eclipse.ui.internal.WorkbenchWindow; |
36 |
//import org.eclipse.ui.internal.WorkbenchWindow; |
| 37 |
|
37 |
// |
| 38 |
/** |
38 |
///** |
| 39 |
* @author Brian de Alwis |
39 |
// * @author Brian de Alwis |
| 40 |
* @author Mik Kersten |
40 |
// * @author Mik Kersten |
| 41 |
* @author Shawn Minto |
41 |
// * @author Shawn Minto |
| 42 |
*/ |
42 |
// */ |
| 43 |
public class MultiWindowMonitorTest extends TestCase { |
43 |
//public class MultiWindowMonitorTest extends TestCase { |
| 44 |
|
44 |
// |
| 45 |
private class ContextAwareWorkbenchWindow extends WorkbenchWindow implements IMonitoredWindow { |
45 |
// private class ContextAwareWorkbenchWindow extends WorkbenchWindow implements IMonitoredWindow { |
| 46 |
|
46 |
// |
| 47 |
private boolean monitored = true; |
47 |
// private boolean monitored = true; |
| 48 |
|
48 |
// |
| 49 |
public ContextAwareWorkbenchWindow(int number, boolean monitored) { |
49 |
// public ContextAwareWorkbenchWindow(int number, boolean monitored) { |
| 50 |
super(number); |
50 |
// super(number); |
| 51 |
this.monitored = monitored; |
51 |
// this.monitored = monitored; |
| 52 |
} |
52 |
// } |
| 53 |
|
53 |
// |
| 54 |
public boolean isMonitored() { |
54 |
// public boolean isMonitored() { |
| 55 |
return monitored; |
55 |
// return monitored; |
| 56 |
} |
56 |
// } |
| 57 |
|
57 |
// |
| 58 |
public boolean isPerspectiveManaged() { |
58 |
// public boolean isPerspectiveManaged() { |
| 59 |
return false; |
59 |
// return false; |
| 60 |
} |
60 |
// } |
| 61 |
} |
61 |
// } |
| 62 |
|
62 |
// |
| 63 |
private class MockWorkbench { |
63 |
// private class MockWorkbench { |
| 64 |
private final Workbench wBench; |
64 |
// private final Workbench wBench; |
| 65 |
|
65 |
// |
| 66 |
private WindowManager parentManager = null; |
66 |
// private WindowManager parentManager = null; |
| 67 |
|
67 |
// |
| 68 |
private MockWorkbench() { |
68 |
// private MockWorkbench() { |
| 69 |
wBench = Workbench.getInstance(); |
69 |
// wBench = Workbench.getInstance(); |
| 70 |
|
70 |
// |
| 71 |
Field wManagerField; |
71 |
// Field wManagerField; |
| 72 |
try { |
72 |
// try { |
| 73 |
wManagerField = Workbench.class.getDeclaredField("windowManager"); |
73 |
// wManagerField = Workbench.class.getDeclaredField("windowManager"); |
| 74 |
wManagerField.setAccessible(true); |
74 |
// wManagerField.setAccessible(true); |
| 75 |
parentManager = (WindowManager) wManagerField.get(wBench); |
75 |
// parentManager = (WindowManager) wManagerField.get(wBench); |
| 76 |
|
76 |
// |
| 77 |
} catch (Exception e) { |
77 |
// } catch (Exception e) { |
| 78 |
} |
78 |
// } |
| 79 |
} |
79 |
// } |
| 80 |
|
80 |
// |
| 81 |
private int getNewWindowNumber() { |
81 |
// private int getNewWindowNumber() { |
| 82 |
Window[] windows = parentManager.getWindows(); |
82 |
// Window[] windows = parentManager.getWindows(); |
| 83 |
int count = windows.length; |
83 |
// int count = windows.length; |
| 84 |
|
84 |
// |
| 85 |
boolean checkArray[] = new boolean[count]; |
85 |
// boolean checkArray[] = new boolean[count]; |
| 86 |
for (int nX = 0; nX < count; nX++) { |
86 |
// for (int nX = 0; nX < count; nX++) { |
| 87 |
if (windows[nX] instanceof WorkbenchWindow) { |
87 |
// if (windows[nX] instanceof WorkbenchWindow) { |
| 88 |
WorkbenchWindow ww = (WorkbenchWindow) windows[nX]; |
88 |
// WorkbenchWindow ww = (WorkbenchWindow) windows[nX]; |
| 89 |
int index = ww.getNumber() - 1; |
89 |
// int index = ww.getNumber() - 1; |
| 90 |
if (index >= 0 && index < count) { |
90 |
// if (index >= 0 && index < count) { |
| 91 |
checkArray[index] = true; |
91 |
// checkArray[index] = true; |
| 92 |
} |
92 |
// } |
| 93 |
} |
93 |
// } |
| 94 |
} |
94 |
// } |
| 95 |
|
95 |
// |
| 96 |
for (int index = 0; index < count; index++) { |
96 |
// for (int index = 0; index < count; index++) { |
| 97 |
if (!checkArray[index]) { |
97 |
// if (!checkArray[index]) { |
| 98 |
return index + 1; |
98 |
// return index + 1; |
| 99 |
} |
99 |
// } |
| 100 |
} |
100 |
// } |
| 101 |
return count + 1; |
101 |
// return count + 1; |
| 102 |
} |
102 |
// } |
| 103 |
|
103 |
// |
| 104 |
private ContextAwareWorkbenchWindow newWorkbenchWindow(boolean isMonitored) { |
104 |
// private ContextAwareWorkbenchWindow newWorkbenchWindow(boolean isMonitored) { |
| 105 |
return new ContextAwareWorkbenchWindow(getNewWindowNumber(), isMonitored); |
105 |
// return new ContextAwareWorkbenchWindow(getNewWindowNumber(), isMonitored); |
| 106 |
} |
106 |
// } |
| 107 |
|
107 |
// |
| 108 |
public ContextAwareWorkbenchWindow restoreState(IMemento memento, boolean isMonitored) { |
108 |
// public ContextAwareWorkbenchWindow restoreState(IMemento memento, boolean isMonitored) { |
| 109 |
|
109 |
// |
| 110 |
ContextAwareWorkbenchWindow newWindow = newWorkbenchWindow(isMonitored); |
110 |
// ContextAwareWorkbenchWindow newWindow = newWorkbenchWindow(isMonitored); |
| 111 |
newWindow.create(); |
111 |
// newWindow.create(); |
| 112 |
|
112 |
// |
| 113 |
parentManager.add(newWindow); |
113 |
// parentManager.add(newWindow); |
| 114 |
|
114 |
// |
| 115 |
boolean opened = false; |
115 |
// boolean opened = false; |
| 116 |
|
116 |
// |
| 117 |
try { |
117 |
// try { |
| 118 |
newWindow.restoreState(memento, null); |
118 |
// newWindow.restoreState(memento, null); |
| 119 |
newWindow.open(); |
119 |
// newWindow.open(); |
| 120 |
opened = true; |
120 |
// opened = true; |
| 121 |
} finally { |
121 |
// } finally { |
| 122 |
if (!opened) { |
122 |
// if (!opened) { |
| 123 |
newWindow.close(); |
123 |
// newWindow.close(); |
| 124 |
} |
124 |
// } |
| 125 |
} |
125 |
// } |
| 126 |
|
126 |
// |
| 127 |
return newWindow; |
127 |
// return newWindow; |
| 128 |
} |
128 |
// } |
| 129 |
} |
129 |
// } |
| 130 |
|
130 |
// |
| 131 |
private final InteractionEventLogger logger = UiUsageMonitorPlugin.getDefault().getInteractionLogger(); |
131 |
// private final InteractionEventLogger logger = UiUsageMonitorPlugin.getDefault().getInteractionLogger(); |
| 132 |
|
132 |
// |
| 133 |
private MockSelectionMonitor selectionMonitor; |
133 |
// private MockSelectionMonitor selectionMonitor; |
| 134 |
|
134 |
// |
| 135 |
private IWorkbenchWindow window1; |
135 |
// private IWorkbenchWindow window1; |
| 136 |
|
136 |
// |
| 137 |
private IWorkbenchWindow window2; |
137 |
// private IWorkbenchWindow window2; |
| 138 |
|
138 |
// |
| 139 |
private IWorkbenchWindow window3; |
139 |
// private IWorkbenchWindow window3; |
| 140 |
|
140 |
// |
| 141 |
private IWorkbenchWindow window4; |
141 |
// private IWorkbenchWindow window4; |
| 142 |
|
142 |
// |
| 143 |
private boolean monitoringWasEnabled; |
143 |
// private boolean monitoringWasEnabled; |
| 144 |
|
144 |
// |
| 145 |
@Override |
145 |
// @Override |
| 146 |
protected void setUp() throws Exception { |
146 |
// protected void setUp() throws Exception { |
| 147 |
super.setUp(); |
147 |
// super.setUp(); |
| 148 |
selectionMonitor = new MockSelectionMonitor(); |
148 |
// selectionMonitor = new MockSelectionMonitor(); |
| 149 |
monitoringWasEnabled = UiUsageMonitorPlugin.getDefault().isMonitoringEnabled(); |
149 |
// monitoringWasEnabled = UiUsageMonitorPlugin.getDefault().isMonitoringEnabled(); |
| 150 |
UiUsageMonitorPlugin.getDefault().stopMonitoring(); |
150 |
// UiUsageMonitorPlugin.getDefault().stopMonitoring(); |
| 151 |
|
151 |
// |
| 152 |
// make sure the MonitorUiPlugin is fully initialized |
152 |
// // make sure the MonitorUiPlugin is fully initialized |
| 153 |
while (PlatformUI.getWorkbench().getDisplay().readAndDispatch()) { |
153 |
// while (PlatformUI.getWorkbench().getDisplay().readAndDispatch()) { |
| 154 |
} |
154 |
// } |
| 155 |
|
155 |
// |
| 156 |
window1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
156 |
// window1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 157 |
assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window1)); |
157 |
// assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window1)); |
| 158 |
window2 = duplicateWindow(window1); |
158 |
// window2 = duplicateWindow(window1); |
| 159 |
assertNotNull(window2); |
159 |
// assertNotNull(window2); |
| 160 |
assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window2)); |
160 |
// assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window2)); |
| 161 |
window3 = createContextAwareWindow(true, window1); |
161 |
// window3 = createContextAwareWindow(true, window1); |
| 162 |
assertNotNull(window3); |
162 |
// assertNotNull(window3); |
| 163 |
assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window3)); |
163 |
// assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window3)); |
| 164 |
window4 = createContextAwareWindow(false, window1); |
164 |
// window4 = createContextAwareWindow(false, window1); |
| 165 |
assertNotNull(window4); |
165 |
// assertNotNull(window4); |
| 166 |
assertFalse(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window4)); |
166 |
// assertFalse(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window4)); |
| 167 |
} |
167 |
// } |
| 168 |
|
168 |
// |
| 169 |
@Override |
169 |
// @Override |
| 170 |
protected void tearDown() throws Exception { |
170 |
// protected void tearDown() throws Exception { |
| 171 |
super.tearDown(); |
171 |
// super.tearDown(); |
| 172 |
window2.close(); |
172 |
// window2.close(); |
| 173 |
window3.close(); |
173 |
// window3.close(); |
| 174 |
window4.close(); |
174 |
// window4.close(); |
| 175 |
if (monitoringWasEnabled) { |
175 |
// if (monitoringWasEnabled) { |
| 176 |
UiUsageMonitorPlugin.getDefault().startMonitoring(); |
176 |
// UiUsageMonitorPlugin.getDefault().startMonitoring(); |
| 177 |
} |
177 |
// } |
| 178 |
if (selectionMonitor != null) { |
178 |
// if (selectionMonitor != null) { |
| 179 |
selectionMonitor.dispose(); |
179 |
// selectionMonitor.dispose(); |
| 180 |
} |
180 |
// } |
| 181 |
} |
181 |
// } |
| 182 |
|
182 |
// |
| 183 |
protected void generateSelection(IWorkbenchWindow w) { |
183 |
// protected void generateSelection(IWorkbenchWindow w) { |
| 184 |
selectionMonitor.selectionChanged(w.getActivePage().getActivePart(), new StructuredSelection("yo")); |
184 |
// selectionMonitor.selectionChanged(w.getActivePage().getActivePart(), new StructuredSelection("yo")); |
| 185 |
} |
185 |
// } |
| 186 |
|
186 |
// |
| 187 |
public void testMultipleWindows() throws IOException { |
187 |
// public void testMultipleWindows() throws IOException { |
| 188 |
File monitorFile = UiUsageMonitorPlugin.getDefault().getMonitorLogFile(); |
188 |
// File monitorFile = UiUsageMonitorPlugin.getDefault().getMonitorLogFile(); |
| 189 |
logger.clearInteractionHistory(); |
189 |
// logger.clearInteractionHistory(); |
| 190 |
assertEquals(0, logger.getHistoryFromFile(monitorFile).size()); |
190 |
// assertEquals(0, logger.getHistoryFromFile(monitorFile).size()); |
| 191 |
|
191 |
// |
| 192 |
generateSelection(window1); |
192 |
// generateSelection(window1); |
| 193 |
assertEquals(0, logger.getHistoryFromFile(monitorFile).size()); |
193 |
// assertEquals(0, logger.getHistoryFromFile(monitorFile).size()); |
| 194 |
|
194 |
// |
| 195 |
UiUsageMonitorPlugin.getDefault().startMonitoring(); |
195 |
// UiUsageMonitorPlugin.getDefault().startMonitoring(); |
| 196 |
generateSelection(window1); |
196 |
// generateSelection(window1); |
| 197 |
generateSelection(window2); |
197 |
// generateSelection(window2); |
| 198 |
generateSelection(window3); |
198 |
// generateSelection(window3); |
| 199 |
generateSelection(window4); |
199 |
// generateSelection(window4); |
| 200 |
assertEquals(3, logger.getHistoryFromFile(monitorFile).size()); |
200 |
// assertEquals(3, logger.getHistoryFromFile(monitorFile).size()); |
| 201 |
} |
201 |
// } |
| 202 |
|
202 |
// |
| 203 |
protected IWorkbenchWindow duplicateWindow(IWorkbenchWindow window) { |
203 |
// protected IWorkbenchWindow duplicateWindow(IWorkbenchWindow window) { |
| 204 |
WorkbenchWindow w = (WorkbenchWindow) window; |
204 |
// WorkbenchWindow w = (WorkbenchWindow) window; |
| 205 |
XMLMemento memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WINDOW); |
205 |
// XMLMemento memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WINDOW); |
| 206 |
IStatus status = w.saveState(memento); |
206 |
// IStatus status = w.saveState(memento); |
| 207 |
if (!status.isOK()) { |
207 |
// if (!status.isOK()) { |
| 208 |
fail("failed to duplicate window: " + status); |
208 |
// fail("failed to duplicate window: " + status); |
| 209 |
} |
209 |
// } |
| 210 |
return restoreWorkbenchWindow((Workbench) w.getWorkbench(), memento); |
210 |
// return restoreWorkbenchWindow((Workbench) w.getWorkbench(), memento); |
| 211 |
} |
211 |
// } |
| 212 |
|
212 |
// |
| 213 |
private IWorkbenchWindow createContextAwareWindow(boolean monitored, IWorkbenchWindow window) { |
213 |
// private IWorkbenchWindow createContextAwareWindow(boolean monitored, IWorkbenchWindow window) { |
| 214 |
WorkbenchWindow w = (WorkbenchWindow) window; |
214 |
// WorkbenchWindow w = (WorkbenchWindow) window; |
| 215 |
XMLMemento memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WINDOW); |
215 |
// XMLMemento memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WINDOW); |
| 216 |
IStatus status = w.saveState(memento); |
216 |
// IStatus status = w.saveState(memento); |
| 217 |
if (!status.isOK()) { |
217 |
// if (!status.isOK()) { |
| 218 |
fail("failed to duplicate window: " + status); |
218 |
// fail("failed to duplicate window: " + status); |
| 219 |
} |
219 |
// } |
| 220 |
return new MockWorkbench().restoreState(memento, monitored); |
220 |
// return new MockWorkbench().restoreState(memento, monitored); |
| 221 |
} |
221 |
// } |
| 222 |
|
222 |
// |
| 223 |
protected IWorkbenchWindow restoreWorkbenchWindow(Workbench workbench, IMemento memento) { |
223 |
// protected IWorkbenchWindow restoreWorkbenchWindow(Workbench workbench, IMemento memento) { |
| 224 |
return (IWorkbenchWindow) invokeMethod(workbench, "restoreWorkbenchWindow", new Class[] { IMemento.class }, |
224 |
// return (IWorkbenchWindow) invokeMethod(workbench, "restoreWorkbenchWindow", new Class[] { IMemento.class }, |
| 225 |
new Object[] { memento }); |
225 |
// new Object[] { memento }); |
| 226 |
} |
226 |
// } |
| 227 |
|
227 |
// |
| 228 |
protected Object invokeMethod(Object instance, String methodName, Class<?> argTypes[], Object arguments[]) { |
228 |
// protected Object invokeMethod(Object instance, String methodName, Class<?> argTypes[], Object arguments[]) { |
| 229 |
Class<?> clas = instance.getClass(); |
229 |
// Class<?> clas = instance.getClass(); |
| 230 |
try { |
230 |
// try { |
| 231 |
Method method = clas.getDeclaredMethod(methodName, argTypes); |
231 |
// Method method = clas.getDeclaredMethod(methodName, argTypes); |
| 232 |
method.setAccessible(true); |
232 |
// method.setAccessible(true); |
| 233 |
return method.invoke(instance, arguments); |
233 |
// return method.invoke(instance, arguments); |
| 234 |
} catch (Exception ex) { |
234 |
// } catch (Exception ex) { |
| 235 |
fail("exception during reflective invocation of " + clas.getName() + "." + methodName + ": " + ex); |
235 |
// fail("exception during reflective invocation of " + clas.getName() + "." + methodName + ": " + ex); |
| 236 |
return null; |
236 |
// return null; |
| 237 |
} |
237 |
// } |
| 238 |
} |
238 |
// } |
| 239 |
|
239 |
// |
| 240 |
} |
240 |
//} |