Add utility method to close secondary shells after tests
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests / src / org / eclipse / tracecompass / lttng2 / kernel / ui / swtbot / tests / KernelTestBase.java
CommitLineData
2fe6a9ea 1/*******************************************************************************
723a1d49 2 * Copyright (c) 2013, 2016 Ericsson
2fe6a9ea
PT
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 * Marc-Andre Laperle
12 * Patrick Tasse - Extract base class from ImportAndReadKernelSmokeTest
13 *******************************************************************************/
14
15package org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests;
16
17import static org.junit.Assert.fail;
18
19import java.util.List;
20
21import org.apache.log4j.ConsoleAppender;
22import org.apache.log4j.Logger;
23import org.apache.log4j.SimpleLayout;
723a1d49 24import org.eclipse.swt.widgets.TreeItem;
2fe6a9ea
PT
25import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
26import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
27import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
28import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
29import org.eclipse.swtbot.swt.finder.results.BoolResult;
723a1d49 30import org.eclipse.swtbot.swt.finder.results.Result;
2fe6a9ea 31import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
723a1d49 32import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
4eebea10 33import org.eclipse.tracecompass.ctf.core.tests.shared.LttngTraceGenerator;
2fe6a9ea
PT
34import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
35import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
36import org.eclipse.ui.PlatformUI;
37import org.eclipse.ui.WorkbenchException;
38import org.junit.After;
39import org.junit.AfterClass;
40import org.junit.Before;
41import org.junit.BeforeClass;
42import org.junit.runner.RunWith;
43
44/**
45 * Base SWTBot test for LTTng Kernel UI.
46 *
47 * @author Matthew Khouzam
48 */
49@RunWith(SWTBotJunit4ClassRunner.class)
b0d2c558 50public abstract class KernelTestBase {
2fe6a9ea 51
723a1d49
BH
52 /** LTTng kernel trace type */
53 protected static final String KERNEL_TRACE_TYPE = "org.eclipse.linuxtools.lttng2.kernel.tracetype";
54 /** LTTng kernel perspective */
55 protected static final String KERNEL_PERSPECTIVE_ID = "org.eclipse.linuxtools.lttng2.kernel.ui.perspective";
56 /** Default project name */
57 protected static final String TRACE_PROJECT_NAME = "test";
2fe6a9ea
PT
58
59 /** The workbench bot */
60 protected static SWTWorkbenchBot fBot;
61
62 /** The Log4j logger instance. */
63 private static final Logger fLogger = Logger.getRootLogger();
64
65 /**
66 * Before Class
67 */
68 @BeforeClass
69 public static void beforeClass() {
5785ab49 70 SWTBotUtils.initialize();
2fe6a9ea
PT
71
72 /* set up for swtbot */
73 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
5cd20891 74 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
2fe6a9ea
PT
75 fLogger.removeAllAppenders();
76 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
77 fBot = new SWTWorkbenchBot();
78
79 final List<SWTBotView> openViews = fBot.views();
80 for (SWTBotView view : openViews) {
81 if (view.getTitle().equals("Welcome")) {
82 view.close();
83 fBot.waitUntil(ConditionHelpers.ViewIsClosed(view));
84 }
85 }
86 /* Switch perspectives */
87 switchKernelPerspective();
2a52b85b
PT
88 /* Create the trace project */
89 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
2fe6a9ea
PT
90 /* Finish waiting for eclipse to load */
91 SWTBotUtils.waitForJobs();
2fe6a9ea
PT
92 }
93
94 /**
95 * After Class
96 */
97 @AfterClass
98 public static void afterClass() {
2a52b85b 99 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
2fe6a9ea
PT
100 fLogger.removeAllAppenders();
101 }
102
103 private static void switchKernelPerspective() {
104 final Exception retE[] = new Exception[1];
105 if (!UIThreadRunnable.syncExec(new BoolResult() {
106 @Override
107 public Boolean run() {
108 try {
109 PlatformUI.getWorkbench().showPerspective(KERNEL_PERSPECTIVE_ID,
110 PlatformUI.getWorkbench().getActiveWorkbenchWindow());
111 } catch (WorkbenchException e) {
112 retE[0] = e;
113 return false;
114 }
115 return true;
116 }
117 })) {
118 fail(retE[0].getMessage());
119 }
120
121 }
122
123 /**
124 * Before Test
125 */
126 @Before
127 public void before() {
4eebea10
MAL
128 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, LttngTraceGenerator.getPath(), KERNEL_TRACE_TYPE);
129 SWTBotUtils.activateEditor(fBot, LttngTraceGenerator.getName());
2fe6a9ea
PT
130 }
131
132 /**
133 * After Test
134 */
135 @After
136 public void after() {
137 fBot.closeAllEditors();
1dfcd42b 138 SWTBotUtils.closeSecondaryShells(fBot);
2fe6a9ea 139 }
723a1d49
BH
140
141 /**
142 * Class to check number of checked items
143 */
144 static final class TreeCheckedCounter implements Result<Integer> {
145 private final SWTBotTree fTreeBot;
146
147 TreeCheckedCounter(SWTBotTree treeBot) {
148 fTreeBot = treeBot;
149 }
150
151 @Override
152 public Integer run() {
153 int checked = 0;
154 for (TreeItem item : fTreeBot.widget.getItems()) {
155 checked += getChecked(item);
156 }
157 return checked;
158 }
159
160 private int getChecked(TreeItem item) {
161 int total = 0;
162 if (item.getChecked()) {
163 total++;
164 }
165 for (TreeItem child : item.getItems()) {
166 total += getChecked(child);
167 }
168 return total;
169 }
170 }
2fe6a9ea 171}
This page took 0.050273 seconds and 5 git commands to generate.