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 * Patrick Tasse - Fix editor handling
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assume
.assumeTrue
;
20 import java
.io
.IOException
;
21 import java
.util
.regex
.Matcher
;
22 import java
.util
.regex
.Pattern
;
24 import org
.apache
.log4j
.ConsoleAppender
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.apache
.log4j
.SimpleLayout
;
27 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
28 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
29 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
30 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
31 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
32 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
33 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
34 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
35 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
39 import org
.junit
.After
;
40 import org
.junit
.Before
;
41 import org
.junit
.Test
;
42 import org
.junit
.runner
.RunWith
;
45 * Test trace offsetting
47 * @author Matthew Khouzam
49 @RunWith(SWTBotJunit4ClassRunner
.class)
50 public class TestTraceOffsetting
{
52 private static final String TRACE_START
= "<trace>";
53 private static final String EVENT_BEGIN
= "<event timestamp=\"";
54 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
55 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
56 private static final String TRACE_END
= "</trace>";
58 private static final String PROJET_NAME
= "TestForOffsetting";
59 private static final int NUM_EVENTS
= 100;
61 /** The Log4j logger instance. */
62 private static final Logger fLogger
= Logger
.getRootLogger();
63 private static SWTWorkbenchBot fBot
;
65 private static String
makeEvent(int ts
, int val
) {
66 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
69 private File fLocation
;
72 * Initialization, creates a temp trace
78 public void init() throws IOException
{
79 SWTBotUtils
.initialize();
80 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
81 /* set up for swtbot */
82 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
83 fLogger
.removeAllAppenders();
84 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
85 fBot
= new SWTWorkbenchBot();
87 SWTBotUtils
.closeView("welcome", fBot
);
89 SWTBotUtils
.switchToTracingPerspective();
90 /* finish waiting for eclipse to load */
91 SWTBotUtils
.waitForJobs();
92 fLocation
= File
.createTempFile("sample", ".xml");
93 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fLocation
, "rw")) {
94 braf
.writeBytes(TRACE_START
);
95 for (int i
= 0; i
< NUM_EVENTS
; i
++) {
96 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
98 braf
.writeBytes(TRACE_END
);
106 public void cleanup() {
108 fLogger
.removeAllAppenders();
112 * Test offsetting by 99 ns
115 public void testOffsetting() {
116 // Skip this test on Mac OS X 10.11.1 because of bug 481611
117 // FIXME: Remove this work around once bug 481611 is fixed
118 MacOsVersion macOsVersion
= MacOsVersion
.getMacOsVersion();
119 boolean macBugPresent
= macOsVersion
!= null && macOsVersion
.compareTo(new MacOsVersion(10, 11, 1)) >= 0;
120 assumeTrue(!macBugPresent
);
122 SWTBotUtils
.createProject(PROJET_NAME
);
123 SWTBotTreeItem traceFolderItem
= SWTBotUtils
.selectTracesFolder(fBot
, PROJET_NAME
);
124 SWTBotUtils
.openTrace(PROJET_NAME
, fLocation
.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
125 SWTBotEditor editor
= fBot
.editorByTitle(fLocation
.getName());
126 SWTBotTable eventsTableBot
= editor
.bot().table();
127 String timestamp
= eventsTableBot
.cell(1, 1);
128 assertEquals("19:00:00.000 000 000", timestamp
);
129 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, traceFolderItem
, fLocation
.getName());
131 traceItem
.contextMenu("Apply Time Offset...").click();
132 SWTBotUtils
.waitForJobs();
133 // set offset to 99 ns
134 SWTBotShell shell
= fBot
.shell("Apply time offset");
136 SWTBotTreeItem
[] allItems
= fBot
.tree().getAllItems();
137 final SWTBotTreeItem swtBotTreeItem
= allItems
[0];
138 swtBotTreeItem
.select();
139 swtBotTreeItem
.click(1);
140 swtBotTreeItem
.pressShortcut(KeyStroke
.getInstance('9'));
141 swtBotTreeItem
.pressShortcut(KeyStroke
.getInstance('9'));
142 swtBotTreeItem
.pressShortcut(KeyStroke
.getInstance('\n'));
143 SWTBotUtils
.waitForJobs();
144 fBot
.button("OK").click();
146 // wait for trace to close
147 fBot
.waitWhile(ConditionHelpers
.isEditorOpened(fBot
, fLocation
.getName()));
150 SWTBotUtils
.openTrace(PROJET_NAME
, fLocation
.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
151 editor
= fBot
.editorByTitle(fLocation
.getName());
152 eventsTableBot
= editor
.bot().table();
153 timestamp
= eventsTableBot
.cell(1, 1);
154 assertEquals("19:00:00.000 000 099", timestamp
);
155 SWTBotUtils
.deleteProject(PROJET_NAME
, fBot
);
159 * Class to store the Mac OS version.
161 * This could be moved if other tests start using this.
163 private static class MacOsVersion
implements Comparable
<MacOsVersion
> {
164 private static final Pattern MAC_OS_VERSION_PATTERN
= Pattern
.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
166 private int fMajorVersion
;
167 private int fMinorVersion
;
168 private int fBugFixVersion
;
170 private static MacOsVersion fRunningMacOsVersion
;
172 private MacOsVersion(int version
, int majorVersion
, int patchVersion
) {
173 this.fMajorVersion
= version
;
174 this.fMinorVersion
= majorVersion
;
175 this.fBugFixVersion
= patchVersion
;
178 private int getMajorVersion() {
179 return fMajorVersion
;
182 private int getMinorVersion() {
183 return fMinorVersion
;
186 private int getBugFixVersion() {
187 return fBugFixVersion
;
190 private static MacOsVersion
getMacOsVersion() {
191 if (fRunningMacOsVersion
!= null) {
192 return fRunningMacOsVersion
;
195 if (!SWTUtils
.isMac()) {
199 String osVersion
= System
.getProperty("os.version");
200 if (osVersion
== null) {
203 Matcher matcher
= MAC_OS_VERSION_PATTERN
.matcher(osVersion
);
204 if (matcher
.matches()) {
206 fRunningMacOsVersion
= new MacOsVersion(Integer
.parseInt(matcher
.group(1)), Integer
.parseInt(matcher
.group(2)), Integer
.parseInt(matcher
.group(3)));
207 return fRunningMacOsVersion
;
208 } catch (NumberFormatException e
) {
216 public int compareTo(MacOsVersion o
) {
217 int compareTo
= Integer
.compare(fMajorVersion
, o
.getMajorVersion());
218 if (compareTo
!= 0) {
222 compareTo
= Integer
.compare(fMinorVersion
, o
.getMinorVersion());
223 if (compareTo
!= 0) {
227 return Integer
.compare(fBugFixVersion
, o
.getBugFixVersion());