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