tmf: Add waitUntil / condition to tmf.ui.tests
[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.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;
50
51 /**
52 * Test trace offsetting
53 *
54 * @author Matthew Khouzam
55 */
56 @RunWith(SWTBotJunit4ClassRunner.class)
57 public class TestTraceOffsetting {
58
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>";
64
65 private static final String PROJET_NAME = "TestForOffsetting";
66 private static final int NUM_EVENTS = 100;
67
68 /** The Log4j logger instance. */
69 private static final Logger fLogger = Logger.getRootLogger();
70 private static SWTWorkbenchBot fBot;
71
72 private static String makeEvent(int ts, int val) {
73 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
74 }
75
76 private File fLocation;
77
78 /**
79 * Initialization, creates a temp trace
80 *
81 * @throws IOException
82 * should not happen
83 */
84 @Before
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();
93
94 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
95 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, "GMT-05:00");
96 TmfTimestampFormat.updateDefaultFormats();
97
98 SWTBotUtils.closeView("welcome", fBot);
99
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));
108 }
109 braf.writeBytes(TRACE_END);
110 }
111 }
112
113 /**
114 * Delete file
115 */
116 @After
117 public void cleanup() {
118 fLocation.delete();
119 fLogger.removeAllAppenders();
120
121 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
122 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, TmfTimePreferences.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants.TIME_ZONE));
123 TmfTimestampFormat.updateDefaultFormats();
124 }
125
126 /**
127 * Test offsetting by 99 ns
128 */
129 @Test
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);
136
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());
145 traceItem.select();
146 traceItem.contextMenu("Apply Time Offset...").click();
147 WaitUtils.waitForJobs();
148 // set offset to 99 ns
149 SWTBotShell shell = fBot.shell("Apply time offset");
150 shell.setFocus();
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();
160
161 // wait for trace to close
162 fBot.waitWhile(ConditionHelpers.isEditorOpened(fBot, fLocation.getName()));
163
164 // re-open trace
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);
171 }
172
173 /**
174 * Class to store the Mac OS version.
175 *
176 * This could be moved if other tests start using this.
177 */
178 private static class MacOsVersion implements Comparable<MacOsVersion> {
179 private static final Pattern MAC_OS_VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
180
181 private int fMajorVersion;
182 private int fMinorVersion;
183 private int fBugFixVersion;
184
185 private static MacOsVersion fRunningMacOsVersion;
186
187 private MacOsVersion(int version, int majorVersion, int patchVersion) {
188 this.fMajorVersion = version;
189 this.fMinorVersion = majorVersion;
190 this.fBugFixVersion = patchVersion;
191 }
192
193 private int getMajorVersion() {
194 return fMajorVersion;
195 }
196
197 private int getMinorVersion() {
198 return fMinorVersion;
199 }
200
201 private int getBugFixVersion() {
202 return fBugFixVersion;
203 }
204
205 private static MacOsVersion getMacOsVersion() {
206 if (fRunningMacOsVersion != null) {
207 return fRunningMacOsVersion;
208 }
209
210 if (!SWTUtils.isMac()) {
211 return null;
212 }
213
214 String osVersion = System.getProperty("os.version");
215 if (osVersion == null) {
216 return null;
217 }
218 Matcher matcher = MAC_OS_VERSION_PATTERN.matcher(osVersion);
219 if (matcher.matches()) {
220 try {
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) {
224 // ignore
225 }
226 }
227 return null;
228 }
229
230 @Override
231 public int compareTo(MacOsVersion o) {
232 int compareTo = Integer.compare(fMajorVersion, o.getMajorVersion());
233 if (compareTo != 0) {
234 return compareTo;
235 }
236
237 compareTo = Integer.compare(fMinorVersion, o.getMinorVersion());
238 if (compareTo != 0) {
239 return compareTo;
240 }
241
242 return Integer.compare(fBugFixVersion, o.getBugFixVersion());
243 }
244 }
245 }
This page took 0.039027 seconds and 5 git commands to generate.