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 | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | ||
17 | import java.io.File; | |
18 | import java.io.IOException; | |
19 | ||
20 | import org.apache.log4j.ConsoleAppender; | |
21 | import org.apache.log4j.Logger; | |
22 | import org.apache.log4j.SimpleLayout; | |
23 | import org.eclipse.jface.bindings.keys.KeyStroke; | |
24 | import org.eclipse.swt.SWT; | |
25 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
26 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
27 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; | |
28 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; | |
29 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; | |
30 | import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile; | |
31 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
32 | import org.junit.After; | |
33 | import org.junit.Before; | |
34 | import org.junit.Test; | |
35 | ||
36 | /** | |
37 | * Test trace offsetting | |
38 | * | |
39 | * @author Matthew Khouzam | |
40 | */ | |
41 | public class TestTraceOffsetting { | |
42 | ||
43 | private static final String TRACE_START = "<trace>"; | |
44 | private static final String EVENT_BEGIN = "<event timestamp=\""; | |
45 | private static final String EVENT_MIDDLE = " \" name=\"event\"><field name=\"field\" value=\""; | |
46 | private static final String EVENT_END = "\" type=\"int\" />" + "</event>"; | |
47 | private static final String TRACE_END = "</trace>"; | |
48 | ||
49 | private static final String PROJET_NAME = "TestForOffsetting"; | |
50 | private static final int NUM_EVENTS = 100; | |
51 | ||
52 | /** The Log4j logger instance. */ | |
53 | private static final Logger fLogger = Logger.getRootLogger(); | |
54 | private static SWTWorkbenchBot fBot; | |
55 | ||
56 | private static String makeEvent(int ts, int val) { | |
57 | return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n"; | |
58 | } | |
59 | ||
60 | private File fLocation; | |
61 | ||
62 | /** | |
63 | * Initialization, creates a temp trace | |
64 | * | |
65 | * @throws IOException | |
66 | * should not happen | |
67 | */ | |
68 | @Before | |
69 | public void init() throws IOException { | |
70 | SWTBotUtils.failIfUIThread(); | |
71 | Thread.currentThread().setName("SWTBot Thread"); // for the debugger | |
72 | /* set up for swtbot */ | |
73 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ | |
f10c30a0 | 74 | fLogger.removeAllAppenders(); |
a5c211e1 MK |
75 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout())); |
76 | fBot = new SWTWorkbenchBot(); | |
77 | ||
78 | SWTBotUtils.closeView("welcome", fBot); | |
79 | ||
80 | SWTBotUtils.switchToTracingPerspective(); | |
81 | /* finish waiting for eclipse to load */ | |
82 | SWTBotUtils.waitForJobs(); | |
83 | fLocation = File.createTempFile("sample", ".xml"); | |
84 | try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fLocation, "rw")) { | |
85 | braf.writeBytes(TRACE_START); | |
86 | for (int i = 0; i < NUM_EVENTS; i++) { | |
87 | braf.writeBytes(makeEvent(i * 100, i % 4)); | |
88 | } | |
89 | braf.writeBytes(TRACE_END); | |
90 | } | |
91 | } | |
92 | ||
93 | /** | |
94 | * Delete file | |
95 | */ | |
96 | @After | |
97 | public void cleanup() { | |
98 | fLocation.delete(); | |
f10c30a0 | 99 | fLogger.removeAllAppenders(); |
a5c211e1 MK |
100 | } |
101 | ||
102 | /** | |
103 | * Test offsetting by 99 ns | |
104 | */ | |
105 | @Test | |
106 | public void testOffsetting() { | |
107 | SWTBotUtils.createProject(PROJET_NAME); | |
108 | SWTBotUtils.openTrace(PROJET_NAME, fLocation.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub"); | |
109 | SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(fBot, PROJET_NAME); | |
110 | SWTBotTable eventsTableBot = fBot.activeEditor().bot().table(); | |
111 | String timestamp = eventsTableBot.cell(1, 1); | |
112 | assertEquals("19:00:00.000 000 000", timestamp); | |
113 | treeItem.select(); | |
114 | treeItem.getItems()[0].contextMenu("Apply Time Offset...").click(); | |
115 | SWTBotUtils.waitForJobs(); | |
116 | // set offset to 99 ns | |
117 | SWTBotShell shell = fBot.shell("Apply time offset"); | |
118 | shell.setFocus(); | |
119 | SWTBotTreeItem[] allItems = fBot.tree().getAllItems(); | |
120 | final SWTBotTreeItem swtBotTreeItem = allItems[0]; | |
121 | swtBotTreeItem.select(); | |
122 | swtBotTreeItem.click(1); | |
123 | KeyStroke[] keyStrokes = new KeyStroke[1]; | |
124 | keyStrokes[0] = KeyStroke.getInstance('9'); | |
125 | swtBotTreeItem.pressShortcut(keyStrokes); | |
126 | keyStrokes[0] = KeyStroke.getInstance('9'); | |
127 | swtBotTreeItem.pressShortcut(keyStrokes); | |
128 | keyStrokes[0] = KeyStroke.getInstance(SWT.CR); | |
129 | swtBotTreeItem.pressShortcut(keyStrokes); | |
130 | SWTBotUtils.waitForJobs(); | |
131 | fBot.button("OK").click(); | |
132 | // re-open trace | |
133 | SWTBotUtils.openTrace(PROJET_NAME, fLocation.getAbsolutePath(), "org.eclipse.linuxtools.tmf.core.tests.xmlstub"); | |
134 | SWTBotUtils.waitForJobs(); | |
135 | ||
136 | timestamp = eventsTableBot.cell(1, 1); | |
137 | assertEquals("19:00:00.000 000 000", timestamp); | |
138 | SWTBotUtils.deleteProject(PROJET_NAME, fBot); | |
139 | } | |
140 | ||
141 | } |