tmf: Close Sequence Diagram view after tests
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / SDViewTest.java
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 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
13
14 import static org.junit.Assert.assertArrayEquals;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.log4j.ConsoleAppender;
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.SimpleLayout;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
28 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
30 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
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.SWTBotUtils;
34 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDView;
35 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.Frame;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41
42 /**
43 * Test for Sequence Diagram view in trace compass
44 */
45 public class SDViewTest {
46
47 private static final String UML2DVIEW_ID = "org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView";
48
49 private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
50
51 private static final String TRACE_START = "<trace>";
52 private static final String EVENT_BEGIN = "<event timestamp=\"";
53 private static final String EVENT_MIDDLE1 = " \" name=\"";
54 private static final String EVENT_MIDDLE2 = "\">";
55 private static final String FIELD_SENDER = "<field name=\"sender\" value=\"";
56 private static final String FIELD_RECEIVER = "<field name=\"receiver\" value=\"";
57 private static final String FIELD_SIGNAL = "<field name=\"signal\" value=\"";
58 private static final String FIELD_END = "\" type=\"string\" />";
59 private static final String EVENT_END = "</event>";
60 private static final String TRACE_END = "</trace>";
61
62 private static final String PROJECT_NAME = "TestForFiltering";
63
64 /** The Log4j logger instance. */
65 private static final Logger fLogger = Logger.getRootLogger();
66 private static SWTWorkbenchBot fBot;
67
68 private static String makeEvent(int ts, String eventName, String send, String recv, String signal) {
69 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE1 + eventName + EVENT_MIDDLE2 + FIELD_SENDER + send + FIELD_END + FIELD_RECEIVER + recv + FIELD_END + FIELD_SIGNAL + signal + FIELD_END + EVENT_END + "\n";
70 }
71
72 private static File fFileLocation;
73
74 /**
75 * Initialization, creates a temp trace
76 *
77 * @throws IOException
78 * should not happen
79 */
80 @BeforeClass
81 public static void init() throws IOException {
82 SWTBotUtils.initialize();
83 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
84 /* set up for swtbot */
85 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
86 fLogger.removeAllAppenders();
87 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
88 fBot = new SWTWorkbenchBot();
89
90 SWTBotUtils.closeView("welcome", fBot);
91
92 SWTBotUtils.switchToTracingPerspective();
93 /* finish waiting for eclipse to load */
94 SWTBotUtils.waitForJobs();
95 fFileLocation = File.createTempFile("sample", ".xml");
96 String eventNames[] = { "test:SEND", "test:RECEIVE" };
97 String targets[] = { "peer1", "peer2" };
98
99 try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fFileLocation, "rw")) {
100 braf.writeBytes(TRACE_START);
101 for (int i = 0; i < 100; i++) {
102 braf.writeBytes(makeEvent(i * i * 100, eventNames[i % 2], targets[i % 2], targets[(i + 1) % 2], Integer.toString(i % 2 + 1000)));
103 }
104 braf.writeBytes(TRACE_END);
105 }
106 }
107
108 /**
109 * Open a trace in an editor
110 */
111 @Before
112 public void beforeTest() {
113 SWTBotUtils.createProject(PROJECT_NAME);
114 SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(fBot, PROJECT_NAME);
115 assertNotNull(treeItem);
116 SWTBotUtils.openTrace(PROJECT_NAME, fFileLocation.getAbsolutePath(), XMLSTUB_ID);
117 SWTBotUtils.openView(UML2DVIEW_ID);
118 }
119
120 /**
121 * Delete the file
122 */
123 @AfterClass
124 public static void cleanUp() {
125 SWTBotUtils.closeViewById(UML2DVIEW_ID, fBot);
126 fFileLocation.delete();
127 fLogger.removeAllAppenders();
128 }
129
130 /**
131 * Close the editor
132 */
133 @After
134 public void tearDown() {
135 fBot.closeAllEditors();
136 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
137 }
138
139 /**
140 * Test Sequence diagram view, counting the columns
141 */
142 @Test
143 public void testSDView() {
144 SWTBotView viewBot = fBot.viewById(UML2DVIEW_ID);
145
146 assertNotNull(viewBot);
147 viewBot.setFocus();
148 SWTBotUtils.waitForJobs();
149 List<SWTBotToolbarButton> viewButtons = viewBot.getToolbarButtons();
150 List<String> titles = new ArrayList<>();
151 for (SWTBotToolbarButton buttonBot : viewButtons) {
152 titles.add(buttonBot.getToolTipText());
153 }
154
155 final char commandKeyChar = (char) 0x2318;
156 final String findShortcut = (Platform.getOS().equals(Platform.OS_MACOSX) ? commandKeyChar : "Ctrl+") + "F";
157 String[] expected = { "Reset zoom factor", "Select",
158 "Zoom in the diagram", "Zoom out the diagram",
159 "Go to next page", "Go to previous page",
160 "Go to first page", "Go to last page",
161 "Find... (" + findShortcut + ")"
162 };
163 assertArrayEquals("Buttons", expected, titles.toArray(new String[0]));
164 SDView view = (SDView) viewBot.getViewReference().getPart(false);
165 Frame frame = view.getFrame();
166 assertEquals(2, frame.lifeLinesCount());
167 }
168 }
This page took 0.064149 seconds and 5 git commands to generate.