Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9a692560d11bb039bd12713f88365c46f3c7c723 (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
/*******************************************************************************
 * Copyright (c) 2004, 2010 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.sandbox.tests;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.mylyn.context.ui.AbstractFocusViewAction;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.context.ui.ContextUiPlugin;
import org.eclipse.mylyn.internal.java.ui.JavaUiBridgePlugin;
import org.eclipse.mylyn.internal.monitor.core.collection.IUsageCollector;
import org.eclipse.mylyn.internal.monitor.reports.collectors.FocusedUiUsageAnalysisCollector;
import org.eclipse.mylyn.internal.monitor.reports.collectors.FocusedUiViewUsageCollector;
import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin;
import org.eclipse.mylyn.internal.monitor.usage.InteractionEventLogger;
import org.eclipse.mylyn.internal.monitor.usage.ReportGenerator;
import org.eclipse.mylyn.internal.monitor.usage.UiUsageMonitorPlugin;
import org.eclipse.mylyn.internal.monitor.usage.UsageStatisticsSummary;
import org.eclipse.mylyn.internal.tasks.ui.actions.TaskActivateAction;
import org.eclipse.mylyn.monitor.core.InteractionEvent;

/**
 * @author Mik Kersten
 */
public class StatisticsReportingTest extends TestCase {

	private InteractionEventLogger logger;

	private final FocusedUiViewUsageCollector viewCollector = new FocusedUiViewUsageCollector();

	private final FocusedUiUsageAnalysisCollector editRatioCollector = new FocusedUiUsageAnalysisCollector();;

	private ReportGenerator report;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		assertNotNull(ContextCorePlugin.getDefault());
		assertNotNull(JavaUiBridgePlugin.getDefault());
		assertNotNull(PackageExplorerPart.openInActivePerspective());

		UiUsageMonitorPlugin.getDefault().startMonitoring();
		assertTrue(UiUsageMonitorPlugin.getDefault().isMonitoringEnabled());
		logger = UiUsageMonitorPlugin.getDefault().getInteractionLogger();
		logger.clearInteractionHistory();

		List<IUsageCollector> collectors = new ArrayList<IUsageCollector>();
		collectors.add(viewCollector);
		collectors.add(editRatioCollector);
		report = new ReportGenerator(logger, collectors);
		report.forceSyncForTesting(true);
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	protected InteractionEvent mockExplorerSelection(String handle) {
		InteractionEvent event = new InteractionEvent(InteractionEvent.Kind.SELECTION, "java", handle,
				JavaUI.ID_PACKAGES);
		MonitorUiPlugin.getDefault().notifyInteractionObserved(event);
		return event;
	}

	protected void mockEdit(String handle) {
		MonitorUiPlugin.getDefault().notifyInteractionObserved(
				new InteractionEvent(InteractionEvent.Kind.EDIT, "java", handle, JavaUI.ID_PACKAGES));
	}

	protected void mockTypesSelection(String handle) {
		MonitorUiPlugin.getDefault().notifyInteractionObserved(
				new InteractionEvent(InteractionEvent.Kind.SELECTION, "java", handle, JavaUI.ID_TYPES_VIEW));
	}

	public void testEditRatio() throws InvocationTargetException, InterruptedException {
		logger.stopMonitoring();
		PackageExplorerPart part = PackageExplorerPart.openInActivePerspective();
		assertNotNull(part.getTreeViewer());
		part.setFocus();

		logger.startMonitoring();
		final InteractionEvent first = mockExplorerSelection("A.java");
		mockUserDelay();
		mockUserDelay();
		final InteractionEvent second = mockExplorerSelection("A.java");

		assertTrue(!first.getDate().equals(second.getDelta()));

		try {
			// XXX: this is a hack and sensitive to CPU speeds
			Thread.sleep(3000);
		} catch (InterruptedException ie) {
			fail();
		}

		mockEdit("A.java");

		MonitorUiPlugin.getDefault().notifyInteractionObserved(InteractionEvent.makeCommand(TaskActivateAction.ID, ""));

		mockExplorerSelection("A.java");
		mockEdit("A.java");
		mockUserDelay();
		mockEdit("A.java");

		logger.stopMonitoring();
		report.getStatisticsFromInteractionHistory(logger.getOutputFile(), null);

		// TODO: these are off from expected when test run alone, due to unknown
		// element selections
		assertEquals(0.5f, editRatioCollector.getBaselineRatio(-1));
		assertEquals(2f, editRatioCollector.getMylynRatio(-1));
	}

	@SuppressWarnings("unused")
	public void testSimpleSelection() {
		mockExplorerSelection("A.java");
		report.getStatisticsFromInteractionHistory(logger.getOutputFile(), new JobChangeAdapter() {
			public void done() {
				UsageStatisticsSummary summary = report.getLastParsedSummary();
				assertTrue(summary.getSingleSummaries().size() > 0);
			}
		});

	}

	@SuppressWarnings("unused")
	public void testFilteredModeDetection() throws IOException {
		UiUsageMonitorPlugin.getDefault().addMonitoredPreferences(ContextUiPlugin.getDefault().getPreferenceStore());

		UiUsageMonitorPlugin.getDefault().getInteractionLogger().clearInteractionHistory();
		mockExplorerSelection("A.java");
		mockUserDelay();
		mockExplorerSelection("A.java");
		mockUserDelay();
		mockTypesSelection("A.java");

		assertNotNull(ContextUiPlugin.getDefault().getPreferenceStore());
		String prefId = AbstractFocusViewAction.PREF_ID_PREFIX + JavaUI.ID_PACKAGES;
		assertNotNull(prefId);

		PackageExplorerPart part = PackageExplorerPart.openInActivePerspective();
		assertNotNull(part);
		// AbstractFocusViewAction action =
		// FocusPackageExplorerAction.getActionForPart(part);
		// assertNotNull(action);

		ContextUiPlugin.getDefault().getPreferenceStore().setValue(prefId, true);

		mockExplorerSelection("A.java");
		mockUserDelay();
		mockExplorerSelection("A.java");
		mockUserDelay();
		mockTypesSelection("A.java");

		ContextUiPlugin.getDefault().getPreferenceStore().setValue(prefId, false);

		mockExplorerSelection("A.java");

		logger.stopMonitoring();
		report.getStatisticsFromInteractionHistory(logger.getOutputFile(), new JobChangeAdapter() {

			public void done() {
				int normal = viewCollector.getNormalViewSelections().get(JavaUI.ID_PACKAGES);
				assertEquals(5, normal);

				int filtered = viewCollector.getFilteredViewSelections().get(JavaUI.ID_PACKAGES);
				assertEquals(2, filtered);

				UiUsageMonitorPlugin.getDefault().removeMonitoredPreferences(
						ContextUiPlugin.getDefault().getPreferenceStore());
			}
		});

	}

	/**
	 * Delay enough to make replicated events different
	 */
	private void mockUserDelay() {
		try {
			// XXX: this could be sensitive to CPU speeds
			Thread.sleep(100);
		} catch (InterruptedException ie) {
			fail();
		}
	}
}

Back to the top