1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
19 import java
.io
.IOException
;
20 import java
.util
.Arrays
;
22 import org
.apache
.log4j
.ConsoleAppender
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.apache
.log4j
.SimpleLayout
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.custom
.SashForm
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.widgets
.Sash
;
29 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
30 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
31 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
32 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
33 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
34 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.wizards
.NewTmfProjectWizard
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotSash
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.callstack
.CallStackView
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.histogram
.HistogramView
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timechart
.TimeChartView
;
42 import org
.eclipse
.ui
.IFolderLayout
;
43 import org
.eclipse
.ui
.IPageLayout
;
44 import org
.eclipse
.ui
.IPerspectiveFactory
;
45 import org
.hamcrest
.BaseMatcher
;
46 import org
.hamcrest
.Description
;
47 import org
.junit
.After
;
48 import org
.junit
.AfterClass
;
49 import org
.junit
.Before
;
50 import org
.junit
.BeforeClass
;
51 import org
.junit
.Test
;
52 import org
.junit
.runner
.RunWith
;
55 * Test common time axis for views
57 * @author Matthew Khouzam
59 @RunWith(SWTBotJunit4ClassRunner
.class)
60 public class TmfAlignTimeAxisTest
{
63 * wait for throttler (2x the throttler time)
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>";
72 private static final String PROJET_NAME
= "TestAxisAlignment";
73 private static final int NUM_EVENTS
= 100;
75 /** The Log4j logger instance. */
76 private static final Logger fLogger
= Logger
.getRootLogger();
77 private static SWTWorkbenchBot fBot
;
79 private static String
makeEvent(int ts
, int val
) {
80 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
83 private static File fLocation
;
84 private static final BaseMatcher
<Sash
> SASH_MATCHER
= new SashMatcher();
87 * Initialization, creates a temp trace
93 public static void init() throws IOException
{
94 SWTBotUtils
.failIfUIThread();
95 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
96 /* set up for swtbot */
97 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
98 fLogger
.removeAllAppenders();
99 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
100 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
102 SWTBotUtils
.closeView("welcome", bot
);
104 SWTBotUtils
.switchToTracingPerspective();
105 /* finish waiting for eclipse to load */
106 SWTBotUtils
.waitForJobs();
107 fLocation
= File
.createTempFile("sample", ".xml");
108 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fLocation
, "rw")) {
109 braf
.writeBytes(TRACE_START
);
110 for (int i
= 0; i
< NUM_EVENTS
; i
++) {
111 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
113 braf
.writeBytes(TRACE_END
);
115 SWTBotUtils
.createProject(PROJET_NAME
);
116 SWTBotUtils
.selectTracesFolder(bot
, PROJET_NAME
);
123 public static void cleanup() {
124 SWTBotUtils
.deleteProject(PROJET_NAME
, new SWTWorkbenchBot());
126 fLogger
.removeAllAppenders();
133 public void before() {
134 SWTBotUtils
.openTrace(PROJET_NAME
, fLocation
.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
141 public void after() {
142 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
143 SWTBotUtils
.activeEventsEditor(bot
).close();
147 * Test 3 views, none overlap, the histogram, the callstack and the
148 * timechart are aligned, the histogram is moved, callstack and timechart
152 public void testMoveHistogramCallstackFollows() {
153 fBot
= new SWTWorkbenchBot();
154 SWTBotUtils
.switchToPerspective(AlignPerspectiveFactory1
.ID
);
155 testOverlap(HistogramView
.ID
, CallStackView
.ID
);
159 * Test 3 views, none overlap, the histogram, the callstack and the
160 * timechart are aligned, the callstack is moved, histogram and timechart
164 public void testMoveCallstackHistogramFollows() {
165 fBot
= new SWTWorkbenchBot();
166 SWTBotUtils
.switchToPerspective(AlignPerspectiveFactory1
.ID
);
167 testOverlap(CallStackView
.ID
, HistogramView
.ID
);
171 * Test 3 views, overlap on the resizing view. The timechart and callstack
172 * are overlapping. Test that when the histogram moves, the timechart
173 * follows. Hidden views are not tested as their state does not matter until
177 public void testOverlappingHistogramMove() {
178 fBot
= new SWTWorkbenchBot();
179 SWTBotUtils
.switchToPerspective(AlignPerspectiveFactory2
.ID
);
180 testOverlap(HistogramView
.ID
, CallStackView
.ID
);
184 * Test 3 views, overlap on the resizing view. The timechart and callstack
185 * are overlapping. Test that when the timechart moves, the histogram
186 * follows. Hidden views are not tested as their state does not matter until
190 public void testOverlappingTimechartMove() {
191 fBot
= new SWTWorkbenchBot();
192 SWTBotUtils
.switchToPerspective(AlignPerspectiveFactory2
.ID
);
193 testOverlap(CallStackView
.ID
, HistogramView
.ID
);
197 * Test 3 views. No overlap. The callstack is not aligned with the
198 * histogram, the histogram is moved, we check that the callstack does NOT
202 public void testNotOverlappingHistogramMove() {
203 fBot
= new SWTWorkbenchBot();
204 testNonOverlap(HistogramView
.ID
, CallStackView
.ID
);
208 * Test 3 views. No overlap. The callstack is not aligned with the
209 * histogram, the callstack is moved, we check that the histogram does NOT
213 public void testNotOverlappingCallstackMove() {
214 fBot
= new SWTWorkbenchBot();
215 testNonOverlap(CallStackView
.ID
, HistogramView
.ID
);
218 private static void testNonOverlap(String vId1
, String vId2
) {
219 final int offset
= 100;
220 // switch to the proper perspective and wait for views to align
221 SWTBotUtils
.switchToPerspective(AlignPerspectiveFactory3
.ID
);
222 SWTBotUtils
.waitForJobs();
223 SWTBotUtils
.delay(SYNC_DELAY
);
225 SWTBotView masterView
= fBot
.viewById(vId1
);
226 SWTBotView slaveView
= fBot
.viewById(vId2
);
227 final Sash slaveSash
= slaveView
.bot().widget(SASH_MATCHER
, 0);
228 SWTBotSash slaveSashBot
= new SWTBotSash(slaveSash
, null);
229 Point before
= slaveSashBot
.getPoint();
230 // move master and wait for slaves to follow
231 drag(masterView
, offset
);
232 SWTBotUtils
.waitForJobs();
233 SWTBotUtils
.delay(SYNC_DELAY
);
234 // verify that the slave did not follow
235 assertEquals(before
, slaveSashBot
.getPoint());
236 // put everything back the way it was
237 drag(masterView
, -offset
);
238 SWTBotUtils
.delay(SYNC_DELAY
);
241 private static final class SashMatcher
extends BaseMatcher
<Sash
> {
243 public boolean matches(Object item
) {
244 return (item
instanceof Sash
);
248 public void describeTo(Description description
) {
252 private static final class SashFormMatcher
extends BaseMatcher
<SashForm
> {
254 public boolean matches(Object item
) {
255 return (item
instanceof SashForm
);
259 public void describeTo(Description description
) {
264 * Simulate a drag operation using "setWeights"
266 private static void drag(final SWTBotView view
, final int offset
) {
267 // this is the final sash form
268 final SashForm sashForm
= view
.bot().widget(new SashFormMatcher(), 0);
269 assertNotNull(sashForm
);
270 // resize widgets using sashform
271 UIThreadRunnable
.syncExec(new VoidResult() {
274 int[] originalWeights
= sashForm
.getWeights();
275 int[] newWeights
= Arrays
.copyOf(originalWeights
, originalWeights
.length
);
276 newWeights
[0] += offset
;
277 newWeights
[1] -= offset
;
278 sashForm
.setWeights(newWeights
);
279 sashForm
.getParent().layout();
282 // send update signals
283 UIThreadRunnable
.syncExec(new VoidResult() {
286 sashForm
.getChildren()[0].notifyListeners(SWT
.Resize
, null);
287 sashForm
.getChildren()[1].notifyListeners(SWT
.Resize
, null);
289 * This one is the most important, the previous two are added to
290 * be a good citizen, this event (selection) is the one that
291 * triggers an alignment
293 sashForm
.getChildren()[2].notifyListeners(SWT
.Selection
, null);
298 private static void testOverlap(String masterView
, String slaveView
) {
299 final int offset
= 100;
300 final int delta
= offset
/ 2;
301 // wait for the perspective switch to propagate alignments
302 SWTBotUtils
.waitForJobs();
303 SWTBotUtils
.delay(SYNC_DELAY
);
304 // select master and slave parts to observe
305 SWTBotView masterViewBot
= fBot
.viewById(masterView
);
306 final Sash masterSash
= masterViewBot
.bot().widget(SASH_MATCHER
, 0);
307 SWTBotSash masterSashBot
= new SWTBotSash(masterSash
, null);
309 SWTBotView slaveViewBot
= fBot
.viewById(slaveView
);
310 final Sash slaveSash
= slaveViewBot
.bot().widget(SASH_MATCHER
, 0);
311 SWTBotSash slaveSashBot
= new SWTBotSash(slaveSash
, null);
313 double masterOriginalSashX
= masterSashBot
.getPoint().x
;
314 // check that the views are already aligned
315 assertEquals("Approx align", masterOriginalSashX
, slaveSashBot
.getPoint().x
, delta
);
316 // move sash and wait for alignment
317 drag(masterViewBot
, offset
);
318 SWTBotUtils
.waitForJobs();
319 SWTBotUtils
.delay(SYNC_DELAY
);
321 double masterNewSashX
= masterSashBot
.getPoint().x
;
322 assertEquals("Approx align", masterNewSashX
, slaveSashBot
.getPoint().x
, delta
);
323 assertEquals(masterOriginalSashX
, masterNewSashX
- offset
, delta
);
324 // put things back the way they were
325 drag(masterViewBot
, -offset
);
326 SWTBotUtils
.delay(SYNC_DELAY
);
330 * Histogram, Callstack and timechart aligned but in different sites
332 public static class AlignPerspectiveFactory1
implements IPerspectiveFactory
{
334 /** The Perspective ID */
335 public static final String ID
= "org.eclipse.linuxtools.tmf.test.align.1"; //$NON-NLS-1$
338 public void createInitialLayout(IPageLayout layout
) {
339 if (layout
== null) {
344 layout
.setEditorAreaVisible(true);
347 layout
.setEditorAreaVisible(true);
349 // Create the top left folder
350 IFolderLayout topLeftFolder
= layout
.createFolder("topLeftFolder", IPageLayout
.LEFT
, 0.15f
, IPageLayout
.ID_EDITOR_AREA
); //$NON-NLS-1$
351 topLeftFolder
.addView(IPageLayout
.ID_PROJECT_EXPLORER
);
353 // Create the top right folder
354 IFolderLayout topRightFolder
= layout
.createFolder("topRightFolder", IPageLayout
.BOTTOM
, 0.30f
, IPageLayout
.ID_EDITOR_AREA
); //$NON-NLS-1$
355 topRightFolder
.addView(HistogramView
.ID
);
357 // Create the middle right folder
358 IFolderLayout middleRightFolder
= layout
.createFolder("middleRightFolder", IPageLayout
.BOTTOM
, 0.50f
, "topRightFolder"); //$NON-NLS-1$
359 middleRightFolder
.addView(CallStackView
.ID
);
361 // Create the bottom right folder
362 IFolderLayout bottomRightFolder
= layout
.createFolder("bottomRightFolder", IPageLayout
.BOTTOM
, 0.65f
, "middleRightFolder"); //$NON-NLS-1$ //$NON-NLS-2$
363 bottomRightFolder
.addView(TimeChartView
.ID
);
365 // Populate menus, etc
366 layout
.addPerspectiveShortcut(ID
);
367 layout
.addNewWizardShortcut(NewTmfProjectWizard
.ID
);
373 * Timechart and Histogram share a site, all views aligned
375 public static class AlignPerspectiveFactory2
implements IPerspectiveFactory
{
377 /** The Perspective ID */
378 public static final String ID
= "org.eclipse.linuxtools.tmf.test.align.2"; //$NON-NLS-1$
381 public void createInitialLayout(IPageLayout layout
) {
382 if (layout
== null) {
387 layout
.setEditorAreaVisible(true);
390 layout
.setEditorAreaVisible(true);
392 // Create the top left folder
393 IFolderLayout topLeftFolder
= layout
.createFolder("topLeftFolder", IPageLayout
.LEFT
, 0.15f
, IPageLayout
.ID_EDITOR_AREA
); //$NON-NLS-1$
394 topLeftFolder
.addView(IPageLayout
.ID_PROJECT_EXPLORER
);
396 // Create the middle right folder
397 IFolderLayout middleRightFolder
= layout
.createFolder("middleRightFolder", IPageLayout
.BOTTOM
, 0.40f
, IPageLayout
.ID_EDITOR_AREA
); //$NON-NLS-1$
398 middleRightFolder
.addView(HistogramView
.ID
);
399 middleRightFolder
.addView(TimeChartView
.ID
);
401 // Create the bottom right folder
402 IFolderLayout bottomRightFolder
= layout
.createFolder("bottomRightFolder", IPageLayout
.BOTTOM
, 0.65f
, "middleRightFolder"); //$NON-NLS-1$ //$NON-NLS-2$
403 bottomRightFolder
.addView(CallStackView
.ID
);
405 // Populate menus, etc
406 layout
.addPerspectiveShortcut(ID
);
407 layout
.addNewWizardShortcut(NewTmfProjectWizard
.ID
);
413 * Histogram and timechart aligned, callstack not aligned
415 public static class AlignPerspectiveFactory3
implements IPerspectiveFactory
{
417 /** The Perspective ID */
418 public static final String ID
= "org.eclipse.linuxtools.tmf.test.align.3"; //$NON-NLS-1$
421 public void createInitialLayout(IPageLayout layout
) {
422 if (layout
== null) {
427 layout
.setEditorAreaVisible(true);
430 layout
.setEditorAreaVisible(true);
432 // Create the top left folder
433 IFolderLayout topLeftFolder
= layout
.createFolder("topLeftFolder", IPageLayout
.LEFT
, 0.15f
, IPageLayout
.ID_EDITOR_AREA
); //$NON-NLS-1$
434 topLeftFolder
.addView(IPageLayout
.ID_PROJECT_EXPLORER
);
436 IFolderLayout bottomLeftFolder
= layout
.createFolder("bottomLeftFolder", IPageLayout
.BOTTOM
, 0.5f
, "topLeftFolder"); //$NON-NLS-1$
437 bottomLeftFolder
.addView(CallStackView
.ID
);
439 // Create the middle right folder
440 IFolderLayout middleRightFolder
= layout
.createFolder("middleRightFolder", IPageLayout
.BOTTOM
, 0.40f
, IPageLayout
.ID_EDITOR_AREA
); //$NON-NLS-1$
441 middleRightFolder
.addView(HistogramView
.ID
);
443 // Create the bottom right folder
444 IFolderLayout bottomRightFolder
= layout
.createFolder("bottomRightFolder", IPageLayout
.BOTTOM
, 0.65f
, "middleRightFolder"); //$NON-NLS-1$ //$NON-NLS-2$
445 bottomRightFolder
.addView(TimeChartView
.ID
);
447 // Populate menus, etc
448 layout
.addPerspectiveShortcut(ID
);
449 layout
.addNewWizardShortcut(NewTmfProjectWizard
.ID
);