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