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
.core
.runtime
.preferences
.IEclipsePreferences
;
28 import org
.eclipse
.core
.runtime
.preferences
.InstanceScope
;
29 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
30 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
31 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
32 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
33 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
34 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
35 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
36 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
37 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimePreferencesConstants
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimePreferences
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestampFormat
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
46 import org
.junit
.After
;
47 import org
.junit
.Before
;
48 import org
.junit
.Test
;
49 import org
.junit
.runner
.RunWith
;
52 * Test trace offsetting
54 * @author Matthew Khouzam
56 @RunWith(SWTBotJunit4ClassRunner
.class)
57 public class TestTraceOffsetting
{
59 private static final String TRACE_START
= "<trace>";
60 private static final String EVENT_BEGIN
= "<event timestamp=\"";
61 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
62 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
63 private static final String TRACE_END
= "</trace>";
65 private static final String PROJET_NAME
= "TestForOffsetting";
66 private static final int NUM_EVENTS
= 100;
68 /** The Log4j logger instance. */
69 private static final Logger fLogger
= Logger
.getRootLogger();
70 private static SWTWorkbenchBot fBot
;
72 private static String
makeEvent(int ts
, int val
) {
73 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
76 private File fLocation
;
79 * Initialization, creates a temp trace
85 public void init() throws IOException
{
86 SWTBotUtils
.initialize();
87 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
88 /* set up for swtbot */
89 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
90 fLogger
.removeAllAppenders();
91 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
92 fBot
= new SWTWorkbenchBot();
94 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
95 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, "GMT-05:00");
96 TmfTimestampFormat
.updateDefaultFormats();
98 SWTBotUtils
.closeView("welcome", fBot
);
100 SWTBotUtils
.switchToTracingPerspective();
101 /* finish waiting for eclipse to load */
102 WaitUtils
.waitForJobs();
103 fLocation
= File
.createTempFile("sample", ".xml");
104 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fLocation
, "rw")) {
105 braf
.writeBytes(TRACE_START
);
106 for (int i
= 0; i
< NUM_EVENTS
; i
++) {
107 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
109 braf
.writeBytes(TRACE_END
);
117 public void cleanup() {
119 fLogger
.removeAllAppenders();
121 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
122 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, TmfTimePreferences
.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants
.TIME_ZONE
));
123 TmfTimestampFormat
.updateDefaultFormats();
127 * Test offsetting by 99 ns
130 public void testOffsetting() {
131 // Skip this test on Mac OS X 10.11.1 because of bug 481611
132 // FIXME: Remove this work around once bug 481611 is fixed
133 MacOsVersion macOsVersion
= MacOsVersion
.getMacOsVersion();
134 boolean macBugPresent
= macOsVersion
!= null && macOsVersion
.compareTo(new MacOsVersion(10, 11, 1)) >= 0;
135 assumeTrue(!macBugPresent
);
137 SWTBotUtils
.createProject(PROJET_NAME
);
138 SWTBotTreeItem traceFolderItem
= SWTBotUtils
.selectTracesFolder(fBot
, PROJET_NAME
);
139 SWTBotUtils
.openTrace(PROJET_NAME
, fLocation
.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
140 SWTBotEditor editor
= fBot
.editorByTitle(fLocation
.getName());
141 SWTBotTable eventsTableBot
= editor
.bot().table();
142 String timestamp
= eventsTableBot
.cell(1, 1);
143 assertEquals("19:00:00.000 000 000", timestamp
);
144 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, traceFolderItem
, fLocation
.getName());
146 traceItem
.contextMenu("Apply Time Offset...").click();
147 WaitUtils
.waitForJobs();
148 // set offset to 99 ns
149 SWTBotShell shell
= fBot
.shell("Apply time offset");
151 SWTBotTreeItem
[] allItems
= fBot
.tree().getAllItems();
152 final SWTBotTreeItem swtBotTreeItem
= allItems
[0];
153 swtBotTreeItem
.select();
154 swtBotTreeItem
.click(1);
155 swtBotTreeItem
.pressShortcut(KeyStroke
.getInstance('9'));
156 swtBotTreeItem
.pressShortcut(KeyStroke
.getInstance('9'));
157 swtBotTreeItem
.pressShortcut(KeyStroke
.getInstance('\n'));
158 WaitUtils
.waitForJobs();
159 fBot
.button("OK").click();
161 // wait for trace to close
162 fBot
.waitWhile(ConditionHelpers
.isEditorOpened(fBot
, fLocation
.getName()));
165 SWTBotUtils
.openTrace(PROJET_NAME
, fLocation
.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
166 editor
= fBot
.editorByTitle(fLocation
.getName());
167 eventsTableBot
= editor
.bot().table();
168 timestamp
= eventsTableBot
.cell(1, 1);
169 assertEquals("19:00:00.000 000 099", timestamp
);
170 SWTBotUtils
.deleteProject(PROJET_NAME
, fBot
);
174 * Class to store the Mac OS version.
176 * This could be moved if other tests start using this.
178 private static class MacOsVersion
implements Comparable
<MacOsVersion
> {
179 private static final Pattern MAC_OS_VERSION_PATTERN
= Pattern
.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
181 private int fMajorVersion
;
182 private int fMinorVersion
;
183 private int fBugFixVersion
;
185 private static MacOsVersion fRunningMacOsVersion
;
187 private MacOsVersion(int version
, int majorVersion
, int patchVersion
) {
188 this.fMajorVersion
= version
;
189 this.fMinorVersion
= majorVersion
;
190 this.fBugFixVersion
= patchVersion
;
193 private int getMajorVersion() {
194 return fMajorVersion
;
197 private int getMinorVersion() {
198 return fMinorVersion
;
201 private int getBugFixVersion() {
202 return fBugFixVersion
;
205 private static MacOsVersion
getMacOsVersion() {
206 if (fRunningMacOsVersion
!= null) {
207 return fRunningMacOsVersion
;
210 if (!SWTUtils
.isMac()) {
214 String osVersion
= System
.getProperty("os.version");
215 if (osVersion
== null) {
218 Matcher matcher
= MAC_OS_VERSION_PATTERN
.matcher(osVersion
);
219 if (matcher
.matches()) {
221 fRunningMacOsVersion
= new MacOsVersion(Integer
.parseInt(matcher
.group(1)), Integer
.parseInt(matcher
.group(2)), Integer
.parseInt(matcher
.group(3)));
222 return fRunningMacOsVersion
;
223 } catch (NumberFormatException e
) {
231 public int compareTo(MacOsVersion o
) {
232 int compareTo
= Integer
.compare(fMajorVersion
, o
.getMajorVersion());
233 if (compareTo
!= 0) {
237 compareTo
= Integer
.compare(fMinorVersion
, o
.getMinorVersion());
238 if (compareTo
!= 0) {
242 return Integer
.compare(fBugFixVersion
, o
.getBugFixVersion());