1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2017 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 "primitives/GTSpinBox.h"
23 #include <utils/GTThread.h>
24
25 #include "drivers/GTKeyboardDriver.h"
26 #include "drivers/GTMouseDriver.h"
27 #include "primitives/GTWidget.h"
28
29 namespace HI {
30
31 #define GT_CLASS_NAME "GTSpinBox"
32
33 #define GT_METHOD_NAME "getValue"
getValue(GUITestOpStatus & os,QSpinBox * spinBox)34 int GTSpinBox::getValue(GUITestOpStatus &os, QSpinBox *spinBox) {
35 Q_UNUSED(os);
36 GT_CHECK_RESULT(spinBox != NULL, "spinBox is NULL", -1);
37 return spinBox->value();
38 }
39 #undef GT_METHOD_NAME
40
41 #define GT_METHOD_NAME "getValue"
getValue(GUITestOpStatus & os,const QString & spinBoxName,QWidget * parent)42 int GTSpinBox::getValue(GUITestOpStatus &os, const QString &spinBoxName, QWidget *parent) {
43 return GTSpinBox::getValue(os, GTWidget::findExactWidget<QSpinBox *>(os, spinBoxName, parent));
44 }
45 #undef GT_METHOD_NAME
46
47 #define GT_METHOD_NAME "setValue"
setValue(GUITestOpStatus & os,QSpinBox * spinBox,int v,GTGlobals::UseMethod useMethod)48 void GTSpinBox::setValue(GUITestOpStatus &os, QSpinBox *spinBox, int v, GTGlobals::UseMethod useMethod) {
49 GT_CHECK(spinBox != NULL, "spinBox is NULL");
50 if (spinBox->value() == v) {
51 return;
52 }
53
54 GT_CHECK(v <= spinBox->maximum(), QString("value for this spinbox cannot be more then %1").arg(spinBox->maximum()));
55 GT_CHECK(v >= spinBox->minimum(), QString("value for this spinbox cannot be less then %1").arg(spinBox->minimum()));
56
57 QPoint arrowPos;
58 QRect spinBoxRect;
59
60 GT_CHECK(spinBox->isEnabled(), "SpinBox is not enabled");
61
62 if (spinBox->value() != v) {
63 switch (useMethod) {
64 case GTGlobals::UseMouse:
65 spinBoxRect = spinBox->rect();
66 if (v > spinBox->value()) {
67 arrowPos = QPoint(spinBoxRect.right() - 5, spinBoxRect.height() / 4); // -5 it's needed that area under cursor was clickable
68 } else {
69 arrowPos = QPoint(spinBoxRect.right() - 5, spinBoxRect.height() * 3 / 4);
70 }
71
72 GTMouseDriver::moveTo(spinBox->mapToGlobal(arrowPos));
73 while (spinBox->value() != v) {
74 GTMouseDriver::click();
75 GTGlobals::sleep(100);
76 }
77 break;
78
79 case GTGlobals::UseKey: {
80 Qt::Key key;
81 if (v > spinBox->value()) {
82 key = Qt::Key_Up;
83 } else {
84 key = Qt::Key_Down;
85 }
86
87 GTWidget::setFocus(os, spinBox);
88 while (spinBox->value() != v) {
89 GTKeyboardDriver::keyClick(key);
90 GTGlobals::sleep(100);
91 }
92 break;
93 }
94 case GTGlobals::UseKeyBoard:
95 QString s = QString::number(v);
96 GTWidget::setFocus(os, spinBox);
97 GTGlobals::sleep(100);
98 GTKeyboardDriver::keyClick('a', Qt::ControlModifier);
99 GTGlobals::sleep(100);
100 GTKeyboardDriver::keyClick(Qt::Key_Delete);
101 GTGlobals::sleep(100);
102 GTKeyboardDriver::keySequence(s);
103 GTGlobals::sleep(100);
104 }
105 }
106 GTThread::waitForMainThread();
107 int currIndex = spinBox->value();
108 GT_CHECK(currIndex == v, QString("Can't set index. Expected: %1 actual: %2").arg(v).arg(currIndex));
109 GTGlobals::sleep(100);
110 }
111 #undef GT_METHOD_NAME
112
113 #define GT_METHOD_NAME "setValue"
setValue(GUITestOpStatus & os,const QString & spinBoxName,int v,GTGlobals::UseMethod useMethod,QWidget * parent)114 void GTSpinBox::setValue(GUITestOpStatus &os, const QString &spinBoxName, int v, GTGlobals::UseMethod useMethod, QWidget *parent) {
115 GTSpinBox::setValue(os, GTWidget::findExactWidget<QSpinBox *>(os, spinBoxName, parent), v, useMethod);
116 }
117 #undef GT_METHOD_NAME
118
119 #define GT_METHOD_NAME "setValue"
setValue(GUITestOpStatus & os,const QString & spinBoxName,int v,QWidget * parent)120 void GTSpinBox::setValue(GUITestOpStatus &os, const QString &spinBoxName, int v, QWidget *parent) {
121 GTSpinBox::setValue(os, GTWidget::findExactWidget<QSpinBox *>(os, spinBoxName, parent), v);
122 }
123 #undef GT_METHOD_NAME
124
125 #define GT_METHOD_NAME "checkLimits"
checkLimits(GUITestOpStatus & os,QSpinBox * spinBox,int min,int max)126 void GTSpinBox::checkLimits(GUITestOpStatus &os, QSpinBox *spinBox, int min, int max) {
127 GT_CHECK(spinBox != NULL, "spinbox is NULL");
128 int actualMin = spinBox->minimum();
129 int actualMax = spinBox->maximum();
130 GT_CHECK(actualMin == min, QString("wrong minimum. Expected: %1, actual: %2").arg(min).arg(actualMin));
131 GT_CHECK(actualMax == max, QString("wrong maximum. Expected: %1, actual: %2").arg(max).arg(actualMax));
132 }
133 #undef GT_METHOD_NAME
134
135 #define GT_METHOD_NAME "checkLimits"
checkLimits(GUITestOpStatus & os,const QString & spinBoxName,int min,int max,QWidget * parent)136 void GTSpinBox::checkLimits(GUITestOpStatus &os, const QString &spinBoxName, int min, int max, QWidget *parent) {
137 GTSpinBox::checkLimits(os, GTWidget::findExactWidget<QSpinBox *>(os, spinBoxName, parent), min, max);
138 }
139 #undef GT_METHOD_NAME
140
141 #undef GT_CLASS_NAME
142
143 } // namespace HI
144