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