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
.jface
.viewers
.ISelection
;
16 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
17 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
18 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
19 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
20 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
21 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.KeyboardFactory
;
22 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
23 import org
.eclipse
.swtbot
.swt
.finder
.matchers
.WidgetOfType
;
24 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
25 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
26 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
27 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
28 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCombo
;
29 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotLabel
;
30 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotRadio
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphControl
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphSelection
;
43 import org
.junit
.AfterClass
;
44 import org
.junit
.Before
;
45 import org
.junit
.Test
;
46 import org
.junit
.runner
.RunWith
;
49 * Abstract class to build SWTBot test for time graph find dialog. Test the time
50 * graph view find dialog and its options.
52 * @author Jean-Christian Kouame
54 @RunWith(SWTBotJunit4ClassRunner
.class)
55 public abstract class FindDialogTestBase
extends KernelTestBase
{
57 private static final Keyboard KEYBOARD
= KeyboardFactory
.getSWTKeyboard();
58 private static final @NonNull ITmfTimestamp START_TIME
= TmfTimestamp
.create(1368000272650993664L, ITmfTimestamp
.NANOSECOND_SCALE
);
59 private static final @NonNull String SPACE
= " ";
60 private static final String REGEX_PREFIX
= "\\A";
61 private static final String DIALOG_TITLE
= "Find";
62 private String fFindText
;
63 private SWTBotView fViewBot
;
65 private static int fSelectionIndex
;
68 * Get the title of the time graph view the find dialog will use
70 * @return The view title
72 protected abstract String
getViewTitle();
75 * Get the string that will be used for the search
79 protected abstract String
getFindText();
82 * Initialize the test and open the timegraph view and the find dialog
86 String title
= getViewTitle();
87 fViewBot
= fBot
.viewByTitle(title
);
91 TmfSignalManager
.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, START_TIME
));
92 fBot
.waitUntil(ConditionHelpers
.timeGraphIsReadyCondition((AbstractTimeGraphView
) fViewBot
.getViewReference().getPart(false), new TmfTimeRange(START_TIME
, START_TIME
), START_TIME
));
95 fFindText
= getFindText();
99 * After class method to close the dialog
102 public static void afterTest() {
103 closeDialog(getDialogBot());
106 private static void openDialog(SWTBotView view
) {
108 SWTBotUtils
.pressShortcutGoToTreeTop(KEYBOARD
);
109 if (SWTUtils
.isMac()) {
110 KEYBOARD
.pressShortcut(Keystrokes
.COMMAND
, KeyStroke
.getInstance('F'));
112 KEYBOARD
.pressShortcut(Keystrokes
.CTRL
, KeyStroke
.getInstance('F'));
114 fBot
.waitUntil(Conditions
.shellIsActive(DIALOG_TITLE
));
118 * Test wrap search option
121 public void testWrapSearch() {
122 SWTBot bot
= getDialogBot();
123 SWTBotButton findButton
= bot
.button("Find");
125 SearchOptions options
= getOptions(false, false, true, false, false);
126 search(fFindText
, options
, findButton
, bot
);
127 assertTrue(isWrapped(bot
));
128 verifySelection(fFindText
, options
, fViewBot
, isWrapped(bot
));
129 options
= getOptions(true, false, true, false, false);
130 search(fFindText
, options
, findButton
, bot
);
131 assertTrue(isWrapped(bot
));
135 * Test the direction search option
138 public void testDirection() {
139 SWTBot bot
= getDialogBot();
140 SWTBotButton findButton
= bot
.button("Find");
143 testDirectionSearch(true, fFindText
, bot
, findButton
, fViewBot
);
144 testDirectionSearch(false, fFindText
, bot
, findButton
, fViewBot
);
147 private void testDirectionSearch(boolean forward
, String findText
, SWTBot bot
, SWTBotButton findButton
, SWTBotView view
) {
148 SearchOptions options
= getOptions(forward
, false, true, false, false);
149 fSelectionIndex
= getSelectionIndex(view
);
150 search(findText
, options
, findButton
, bot
);
151 verifySelection(findText
, options
, view
, isWrapped(bot
));
155 * Test the case sensitive search option
158 public void testCaseSensitive() {
159 SWTBot bot
= getDialogBot();
160 SWTBotButton findButton
= bot
.button("Find");
162 SearchOptions options
= getOptions(true, true, false, false, false);
163 search(fFindText
, options
, findButton
, bot
);
164 verifyStatusLabel(bot
, true);
165 search(fFindText
.toLowerCase(), options
, findButton
, bot
);
166 verifyStatusLabel(bot
, false);
170 * Test the whole word search option
173 public void testWholeWord() {
174 SWTBot bot
= getDialogBot();
175 SWTBotButton findButton
= bot
.button("Find");
178 String text
= fFindText
.split(SPACE
)[0];
179 System
.out
.println("Reg ex : " + text
);
180 SearchOptions options
= getOptions(true, false, false, true, false);
181 search(text
, options
, findButton
, bot
);
182 verifyStatusLabel(bot
, true);
183 search(text
.substring(0, text
.length() - 1), options
, findButton
, bot
);
184 verifyStatusLabel(bot
, false);
188 * Test the regular expression search option
191 public void testRegEx() {
192 SWTBot bot
= getDialogBot();
193 SWTBotButton findButton
= bot
.button("Find");
195 final String text
= REGEX_PREFIX
+ fFindText
.split(SPACE
)[0];
196 System
.out
.println("Reg ex : " + text
);
197 SearchOptions options
= getOptions(true, false, false, false, true);
198 search(text
, options
, findButton
, bot
);
199 verifyStatusLabel(bot
, true);
200 options
= getOptions(true, false, false, false, false);
201 search(text
, options
, findButton
, bot
);
202 verifyStatusLabel(bot
, false);
206 * Test open/close the find dialog
209 public void testOpenCloseDialog() {
210 SWTBotShell shell
= getDialogShell();
211 closeDialog(getDialogBot());
212 fBot
.waitUntil(Conditions
.shellCloses(shell
));
213 openDialog(fViewBot
);
216 private static void verifyStatusLabel(SWTBot bot
, boolean shouldBeFound
) {
217 // Get the second label in the dialog
218 SWTBotLabel statusLabel
= bot
.label(1);
219 assertTrue("status label", shouldBeFound
== !statusLabel
.getText().equals("Entry not found"));
223 * Get the find dialog bot
227 private static SWTBot
getDialogBot() {
228 return getDialogShell().bot();
232 * Get the find dialog shell bot
234 * @return The shell bot
236 private static SWTBotShell
getDialogShell() {
237 return fBot
.shell(DIALOG_TITLE
);
240 private static void closeDialog(SWTBot bot
) {
241 bot
.button("Close").click();
244 private static void search(String findText
, SearchOptions options
, SWTBotButton findButton
, SWTBot bot
) {
245 // set the text to search
246 SWTBotCombo findFieldCombo
= bot
.comboBox();
247 findFieldCombo
.setText(findText
);
248 assertTrue("Find combo", findFieldCombo
.getText().equals(findText
));
251 SWTBotRadio directions
= options
.forwardSearch ? bot
.radio("Forward").click() : bot
.radio("Backward").click();
252 assertTrue("direction", directions
.isSelected());
254 setCheckButton("Case sensitive", options
.caseSensitive
, bot
);
255 setCheckButton("Wrap search", options
.wrapSearch
, bot
);
256 setCheckButton("Whole word", options
.wholeWord
, bot
);
257 setCheckButton("Regular expression", options
.regExSearch
, bot
);
262 private SearchOptions
getOptions(boolean forward
, boolean caseSensitive
, boolean wrapSearch
, boolean wholeWord
, boolean regEx
) {
263 SearchOptions options
= new SearchOptions();
264 options
.forwardSearch
= forward
;
265 options
.caseSensitive
= caseSensitive
;
266 options
.wrapSearch
= wrapSearch
;
267 options
.wholeWord
= wholeWord
;
268 options
.regExSearch
= regEx
;
272 private static void setCheckButton(String mnemonic
, boolean option
, SWTBot bot
) {
273 final SWTBotCheckBox checkBox
= bot
.checkBox(mnemonic
);
274 if (checkBox
.isEnabled()) {
283 private static void verifySelection(String name
, SearchOptions options
, SWTBotView view
, boolean isWrapped
) {
284 final String entryName
= getTimegraphSelectionName(view
);
285 assertTrue("entry name", entryName
!= null && entryName
.contains(name
));
287 final int selectionIndex
= getSelectionIndex(view
);
289 assertTrue("selection index", options
.forwardSearch ? selectionIndex
> fSelectionIndex
: selectionIndex
< fSelectionIndex
);
291 assertTrue("selection index", options
.forwardSearch ? selectionIndex
<= fSelectionIndex
: selectionIndex
>= fSelectionIndex
);
293 fSelectionIndex
= selectionIndex
;
296 private static boolean isWrapped(final SWTBot bot
) {
297 return bot
.label(1).getText().equals("Wrapped search");
301 * Get the timegraph view selected entry
304 * The timegraph view bot
305 * @return The selected entry
307 private static String
getTimegraphSelectionName(final SWTBotView view
) {
308 final TimeGraphControl timegraph
= view
.bot().widget(WidgetOfType
.widgetOfType(TimeGraphControl
.class));
309 return UIThreadRunnable
.syncExec(() -> {
310 ISelection selection
= timegraph
.getSelectionTrace();
311 if (selection
instanceof TimeGraphSelection
) {
312 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
313 ITimeGraphEntry entry
= (ITimeGraphEntry
) sel
.getFirstElement();
314 return entry
.getName();
321 * Get the index of the entry selected in the timegraph view
324 * The timegraph view bot
327 private static Integer
getSelectionIndex(SWTBotView viewBot
) {
328 final TimeGraphControl timegraph
= viewBot
.bot().widget(WidgetOfType
.widgetOfType(TimeGraphControl
.class));
329 return UIThreadRunnable
.syncExec(() -> {
330 return timegraph
.getSelectedIndex();
334 private class SearchOptions
{
335 boolean forwardSearch
;
336 boolean caseSensitive
;