1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include <base_dialogs/GTFileDialog.h>
23 #include <base_dialogs/MessageBoxFiller.h>
24 #include <drivers/GTKeyboardDriver.h>
25 #include <drivers/GTMouseDriver.h>
26 #include <primitives/GTAction.h>
27 #include <primitives/GTCheckBox.h>
28 #include <primitives/GTComboBox.h>
29 #include <primitives/GTDoubleSpinBox.h>
30 #include <primitives/GTGroupBox.h>
31 #include <primitives/GTLineEdit.h>
32 #include <primitives/GTMenu.h>
33 #include <primitives/GTScrollBar.h>
34 #include <primitives/GTSpinBox.h>
35 #include <primitives/GTTabWidget.h>
36 #include <primitives/GTTableView.h>
37 #include <primitives/GTToolbar.h>
38 #include <primitives/GTTreeWidget.h>
39 #include <primitives/GTWidget.h>
40 #include <primitives/PopupChooser.h>
41 #include <utils/GTThread.h>
42 
43 #include <QApplication>
44 #include <QDialogButtonBox>
45 #include <QFileInfo>
46 #include <QGraphicsView>
47 #include <QGroupBox>
48 #include <QListWidget>
49 #include <QMainWindow>
50 #include <QMessageBox>
51 #include <QSpinBox>
52 #include <QStandardItemModel>
53 #include <QTableView>
54 #include <QTableWidget>
55 #include <QTextEdit>
56 #include <QToolButton>
57 #include <QTreeWidget>
58 
59 #include <U2Core/AppContext.h>
60 #include <U2Core/U2SafePoints.h>
61 
62 #include <U2View/MSAEditor.h>
63 
64 #include "../../workflow_designer/src/WorkflowViewItems.h"
65 #include "GTUtilsMdi.h"
66 #include "GTUtilsWorkflowDesigner.h"
67 #include "api/GTGraphicsItem.h"
68 #include "runnables/ugene/corelibs/U2Gui/AppSettingsDialogFiller.h"
69 #include "runnables/ugene/plugins/workflow_designer/DatasetNameEditDialogFiller.h"
70 #include "runnables/ugene/plugins/workflow_designer/StartupDialogFiller.h"
71 #include "runnables/ugene/plugins/workflow_designer/WorkflowMetadialogFiller.h"
72 
73 namespace U2 {
74 using namespace HI;
75 
76 const int GTUtilsWorkflowDesigner::verticalShift = 35;
77 #define GT_CLASS_NAME "GTUtilsWorkflowDesigner"
78 
79 #define GT_METHOD_NAME "getActiveWorkflowDesignerWindow"
getActiveWorkflowDesignerWindow(HI::GUITestOpStatus & os)80 QWidget *GTUtilsWorkflowDesigner::getActiveWorkflowDesignerWindow(HI::GUITestOpStatus &os) {
81     QWidget *wdWindow = nullptr;
82     for (int time = 0; time < GT_OP_WAIT_MILLIS && wdWindow == nullptr; time += GT_OP_CHECK_MILLIS) {
83         GTGlobals::sleep(time > 0 ? GT_OP_CHECK_MILLIS : 0);
84         MainWindow *mainWindow = AppContext::getMainWindow();
85         QWidget *mdiWindow = mainWindow == nullptr ? nullptr : mainWindow->getMDIManager()->getActiveWindow();
86         if (mdiWindow != nullptr && mdiWindow->objectName() == "Workflow Designer") {
87             wdWindow = mdiWindow;
88         }
89     }
90     GT_CHECK_RESULT(wdWindow != nullptr, "No active WD window!", nullptr);
91     GTThread::waitForMainThread();
92     return wdWindow;
93 }
94 #undef GT_METHOD_NAME
95 
96 #define GT_METHOD_NAME "checkWorkflowDesignerWindowIsActive"
checkWorkflowDesignerWindowIsActive(HI::GUITestOpStatus & os)97 void GTUtilsWorkflowDesigner::checkWorkflowDesignerWindowIsActive(HI::GUITestOpStatus &os) {
98     getActiveWorkflowDesignerWindow(os);
99 }
100 #undef GT_METHOD_NAME
101 
102 #define GT_METHOD_NAME "openWorkflowDesigner"
openWorkflowDesigner(HI::GUITestOpStatus & os)103 void GTUtilsWorkflowDesigner::openWorkflowDesigner(HI::GUITestOpStatus &os) {
104     StartupDialogFiller *filler = new StartupDialogFiller(os);
105     GTUtilsDialog::waitForDialogWhichMayRunOrNot(os, filler);
106     GTMenu::clickMainMenuItem(os, QStringList() << "Tools"
107                                                 << "Workflow Designer...");
108     checkWorkflowDesignerWindowIsActive(os);
109     GTUtilsDialog::removeRunnable(filler);
110 }
111 #undef GT_METHOD_NAME
112 
113 #define GT_METHOD_NAME "currentTab"
currentTab(HI::GUITestOpStatus & os)114 GTUtilsWorkflowDesigner::tab GTUtilsWorkflowDesigner::currentTab(HI::GUITestOpStatus &os) {
115     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
116     QTabWidget *tabs = qobject_cast<QTabWidget *>(GTWidget::findWidget(os, "tabs", wdWindow));
117     GT_CHECK_RESULT(nullptr != tabs, "tabs widget is not found", algorithms);
118     return tab(tabs->currentIndex());
119 }
120 #undef GT_METHOD_NAME
121 
122 #define GT_METHOD_NAME "setCurrentTab"
setCurrentTab(HI::GUITestOpStatus & os,tab t)123 void GTUtilsWorkflowDesigner::setCurrentTab(HI::GUITestOpStatus &os, tab t) {
124     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
125     QTabWidget *tabs = qobject_cast<QTabWidget *>(GTWidget::findWidget(os, "tabs", wdWindow));
126     GT_CHECK(nullptr != tabs, "tabs widget is not found");
127     GTTabWidget::setCurrentIndex(os, tabs, int(t));
128 }
129 #undef GT_METHOD_NAME
130 
131 #define GT_METHOD_NAME "loadWorkflow"
loadWorkflow(HI::GUITestOpStatus & os,const QString & fileUrl)132 void GTUtilsWorkflowDesigner::loadWorkflow(HI::GUITestOpStatus &os, const QString &fileUrl) {
133     GTUtilsDialog::waitForDialog(os, new GTFileDialogUtils(os, fileUrl));
134     QToolBar *wdToolbar = GTToolbar::getToolbar(os, "mwtoolbar_activemdi");
135     GT_CHECK(wdToolbar, "Toolbar is not found");
136     QWidget *loadButton = GTToolbar::getWidgetForActionObjectName(os, wdToolbar, "Load workflow");
137     GT_CHECK(loadButton, "Load button is not found");
138     GTWidget::click(os, loadButton);
139     GTGlobals::sleep();
140 }
141 #undef GT_METHOD_NAME
142 
143 #define GT_METHOD_NAME "saveWorkflow"
saveWorkflow(HI::GUITestOpStatus & os)144 void GTUtilsWorkflowDesigner::saveWorkflow(HI::GUITestOpStatus &os) {
145     GTToolbar::clickButtonByTooltipOnToolbar(os, MWTOOLBAR_ACTIVEMDI, "Save workflow");
146 }
147 #undef GT_METHOD_NAME
148 
149 #define GT_METHOD_NAME "saveWorkflowAs"
saveWorkflowAs(HI::GUITestOpStatus & os,const QString & fileUrl,const QString & workflowName)150 void GTUtilsWorkflowDesigner::saveWorkflowAs(HI::GUITestOpStatus &os, const QString &fileUrl, const QString &workflowName) {
151     GTUtilsDialog::waitForDialog(os, new WorkflowMetaDialogFiller(os, fileUrl, workflowName));
152     GTToolbar::clickButtonByTooltipOnToolbar(os, MWTOOLBAR_ACTIVEMDI, "Save workflow as");
153 }
154 #undef GT_METHOD_NAME
155 
156 #define GT_METHOD_NAME "validateWorkflow"
validateWorkflow(GUITestOpStatus & os)157 void GTUtilsWorkflowDesigner::validateWorkflow(GUITestOpStatus &os) {
158     GTWidget::click(os, GTAction::button(os, "Validate workflow"));
159 }
160 #undef GT_METHOD_NAME
161 
162 #define GT_METHOD_NAME "runWorkflow"
runWorkflow(HI::GUITestOpStatus & os)163 void GTUtilsWorkflowDesigner::runWorkflow(HI::GUITestOpStatus &os) {
164     GTWidget::click(os, GTAction::button(os, "Run workflow", GTUtilsMdi::activeWindow(os)));
165 }
166 #undef GT_METHOD_NAME
167 
168 #define GT_METHOD_NAME "stopWorkflow"
stopWorkflow(HI::GUITestOpStatus & os)169 void GTUtilsWorkflowDesigner::stopWorkflow(HI::GUITestOpStatus &os) {
170     GTWidget::click(os, GTAction::button(os, "Stop workflow", GTUtilsMdi::activeWindow(os)));
171 }
172 #undef GT_METHOD_NAME
173 
174 #define GT_METHOD_NAME "returnToWorkflow"
returnToWorkflow(HI::GUITestOpStatus & os)175 void GTUtilsWorkflowDesigner::returnToWorkflow(HI::GUITestOpStatus &os) {
176     GTWidget::click(os, GTAction::button(os, GTAction::findActionByText(os, "To Workflow Designer")));
177 }
178 #undef GT_METHOD_NAME
179 
compare(QString s1,QString s2,bool isExactMatch)180 static bool compare(QString s1, QString s2, bool isExactMatch) {
181     return isExactMatch ? s1 == s2 : s1.toLower().contains(s2.toLower());
182 }
183 
184 #define GT_METHOD_NAME "findTreeItem"
findTreeItem(HI::GUITestOpStatus & os,const QString & itemName,tab t,bool exactMatch,bool failIfNULL)185 QTreeWidgetItem *GTUtilsWorkflowDesigner::findTreeItem(HI::GUITestOpStatus &os, const QString &itemName, tab t, bool exactMatch, bool failIfNULL) {
186     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
187     QTreeWidgetItem *foundItem = nullptr;
188     QTreeWidget *w;
189     if (t == algorithms) {
190         w = qobject_cast<QTreeWidget *>(GTWidget::findWidget(os, "WorkflowPaletteElements", wdWindow));
191     } else {
192         w = qobject_cast<QTreeWidget *>(GTWidget::findWidget(os, "samples", wdWindow));
193     }
194     GT_CHECK_RESULT(w != nullptr, "WorkflowPaletteElements is null", nullptr);
195 
196     QList<QTreeWidgetItem *> outerList = w->findItems("", Qt::MatchContains);
197 
198     for (int i = 0; i < outerList.count(); i++) {
199         QList<QTreeWidgetItem *> innerList;
200 
201         for (int j = 0; j < outerList.value(i)->childCount(); j++) {
202             innerList.append(outerList.value(i)->child(j));
203         }
204 
205         foreach (QTreeWidgetItem *item, innerList) {
206             if (t == algorithms) {
207                 QString s = item->data(0, Qt::UserRole).value<QAction *>()->text();
208                 if (compare(s, itemName, exactMatch)) {
209                     GT_CHECK_RESULT(foundItem == nullptr, "several items have this discription", item);
210                     foundItem = item;
211                 }
212             } else {
213                 QString s = item->text(0);
214                 if (compare(s, itemName, exactMatch)) {
215                     GT_CHECK_RESULT(foundItem == nullptr, "several items have this discription", item);
216                     foundItem = item;
217                 }
218             }
219         }
220     }
221     if (failIfNULL) {
222         GT_CHECK_RESULT(foundItem != nullptr, "Item \"" + itemName + "\" not found in treeWidget", nullptr);
223     }
224     return foundItem;
225 }
226 #undef GT_METHOD_NAME
227 
228 #define GT_METHOD_NAME "getVisibleSamples"
getVisibleSamples(HI::GUITestOpStatus & os)229 QList<QTreeWidgetItem *> GTUtilsWorkflowDesigner::getVisibleSamples(HI::GUITestOpStatus &os) {
230     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
231     QTreeWidget *w = qobject_cast<QTreeWidget *>(GTWidget::findWidget(os, "samples", wdWindow));
232     GT_CHECK_RESULT(w != nullptr, "WorkflowPaletteElements is null", QList<QTreeWidgetItem *>());
233 
234     QList<QTreeWidgetItem *> outerList = w->findItems("", Qt::MatchContains);
235     QList<QTreeWidgetItem *> resultList;
236     for (int i = 0; i < outerList.count(); i++) {
237         QList<QTreeWidgetItem *> innerList;
238 
239         for (int j = 0; j < outerList.value(i)->childCount(); j++) {
240             innerList.append(outerList.value(i)->child(j));
241         }
242 
243         foreach (QTreeWidgetItem *item, innerList) {
244             if (!item->isHidden()) {
245                 resultList.append(item);
246             }
247         }
248     }
249     return resultList;
250 }
251 #undef GT_METHOD_NAME
252 
253 #define GT_METHOD_NAME "addAlgorithm"
addAlgorithm(HI::GUITestOpStatus & os,const QString & algName,bool exactMatch,bool useDragAndDrop)254 void GTUtilsWorkflowDesigner::addAlgorithm(HI::GUITestOpStatus &os, const QString &algName, bool exactMatch, bool useDragAndDrop) {
255     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
256     expandTabs(os);
257     QTabWidget *tabs = qobject_cast<QTabWidget *>(GTWidget::findWidget(os, "tabs", wdWindow));
258     GT_CHECK(tabs != nullptr, "tabs widget not found");
259 
260     GTTabWidget::setCurrentIndex(os, tabs, 0);
261 
262     QTreeWidgetItem *alg = findTreeItem(os, algName, algorithms, exactMatch);
263     GT_CHECK(alg != nullptr, "algorithm is NULL");
264 
265     selectAlgorithm(os, alg);
266     QWidget *w = GTWidget::findWidget(os, "sceneView", wdWindow);
267 
268     // Put the new worker in to the grid.
269     int columnWidth = 250;
270     int columnHeight = 250;
271     int workersPerRow = 3;
272 
273     int numberOfWorkers = getWorkers(os).size();
274     int currentWorkerRow = numberOfWorkers / workersPerRow;
275     int currentWorkerColumn = numberOfWorkers % workersPerRow;
276     QPoint newWorkerPosition(w->rect().topLeft() + QPoint(currentWorkerColumn * columnWidth, currentWorkerRow * columnHeight) + QPoint(100, 100));
277     if (useDragAndDrop) {
278         GTMouseDriver::dragAndDrop(GTMouseDriver::getMousePosition(), w->mapToGlobal(newWorkerPosition));
279     } else {
280         GTWidget::click(os, w, Qt::LeftButton, newWorkerPosition);
281     }
282     GTThread::waitForMainThread();
283 }
284 #undef GT_METHOD_NAME
285 
286 #define GT_METHOD_NAME "addElement"
addElement(HI::GUITestOpStatus & os,const QString & algName,bool exactMatch)287 WorkflowProcessItem *GTUtilsWorkflowDesigner::addElement(HI::GUITestOpStatus &os, const QString &algName, bool exactMatch) {
288     addAlgorithm(os, algName, exactMatch);
289     CHECK_OP(os, nullptr);
290     return getWorker(os, algName);
291 }
292 #undef GT_METHOD_NAME
293 
294 #define GT_METHOD_NAME "addElementByUsingNameFilter"
addElementByUsingNameFilter(HI::GUITestOpStatus & os,const QString & elementName,bool exactMatch)295 WorkflowProcessItem *GTUtilsWorkflowDesigner::addElementByUsingNameFilter(HI::GUITestOpStatus &os, const QString &elementName, bool exactMatch) {
296     GTUtilsWorkflowDesigner::findByNameFilter(os, elementName);
297     WorkflowProcessItem *item = GTUtilsWorkflowDesigner::addElement(os, elementName, exactMatch);
298     GTUtilsWorkflowDesigner::cleanNameFilter(os);
299     return item;
300 }
301 #undef GT_METHOD_NAME
302 
303 #define GT_METHOD_NAME "selectAlgorithm"
selectAlgorithm(HI::GUITestOpStatus & os,QTreeWidgetItem * algorithm)304 void GTUtilsWorkflowDesigner::selectAlgorithm(HI::GUITestOpStatus &os, QTreeWidgetItem *algorithm) {
305     GT_CHECK(algorithm != nullptr, "algorithm is nullptr");
306 
307     class MainThreadAction : public CustomScenario {
308     public:
309         MainThreadAction(QTreeWidgetItem *algorithm)
310             : CustomScenario(), algorithm(algorithm) {
311         }
312         void run(HI::GUITestOpStatus &os) {
313             Q_UNUSED(os);
314             algorithm->treeWidget()->scrollToItem(algorithm, QAbstractItemView::PositionAtCenter);
315         }
316         QTreeWidgetItem *algorithm;
317     };
318     GTThread::runInMainThread(os, new MainThreadAction(algorithm));
319     GTMouseDriver::moveTo(GTTreeWidget::getItemCenter(os, algorithm));
320 }
321 #undef GT_METHOD_NAME
322 
323 #define GT_METHOD_NAME "addSample"
addSample(HI::GUITestOpStatus & os,const QString & sampName,QWidget * parentWidget)324 void GTUtilsWorkflowDesigner::addSample(HI::GUITestOpStatus &os, const QString &sampName, QWidget *parentWidget) {
325     expandTabs(os, parentWidget);
326     QTabWidget *tabs = qobject_cast<QTabWidget *>(GTWidget::findWidget(os, "tabs", parentWidget));
327     GT_CHECK(tabs != nullptr, "tabs widget not found");
328 
329     GTTabWidget::setCurrentIndex(os, tabs, 1);
330 
331     QTreeWidgetItem *samp = findTreeItem(os, sampName, samples);
332     GT_CHECK(samp != nullptr, "sample is NULL");
333 
334     selectSample(os, samp, parentWidget);
335     GTGlobals::sleep(500);
336 }
337 #undef GT_METHOD_NAME
338 
339 #define GT_METHOD_NAME "selectSample"
selectSample(HI::GUITestOpStatus & os,QTreeWidgetItem * sample,QWidget * parentWidget)340 void GTUtilsWorkflowDesigner::selectSample(HI::GUITestOpStatus &os, QTreeWidgetItem *sample, QWidget *parentWidget) {
341     GT_CHECK(sample != nullptr, "sample is nullptr");
342     QTreeWidget *paletteTree = qobject_cast<QTreeWidget *>(GTWidget::findWidget(os, "samples", parentWidget));
343     GT_CHECK(paletteTree != nullptr, "paletteTree is nullptr");
344 
345     class MainThreadAction : public CustomScenario {
346     public:
347         MainThreadAction(QTreeWidget *paletteTree, QTreeWidgetItem *sample)
348             : CustomScenario(), paletteTree(paletteTree), sample(sample) {
349         }
350         void run(HI::GUITestOpStatus &os) {
351             Q_UNUSED(os);
352             paletteTree->scrollToItem(sample);
353         }
354         QTreeWidget *paletteTree;
355         QTreeWidgetItem *sample;
356     };
357     GTThread::runInMainThread(os, new MainThreadAction(paletteTree, sample));
358 
359     GTMouseDriver::moveTo(GTTreeWidget::getItemCenter(os, sample));
360     GTMouseDriver::doubleClick();
361     GTThread::waitForMainThread();
362 }
363 #undef GT_METHOD_NAME
364 
365 #define GT_METHOD_NAME "expandTabs"
expandTabs(HI::GUITestOpStatus & os,QWidget * parentWidget)366 void GTUtilsWorkflowDesigner::expandTabs(HI::GUITestOpStatus &os, QWidget *parentWidget) {
367     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
368     QSplitter *splitter = qobject_cast<QSplitter *>(GTWidget::findWidget(os, "WorkflowViewMainSplitter", parentWidget == nullptr ? wdWindow : parentWidget));
369     GT_CHECK(splitter, "splitter not found");
370     QList<int> s;
371     s = splitter->sizes();
372 
373     if (s.first() == 0) {  // expands tabs if collapsed
374         QPoint p;
375         p.setX(splitter->geometry().left() + 2);
376         p.setY(splitter->geometry().center().y());
377         GTMouseDriver::moveTo(p);
378         GTGlobals::sleep(300);
379         GTMouseDriver::press();
380         p.setX(p.x() + 200);
381         GTMouseDriver::moveTo(p);
382         GTMouseDriver::release();
383         GTThread::waitForMainThread();
384     }
385 }
386 #undef GT_METHOD_NAME
387 
388 #define GT_METHOD_NAME "findByNameFilter"
findByNameFilter(HI::GUITestOpStatus & os,const QString & elementName)389 void GTUtilsWorkflowDesigner::findByNameFilter(HI::GUITestOpStatus &os, const QString &elementName) {
390     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
391     QWidget *paletteWidget = GTWidget::findWidget(os, "palette", wdWindow);
392     QLineEdit *nameFilterLineEdit = qobject_cast<QLineEdit *>(GTWidget::findWidget(os, "nameFilterLineEdit", paletteWidget));
393     GT_CHECK(nameFilterLineEdit != nullptr, "Filter name line edit is not found");
394 
395     const QPoint mappedLineEditPos = nameFilterLineEdit->mapToGlobal(nameFilterLineEdit->pos());
396     const QPoint pos(mappedLineEditPos.x() + 75, mappedLineEditPos.y() + 10);
397     GTMouseDriver::moveTo(pos);
398     GTGlobals::sleep(500);
399     GTMouseDriver::click();
400     GTGlobals::sleep(100);
401     GTKeyboardDriver::keyClick(Qt::Key_Home);
402     GTGlobals::sleep(100);
403     GTKeyboardDriver::keyClick(Qt::Key_End, Qt::ShiftModifier);
404     GTGlobals::sleep(100);
405     GTKeyboardDriver::keyClick(Qt::Key_Backspace);
406     GTGlobals::sleep(500);
407     for (int i = 0; i < elementName.size(); i++) {
408         GTKeyboardDriver::keyClick(elementName[i].toLatin1());
409         GTGlobals::sleep(50);
410     }
411     GTGlobals::sleep(1000);
412 }
413 #undef GT_METHOD_NAME
414 
415 #define GT_METHOD_NAME "cleanNameFilter"
cleanNameFilter(HI::GUITestOpStatus & os)416 void GTUtilsWorkflowDesigner::cleanNameFilter(HI::GUITestOpStatus &os) {
417     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
418     QWidget *paletteWidget = GTWidget::findWidget(os, "palette", wdWindow);
419     QLineEdit *nameFilterLineEdit = qobject_cast<QLineEdit *>(GTWidget::findWidget(os, "nameFilterLineEdit", paletteWidget));
420     GT_CHECK(nameFilterLineEdit != nullptr, "Filter name line edit is not found");
421 
422     const QPoint mappedLineEditPos = nameFilterLineEdit->mapToGlobal(nameFilterLineEdit->pos());
423     const QPoint pos(mappedLineEditPos.x() + 75, mappedLineEditPos.y() + 10);
424     GTMouseDriver::moveTo(pos);
425     GTGlobals::sleep(500);
426     GTMouseDriver::click();
427     GTGlobals::sleep(100);
428     GTKeyboardDriver::keyClick(Qt::Key_Home);
429     GTGlobals::sleep(100);
430     GTKeyboardDriver::keyClick(Qt::Key_End, Qt::ShiftModifier);
431     GTGlobals::sleep(100);
432     GTKeyboardDriver::keyClick(Qt::Key_Backspace);
433     GTGlobals::sleep(1000);
434 }
435 #undef GT_METHOD_NAME
436 
437 #define GT_METHOD_NAME "clickOnPalette"
clickOnPalette(HI::GUITestOpStatus & os,const QString & itemName,Qt::MouseButton mouseButton)438 void GTUtilsWorkflowDesigner::clickOnPalette(HI::GUITestOpStatus &os, const QString &itemName, Qt::MouseButton mouseButton) {
439     selectAlgorithm(os, findTreeItem(os, itemName, algorithms, true));
440     GTMouseDriver::click(mouseButton);
441 }
442 #undef GT_METHOD_NAME
443 
444 #define GT_METHOD_NAME "getPaletteGroup"
getPaletteGroup(HI::GUITestOpStatus & os,const QString & groupName)445 QTreeWidgetItem *GTUtilsWorkflowDesigner::getPaletteGroup(HI::GUITestOpStatus &os, const QString &groupName) {
446     QTreeWidget *tree = getCurrentTabTreeWidget(os);
447     GT_CHECK_RESULT(nullptr != tree, "WorkflowPaletteElements is NULL", nullptr);
448 
449     GTGlobals::FindOptions options;
450     options.depth = 1;
451     options.matchPolicy = Qt::MatchExactly;
452 
453     return GTTreeWidget::findItem(os, tree, groupName, nullptr, 0, options);
454 }
455 #undef GT_METHOD_NAME
456 
457 #define GT_METHOD_NAME "getPaletteGroups"
getPaletteGroups(HI::GUITestOpStatus & os)458 QList<QTreeWidgetItem *> GTUtilsWorkflowDesigner::getPaletteGroups(HI::GUITestOpStatus &os) {
459     QList<QTreeWidgetItem *> groupItems;
460 
461     QTreeWidget *tree = getCurrentTabTreeWidget(os);
462     GT_CHECK_RESULT(nullptr != tree, "WorkflowPaletteElements is NULL", groupItems);
463 
464     GTGlobals::FindOptions options;
465     options.depth = 1;
466     options.matchPolicy = Qt::MatchContains;
467 
468     return GTTreeWidget::findItems(os, tree, "", nullptr, 0, options);
469 }
470 #undef GT_METHOD_NAME
471 
472 #define GT_METHOD_NAME "getPaletteGroupNames"
getPaletteGroupNames(HI::GUITestOpStatus & os)473 QStringList GTUtilsWorkflowDesigner::getPaletteGroupNames(HI::GUITestOpStatus &os) {
474     QStringList groupNames;
475     const QList<QTreeWidgetItem *> groupItems = getPaletteGroups(os);
476     foreach (QTreeWidgetItem *groupItem, groupItems) {
477         groupNames << groupItem->text(0);
478     }
479     return groupNames;
480 }
481 #undef GT_METHOD_NAME
482 
483 #define GT_METHOD_NAME "getPaletteGroupEntries"
getPaletteGroupEntries(HI::GUITestOpStatus & os,QTreeWidgetItem * groupItem)484 QList<QTreeWidgetItem *> GTUtilsWorkflowDesigner::getPaletteGroupEntries(HI::GUITestOpStatus &os, QTreeWidgetItem *groupItem) {
485     QList<QTreeWidgetItem *> items;
486 
487     GT_CHECK_RESULT(nullptr != groupItem, "Group item is NULL", items);
488 
489     QTreeWidget *tree = getCurrentTabTreeWidget(os);
490     GT_CHECK_RESULT(nullptr != tree, "WorkflowPaletteElements is NULL", items);
491 
492     GTGlobals::FindOptions options;
493     options.depth = 0;
494     options.matchPolicy = Qt::MatchContains;
495 
496     return GTTreeWidget::findItems(os, tree, "", groupItem, 0, options);
497 }
498 #undef GT_METHOD_NAME
499 
500 #define GT_METHOD_NAME "getPaletteGroupEntries"
getPaletteGroupEntries(HI::GUITestOpStatus & os,const QString & groupName)501 QList<QTreeWidgetItem *> GTUtilsWorkflowDesigner::getPaletteGroupEntries(HI::GUITestOpStatus &os, const QString &groupName) {
502     return getPaletteGroupEntries(os, getPaletteGroup(os, groupName));
503 }
504 #undef GT_METHOD_NAME
505 
506 #define GT_METHOD_NAME "getPaletteGroupEntriesNames"
getPaletteGroupEntriesNames(GUITestOpStatus & os,const QString & groupName)507 QStringList GTUtilsWorkflowDesigner::getPaletteGroupEntriesNames(GUITestOpStatus &os, const QString &groupName) {
508     QStringList entriesNames;
509     foreach (QTreeWidgetItem *entryItem, getPaletteGroupEntries(os, groupName)) {
510         entriesNames << entryItem->text(0);
511     }
512     return entriesNames;
513 }
514 #undef GT_METHOD_NAME
515 
getItemCenter(HI::GUITestOpStatus & os,const QString & itemName)516 QPoint GTUtilsWorkflowDesigner::getItemCenter(HI::GUITestOpStatus &os, const QString &itemName) {
517     QRect r = getItemRect(os, itemName);
518     QPoint p = r.center();
519     return p;
520 }
521 
522 #define GT_METHOD_NAME "removeItem"
removeItem(HI::GUITestOpStatus & os,const QString & itemName)523 void GTUtilsWorkflowDesigner::removeItem(HI::GUITestOpStatus &os, const QString &itemName) {
524     click(os, itemName);
525     GTKeyboardDriver::keyClick(Qt::Key_Delete);
526     GTThread::waitForMainThread();
527 }
528 #undef GT_METHOD_NAME
529 
getItemLeft(HI::GUITestOpStatus & os,const QString & itemName)530 int GTUtilsWorkflowDesigner::getItemLeft(HI::GUITestOpStatus &os, const QString &itemName) {
531     QRect r = getItemRect(os, itemName);
532     int i = r.left();
533     return i;
534 }
535 
getItemRight(HI::GUITestOpStatus & os,const QString & itemName)536 int GTUtilsWorkflowDesigner::getItemRight(HI::GUITestOpStatus &os, const QString &itemName) {
537     QRect r = getItemRect(os, itemName);
538     int i = r.right();
539     return i;
540 }
541 
getItemTop(HI::GUITestOpStatus & os,const QString & itemName)542 int GTUtilsWorkflowDesigner::getItemTop(HI::GUITestOpStatus &os, const QString &itemName) {
543     QRect r = getItemRect(os, itemName);
544     int i = r.top();
545     return i;
546 }
547 
getItemBottom(HI::GUITestOpStatus & os,const QString & itemName)548 int GTUtilsWorkflowDesigner::getItemBottom(HI::GUITestOpStatus &os, const QString &itemName) {
549     QRect r = getItemRect(os, itemName);
550     int i = r.bottom();
551     return i;
552 }
553 #define GT_METHOD_NAME "click"
click(HI::GUITestOpStatus & os,const QString & itemName,QPoint p,Qt::MouseButton button)554 void GTUtilsWorkflowDesigner::click(HI::GUITestOpStatus &os, const QString &itemName, QPoint p, Qt::MouseButton button) {
555     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
556     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(GTWidget::findWidget(os, "sceneView", wdWindow));
557     GT_CHECK(sceneView != nullptr, "scene view is NULL");
558     sceneView->ensureVisible(getWorker(os, itemName));
559     GTThread::waitForMainThread();
560 
561     GTMouseDriver::moveTo(getItemCenter(os, itemName) + p);
562     GTMouseDriver::click();
563     if (Qt::RightButton == button) {
564         GTMouseDriver::click(Qt::RightButton);
565     }
566 }
567 #undef GT_METHOD_NAME
568 
569 #define GT_METHOD_NAME "click"
click(HI::GUITestOpStatus & os,QGraphicsItem * item,QPoint p,Qt::MouseButton button)570 void GTUtilsWorkflowDesigner::click(HI::GUITestOpStatus &os, QGraphicsItem *item, QPoint p, Qt::MouseButton button) {
571     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
572     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(GTWidget::findWidget(os, "sceneView", wdWindow));
573     GT_CHECK(sceneView != nullptr, "scene view is NULL");
574     sceneView->ensureVisible(item);
575     QRect rect = GTGraphicsItem::getGraphicsItemRect(os, item);
576 
577     GTMouseDriver::moveTo(rect.center() + p);
578     GTMouseDriver::click();
579     if (Qt::RightButton == button) {
580         GTMouseDriver::click(Qt::RightButton);
581     }
582     GTGlobals::sleep(200);
583 }
584 #undef GT_METHOD_NAME
585 
586 #define GT_METHOD_NAME "getWorker"
getWorker(HI::GUITestOpStatus & os,const QString & itemName,const GTGlobals::FindOptions & options)587 WorkflowProcessItem *GTUtilsWorkflowDesigner::getWorker(HI::GUITestOpStatus &os, const QString &itemName, const GTGlobals::FindOptions &options) {
588     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
589     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(GTWidget::findWidget(os, "sceneView", wdWindow));
590     GT_CHECK_RESULT(sceneView, "sceneView not found", nullptr);
591     // Wait for the item up to GT_OP_WAIT_MILLIS.
592     for (int time = 0; time < GT_OP_WAIT_MILLIS; time += GT_OP_CHECK_MILLIS) {
593         GTGlobals::sleep(time > 0 ? GT_OP_CHECK_MILLIS : 0);
594         QList<QGraphicsItem *> items = sceneView->items();
595         foreach (QGraphicsItem *item, items) {
596             QGraphicsObject *graphicsObject = item->toGraphicsObject();
597             QGraphicsTextItem *graphicsTextItem = qobject_cast<QGraphicsTextItem *>(graphicsObject);
598             if (graphicsTextItem != nullptr) {
599                 QString text = graphicsTextItem->toPlainText();
600                 int lineSeparatorIndex = text.indexOf('\n');
601                 if (lineSeparatorIndex == -1) {
602                     continue;
603                 }
604                 text = text.left(lineSeparatorIndex);
605                 if (text == itemName) {
606                     WorkflowProcessItem *result = qgraphicsitem_cast<WorkflowProcessItem *>(item->parentItem()->parentItem());
607                     if (result != nullptr) {
608                         return result;
609                     }
610                     break;
611                 }
612             }
613         }
614         if (!options.failIfNotFound) {
615             break;
616         }
617     }
618     GT_CHECK_RESULT(!options.failIfNotFound, "Item '" + itemName + "' is not found", nullptr);
619     return nullptr;
620 }
621 #undef GT_METHOD_NAME
622 
623 #define GT_METHOD_NAME "getWorkerText"
getWorkerText(HI::GUITestOpStatus & os,const QString & itemName,const GTGlobals::FindOptions & options)624 QString GTUtilsWorkflowDesigner::getWorkerText(HI::GUITestOpStatus &os, const QString &itemName, const GTGlobals::FindOptions &options) {
625     WorkflowProcessItem *worker = getWorker(os, itemName, options);
626     foreach (QGraphicsItem *child, worker->childItems()) {
627         foreach (QGraphicsItem *subchild, child->childItems()) {
628             QGraphicsObject *graphObject = subchild->toGraphicsObject();
629             QGraphicsTextItem *textItem = qobject_cast<QGraphicsTextItem *>(graphObject);
630             if (nullptr != textItem) {
631                 return textItem->toPlainText();
632             }
633         }
634     }
635     return QString();
636 }
637 #undef GT_METHOD_NAME
638 
clickLink(HI::GUITestOpStatus & os,const QString & itemName,Qt::MouseButton button,int step)639 void GTUtilsWorkflowDesigner::clickLink(HI::GUITestOpStatus &os, const QString &itemName, Qt::MouseButton button, int step) {
640     WorkflowProcessItem *worker = getWorker(os, itemName);
641 
642     int left = GTUtilsWorkflowDesigner::getItemLeft(os, itemName);
643     int right = GTUtilsWorkflowDesigner::getItemRight(os, itemName);
644     int top = GTUtilsWorkflowDesigner::getItemTop(os, itemName);
645     int bottom = GTUtilsWorkflowDesigner::getItemBottom(os, itemName);
646     for (int i = left; i < right; i += step) {
647         for (int j = top; j < bottom; j += step) {
648             GTMouseDriver::moveTo(QPoint(i, j));
649             if (worker->cursor().shape() == Qt::PointingHandCursor) {
650                 GTMouseDriver::click(button);
651                 return;
652             }
653         }
654     }
655 }
656 
657 #define GT_METHOD_NAME "isWorkerExtended"
isWorkerExtended(HI::GUITestOpStatus & os,const QString & itemName)658 bool GTUtilsWorkflowDesigner::isWorkerExtended(HI::GUITestOpStatus &os, const QString &itemName) {
659     return "ext" == getWorker(os, itemName)->getStyle();
660 }
661 #undef GT_METHOD_NAME
662 
663 #define GT_METHOD_NAME "getPortById"
getPortById(HI::GUITestOpStatus & os,WorkflowProcessItem * worker,QString id)664 WorkflowPortItem *GTUtilsWorkflowDesigner::getPortById(HI::GUITestOpStatus &os, WorkflowProcessItem *worker, QString id) {
665     QList<WorkflowPortItem *> list = getPorts(os, worker);
666     foreach (WorkflowPortItem *p, list) {
667         if (p && p->getPort()->getId() == id) {
668             return p;
669         }
670     }
671     GT_CHECK_RESULT(false, "port with id " + id + "not found", nullptr);
672 }
673 #undef GT_METHOD_NAME
674 
675 #define GT_METHOD_NAME "getPorts"
getPorts(HI::GUITestOpStatus & os,WorkflowProcessItem * worker)676 QList<WorkflowPortItem *> GTUtilsWorkflowDesigner::getPorts(HI::GUITestOpStatus &os, WorkflowProcessItem *worker) {
677     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
678     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(GTWidget::findWidget(os, "sceneView", wdWindow));
679     GT_CHECK_RESULT(sceneView, "sceneView not found", QList<WorkflowPortItem *>())
680     return worker->getPortItems();
681 }
682 #undef GT_METHOD_NAME
683 
684 #define GT_METHOD_NAME "getItemRect"
getItemRect(HI::GUITestOpStatus & os,const QString & itemName)685 QRect GTUtilsWorkflowDesigner::getItemRect(HI::GUITestOpStatus &os, const QString &itemName) {
686     // TODO: support finding items when there are several similar workers in scheme
687     WorkflowProcessItem *w = getWorker(os, itemName);
688     QRect result = GTGraphicsItem::getGraphicsItemRect(os, w);
689     result.setTop(result.top() + verticalShift);
690     return result;
691 }
692 #undef GT_METHOD_NAME
693 
694 #define GT_METHOD_NAME "getCurrentTabTreeWidget"
getCurrentTabTreeWidget(HI::GUITestOpStatus & os)695 QTreeWidget *GTUtilsWorkflowDesigner::getCurrentTabTreeWidget(HI::GUITestOpStatus &os) {
696     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
697     switch (currentTab(os)) {
698         case algorithms:
699             return GTWidget::findExactWidget<QTreeWidget *>(os, "WorkflowPaletteElements", wdWindow);
700         case samples:
701             return GTWidget::findExactWidget<QTreeWidget *>(os, "samples", wdWindow);
702         default:
703             os.setError("An unexpected current tab");
704             return nullptr;
705     }
706 }
707 #undef GT_METHOD_NAME
708 
709 #define GT_METHOD_NAME "toggleDebugMode"
toggleDebugMode(HI::GUITestOpStatus & os,bool enable)710 void GTUtilsWorkflowDesigner::toggleDebugMode(HI::GUITestOpStatus &os, bool enable) {
711     class DebugModeToggleScenario : public CustomScenario {
712     public:
713         DebugModeToggleScenario(bool enable)
714             : enable(enable) {
715         }
716 
717         void run(HI::GUITestOpStatus &os) {
718             QWidget *dialog = QApplication::activeModalWidget();
719             GT_CHECK(dialog, "activeModalWidget is NULL");
720 
721             GTTreeWidget::click(os, GTTreeWidget::findItem(os, GTWidget::findExactWidget<QTreeWidget *>(os, "tree"), "  Workflow Designer"));
722             GTCheckBox::setChecked(os, GTWidget::findExactWidget<QCheckBox *>(os, "debuggerBox"), enable);
723 
724             GTUtilsDialog::clickButtonBox(os, dialog, QDialogButtonBox::Ok);
725         }
726 
727     private:
728         bool enable;
729     };
730 
731     GTUtilsDialog::waitForDialog(os, new AppSettingsDialogFiller(os, new DebugModeToggleScenario(enable)));
732     GTMenu::clickMainMenuItem(os, QStringList() << "Settings"
733                                                 << "Preferences...");
734 }
735 #undef GT_METHOD_NAME
736 
737 #define GT_METHOD_NAME "toggleBreakpointManager"
toggleBreakpointManager(HI::GUITestOpStatus & os)738 void GTUtilsWorkflowDesigner::toggleBreakpointManager(HI::GUITestOpStatus &os) {
739     GTWidget::click(os, GTToolbar::getWidgetForActionTooltip(os, GTToolbar::getToolbar(os, MWTOOLBAR_ACTIVEMDI), "Show or hide breakpoint manager"));
740 }
741 #undef GT_METHOD_NAME
742 
743 #define GT_METHOD_NAME "setBreakpoint"
setBreakpoint(HI::GUITestOpStatus & os,const QString & itemName)744 void GTUtilsWorkflowDesigner::setBreakpoint(HI::GUITestOpStatus &os, const QString &itemName) {
745     click(os, itemName);
746     GTWidget::click(os, GTToolbar::getWidgetForActionTooltip(os, GTToolbar::getToolbar(os, MWTOOLBAR_ACTIVEMDI), "Break at element"));
747 }
748 #undef GT_METHOD_NAME
749 
750 #define GT_METHOD_NAME "getBreakpointList"
getBreakpointList(HI::GUITestOpStatus & os)751 QStringList GTUtilsWorkflowDesigner::getBreakpointList(HI::GUITestOpStatus &os) {
752     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
753     return GTTreeWidget::getItemNames(os, GTWidget::findExactWidget<QTreeWidget *>(os, "breakpoints list", wdWindow));
754 }
755 #undef GT_METHOD_NAME
756 
757 #define GT_METHOD_NAME "getAllConnectionArrows"
getAllConnectionArrows(HI::GUITestOpStatus & os)758 QList<WorkflowBusItem *> GTUtilsWorkflowDesigner::getAllConnectionArrows(HI::GUITestOpStatus &os) {
759     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
760     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(GTWidget::findWidget(os, "sceneView", wdWindow));
761     GT_CHECK_RESULT(sceneView, "sceneView not found", QList<WorkflowBusItem *>());
762 
763     QList<WorkflowBusItem *> result;
764 
765     foreach (QGraphicsItem *item, sceneView->items()) {
766         WorkflowBusItem *arrow = qgraphicsitem_cast<WorkflowBusItem *>(item);
767         if (arrow != nullptr) {
768             result.append(arrow);
769         }
770     };
771 
772     return result;
773 }
774 #undef GT_METHOD_NAME
775 
776 #define GT_METHOD_NAME "removeCmdlineWorkerFromPalette"
removeCmdlineWorkerFromPalette(HI::GUITestOpStatus & os,const QString & workerName)777 void GTUtilsWorkflowDesigner::removeCmdlineWorkerFromPalette(HI::GUITestOpStatus &os, const QString &workerName) {
778     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
779     QTabWidget *tabs = qobject_cast<QTabWidget *>(GTWidget::findWidget(os, "tabs", wdWindow));
780     GT_CHECK(tabs != nullptr, "tabs widget not found");
781 
782     GTTabWidget::setCurrentIndex(os, tabs, 0);
783 
784     QTreeWidget *w = qobject_cast<QTreeWidget *>(GTWidget::findWidget(os, "WorkflowPaletteElements", wdWindow));
785     GT_CHECK(w != nullptr, "WorkflowPaletteElements is null");
786 
787     QTreeWidgetItem *foundItem = nullptr;
788     QList<QTreeWidgetItem *> outerList = w->findItems("", Qt::MatchContains);
789     for (int i = 0; i < outerList.count(); i++) {
790         QList<QTreeWidgetItem *> innerList;
791 
792         for (int j = 0; j < outerList.value(i)->childCount(); j++) {
793             innerList.append(outerList.value(i)->child(j));
794         }
795 
796         foreach (QTreeWidgetItem *item, innerList) {
797             const QString s = item->data(0, Qt::UserRole).value<QAction *>()->text();
798             if (s == workerName) {
799                 foundItem = item;
800             }
801         }
802     }
803     if (foundItem != nullptr) {
804         GTUtilsDialog::waitForDialog(os, new PopupChooserByText(os, QStringList() << "Remove"));
805         GTUtilsDialog::waitForDialog(os, new MessageBoxDialogFiller(os, QMessageBox::Ok, "", "Remove element"));
806         GTUtilsWorkflowDesigner::clickOnPalette(os, workerName, Qt::RightButton);
807     }
808 }
809 #undef GT_METHOD_NAME
810 
811 #define GT_METHOD_NAME "increaseOutputPortBoxHeight"
changeInputPortBoxHeight(HI::GUITestOpStatus & os,const int offset)812 void GTUtilsWorkflowDesigner::changeInputPortBoxHeight(HI::GUITestOpStatus &os, const int offset) {
813     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
814     QTextEdit *doc = GTWidget::findExactWidget<QTextEdit *>(os, "doc", wdWindow);
815     GT_CHECK(doc != nullptr, "doc is not found");
816 
817     QGroupBox *paramBox = GTWidget::findExactWidget<QGroupBox *>(os, "paramBox", wdWindow);
818     GT_CHECK(paramBox != nullptr, "Param Box is not found");
819 
820     QGroupBox *inputPortBox = GTWidget::findExactWidget<QGroupBox *>(os, "inputPortBox", wdWindow);
821     GT_CHECK(paramBox != nullptr, "inputPortBox is not found");
822 
823     QPoint docGlobal = doc->mapToGlobal(doc->pos());
824     QPoint bottomDevidePos(docGlobal.x() + (inputPortBox->width() / 2), docGlobal.y() + doc->height() + paramBox->height() + inputPortBox->height() + 10);
825     QPoint newBottomDevidePos(bottomDevidePos.x(), bottomDevidePos.y() + offset);
826     GTMouseDriver::dragAndDrop(bottomDevidePos, newBottomDevidePos);
827     GTGlobals::sleep();
828 }
829 #undef GT_METHOD_NAME
830 
831 #define GT_METHOD_NAME "importCmdlineBasedElement"
importCmdlineBasedElement(GUITestOpStatus & os,const QString & path)832 void GTUtilsWorkflowDesigner::importCmdlineBasedElement(GUITestOpStatus &os, const QString &path) {
833     GTUtilsDialog::waitForDialog(os, new GTFileDialogUtils(os, path));
834     GTToolbar::clickButtonByTooltipOnToolbar(os, MWTOOLBAR_ACTIVEMDI, "Add element with external tool");
835     GTGlobals::sleep(500);
836 }
837 #undef GT_METHOD_NAME
838 
839 #define GT_METHOD_NAME "connect"
connect(HI::GUITestOpStatus & os,WorkflowProcessItem * from,WorkflowProcessItem * to)840 void GTUtilsWorkflowDesigner::connect(HI::GUITestOpStatus &os, WorkflowProcessItem *from, WorkflowProcessItem *to) {
841     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(from->scene()->views().at(0));
842     GT_CHECK(sceneView, "sceneView not found")
843     QList<WorkflowPortItem *> fromList = from->getPortItems();
844     QList<WorkflowPortItem *> toList = to->getPortItems();
845 
846     foreach (WorkflowPortItem *fromPort, fromList) {
847         foreach (WorkflowPortItem *toPort, toList) {
848             if (fromPort->getPort()->canBind(toPort->getPort())) {
849                 GTMouseDriver::moveTo(GTGraphicsItem::getItemCenter(os, fromPort));
850                 GTMouseDriver::press();
851                 GTMouseDriver::moveTo(GTGraphicsItem::getItemCenter(os, toPort));
852                 GTMouseDriver::release();
853                 GTGlobals::sleep(1000);
854                 return;
855             }
856         }
857     }
858 
859     GT_CHECK(false, "no suitable ports to connect");
860 }
861 #undef GT_METHOD_NAME
862 
863 #define GT_METHOD_NAME "disconnect"
disconect(HI::GUITestOpStatus & os,WorkflowProcessItem * from,WorkflowProcessItem * to)864 void GTUtilsWorkflowDesigner::disconect(HI::GUITestOpStatus &os, WorkflowProcessItem *from, WorkflowProcessItem *to) {
865     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(from->scene()->views().at(0));
866     GT_CHECK(sceneView, "sceneView not found");
867 
868     WorkflowBusItem *arrow = getConnectionArrow(os, from, to);
869     QGraphicsTextItem *hint = getArrowHint(os, arrow);
870     click(os, hint);
871 
872     GTKeyboardDriver::keyClick(Qt::Key_Delete);
873     GTGlobals::sleep(1000);
874 }
875 #undef GT_METHOD_NAME
876 
877 #define GT_METHOD_NAME "getConnectionArrow"
getConnectionArrow(HI::GUITestOpStatus & os,WorkflowProcessItem * from,WorkflowProcessItem * to)878 WorkflowBusItem *GTUtilsWorkflowDesigner::getConnectionArrow(HI::GUITestOpStatus &os, WorkflowProcessItem *from, WorkflowProcessItem *to) {
879     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(from->scene()->views().at(0));
880     GT_CHECK_RESULT(sceneView, "sceneView not found", nullptr)
881     QList<WorkflowPortItem *> fromList = from->getPortItems();
882     QList<WorkflowPortItem *> toList = to->getPortItems();
883 
884     QList<WorkflowBusItem *> arrows = getAllConnectionArrows(os);
885 
886     foreach (WorkflowPortItem *fromPort, fromList) {
887         foreach (WorkflowPortItem *toPort, toList) {
888             foreach (WorkflowBusItem *arrow, arrows) {
889                 if (arrow->getInPort() == toPort && arrow->getOutPort() == fromPort) {
890                     return arrow;
891                 }
892             }
893         }
894     }
895 
896     GT_CHECK_RESULT(false, "no suitable ports to connect", nullptr);
897 }
898 #undef GT_METHOD_NAME
899 
900 #define GT_METHOD_NAME "getArrowHint"
getArrowHint(HI::GUITestOpStatus & os,WorkflowBusItem * arrow)901 QGraphicsTextItem *GTUtilsWorkflowDesigner::getArrowHint(HI::GUITestOpStatus &os, WorkflowBusItem *arrow) {
902     GT_CHECK_RESULT(arrow != nullptr, "arrow item is NULL", nullptr);
903 
904     foreach (QGraphicsItem *item, arrow->childItems()) {
905         QGraphicsTextItem *hint = qgraphicsitem_cast<QGraphicsTextItem *>(item);
906         if (hint != nullptr) {
907             return hint;
908         }
909     }
910 
911     GT_CHECK_RESULT(false, "hint not found", nullptr);
912 }
913 #undef GT_METHOD_NAME
914 
getWorkers(HI::GUITestOpStatus & os)915 QList<WorkflowProcessItem *> GTUtilsWorkflowDesigner::getWorkers(HI::GUITestOpStatus &os) {
916     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
917     QList<WorkflowProcessItem *> result;
918     QGraphicsView *sceneView = qobject_cast<QGraphicsView *>(GTWidget::findWidget(os, "sceneView", wdWindow));
919     QList<QGraphicsItem *> items = sceneView->items();
920     foreach (QGraphicsItem *it, items) {
921         WorkflowProcessItem *worker = qgraphicsitem_cast<WorkflowProcessItem *>(it);
922         if (worker) {
923             result.append(worker);
924         }
925     }
926     return result;
927 }
928 
929 #define GT_METHOD_NAME "getDatasetsListWidget"
getDatasetsListWidget(GUITestOpStatus & os)930 QWidget *GTUtilsWorkflowDesigner::getDatasetsListWidget(GUITestOpStatus &os) {
931     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
932     return GTWidget::findWidget(os, "DatasetsListWidget", wdWindow);
933 }
934 #undef GT_METHOD_NAME
935 
936 #define GT_METHOD_NAME "getCurrentDatasetWidget"
getCurrentDatasetWidget(GUITestOpStatus & os)937 QWidget *GTUtilsWorkflowDesigner::getCurrentDatasetWidget(GUITestOpStatus &os) {
938     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
939     QTabWidget *datasetsTabWidget = GTWidget::findExactWidget<QTabWidget *>(os, "DatasetsTabWidget", wdWindow);
940     return datasetsTabWidget->currentWidget();
941 }
942 #undef GT_METHOD_NAME
943 
944 #define GT_METHOD_NAME "setDatasetInputFile"
setDatasetInputFile(GUITestOpStatus & os,const QString & filePath,bool pastePath,QWidget * datasetWidget)945 void GTUtilsWorkflowDesigner::setDatasetInputFile(GUITestOpStatus &os, const QString &filePath, bool pastePath, QWidget *datasetWidget) {
946     QWidget *currentDatasetWidget = datasetWidget == nullptr ? getCurrentDatasetWidget(os) : datasetWidget;
947     GT_CHECK(currentDatasetWidget != nullptr, "Current dataset widget not found");
948 
949     QWidget *addFileButton = GTWidget::findWidget(os, "addFileButton", currentDatasetWidget);
950     GT_CHECK(addFileButton, "addFileButton not found");
951 
952     GTFileDialogUtils::TextInput t = pastePath ? GTFileDialogUtils::CopyPaste : GTFileDialogUtils::Typing;
953 
954     GTFileDialogUtils *ob = new GTFileDialogUtils(os, filePath, GTGlobals::UseMouse, GTFileDialogUtils::Open, t);
955     GTUtilsDialog::waitForDialog(os, ob);
956 
957     GTWidget::click(os, addFileButton);
958     GTGlobals::sleep(3000);
959 }
960 #undef GT_METHOD_NAME
961 
962 #define GT_METHOD_NAME "setDatasetInputFiles"
setDatasetInputFiles(GUITestOpStatus & os,const QStringList & filePaths,QWidget * datasetWidget)963 void GTUtilsWorkflowDesigner::setDatasetInputFiles(GUITestOpStatus &os, const QStringList &filePaths, QWidget *datasetWidget) {
964     QWidget *currentDatasetWidget = datasetWidget == nullptr ? getCurrentDatasetWidget(os) : datasetWidget;
965     QWidget *addFileButton = GTWidget::findWidget(os, "addFileButton", currentDatasetWidget);
966 
967     GTUtilsDialog::waitForDialog(os, new GTFileDialogUtils_list(os, filePaths));
968     GTWidget::click(os, addFileButton);
969 }
970 #undef GT_METHOD_NAME
971 
972 #define GT_METHOD_NAME "addInputFile"
addInputFile(HI::GUITestOpStatus & os,const QString & elementName,const QString & url)973 void GTUtilsWorkflowDesigner::addInputFile(HI::GUITestOpStatus &os, const QString &elementName, const QString &url) {
974     click(os, elementName);
975     CHECK_OP(os, );
976     QFileInfo info(url);
977     setDatasetInputFile(os, info.path() + "/" + info.fileName());
978 }
979 #undef GT_METHOD_NAME
980 
981 #define GT_METHOD_NAME "createDataset"
createDataset(HI::GUITestOpStatus & os,QString datasetName)982 void GTUtilsWorkflowDesigner::createDataset(HI::GUITestOpStatus &os, QString datasetName) {
983     QWidget *plusButton = GTWidget::findButtonByText(os, "+", getDatasetsListWidget(os));
984     GT_CHECK(plusButton, "plusButton not found");
985 
986     GTUtilsDialog::waitForDialog(os, new DatasetNameEditDialogFiller(os, datasetName));
987 
988     GTWidget::click(os, plusButton);
989     GTGlobals::sleep();
990 }
991 #undef GT_METHOD_NAME
992 
993 #define GT_METHOD_NAME "setDatasetInputFolder"
setDatasetInputFolder(HI::GUITestOpStatus & os,const QString & filePath,QWidget * datasetWidget)994 void GTUtilsWorkflowDesigner::setDatasetInputFolder(HI::GUITestOpStatus &os, const QString &filePath, QWidget *datasetWidget) {
995     QWidget *currentDatasetWidget = datasetWidget == nullptr ? getCurrentDatasetWidget(os) : datasetWidget;
996     GT_CHECK(nullptr != currentDatasetWidget, "Current dataset widget not found");
997 
998     QWidget *addDirButton = GTWidget::findWidget(os, "addDirButton", currentDatasetWidget);
999     GT_CHECK(addDirButton, "addFileButton not found");
1000 
1001     GTFileDialogUtils *ob = new GTFileDialogUtils(os, filePath, "", GTFileDialogUtils::Choose, GTGlobals::UseMouse);
1002     GTUtilsDialog::waitForDialog(os, ob);
1003 
1004     GTWidget::click(os, addDirButton);
1005 }
1006 #undef GT_METHOD_NAME
1007 
1008 #define GT_METHOD_NAME "setDatasetInputFolders"
setDatasetInputFolders(GUITestOpStatus & os,const QStringList & dirPaths,QWidget * datasetWidget)1009 void GTUtilsWorkflowDesigner::setDatasetInputFolders(GUITestOpStatus &os, const QStringList &dirPaths, QWidget *datasetWidget) {
1010     QWidget *currentDatasetWidget = datasetWidget == nullptr ? getCurrentDatasetWidget(os) : datasetWidget;
1011     GT_CHECK(nullptr != currentDatasetWidget, "Current dataset widget not found");
1012 
1013     QWidget *addDirButton = GTWidget::findWidget(os, "addDirButton", currentDatasetWidget);
1014     GT_CHECK(nullptr != addDirButton, "addFileButton not found");
1015 
1016     GTUtilsDialog::waitForDialog(os, new GTFileDialogUtils_list(os, dirPaths));
1017     GTWidget::click(os, addDirButton);
1018 }
1019 #undef GT_METHOD_NAME
1020 
1021 #define GT_METHOD_NAME "getRowIndexOrFail"
getRowIndexOrFail(HI::GUITestOpStatus & os,QTableView * table,const QString & parameter)1022 static int getRowIndexOrFail(HI::GUITestOpStatus &os, QTableView *table, const QString &parameter) {
1023     QAbstractItemModel *model = table->model();
1024     int rowIndex = -1;
1025     for (int i = 0; i < model->rowCount(); i++) {
1026         QString s = model->data(model->index(i, 0)).toString();
1027         if (s.compare(parameter, Qt::CaseInsensitive) == 0) {
1028             rowIndex = i;
1029             break;
1030         }
1031     }
1032     GT_CHECK_RESULT(rowIndex != -1, QString("parameter not found: %1").arg(parameter), -1);
1033     return rowIndex;
1034 }
1035 
setParameter(HI::GUITestOpStatus & os,QString parameter,QVariant value,valueType type,GTGlobals::UseMethod method)1036 void GTUtilsWorkflowDesigner::setParameter(HI::GUITestOpStatus &os, QString parameter, QVariant value, valueType type, GTGlobals::UseMethod method) {
1037     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1038     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1039     CHECK_SET_ERR(table, "tableView not found");
1040 
1041     int rowIndex = getRowIndexOrFail(os, table, parameter);
1042     QModelIndex modelIndex = table->model()->index(rowIndex, 1);
1043     GTWidget::scrollToIndex(os, table, modelIndex);
1044 
1045     GTMouseDriver::moveTo(GTTableView::getCellPosition(os, table, 1, rowIndex));
1046     GTThread::waitForMainThread();
1047     GTMouseDriver::click();
1048 
1049     GTGlobals::sleep();
1050 
1051     // SET VALUE
1052     setCellValue(os, table, value, type, method);
1053 }
1054 #undef GT_METHOD_NAME
1055 
1056 #define GT_METHOD_NAME "setTableValue"
setTableValue(HI::GUITestOpStatus & os,QString parameter,QVariant value,valueType type,QTableWidget * table,GTGlobals::UseMethod method)1057 void GTUtilsWorkflowDesigner::setTableValue(HI::GUITestOpStatus &os, QString parameter, QVariant value, valueType type, QTableWidget *table, GTGlobals::UseMethod method) {
1058     int row = -1;
1059     const int rows = table->rowCount();
1060     for (int i = 0; i < rows; i++) {
1061         QString s = table->item(i, 0)->text();
1062         if (s == parameter) {
1063             row = i;
1064             break;
1065         }
1066     }
1067     GT_CHECK(row != -1, QString("parameter not found: %1").arg(parameter));
1068 
1069     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1070     QScrollArea *scrollArea = qobject_cast<QScrollArea *>(GTWidget::findWidget(os, "inputScrollArea", wdWindow));
1071     GT_CHECK(scrollArea != nullptr, "inputPortBox isn't found");
1072     if (!scrollArea->findChildren<QTableWidget *>().contains(table)) {
1073         scrollArea = qobject_cast<QScrollArea *>(GTWidget::findWidget(os, "outputScrollArea", wdWindow));
1074         GT_CHECK(scrollArea != nullptr, "outputPortBox isn't found");
1075         GT_CHECK(scrollArea->findChildren<QTableWidget *>().contains(table), "The owner of the table widget isn't found");
1076     }
1077     QScrollBar *scrollBar = scrollArea->verticalScrollBar();
1078     GT_CHECK(scrollBar != nullptr, "Horizontal scroll bar isn't found");
1079 
1080     QRect parentTableRect = scrollArea->rect();
1081     QPoint globalTopLeftParentTable = scrollArea->mapToGlobal(parentTableRect.topLeft());
1082     QPoint globalBottomRightParentTable = scrollArea->mapToGlobal(parentTableRect.bottomRight());
1083     QRect globalParentRect(globalTopLeftParentTable, globalBottomRightParentTable - QPoint(0, 1));
1084 
1085     QTableWidgetItem *item = table->item(row, 1);
1086     QRect rect = table->visualItemRect(item);
1087     QPoint globalP = table->viewport()->mapToGlobal(rect.center());
1088 
1089     while (!globalParentRect.contains(globalP)) {
1090         GTScrollBar::lineDown(os, scrollBar, method);
1091         rect = table->visualItemRect(item);
1092         globalP = table->viewport()->mapToGlobal(rect.center());
1093     }
1094 
1095     GTMouseDriver::moveTo(globalP);
1096     GTMouseDriver::click();
1097     GTGlobals::sleep(500);
1098 
1099     // SET VALUE
1100     setCellValue(os, table, value, type, method);
1101 }
1102 #undef GT_METHOD_NAME
1103 
1104 #define GT_METHOD_NAME "setCellValue"
setCellValue(HI::GUITestOpStatus & os,QWidget * parent,QVariant value,valueType type,GTGlobals::UseMethod method)1105 void GTUtilsWorkflowDesigner::setCellValue(HI::GUITestOpStatus &os, QWidget *parent, QVariant value, valueType type, GTGlobals::UseMethod method) {
1106     checkWorkflowDesignerWindowIsActive(os);
1107     bool ok = true;
1108     switch (type) {
1109         case (comboWithFileSelector): {
1110             GTUtilsDialog::waitForDialog(os, new GTFileDialogUtils(os, value.toString()));
1111             GTWidget::click(os, GTWidget::findButtonByText(os, "...", parent));
1112 #ifdef Q_OS_WIN
1113             // added to fix UGENE-3597
1114             GTKeyboardDriver::keyClick(Qt::Key_Enter);
1115 #endif
1116             break;
1117         }
1118         case (lineEditWithFileSelector): {
1119             GTLineEdit::setText(os, GTWidget::findExactWidget<QLineEdit *>(os, "mainWidget", parent), value.toString());
1120             GTKeyboardDriver::keyClick(Qt::Key_Enter);
1121             break;
1122         }
1123         case (spinValue): {
1124             int spinVal = value.toInt(&ok);
1125             GT_CHECK(ok, "Wrong input. Int required for GTUtilsWorkflowDesigner::spinValue")
1126             QSpinBox *spinBox = GTWidget::findWidgetByType<QSpinBox *>(os, parent, "Cell has no QSpinBox widget");
1127             GTSpinBox::setValue(os, spinBox, spinVal, GTGlobals::UseKeyBoard);
1128             break;
1129         }
1130         case (doubleSpinValue): {
1131             double spinVal = value.toDouble(&ok);
1132             GT_CHECK(ok, "Wrong input. Double required for GTUtilsWorkflowDesigner::doubleSpinValue")
1133             QDoubleSpinBox *doubleSpinBox = GTWidget::findWidgetByType<QDoubleSpinBox *>(os, parent, "Cell has no QDoubleSpinBox widget");
1134             GTDoubleSpinbox::setValue(os, doubleSpinBox, spinVal, GTGlobals::UseKeyBoard);
1135             break;
1136         }
1137         case (comboValue): {
1138             int itemIndex = value.toInt(&ok);
1139             QComboBox *comboBox = GTWidget::findWidgetByType<QComboBox *>(os, parent, "Cell has no QComboBox widget");
1140             if (!ok) {
1141                 QString itemText = value.toString();
1142                 GTComboBox::selectItemByText(os, comboBox, itemText, method);
1143             } else {
1144                 GTComboBox::selectItemByIndex(os, comboBox, itemIndex, method);
1145             }
1146             break;
1147         }
1148         case (textValue): {
1149             QString lineVal = value.toString();
1150             QLineEdit *lineEdit = GTWidget::findWidgetByType<QLineEdit *>(os, parent, "Cell has no QLineEdit widget");
1151             GTLineEdit::setText(os, lineEdit, lineVal);
1152             GTKeyboardDriver::keyClick(Qt::Key_Enter);
1153             break;
1154         }
1155         case ComboChecks: {
1156             QStringList values = value.value<QStringList>();
1157             QComboBox *comboBox = GTWidget::findWidgetByType<QComboBox *>(os, parent, "Cell has no QComboBox/ComboChecks widget");
1158             GTComboBox::checkValues(os, comboBox, values);
1159 #ifndef Q_OS_WIN
1160             GTKeyboardDriver::keyClick(Qt::Key_Escape);
1161 #endif
1162             break;
1163         }
1164         case customDialogSelector: {
1165             GTWidget::click(os, GTWidget::findButtonByText(os, "...", parent));
1166             break;
1167         }
1168     }
1169     GTThread::waitForMainThread();
1170 }
1171 #undef GT_METHOD_NAME
1172 
1173 #define GT_METHOD_NAME "getCellValue"
getCellValue(HI::GUITestOpStatus & os,QString parameter,QTableWidget * table)1174 QString GTUtilsWorkflowDesigner::getCellValue(HI::GUITestOpStatus &os, QString parameter, QTableWidget *table) {
1175     Q_UNUSED(os);
1176     int row = -1;
1177     for (int i = 0; i < table->rowCount(); i++) {
1178         QString s = table->item(i, 0)->text();
1179         if (s == parameter) {
1180             row = i;
1181             break;
1182         }
1183     }
1184     GT_CHECK_RESULT(row != -1, QString("parameter not found: %1").arg(parameter), QString());
1185 
1186     QString result = table->item(row, 1)->text();
1187     return result;
1188 }
1189 #undef GT_METHOD_NAME
1190 
1191 #define GT_METHOD_NAME "getInputPortsTable"
getInputPortsTable(HI::GUITestOpStatus & os,int index)1192 QTableWidget *GTUtilsWorkflowDesigner::getInputPortsTable(HI::GUITestOpStatus &os, int index) {
1193     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1194     QWidget *inputPortBox = GTWidget::findWidget(os, "inputPortBox", wdWindow);
1195     GTGroupBox::setChecked(os, "inputPortBox", true);
1196     QList<QTableWidget *> tables = inputPortBox->findChildren<QTableWidget *>();
1197     foreach (QTableWidget *w, tables) {
1198         if (!w->isVisible()) {
1199             tables.removeOne(w);
1200         }
1201     }
1202     int number = tables.count();
1203     GT_CHECK_RESULT(index < number, QString("there are %1 visiable tables for input ports").arg(number), nullptr);
1204     return tables[index];
1205 }
1206 #undef GT_METHOD_NAME
1207 
1208 #define GT_METHOD_NAME "getOutputPortsTable"
getOutputPortsTable(GUITestOpStatus & os,int index)1209 QTableWidget *GTUtilsWorkflowDesigner::getOutputPortsTable(GUITestOpStatus &os, int index) {
1210     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1211     QWidget *outputPortBox = GTWidget::findWidget(os, "outputPortBox", wdWindow);
1212     GTGroupBox::setChecked(os, "outputPortBox", true);
1213     QList<QTableWidget *> tables = outputPortBox->findChildren<QTableWidget *>();
1214     foreach (QTableWidget *w, tables) {
1215         if (!w->isVisible()) {
1216             tables.removeOne(w);
1217         }
1218     }
1219     int number = tables.count();
1220     GT_CHECK_RESULT(index < number, QString("there are %1 visables tables for output ports").arg(number), nullptr);
1221     return tables[index];
1222 }
1223 #undef GT_METHOD_NAME
1224 
1225 #define GT_METHOD_NAME "scrollInputPortsWidgetToTableRow"
scrollInputPortsWidgetToTableRow(GUITestOpStatus & os,int tableIndex,const QString & slotName)1226 void GTUtilsWorkflowDesigner::scrollInputPortsWidgetToTableRow(GUITestOpStatus &os, int tableIndex, const QString &slotName) {
1227     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1228     QWidget *inputPortBox = GTWidget::findWidget(os, "inputPortBox", wdWindow);
1229     QTableWidget *table = getInputPortsTable(os, tableIndex);
1230 
1231     QList<QTableWidgetItem *> itemList = table->findItems(slotName, Qt::MatchFixedString);
1232     GT_CHECK(!itemList.isEmpty(), QString("Can't find item for slot name '%1'").arg(slotName));
1233 
1234     const QRect itemLocalRect = table->visualItemRect(itemList.first());
1235     const QRect itemPortWidgetRect = QRect(table->viewport()->mapTo(inputPortBox, itemLocalRect.topLeft()),
1236                                            table->viewport()->mapTo(inputPortBox, itemLocalRect.bottomRight()));
1237 
1238     bool isCenterVisible = inputPortBox->rect().contains(itemPortWidgetRect.center());
1239     if (isCenterVisible) {
1240         return;
1241     }
1242 
1243     QScrollArea *inputScrollArea = GTWidget::findExactWidget<QScrollArea *>(os, "inputScrollArea", inputPortBox);
1244     QScrollBar *scrollBar = inputScrollArea->verticalScrollBar();
1245     GTScrollBar::moveSliderWithMouseToValue(os, scrollBar, itemPortWidgetRect.center().y());
1246 }
1247 #undef GT_METHOD_NAME
1248 
1249 #define GT_METHOD_NAME "getAllParameters"
getAllParameters(HI::GUITestOpStatus & os)1250 QStringList GTUtilsWorkflowDesigner::getAllParameters(HI::GUITestOpStatus &os) {
1251     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1252     QStringList result;
1253     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1254     GT_CHECK_RESULT(table, "tableView not found", result);
1255 
1256     QAbstractItemModel *model = table->model();
1257     int iMax = model->rowCount();
1258     for (int i = 0; i < iMax; i++) {
1259         QString s = model->data(model->index(i, 0)).toString();
1260         result << s;
1261     }
1262     return result;
1263 }
1264 #undef GT_METHOD_NAME
1265 
1266 #define GT_METHOD_NAME "getComboBoxParameterValues"
getComboBoxParameterValues(HI::GUITestOpStatus & os,QString parameter)1267 QStringList GTUtilsWorkflowDesigner::getComboBoxParameterValues(HI::GUITestOpStatus &os, QString parameter) {
1268     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1269     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1270     GT_CHECK_RESULT(table, "tableView not found", QStringList());
1271 
1272     // FIND CELL
1273     QAbstractItemModel *model = table->model();
1274     int iMax = model->rowCount();
1275     int row = -1;
1276     for (int i = 0; i < iMax; i++) {
1277         QString s = model->data(model->index(i, 0)).toString();
1278         if (s.compare(parameter, Qt::CaseInsensitive) == 0) {
1279             row = i;
1280             break;
1281         }
1282     }
1283     GT_CHECK_RESULT(row != -1, QString("parameter not found: %1").arg(parameter), QStringList());
1284     table->scrollTo(model->index(row, 1));
1285 
1286     GTMouseDriver::moveTo(GTTableView::getCellPosition(os, table, 1, row));
1287     GTMouseDriver::click();
1288     GTGlobals::sleep();
1289 
1290     QComboBox *box = qobject_cast<QComboBox *>(table->findChild<QComboBox *>());
1291     GT_CHECK_RESULT(box, "QComboBox not found. Widget in this cell might be not QComboBox", QStringList());
1292 
1293     QStringList result;
1294     int valuesCount = box->count();
1295     for (int i = 0; i < valuesCount; i++) {
1296         result << box->itemText(i);
1297     }
1298 
1299     return result;
1300 }
1301 #undef GT_METHOD_NAME
1302 
1303 #define GT_METHOD_NAME "getCheckableComboboxValuesFromInputPortTable"
getCheckableComboboxValuesFromInputPortTable(GUITestOpStatus & os,int tableIndex,const QString & slotName)1304 QList<QPair<QString, bool>> GTUtilsWorkflowDesigner::getCheckableComboboxValuesFromInputPortTable(GUITestOpStatus &os, int tableIndex, const QString &slotName) {
1305     QList<QPair<QString, bool>> result;
1306 
1307     QTableWidget *table = getInputPortsTable(os, tableIndex);
1308     GT_CHECK_RESULT(nullptr != table, "table is nullptr", result);
1309 
1310     scrollInputPortsWidgetToTableRow(os, tableIndex, slotName);
1311 
1312     QList<QTableWidgetItem *> itemList = table->findItems(slotName, Qt::MatchFixedString);
1313     GT_CHECK_RESULT(!itemList.isEmpty(), QString("Can't find item for slot name '%1'").arg(slotName), result);
1314     const int row = itemList.first()->row();
1315 
1316     GTMouseDriver::moveTo(GTTableView::getCellPosition(os, table, 1, row));
1317     GTMouseDriver::click();
1318     GTGlobals::sleep();
1319 
1320     QComboBox *box = qobject_cast<QComboBox *>(table->findChild<QComboBox *>());
1321     GT_CHECK_RESULT(box, "QComboBox not found. Widget in this cell might be not QComboBox", result);
1322 
1323     QStandardItemModel *checkBoxModel = qobject_cast<QStandardItemModel *>(box->model());
1324     GT_CHECK_RESULT(nullptr != checkBoxModel, "Unexpected checkbox model", result);
1325 
1326     for (int i = 0; i < checkBoxModel->rowCount(); ++i) {
1327         QStandardItem *item = checkBoxModel->item(i);
1328         result << qMakePair(item->data(Qt::DisplayRole).toString(), Qt::Checked == item->checkState());
1329     }
1330 
1331     return result;
1332 }
1333 #undef GT_METHOD_NAME
1334 
1335 namespace {
equalStrings(const QString & where,const QString & what,bool exactMatch)1336 bool equalStrings(const QString &where, const QString &what, bool exactMatch) {
1337     if (exactMatch) {
1338         return (where == what);
1339     } else {
1340         return where.contains(what, Qt::CaseInsensitive);
1341     }
1342 }
1343 }  // namespace
1344 
1345 #define GT_METHOD_NAME "getParameter"
getParameter(HI::GUITestOpStatus & os,QString parameter,bool exactMatch)1346 QString GTUtilsWorkflowDesigner::getParameter(HI::GUITestOpStatus &os, QString parameter, bool exactMatch) {
1347     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1348     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1349     GT_CHECK_RESULT(table, "tableView not found", "");
1350 
1351     QAbstractItemModel *model = table->model();
1352     GT_CHECK_RESULT(model, "model not found", "");
1353     int iMax = model->rowCount();
1354     int row = -1;
1355     for (int i = 0; i < iMax; i++) {
1356         QString s = model->data(model->index(i, 0)).toString();
1357         if (equalStrings(s, parameter, exactMatch)) {
1358             row = i;
1359             break;
1360         }
1361     }
1362     GT_CHECK_RESULT(row != -1, "parameter " + parameter + " not found", "");
1363     QModelIndex idx = model->index(row, 1);
1364 
1365     QVariant var;
1366 
1367     class Scenario : public CustomScenario {
1368     public:
1369         Scenario(QAbstractItemModel *_model, QModelIndex _idx, QVariant &_result)
1370             : model(_model), idx(_idx), result(_result) {
1371         }
1372         void run(HI::GUITestOpStatus &os) {
1373             Q_UNUSED(os);
1374             result = model->data(idx);
1375             GTGlobals::sleep(100);
1376         }
1377 
1378     private:
1379         QAbstractItemModel *model;
1380         QModelIndex idx;
1381         QVariant &result;
1382     };
1383 
1384     GTThread::runInMainThread(os, new Scenario(model, idx, var));
1385     return var.toString();
1386 }
1387 #undef GT_METHOD_NAME
1388 
1389 #define GT_METHOD_NAME "isParameterEnabled"
isParameterEnabled(HI::GUITestOpStatus & os,QString parameter)1390 bool GTUtilsWorkflowDesigner::isParameterEnabled(HI::GUITestOpStatus &os, QString parameter) {
1391     clickParameter(os, parameter);
1392     QWidget *w = QApplication::widgetAt(GTMouseDriver::getMousePosition());
1393     QString s = w->metaObject()->className();
1394 
1395     bool result = !(s == "QWidget");  // if parameter is disabled QWidget is under cursor
1396     return result;
1397 }
1398 #undef GT_METHOD_NAME
1399 
1400 #define GT_METHOD_NAME "isParameterRequired"
isParameterRequired(HI::GUITestOpStatus & os,const QString & parameter)1401 bool GTUtilsWorkflowDesigner::isParameterRequired(HI::GUITestOpStatus &os, const QString &parameter) {
1402     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1403     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1404     GT_CHECK_RESULT(table, "tableView not found", false);
1405 
1406     // find a cell
1407     QAbstractItemModel *model = table->model();
1408     int iMax = model->rowCount();
1409     int row = -1;
1410     for (int i = 0; i < iMax; i++) {
1411         QString s = model->data(model->index(i, 0)).toString();
1412         if (s.contains(parameter, Qt::CaseInsensitive)) {
1413             row = i;
1414         }
1415     }
1416     GT_CHECK_RESULT(row != -1, "parameter not found", false);
1417     table->scrollTo(model->index(row, 0));
1418 
1419     const QFont font = model->data(model->index(row, 0), Qt::FontRole).value<QFont>();
1420     return font.bold();
1421 }
1422 #undef GT_METHOD_NAME
1423 
1424 namespace {
1425 
getParameterRow(QTableView * table,const QString & parameter)1426 int getParameterRow(QTableView *table, const QString &parameter) {
1427     QAbstractItemModel *model = table->model();
1428     int iMax = model->rowCount();
1429     for (int i = 0; i < iMax; i++) {
1430         QString s = model->data(model->index(i, 0)).toString();
1431         if (s == parameter) {
1432             return i;
1433         }
1434     }
1435     return -1;
1436 }
1437 
1438 }  // namespace
1439 
1440 #define GT_METHOD_NAME "clickParameter"
clickParameter(HI::GUITestOpStatus & os,const QString & parameter)1441 void GTUtilsWorkflowDesigner::clickParameter(HI::GUITestOpStatus &os, const QString &parameter) {
1442     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1443     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1444     GT_CHECK_RESULT(table, "tableView not found", );
1445 
1446     // FIND CELL
1447     const int row = getParameterRow(table, parameter);
1448     GT_CHECK_RESULT(row != -1, "parameter not found", );
1449 
1450     QAbstractItemModel *model = table->model();
1451     table->scrollTo(model->index(row, 1));
1452     GTMouseDriver::moveTo(GTTableView::getCellPosition(os, table, 1, row));
1453     GTMouseDriver::click();
1454     GTGlobals::sleep(500);
1455 }
1456 #undef GT_METHOD_NAME
1457 
1458 #define GT_METHOD_NAME "isParameterVisible"
isParameterVisible(HI::GUITestOpStatus & os,const QString & parameter)1459 bool GTUtilsWorkflowDesigner::isParameterVisible(HI::GUITestOpStatus &os, const QString &parameter) {
1460     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1461     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1462     GT_CHECK_RESULT(table, "tableView not found", false);
1463     return -1 != getParameterRow(table, parameter);
1464 }
1465 #undef GT_METHOD_NAME
1466 
1467 #define GT_METHOD_NAME "getParametersTable"
getParametersTable(HI::GUITestOpStatus & os)1468 QTableView *GTUtilsWorkflowDesigner::getParametersTable(HI::GUITestOpStatus &os) {
1469     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1470     return qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1471 }
1472 #undef GT_METHOD_NAME
1473 
1474 #define GT_METHOD_NAME "setParameterScripting"
setParameterScripting(HI::GUITestOpStatus & os,QString parameter,QString scriptMode,bool exactMatch)1475 void GTUtilsWorkflowDesigner::setParameterScripting(HI::GUITestOpStatus &os, QString parameter, QString scriptMode, bool exactMatch) {
1476     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1477     QTableView *table = qobject_cast<QTableView *>(GTWidget::findWidget(os, "table", wdWindow));
1478     CHECK_SET_ERR(table, "tableView not found");
1479 
1480     // FIND CELL
1481     QAbstractItemModel *model = table->model();
1482     int row = -1;
1483     for (int i = 0; i < model->rowCount(); i++) {
1484         QString s = model->data(model->index(i, 0)).toString();
1485         if (equalStrings(s, parameter, exactMatch)) {
1486             row = i;
1487         }
1488     }
1489     GT_CHECK(row != -1, "parameter not found");
1490 
1491     class MainThreadAction : public CustomScenario {
1492     public:
1493         MainThreadAction(QTableView *table, int row)
1494             : CustomScenario(), table(table), row(row) {
1495         }
1496         void run(HI::GUITestOpStatus &os) {
1497             Q_UNUSED(os);
1498             QAbstractItemModel *model = table->model();
1499             table->scrollTo(model->index(row, 1));
1500         }
1501         QTableView *table;
1502         int row;
1503     };
1504     GTThread::runInMainThread(os, new MainThreadAction(table, row));
1505 
1506     GTMouseDriver::moveTo(GTTableView::getCellPosition(os, table, 2, row));
1507     GTMouseDriver::click();
1508 
1509     // SET VALUE
1510     QComboBox *box = qobject_cast<QComboBox *>(table->findChild<QComboBox *>());
1511     GT_CHECK(box != nullptr, "QComboBox not found. Scripting might be unavaluable for this parameter");
1512     GTComboBox::selectItemByText(os, box, scriptMode);
1513 }
1514 #undef GT_METHOD_NAME
1515 
1516 #define GT_METHOD_NAME "checkErrorList"
checkErrorList(HI::GUITestOpStatus & os,QString error)1517 int GTUtilsWorkflowDesigner::checkErrorList(HI::GUITestOpStatus &os, QString error) {
1518     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1519     QListWidget *w = qobject_cast<QListWidget *>(GTWidget::findWidget(os, "infoList", wdWindow));
1520     GT_CHECK_RESULT(w, "ErrorList widget not found", 0);
1521 
1522     QList<QListWidgetItem *> list = w->findItems(error, Qt::MatchContains);
1523     return list.size();
1524 }
1525 #undef GT_METHOD_NAME
1526 
1527 #define GT_METHOD_NAME "getErrors"
getErrors(GUITestOpStatus & os)1528 QStringList GTUtilsWorkflowDesigner::getErrors(GUITestOpStatus &os) {
1529     QWidget *wdWindow = getActiveWorkflowDesignerWindow(os);
1530     QListWidget *w = GTWidget::findExactWidget<QListWidget *>(os, "infoList", wdWindow);
1531     GT_CHECK_RESULT(w, "ErrorList widget not found", QStringList());
1532 
1533     QStringList errors;
1534     for (int i = 0; i < w->count(); i++) {
1535         errors << w->item(i)->text();
1536     }
1537     return errors;
1538 }
1539 #undef GT_METHOD_NAME
1540 
1541 #undef GT_CLASS_NAME
1542 
1543 }  // namespace U2
1544