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