| 1 | /****************************************************************************** |
| 2 | * Copyright (c) 2015 Ericsson |
| 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 | |
| 10 | package org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests; |
| 11 | |
| 12 | import java.io.File; |
| 13 | import java.util.Arrays; |
| 14 | |
| 15 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
| 16 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; |
| 17 | import org.eclipse.swtbot.swt.finder.SWTBot; |
| 18 | import org.eclipse.swtbot.swt.finder.waits.Conditions; |
| 19 | import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
| 20 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; |
| 21 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo; |
| 22 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio; |
| 23 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; |
| 24 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem; |
| 25 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; |
| 26 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
| 27 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 28 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizardPage; |
| 29 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages; |
| 30 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 31 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; |
| 32 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; |
| 33 | |
| 34 | /** |
| 35 | * Several SWTBot utility methods common to testing import test cases. |
| 36 | */ |
| 37 | public final class SWTBotImportWizardUtils { |
| 38 | |
| 39 | /** |
| 40 | * While in the import wizard, select the specified directory as a source. |
| 41 | * |
| 42 | * @param bot |
| 43 | * the SWTBot |
| 44 | * @param directoryPath |
| 45 | * the directory path to set as a source |
| 46 | */ |
| 47 | public static void selectImportFromDirectory(SWTBot bot, String directoryPath) { |
| 48 | SWTBotRadio button = bot.radio("Select roo&t directory:"); |
| 49 | button.click(); |
| 50 | |
| 51 | SWTBotCombo sourceCombo = bot.comboBox(); |
| 52 | File traceFolderParent = new File(directoryPath); |
| 53 | sourceCombo.setText(traceFolderParent.getAbsolutePath()); |
| 54 | |
| 55 | SWTBotText text = bot.text(); |
| 56 | text.setFocus(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * While in the import wizard, select the specified archive as a source. |
| 61 | * |
| 62 | * @param bot |
| 63 | * the SWTBot |
| 64 | * @param archivePath |
| 65 | * the archive path to set as a source |
| 66 | */ |
| 67 | public static void selectImportFromArchive(SWTBot bot, String archivePath) { |
| 68 | SWTBotRadio button = bot.radio("Select &archive file:"); |
| 69 | button.click(); |
| 70 | |
| 71 | SWTBotCombo sourceCombo = bot.comboBox(1); |
| 72 | |
| 73 | sourceCombo.setText(new File(archivePath).getAbsolutePath()); |
| 74 | |
| 75 | SWTBotText text = bot.text(); |
| 76 | text.setFocus(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * While in the import wizard, select an item in the file selection control. |
| 81 | * The item can be a folder in the left tree or a file in the right table. |
| 82 | * The item will be checked. |
| 83 | * |
| 84 | * @param bot |
| 85 | * the SWTBot |
| 86 | * @param treePath |
| 87 | * the path to the item, ending with a file or folder |
| 88 | */ |
| 89 | public static void selectItem(SWTBot bot, String... treePath) { |
| 90 | SWTBotTree tree = bot.tree(); |
| 91 | bot.waitUntil(Conditions.widgetIsEnabled(tree)); |
| 92 | if (treePath.length == 0) { |
| 93 | return; |
| 94 | } |
| 95 | if (treePath.length == 1) { |
| 96 | SWTBotTreeItem rootNode = SWTBotUtils.getTreeItem(bot, tree, treePath); |
| 97 | rootNode.select(); |
| 98 | rootNode.check(); |
| 99 | return; |
| 100 | } |
| 101 | String[] parentPath = Arrays.copyOf(treePath, treePath.length - 1); |
| 102 | String itemName = treePath[treePath.length - 1]; |
| 103 | SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(bot, tree, parentPath); |
| 104 | folderNode.expand(); |
| 105 | if (folderNode.getNodes().contains(itemName)) { |
| 106 | folderNode = folderNode.getNode(itemName); |
| 107 | folderNode.select(); |
| 108 | folderNode.check(); |
| 109 | } else { |
| 110 | folderNode.select(); |
| 111 | SWTBotTable fileTable = bot.table(); |
| 112 | bot.waitUntil(Conditions.widgetIsEnabled(fileTable)); |
| 113 | bot.waitUntil(ConditionHelpers.isTableItemAvailable(itemName, fileTable)); |
| 114 | SWTBotTableItem tableItem = fileTable.getTableItem(itemName); |
| 115 | tableItem.check(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * While in the import wizard, select a folder in the file selection tree. |
| 121 | * |
| 122 | * @param bot |
| 123 | * the SWTBot |
| 124 | * @param check |
| 125 | * whether or not to check the folder item |
| 126 | * @param treePath |
| 127 | * the path to the folder in the tree |
| 128 | */ |
| 129 | public static void selectFolder(SWTBot bot, boolean check, String... treePath) { |
| 130 | SWTBotTree tree = bot.tree(); |
| 131 | bot.waitUntil(Conditions.widgetIsEnabled(tree)); |
| 132 | SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(bot, tree, treePath); |
| 133 | folderNode.select(); |
| 134 | if (check) { |
| 135 | folderNode.check(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * While in the import wizard, select a file in the file selection tree. |
| 141 | * |
| 142 | * @param bot |
| 143 | * the SWTBot |
| 144 | * @param fileName |
| 145 | * the name of the file to select |
| 146 | * @param folderTreePath |
| 147 | * the path to the parent folder in the tree |
| 148 | */ |
| 149 | public static void selectFile(SWTBot bot, String fileName, String... folderTreePath) { |
| 150 | selectFolder(bot, false, folderTreePath); |
| 151 | |
| 152 | SWTBotTable fileTable = bot.table(); |
| 153 | bot.waitUntil(Conditions.widgetIsEnabled(fileTable)); |
| 154 | bot.waitUntil(ConditionHelpers.isTableItemAvailable(fileName, fileTable)); |
| 155 | SWTBotTableItem tableItem = fileTable.getTableItem(fileName); |
| 156 | tableItem.check(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * While in the import wizard, set the various options (checkboxes, trace |
| 161 | * type combo). |
| 162 | * |
| 163 | * @param bot |
| 164 | * the SWTBot |
| 165 | * @param optionFlags |
| 166 | * options that affects mostly checkboxes, see |
| 167 | * {@link ImportTraceWizardPage#OPTION_CREATE_LINKS_IN_WORKSPACE} |
| 168 | * for example. |
| 169 | * @param traceTypeName |
| 170 | * the trace type to select in the combobox, or null for |
| 171 | * auto-detect. |
| 172 | */ |
| 173 | public static void setOptions(SWTBot bot, int optionFlags, String traceTypeName) { |
| 174 | SWTBotCombo comboBox = bot.comboBoxWithLabel(Messages.ImportTraceWizard_TraceType); |
| 175 | if (traceTypeName != null && !traceTypeName.isEmpty()) { |
| 176 | comboBox.setSelection(traceTypeName); |
| 177 | } else { |
| 178 | comboBox.setSelection(ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 179 | } |
| 180 | |
| 181 | SWTBotCheckBox checkBox = bot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace); |
| 182 | if (checkBox.isEnabled()) { |
| 183 | if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0) { |
| 184 | checkBox.select(); |
| 185 | } else { |
| 186 | checkBox.deselect(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | checkBox = bot.checkBox(Messages.ImportTraceWizard_PreserveFolderStructure); |
| 191 | if ((optionFlags & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) { |
| 192 | checkBox.select(); |
| 193 | } else { |
| 194 | checkBox.deselect(); |
| 195 | } |
| 196 | |
| 197 | checkBox = bot.checkBox(Messages.ImportTraceWizard_ImportUnrecognized); |
| 198 | if (checkBox.isEnabled()) { |
| 199 | if ((optionFlags & ImportTraceWizardPage.OPTION_IMPORT_UNRECOGNIZED_TRACES) != 0) { |
| 200 | checkBox.select(); |
| 201 | } else { |
| 202 | checkBox.deselect(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | checkBox = bot.checkBox(Messages.ImportTraceWizard_OverwriteExistingTrace); |
| 207 | if ((optionFlags & ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES) != 0) { |
| 208 | checkBox.select(); |
| 209 | } else { |
| 210 | checkBox.deselect(); |
| 211 | } |
| 212 | |
| 213 | checkBox = bot.checkBox(Messages.ImportTraceWizard_CreateExperiment); |
| 214 | if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT) != 0) { |
| 215 | checkBox.select(); |
| 216 | } else { |
| 217 | checkBox.deselect(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Test that the events editor gets activated and that the first event is |
| 223 | * selectable. |
| 224 | * |
| 225 | * @param bot |
| 226 | * the SWTBot |
| 227 | * @param editorName |
| 228 | * the expected name of the editor |
| 229 | * @param nbEvents |
| 230 | * the expected number of events |
| 231 | * @param firstEventStr |
| 232 | * the first event timestamp in string form. This is used to see |
| 233 | * if the cell contains this text (String.contains()). Since |
| 234 | * there can be timezone issues with hours and days, this value |
| 235 | * should only specify minutes and more precise digits. For |
| 236 | * example: 04:32.650 993 664 |
| 237 | */ |
| 238 | public static void testEventsTable(SWTWorkbenchBot bot, String editorName, long nbEvents, String firstEventStr) { |
| 239 | SWTBotEditor editor = SWTBotUtils.activeEventsEditor(bot, editorName); |
| 240 | bot.waitUntil(ConditionHelpers.numberOfEventsInTrace(TmfTraceManager.getInstance().getActiveTrace(), nbEvents)); |
| 241 | |
| 242 | if (nbEvents == 0 || firstEventStr == null || firstEventStr.isEmpty()) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | SWTBotTable table = editor.bot().table(); |
| 247 | bot.waitUntil(new DefaultCondition() { |
| 248 | @Override |
| 249 | public boolean test() throws Exception { |
| 250 | return table.rowCount() > 1; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public String getFailureMessage() { |
| 255 | return "No items in table"; |
| 256 | } |
| 257 | }); |
| 258 | // Select first event (skip filter/search row) |
| 259 | table.getTableItem(1).select(); |
| 260 | |
| 261 | editor.bot().waitUntil(new DefaultCondition() { |
| 262 | @Override |
| 263 | public boolean test() throws Exception { |
| 264 | boolean ret = table.selection().rowCount() == 1 && table.selection().get(0).toString().contains(firstEventStr); |
| 265 | if (!ret) { |
| 266 | // FIXME: Not sure why, sometimes the first select() ends up |
| 267 | // selecting an empty item. Retry selecting here. |
| 268 | table.getTableItem(1).select(); |
| 269 | } |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public String getFailureMessage() { |
| 275 | return "First event not selected."; |
| 276 | } |
| 277 | }); |
| 278 | } |
| 279 | } |