fe8640641670eeaad28f5d71935eb2d0578460e7
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / TestTraceOffsetting.java
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 * Patrick Tasse - Fix editor handling
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assume.assumeTrue;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
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.junit.After;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49
50 /**
51 * Test trace offsetting
52 *
53 * @author Matthew Khouzam
54 */
55 @RunWith(SWTBotJunit4ClassRunner.class)
56 public class TestTraceOffsetting {
57
58 private static final String TRACE_START = "<trace>";
59 private static final String EVENT_BEGIN = "<event timestamp=\"";
60 private static final String EVENT_MIDDLE = " \" name=\"event\"><field name=\"field\" value=\"";
61 private static final String EVENT_END = "\" type=\"int\" />" + "</event>";
62 private static final String TRACE_END = "</trace>";
63
64 private static final String PROJET_NAME = "TestForOffsetting";
65 private static final int NUM_EVENTS = 100;
66
67 /** The Log4j logger instance. */
68 private static final Logger fLogger = Logger.getRootLogger();
69 private static SWTWorkbenchBot fBot;
70
71 private static String makeEvent(int ts, int val) {
72 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
73 }
74
75 private File fLocation;
76
77 /**
78 * Initialization, creates a temp trace
79 *
80 * @throws IOException
81 * should not happen
82 */
83 @Before
84 public void init() throws IOException {
85 SWTBotUtils.initialize();
86 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
87 /* set up for swtbot */
88 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
89 fLogger.removeAllAppenders();
90 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
91 fBot = new SWTWorkbenchBot();
92
93 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
94 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, "GMT-05:00");
95 TmfTimestampFormat.updateDefaultFormats();
96
97 SWTBotUtils.closeView("welcome", fBot);
98
99 SWTBotUtils.switchToTracingPerspective();
100 /* finish waiting for eclipse to load */
101 SWTBotUtils.waitForJobs();
102 fLocation = File.createTempFile("sample", ".xml");
103 try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fLocation, "rw")) {
104 braf.writeBytes(TRACE_START);
105 for (int i = 0; i < NUM_EVENTS; i++) {
106 braf.writeBytes(makeEvent(i * 100, i % 4));
107 }
108 braf.writeBytes(TRACE_END);
109 }
110 }
111
112 /**
113 * Delete file
114 */
115 @After
116 public void cleanup() {
117 fLocation.delete();
118 fLogger.removeAllAppenders();
119
120 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
121 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, TmfTimePreferences.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants.TIME_ZONE));
122 TmfTimestampFormat.updateDefaultFormats();
123 }
124
125 /**
126 * Test offsetting by 99 ns
127 */
128 @Test
129 public void testOffsetting() {
130 // Skip this test on Mac OS X 10.11.1 because of bug 481611
131 // FIXME: Remove this work around once bug 481611 is fixed
132 MacOsVersion macOsVersion = MacOsVersion.getMacOsVersion();
133 boolean macBugPresent = macOsVersion != null && macOsVersion.compareTo(new MacOsVersion(10, 11, 1)) >= 0;
134 assumeTrue(!macBugPresent);
135
136 SWTBotUtils.createProject(PROJET_NAME);
137 SWTBotTreeItem traceFolderItem = SWTBotUtils.selectTracesFolder(fBot, PROJET_NAME);
138 SWTBotUtils.openTrace(PROJET_NAME, fLocation.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
139 SWTBotEditor editor = fBot.editorByTitle(fLocation.getName());
140 SWTBotTable eventsTableBot = editor.bot().table();
141 String timestamp = eventsTableBot.cell(1, 1);
142 assertEquals("19:00:00.000 000 000", timestamp);
143 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, traceFolderItem, fLocation.getName());
144 traceItem.select();
145 traceItem.contextMenu("Apply Time Offset...").click();
146 SWTBotUtils.waitForJobs();
147 // set offset to 99 ns
148 SWTBotShell shell = fBot.shell("Apply time offset");
149 shell.setFocus();
150 SWTBotTreeItem[] allItems = fBot.tree().getAllItems();
151 final SWTBotTreeItem swtBotTreeItem = allItems[0];
152 swtBotTreeItem.select();
153 swtBotTreeItem.click(1);
154 swtBotTreeItem.pressShortcut(KeyStroke.getInstance('9'));
155 swtBotTreeItem.pressShortcut(KeyStroke.getInstance('9'));
156 swtBotTreeItem.pressShortcut(KeyStroke.getInstance('\n'));
157 SWTBotUtils.waitForJobs();
158 fBot.button("OK").click();
159
160 // wait for trace to close
161 fBot.waitWhile(ConditionHelpers.isEditorOpened(fBot, fLocation.getName()));
162
163 // re-open trace
164 SWTBotUtils.openTrace(PROJET_NAME, fLocation.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
165 editor = fBot.editorByTitle(fLocation.getName());
166 eventsTableBot = editor.bot().table();
167 timestamp = eventsTableBot.cell(1, 1);
168 assertEquals("19:00:00.000 000 099", timestamp);
169 SWTBotUtils.deleteProject(PROJET_NAME, fBot);
170 }
171
172 /**
173 * Class to store the Mac OS version.
174 *
175 * This could be moved if other tests start using this.
176 */
177 private static class MacOsVersion implements Comparable<MacOsVersion> {
178 private static final Pattern MAC_OS_VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
179
180 private int fMajorVersion;
181 private int fMinorVersion;
182 private int fBugFixVersion;
183
184 private static MacOsVersion fRunningMacOsVersion;
185
186 private MacOsVersion(int version, int majorVersion, int patchVersion) {
187 this.fMajorVersion = version;
188 this.fMinorVersion = majorVersion;
189 this.fBugFixVersion = patchVersion;
190 }
191
192 private int getMajorVersion() {
193 return fMajorVersion;
194 }
195
196 private int getMinorVersion() {
197 return fMinorVersion;
198 }
199
200 private int getBugFixVersion() {
201 return fBugFixVersion;
202 }
203
204 private static MacOsVersion getMacOsVersion() {
205 if (fRunningMacOsVersion != null) {
206 return fRunningMacOsVersion;
207 }
208
209 if (!SWTUtils.isMac()) {
210 return null;
211 }
212
213 String osVersion = System.getProperty("os.version");
214 if (osVersion == null) {
215 return null;
216 }
217 Matcher matcher = MAC_OS_VERSION_PATTERN.matcher(osVersion);
218 if (matcher.matches()) {
219 try {
220 fRunningMacOsVersion = new MacOsVersion(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)));
221 return fRunningMacOsVersion;
222 } catch (NumberFormatException e) {
223 // ignore
224 }
225 }
226 return null;
227 }
228
229 @Override
230 public int compareTo(MacOsVersion o) {
231 int compareTo = Integer.compare(fMajorVersion, o.getMajorVersion());
232 if (compareTo != 0) {
233 return compareTo;
234 }
235
236 compareTo = Integer.compare(fMinorVersion, o.getMinorVersion());
237 if (compareTo != 0) {
238 return compareTo;
239 }
240
241 return Integer.compare(fBugFixVersion, o.getBugFixVersion());
242 }
243 }
244 }
This page took 0.040639 seconds and 4 git commands to generate.