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
CommitLineData
a5c211e1
MK
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
88051e61 11 * Patrick Tasse - Fix editor handling
a5c211e1
MK
12 *******************************************************************************/
13
14package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
15
16import static org.junit.Assert.assertEquals;
7c16eaff 17import static org.junit.Assume.assumeTrue;
a5c211e1
MK
18
19import java.io.File;
20import java.io.IOException;
7c16eaff
MAL
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
a5c211e1
MK
23
24import org.apache.log4j.ConsoleAppender;
25import org.apache.log4j.Logger;
26import org.apache.log4j.SimpleLayout;
6941941d
MAL
27import org.eclipse.core.runtime.preferences.IEclipsePreferences;
28import org.eclipse.core.runtime.preferences.InstanceScope;
a5c211e1 29import org.eclipse.jface.bindings.keys.KeyStroke;
a5c211e1 30import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
88051e61
PT
31import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
32import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
a5c211e1 33import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
7c16eaff 34import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
a5c211e1
MK
35import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
36import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
37import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
6941941d 38import org.eclipse.tracecompass.internal.tmf.core.Activator;
a5c211e1 39import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
6941941d 40import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimePreferencesConstants;
9bb4b61e 41import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimePreferences;
6941941d 42import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
88051e61 43import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
a5c211e1 44import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
f0beeb4a 45import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
a5c211e1
MK
46import org.junit.After;
47import org.junit.Before;
48import org.junit.Test;
88051e61 49import org.junit.runner.RunWith;
a5c211e1
MK
50
51/**
52 * Test trace offsetting
53 *
54 * @author Matthew Khouzam
55 */
88051e61 56@RunWith(SWTBotJunit4ClassRunner.class)
a5c211e1
MK
57public 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 {
5785ab49 86 SWTBotUtils.initialize();
a5c211e1
MK
87 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
88 /* set up for swtbot */
89 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
f10c30a0 90 fLogger.removeAllAppenders();
a5c211e1
MK
91 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
92 fBot = new SWTWorkbenchBot();
93
6941941d
MAL
94 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
95 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, "GMT-05:00");
96 TmfTimestampFormat.updateDefaultFormats();
97
a5c211e1
MK
98 SWTBotUtils.closeView("welcome", fBot);
99
100 SWTBotUtils.switchToTracingPerspective();
101 /* finish waiting for eclipse to load */
f0beeb4a 102 WaitUtils.waitForJobs();
a5c211e1
MK
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();
f10c30a0 119 fLogger.removeAllAppenders();
6941941d
MAL
120
121 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
9bb4b61e 122 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, TmfTimePreferences.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants.TIME_ZONE));
6941941d 123 TmfTimestampFormat.updateDefaultFormats();
a5c211e1
MK
124 }
125
126 /**
127 * Test offsetting by 99 ns
128 */
129 @Test
130 public void testOffsetting() {
7c16eaff
MAL
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
a5c211e1 137 SWTBotUtils.createProject(PROJET_NAME);
88051e61 138 SWTBotTreeItem traceFolderItem = SWTBotUtils.selectTracesFolder(fBot, PROJET_NAME);
a5c211e1 139 SWTBotUtils.openTrace(PROJET_NAME, fLocation.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
88051e61
PT
140 SWTBotEditor editor = fBot.editorByTitle(fLocation.getName());
141 SWTBotTable eventsTableBot = editor.bot().table();
a5c211e1
MK
142 String timestamp = eventsTableBot.cell(1, 1);
143 assertEquals("19:00:00.000 000 000", timestamp);
88051e61
PT
144 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, traceFolderItem, fLocation.getName());
145 traceItem.select();
146 traceItem.contextMenu("Apply Time Offset...").click();
f0beeb4a 147 WaitUtils.waitForJobs();
a5c211e1
MK
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);
88051e61
PT
155 swtBotTreeItem.pressShortcut(KeyStroke.getInstance('9'));
156 swtBotTreeItem.pressShortcut(KeyStroke.getInstance('9'));
157 swtBotTreeItem.pressShortcut(KeyStroke.getInstance('\n'));
f0beeb4a 158 WaitUtils.waitForJobs();
a5c211e1 159 fBot.button("OK").click();
88051e61
PT
160
161 // wait for trace to close
162 fBot.waitWhile(ConditionHelpers.isEditorOpened(fBot, fLocation.getName()));
163
a5c211e1
MK
164 // re-open trace
165 SWTBotUtils.openTrace(PROJET_NAME, fLocation.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub");
88051e61
PT
166 editor = fBot.editorByTitle(fLocation.getName());
167 eventsTableBot = editor.bot().table();
a5c211e1 168 timestamp = eventsTableBot.cell(1, 1);
88051e61 169 assertEquals("19:00:00.000 000 099", timestamp);
a5c211e1
MK
170 SWTBotUtils.deleteProject(PROJET_NAME, fBot);
171 }
172
7c16eaff
MAL
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 }
a5c211e1 245}
This page took 0.069042 seconds and 5 git commands to generate.