Commit | Line | Data |
---|---|---|
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 | ||
15 | package org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests; | |
16 | ||
17 | import static org.junit.Assert.fail; | |
18 | ||
19 | import java.util.List; | |
20 | ||
21 | import org.apache.log4j.ConsoleAppender; | |
22 | import org.apache.log4j.Logger; | |
23 | import org.apache.log4j.SimpleLayout; | |
723a1d49 | 24 | import org.eclipse.swt.widgets.TreeItem; |
2fe6a9ea PT |
25 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
26 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; | |
27 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
28 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; | |
29 | import org.eclipse.swtbot.swt.finder.results.BoolResult; | |
723a1d49 | 30 | import org.eclipse.swtbot.swt.finder.results.Result; |
2fe6a9ea | 31 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
723a1d49 | 32 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
c4d57ac1 | 33 | import org.eclipse.tracecompass.ctf.core.tests.shared.LttngKernelTraceGenerator; |
2fe6a9ea PT |
34 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; |
35 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
36 | import org.eclipse.ui.PlatformUI; | |
37 | import org.eclipse.ui.WorkbenchException; | |
38 | import org.junit.After; | |
39 | import org.junit.AfterClass; | |
40 | import org.junit.Before; | |
41 | import org.junit.BeforeClass; | |
42 | import 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 | 50 | public 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() { | |
723a1d49 | 128 | SWTBotUtils.openTrace(TRACE_PROJECT_NAME, LttngKernelTraceGenerator.getPath(), KERNEL_TRACE_TYPE); |
c4d57ac1 | 129 | SWTBotUtils.activateEditor(fBot, LttngKernelTraceGenerator.getName()); |
2fe6a9ea PT |
130 | } |
131 | ||
132 | /** | |
133 | * After Test | |
134 | */ | |
135 | @After | |
136 | public void after() { | |
137 | fBot.closeAllEditors(); | |
138 | } | |
723a1d49 BH |
139 | |
140 | /** | |
141 | * Class to check number of checked items | |
142 | */ | |
143 | static final class TreeCheckedCounter implements Result<Integer> { | |
144 | private final SWTBotTree fTreeBot; | |
145 | ||
146 | TreeCheckedCounter(SWTBotTree treeBot) { | |
147 | fTreeBot = treeBot; | |
148 | } | |
149 | ||
150 | @Override | |
151 | public Integer run() { | |
152 | int checked = 0; | |
153 | for (TreeItem item : fTreeBot.widget.getItems()) { | |
154 | checked += getChecked(item); | |
155 | } | |
156 | return checked; | |
157 | } | |
158 | ||
159 | private int getChecked(TreeItem item) { | |
160 | int total = 0; | |
161 | if (item.getChecked()) { | |
162 | total++; | |
163 | } | |
164 | for (TreeItem child : item.getItems()) { | |
165 | total += getChecked(child); | |
166 | } | |
167 | return total; | |
168 | } | |
169 | } | |
2fe6a9ea | 170 | } |