Clean up top-level pom.xml
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / TmfAlignTimeAxisTest.java
CommitLineData
6ae4e885
MK
1/*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http:/www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNotNull;
17
18import java.io.File;
19import java.io.IOException;
20import java.util.Arrays;
21
22import org.apache.log4j.ConsoleAppender;
23import org.apache.log4j.Logger;
24import org.apache.log4j.SimpleLayout;
25import org.eclipse.swt.SWT;
26import org.eclipse.swt.custom.SashForm;
27import org.eclipse.swt.graphics.Point;
28import org.eclipse.swt.widgets.Sash;
29import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
30import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
31import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
32import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
33import org.eclipse.swtbot.swt.finder.results.VoidResult;
34import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
35import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
36import org.eclipse.tracecompass.tmf.ui.project.wizards.NewTmfProjectWizard;
37import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotSash;
38import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
39import org.eclipse.tracecompass.tmf.ui.views.callstack.CallStackView;
40import org.eclipse.tracecompass.tmf.ui.views.histogram.HistogramView;
41import org.eclipse.tracecompass.tmf.ui.views.timechart.TimeChartView;
42import org.eclipse.ui.IFolderLayout;
43import org.eclipse.ui.IPageLayout;
44import org.eclipse.ui.IPerspectiveFactory;
45import org.hamcrest.BaseMatcher;
46import org.hamcrest.Description;
47import org.junit.After;
48import org.junit.AfterClass;
49import org.junit.Before;
50import org.junit.BeforeClass;
51import org.junit.Test;
52import org.junit.runner.RunWith;
53
54/**
55 * Test common time axis for views
56 *
57 * @author Matthew Khouzam
58 */
59@RunWith(SWTBotJunit4ClassRunner.class)
60public class TmfAlignTimeAxisTest {
61
62 /**
63 * wait for throttler (2x the throttler time)
64 */
65 private static final int SYNC_DELAY = 1000;
66 private static final String TRACE_START = "<trace>";
67 private static final String EVENT_BEGIN = "<event timestamp=\"";
68 private static final String EVENT_MIDDLE = " \" name=\"event\"><field name=\"field\" value=\"";
69 private static final String EVENT_END = "\" type=\"int\" />" + "</event>";
70 private static final String TRACE_END = "</trace>";
71
72 private static final String PROJET_NAME = "TestAxisAlignment";
73 private static final int NUM_EVENTS = 100;
df78b72e
MAL
74 /**
75 * Using a small ratio for the editor so that other views have enough space
76 * to be drawn, even when a low screen resolution is used.
77 */
78 private static final float EDITOR_AREA_RATIO = 0.10f;
6ae4e885
MK
79
80 /** The Log4j logger instance. */
81 private static final Logger fLogger = Logger.getRootLogger();
82 private static SWTWorkbenchBot fBot;
83
84 private static String makeEvent(int ts, int val) {
85 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
86 }
87
88 private static File fLocation;
89 private static final BaseMatcher<Sash> SASH_MATCHER = new SashMatcher();
90
91 /**
92 * Initialization, creates a temp trace
93 *
94 * @throws IOException
95 * should not happen
96 */
97 @BeforeClass
98 public static void init() throws IOException {
5785ab49 99 SWTBotUtils.initialize();
6ae4e885
MK
100 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
101 /* set up for swtbot */
102 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
103 fLogger.removeAllAppenders();
104 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
105 SWTWorkbenchBot bot = new SWTWorkbenchBot();
106
107 SWTBotUtils.closeView("welcome", bot);
108
109 SWTBotUtils.switchToTracingPerspective();
110 /* finish waiting for eclipse to load */
111 SWTBotUtils.waitForJobs();
112 fLocation = File.createTempFile("sample", ".xml");
113 try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fLocation, "rw")) {
114 braf.writeBytes(TRACE_START);
115 for (int i = 0; i < NUM_EVENTS; i++) {
116 braf.writeBytes(makeEvent(i * 100, i % 4));
117 }
118 braf.writeBytes(TRACE_END);
119 }
120 SWTBotUtils.createProject(PROJET_NAME);
121 SWTBotUtils.selectTracesFolder(bot, PROJET_NAME);
122 }
123
124 /**
125 * Delete file
126 */
127 @AfterClass
128 public static void cleanup() {
129 SWTBotUtils.deleteProject(PROJET_NAME, new SWTWorkbenchBot());
130 fLocation.delete();
131 fLogger.removeAllAppenders();
132 }
133
134 /**
135 * Open the trace
136 */
137 @Before
138 public void before() {
139 SWTBotUtils.openTrace(PROJET_NAME, fLocation.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
140 }
141
142 /**
143 * Close the trace
144 */
145 @After
146 public void after() {
147 SWTWorkbenchBot bot = new SWTWorkbenchBot();
a345ad25 148 SWTBotUtils.activeEventsEditor(bot).close();
6ae4e885
MK
149 }
150
151 /**
152 * Test 3 views, none overlap, the histogram, the callstack and the
153 * timechart are aligned, the histogram is moved, callstack and timechart
154 * follow
155 */
156 @Test
157 public void testMoveHistogramCallstackFollows() {
158 fBot = new SWTWorkbenchBot();
159 SWTBotUtils.switchToPerspective(AlignPerspectiveFactory1.ID);
160 testOverlap(HistogramView.ID, CallStackView.ID);
161 }
162
163 /**
164 * Test 3 views, none overlap, the histogram, the callstack and the
165 * timechart are aligned, the callstack is moved, histogram and timechart
166 * follow
167 */
168 @Test
169 public void testMoveCallstackHistogramFollows() {
170 fBot = new SWTWorkbenchBot();
171 SWTBotUtils.switchToPerspective(AlignPerspectiveFactory1.ID);
172 testOverlap(CallStackView.ID, HistogramView.ID);
173 }
174
175 /**
176 * Test 3 views, overlap on the resizing view. The timechart and callstack
177 * are overlapping. Test that when the histogram moves, the timechart
178 * follows. Hidden views are not tested as their state does not matter until
179 * they are displayed
180 */
181 @Test
182 public void testOverlappingHistogramMove() {
183 fBot = new SWTWorkbenchBot();
184 SWTBotUtils.switchToPerspective(AlignPerspectiveFactory2.ID);
185 testOverlap(HistogramView.ID, CallStackView.ID);
186 }
187
188 /**
189 * Test 3 views, overlap on the resizing view. The timechart and callstack
190 * are overlapping. Test that when the timechart moves, the histogram
191 * follows. Hidden views are not tested as their state does not matter until
192 * they are displayed
193 */
194 @Test
195 public void testOverlappingTimechartMove() {
196 fBot = new SWTWorkbenchBot();
197 SWTBotUtils.switchToPerspective(AlignPerspectiveFactory2.ID);
198 testOverlap(CallStackView.ID, HistogramView.ID);
199 }
200
201 /**
202 * Test 3 views. No overlap. The callstack is not aligned with the
203 * histogram, the histogram is moved, we check that the callstack does NOT
204 * follow
205 */
206 @Test
207 public void testNotOverlappingHistogramMove() {
208 fBot = new SWTWorkbenchBot();
209 testNonOverlap(HistogramView.ID, CallStackView.ID);
210 }
211
212 /**
213 * Test 3 views. No overlap. The callstack is not aligned with the
214 * histogram, the callstack is moved, we check that the histogram does NOT
215 * follow
216 */
217 @Test
218 public void testNotOverlappingCallstackMove() {
219 fBot = new SWTWorkbenchBot();
220 testNonOverlap(CallStackView.ID, HistogramView.ID);
221 }
222
223 private static void testNonOverlap(String vId1, String vId2) {
224 final int offset = 100;
225 // switch to the proper perspective and wait for views to align
226 SWTBotUtils.switchToPerspective(AlignPerspectiveFactory3.ID);
227 SWTBotUtils.waitForJobs();
228 SWTBotUtils.delay(SYNC_DELAY);
229 // get views
230 SWTBotView masterView = fBot.viewById(vId1);
231 SWTBotView slaveView = fBot.viewById(vId2);
232 final Sash slaveSash = slaveView.bot().widget(SASH_MATCHER, 0);
233 SWTBotSash slaveSashBot = new SWTBotSash(slaveSash, null);
234 Point before = slaveSashBot.getPoint();
235 // move master and wait for slaves to follow
236 drag(masterView, offset);
237 SWTBotUtils.waitForJobs();
238 SWTBotUtils.delay(SYNC_DELAY);
239 // verify that the slave did not follow
240 assertEquals(before, slaveSashBot.getPoint());
241 // put everything back the way it was
242 drag(masterView, -offset);
243 SWTBotUtils.delay(SYNC_DELAY);
244 }
245
246 private static final class SashMatcher extends BaseMatcher<Sash> {
247 @Override
248 public boolean matches(Object item) {
249 return (item instanceof Sash);
250 }
251
252 @Override
253 public void describeTo(Description description) {
254 }
255 }
256
257 private static final class SashFormMatcher extends BaseMatcher<SashForm> {
258 @Override
259 public boolean matches(Object item) {
260 return (item instanceof SashForm);
261 }
262
263 @Override
264 public void describeTo(Description description) {
265 }
266 }
267
268 /**
269 * Simulate a drag operation using "setWeights"
270 */
271 private static void drag(final SWTBotView view, final int offset) {
272 // this is the final sash form
273 final SashForm sashForm = view.bot().widget(new SashFormMatcher(), 0);
274 assertNotNull(sashForm);
275 // resize widgets using sashform
276 UIThreadRunnable.syncExec(new VoidResult() {
277 @Override
278 public void run() {
279 int[] originalWeights = sashForm.getWeights();
280 int[] newWeights = Arrays.copyOf(originalWeights, originalWeights.length);
281 newWeights[0] += offset;
282 newWeights[1] -= offset;
283 sashForm.setWeights(newWeights);
284 sashForm.getParent().layout();
285 }
286 });
287 // send update signals
288 UIThreadRunnable.syncExec(new VoidResult() {
289 @Override
290 public void run() {
291 sashForm.getChildren()[0].notifyListeners(SWT.Resize, null);
292 sashForm.getChildren()[1].notifyListeners(SWT.Resize, null);
293 /*
294 * This one is the most important, the previous two are added to
295 * be a good citizen, this event (selection) is the one that
296 * triggers an alignment
297 */
298 sashForm.getChildren()[2].notifyListeners(SWT.Selection, null);
299 }
300 });
301 }
302
303 private static void testOverlap(String masterView, String slaveView) {
304 final int offset = 100;
305 final int delta = offset / 2;
306 // wait for the perspective switch to propagate alignments
307 SWTBotUtils.waitForJobs();
308 SWTBotUtils.delay(SYNC_DELAY);
309 // select master and slave parts to observe
310 SWTBotView masterViewBot = fBot.viewById(masterView);
311 final Sash masterSash = masterViewBot.bot().widget(SASH_MATCHER, 0);
312 SWTBotSash masterSashBot = new SWTBotSash(masterSash, null);
313
314 SWTBotView slaveViewBot = fBot.viewById(slaveView);
315 final Sash slaveSash = slaveViewBot.bot().widget(SASH_MATCHER, 0);
316 SWTBotSash slaveSashBot = new SWTBotSash(slaveSash, null);
317
318 double masterOriginalSashX = masterSashBot.getPoint().x;
319 // check that the views are already aligned
320 assertEquals("Approx align", masterOriginalSashX, slaveSashBot.getPoint().x, delta);
321 // move sash and wait for alignment
322 drag(masterViewBot, offset);
323 SWTBotUtils.waitForJobs();
324 SWTBotUtils.delay(SYNC_DELAY);
325 // check results
326 double masterNewSashX = masterSashBot.getPoint().x;
327 assertEquals("Approx align", masterNewSashX, slaveSashBot.getPoint().x, delta);
328 assertEquals(masterOriginalSashX, masterNewSashX - offset, delta);
329 // put things back the way they were
330 drag(masterViewBot, -offset);
331 SWTBotUtils.delay(SYNC_DELAY);
332 }
333
334 /**
335 * Histogram, Callstack and timechart aligned but in different sites
336 */
337 public static class AlignPerspectiveFactory1 implements IPerspectiveFactory {
338
339 /** The Perspective ID */
340 public static final String ID = "org.eclipse.linuxtools.tmf.test.align.1"; //$NON-NLS-1$
341
342 @Override
343 public void createInitialLayout(IPageLayout layout) {
344 if (layout == null) {
345 return;
346 }
347
348 // Editor area
349 layout.setEditorAreaVisible(true);
350
351 // Editor area
352 layout.setEditorAreaVisible(true);
353
354 // Create the top left folder
355 IFolderLayout topLeftFolder = layout.createFolder("topLeftFolder", IPageLayout.LEFT, 0.15f, IPageLayout.ID_EDITOR_AREA); //$NON-NLS-1$
356 topLeftFolder.addView(IPageLayout.ID_PROJECT_EXPLORER);
357
358 // Create the top right folder
df78b72e 359 IFolderLayout topRightFolder = layout.createFolder("topRightFolder", IPageLayout.BOTTOM, EDITOR_AREA_RATIO, IPageLayout.ID_EDITOR_AREA); //$NON-NLS-1$
6ae4e885
MK
360 topRightFolder.addView(HistogramView.ID);
361
362 // Create the middle right folder
363 IFolderLayout middleRightFolder = layout.createFolder("middleRightFolder", IPageLayout.BOTTOM, 0.50f, "topRightFolder"); //$NON-NLS-1$
364 middleRightFolder.addView(CallStackView.ID);
365
366 // Create the bottom right folder
367 IFolderLayout bottomRightFolder = layout.createFolder("bottomRightFolder", IPageLayout.BOTTOM, 0.65f, "middleRightFolder"); //$NON-NLS-1$ //$NON-NLS-2$
368 bottomRightFolder.addView(TimeChartView.ID);
369
370 // Populate menus, etc
371 layout.addPerspectiveShortcut(ID);
372 layout.addNewWizardShortcut(NewTmfProjectWizard.ID);
373 }
374
375 }
376
377 /**
378 * Timechart and Histogram share a site, all views aligned
379 */
380 public static class AlignPerspectiveFactory2 implements IPerspectiveFactory {
381
382 /** The Perspective ID */
383 public static final String ID = "org.eclipse.linuxtools.tmf.test.align.2"; //$NON-NLS-1$
384
385 @Override
386 public void createInitialLayout(IPageLayout layout) {
387 if (layout == null) {
388 return;
389 }
390
391 // Editor area
392 layout.setEditorAreaVisible(true);
393
394 // Editor area
395 layout.setEditorAreaVisible(true);
396
397 // Create the top left folder
398 IFolderLayout topLeftFolder = layout.createFolder("topLeftFolder", IPageLayout.LEFT, 0.15f, IPageLayout.ID_EDITOR_AREA); //$NON-NLS-1$
399 topLeftFolder.addView(IPageLayout.ID_PROJECT_EXPLORER);
400
401 // Create the middle right folder
df78b72e 402 IFolderLayout middleRightFolder = layout.createFolder("middleRightFolder", IPageLayout.BOTTOM, EDITOR_AREA_RATIO, IPageLayout.ID_EDITOR_AREA); //$NON-NLS-1$
6ae4e885
MK
403 middleRightFolder.addView(HistogramView.ID);
404 middleRightFolder.addView(TimeChartView.ID);
405
406 // Create the bottom right folder
407 IFolderLayout bottomRightFolder = layout.createFolder("bottomRightFolder", IPageLayout.BOTTOM, 0.65f, "middleRightFolder"); //$NON-NLS-1$ //$NON-NLS-2$
408 bottomRightFolder.addView(CallStackView.ID);
409
410 // Populate menus, etc
411 layout.addPerspectiveShortcut(ID);
412 layout.addNewWizardShortcut(NewTmfProjectWizard.ID);
413 }
414
415 }
416
417 /**
418 * Histogram and timechart aligned, callstack not aligned
419 */
420 public static class AlignPerspectiveFactory3 implements IPerspectiveFactory {
421
422 /** The Perspective ID */
423 public static final String ID = "org.eclipse.linuxtools.tmf.test.align.3"; //$NON-NLS-1$
424
425 @Override
426 public void createInitialLayout(IPageLayout layout) {
427 if (layout == null) {
428 return;
429 }
430
431 // Editor area
432 layout.setEditorAreaVisible(true);
433
434 // Editor area
435 layout.setEditorAreaVisible(true);
436
437 // Create the top left folder
438 IFolderLayout topLeftFolder = layout.createFolder("topLeftFolder", IPageLayout.LEFT, 0.15f, IPageLayout.ID_EDITOR_AREA); //$NON-NLS-1$
439 topLeftFolder.addView(IPageLayout.ID_PROJECT_EXPLORER);
440
441 IFolderLayout bottomLeftFolder = layout.createFolder("bottomLeftFolder", IPageLayout.BOTTOM, 0.5f, "topLeftFolder"); //$NON-NLS-1$
442 bottomLeftFolder.addView(CallStackView.ID);
443
444 // Create the middle right folder
df78b72e 445 IFolderLayout middleRightFolder = layout.createFolder("middleRightFolder", IPageLayout.BOTTOM, EDITOR_AREA_RATIO, IPageLayout.ID_EDITOR_AREA); //$NON-NLS-1$
6ae4e885
MK
446 middleRightFolder.addView(HistogramView.ID);
447
448 // Create the bottom right folder
449 IFolderLayout bottomRightFolder = layout.createFolder("bottomRightFolder", IPageLayout.BOTTOM, 0.65f, "middleRightFolder"); //$NON-NLS-1$ //$NON-NLS-2$
450 bottomRightFolder.addView(TimeChartView.ID);
451
452 // Populate menus, etc
453 layout.addPerspectiveShortcut(ID);
454 layout.addNewWizardShortcut(NewTmfProjectWizard.ID);
455 }
456
457 }
458
459}
This page took 0.064376 seconds and 5 git commands to generate.