1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson and others
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 package org
.eclipse
.tracecompass
.lttng2
.kernel
.ui
.swtbot
.tests
;
11 import static org
.junit
.Assert
.assertTrue
;
13 import org
.eclipse
.jdt
.annotation
.NonNull
;
14 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
15 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
16 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
17 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
18 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
19 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
20 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.KeyboardFactory
;
21 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
22 import org
.eclipse
.swtbot
.swt
.finder
.matchers
.WidgetOfType
;
23 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
24 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
25 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
26 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
27 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCombo
;
28 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotLabel
;
29 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotRadio
;
30 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphControl
;
41 import org
.junit
.After
;
42 import org
.junit
.Before
;
43 import org
.junit
.Test
;
44 import org
.junit
.runner
.RunWith
;
47 * Abstract class to build SWTBot test for time graph find dialog. Test the time
48 * graph view find dialog and its options.
50 * @author Jean-Christian Kouame
52 @RunWith(SWTBotJunit4ClassRunner
.class)
53 public abstract class FindDialogTestBase
extends KernelTestBase
{
55 private static final Keyboard KEYBOARD
= KeyboardFactory
.getSWTKeyboard();
56 private static final @NonNull ITmfTimestamp START_TIME
= TmfTimestamp
.create(1368000272650993664L, ITmfTimestamp
.NANOSECOND_SCALE
);
57 private static final @NonNull String SPACE
= " ";
58 private static final String REGEX_PREFIX
= "\\A";
59 private static final String DIALOG_TITLE
= "Find";
60 private String fFindText
;
61 private SWTBotView fViewBot
;
63 private static int fSelectionIndex
;
66 * Get the title of the time graph view the find dialog will use
68 * @return The view title
70 protected abstract String
getViewTitle();
73 * Get the string that will be used for the search
77 protected abstract String
getFindText();
80 * Initialize the test and open the timegraph view and the find dialog
84 String title
= getViewTitle();
85 fViewBot
= fBot
.viewByTitle(title
);
89 TmfSignalManager
.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, START_TIME
));
90 fBot
.waitUntil(ConditionHelpers
.timeGraphIsReadyCondition((AbstractTimeGraphView
) fViewBot
.getViewReference().getPart(false), new TmfTimeRange(START_TIME
, START_TIME
), START_TIME
));
93 fFindText
= getFindText();
97 * After method to close the dialog
100 public void afterTest() {
101 closeDialog(getDialogBot());
104 private static void openDialog(SWTBotView view
) {
106 SWTBotUtils
.pressShortcutGoToTreeTop(KEYBOARD
);
107 if (SWTUtils
.isMac()) {
108 KEYBOARD
.pressShortcut(Keystrokes
.COMMAND
, KeyStroke
.getInstance('F'));
110 KEYBOARD
.pressShortcut(Keystrokes
.CTRL
, KeyStroke
.getInstance('F'));
112 fBot
.waitUntil(Conditions
.shellIsActive(DIALOG_TITLE
));
116 * Test wrap search option
119 public void testWrapSearch() {
120 SWTBot bot
= getDialogBot();
121 SWTBotButton findButton
= bot
.button("Find");
123 SearchOptions options
= getOptions(false, false, true, false, false);
124 search(fFindText
, options
, findButton
, bot
);
125 assertTrue(isWrapped(bot
));
126 verifySelection(fFindText
, options
, fViewBot
, isWrapped(bot
));
127 options
= getOptions(true, false, true, false, false);
128 search(fFindText
, options
, findButton
, bot
);
129 assertTrue(isWrapped(bot
));
133 * Test the direction search option
136 public void testDirection() {
137 SWTBot bot
= getDialogBot();
138 SWTBotButton findButton
= bot
.button("Find");
141 testDirectionSearch(true, fFindText
, bot
, findButton
, fViewBot
);
142 testDirectionSearch(false, fFindText
, bot
, findButton
, fViewBot
);
145 private void testDirectionSearch(boolean forward
, String findText
, SWTBot bot
, SWTBotButton findButton
, SWTBotView view
) {
146 SearchOptions options
= getOptions(forward
, false, true, false, false);
147 fSelectionIndex
= getSelectionIndex(view
);
148 search(findText
, options
, findButton
, bot
);
149 verifySelection(findText
, options
, view
, isWrapped(bot
));
153 * Test the case sensitive search option
156 public void testCaseSensitive() {
157 SWTBot bot
= getDialogBot();
158 SWTBotButton findButton
= bot
.button("Find");
160 SearchOptions options
= getOptions(true, true, false, false, false);
161 search(fFindText
, options
, findButton
, bot
);
162 verifyStatusLabel(bot
, true);
163 search(fFindText
.toLowerCase(), options
, findButton
, bot
);
164 verifyStatusLabel(bot
, false);
168 * Test the whole word search option
171 public void testWholeWord() {
172 SWTBot bot
= getDialogBot();
173 SWTBotButton findButton
= bot
.button("Find");
176 String text
= fFindText
.split(SPACE
)[0];
177 System
.out
.println("Reg ex : " + text
);
178 SearchOptions options
= getOptions(true, false, false, true, false);
179 search(text
, options
, findButton
, bot
);
180 verifyStatusLabel(bot
, true);
181 search(text
.substring(0, text
.length() - 1), options
, findButton
, bot
);
182 verifyStatusLabel(bot
, false);
186 * Test the regular expression search option
189 public void testRegEx() {
190 SWTBot bot
= getDialogBot();
191 SWTBotButton findButton
= bot
.button("Find");
193 final String text
= REGEX_PREFIX
+ fFindText
.split(SPACE
)[0];
194 System
.out
.println("Reg ex : " + text
);
195 SearchOptions options
= getOptions(true, false, false, false, true);
196 search(text
, options
, findButton
, bot
);
197 verifyStatusLabel(bot
, true);
198 options
= getOptions(true, false, false, false, false);
199 search(text
, options
, findButton
, bot
);
200 verifyStatusLabel(bot
, false);
204 * Test open/close the find dialog
207 public void testOpenCloseDialog() {
208 SWTBotShell shell
= getDialogShell();
209 closeDialog(getDialogBot());
210 fBot
.waitUntil(Conditions
.shellCloses(shell
));
211 openDialog(fViewBot
);
214 private static void verifyStatusLabel(SWTBot bot
, boolean shouldBeFound
) {
215 // Get the second label in the dialog
216 SWTBotLabel statusLabel
= bot
.label(1);
217 assertTrue("status label", shouldBeFound
== !statusLabel
.getText().equals("Entry not found"));
221 * Get the find dialog bot
225 private static SWTBot
getDialogBot() {
226 return getDialogShell().bot();
230 * Get the find dialog shell bot
232 * @return The shell bot
234 private static SWTBotShell
getDialogShell() {
235 return fBot
.shell(DIALOG_TITLE
);
238 private static void closeDialog(SWTBot bot
) {
239 bot
.button("Close").click();
242 private static void search(String findText
, SearchOptions options
, SWTBotButton findButton
, SWTBot bot
) {
243 // set the text to search
244 SWTBotCombo findFieldCombo
= bot
.comboBox();
245 findFieldCombo
.setText(findText
);
246 assertTrue("Find combo", findFieldCombo
.getText().equals(findText
));
249 SWTBotRadio directions
= options
.forwardSearch ? bot
.radio("Forward").click() : bot
.radio("Backward").click();
250 assertTrue("direction", directions
.isSelected());
252 setCheckButton("Case sensitive", options
.caseSensitive
, bot
);
253 setCheckButton("Wrap search", options
.wrapSearch
, bot
);
254 setCheckButton("Whole word", options
.wholeWord
, bot
);
255 setCheckButton("Regular expression", options
.regExSearch
, bot
);
260 private SearchOptions
getOptions(boolean forward
, boolean caseSensitive
, boolean wrapSearch
, boolean wholeWord
, boolean regEx
) {
261 SearchOptions options
= new SearchOptions();
262 options
.forwardSearch
= forward
;
263 options
.caseSensitive
= caseSensitive
;
264 options
.wrapSearch
= wrapSearch
;
265 options
.wholeWord
= wholeWord
;
266 options
.regExSearch
= regEx
;
270 private static void setCheckButton(String mnemonic
, boolean option
, SWTBot bot
) {
271 final SWTBotCheckBox checkBox
= bot
.checkBox(mnemonic
);
272 if (checkBox
.isEnabled()) {
281 private static void verifySelection(String name
, SearchOptions options
, SWTBotView view
, boolean isWrapped
) {
282 final String entryName
= getTimegraphSelectionName(view
);
283 assertTrue("entry name", entryName
!= null && entryName
.contains(name
));
285 final int selectionIndex
= getSelectionIndex(view
);
287 assertTrue("selection index", options
.forwardSearch ? selectionIndex
> fSelectionIndex
: selectionIndex
< fSelectionIndex
);
289 assertTrue("selection index", options
.forwardSearch ? selectionIndex
<= fSelectionIndex
: selectionIndex
>= fSelectionIndex
);
291 fSelectionIndex
= selectionIndex
;
294 private static boolean isWrapped(final SWTBot bot
) {
295 return bot
.label(1).getText().equals("Wrapped search");
299 * Get the timegraph view selected entry
302 * The timegraph view bot
303 * @return The selected entry
305 private static String
getTimegraphSelectionName(final SWTBotView view
) {
306 final TimeGraphControl timegraph
= view
.bot().widget(WidgetOfType
.widgetOfType(TimeGraphControl
.class));
307 return UIThreadRunnable
.syncExec(() -> {
308 ITimeGraphEntry entry
= timegraph
.getSelectedTrace();
310 return entry
.getName();
317 * Get the index of the entry selected in the timegraph view
320 * The timegraph view bot
323 private static Integer
getSelectionIndex(SWTBotView viewBot
) {
324 final TimeGraphControl timegraph
= viewBot
.bot().widget(WidgetOfType
.widgetOfType(TimeGraphControl
.class));
325 return UIThreadRunnable
.syncExec(() -> {
326 return timegraph
.getSelectedIndex();
330 private class SearchOptions
{
331 boolean forwardSearch
;
332 boolean caseSensitive
;