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/GTLineEdit.h"
23 
24 #include "drivers/GTKeyboardDriver.h"
25 #include "primitives/GTWidget.h"
26 #include "system/GTClipboard.h"
27 #include "utils/GTKeyboardUtils.h"
28 #include "utils/GTThread.h"
29 
30 namespace HI {
31 #define GT_CLASS_NAME "GTLineEdit"
32 
33 #define GT_METHOD_NAME "setText"
setText(GUITestOpStatus & os,QLineEdit * lineEdit,const QString & str,bool noCheck,bool useCopyPaste)34 void GTLineEdit::setText(GUITestOpStatus &os, QLineEdit *lineEdit, const QString &str, bool noCheck /* = false*/, bool useCopyPaste) {
35     GT_CHECK(lineEdit != NULL, "lineEdit is NULL");
36     if (lineEdit->text() == str) {
37         return;
38     }
39 
40     GTWidget::setFocus(os, lineEdit);
41     if (lineEdit->text() == str) {
42         return;
43     }
44     if (!lineEdit->text().isEmpty()) {
45         clear(os, lineEdit);
46     }
47 
48     if (useCopyPaste) {
49         GTClipboard::setText(os, str);
50         GTKeyboardDriver::keyClick('v', Qt::ControlModifier);
51     } else {
52         GTKeyboardDriver::keySequence(str);
53     }
54     GTGlobals::sleep(500);
55 
56     if (noCheck) {
57         return;
58     }
59 
60     QString s = lineEdit->text();
61     for (int i = 0; i <= 10 && (s != str); i++) {
62         GTGlobals::sleep(500);
63         s = lineEdit->text();
64     }
65     GT_CHECK(s == str, QString("Can't set text, set text differs from a given string in lineEdit '%1'. "
66                                "Expected '%2', got '%3'")
67                            .arg(lineEdit->objectName())
68                            .arg(str)
69                            .arg(s));
70 }
71 #undef GT_METHOD_NAME
72 
73 #define GT_METHOD_NAME "setText"
setText(GUITestOpStatus & os,const QString & lineEditName,const QString & text,QWidget const * const parent,bool noCheck,bool useCopyPaste)74 void GTLineEdit::setText(GUITestOpStatus &os, const QString &lineEditName, const QString &text, QWidget const *const parent, bool noCheck, bool useCopyPaste) {
75     setText(os, GTWidget::findExactWidget<QLineEdit *>(os, lineEditName, parent), text, noCheck, useCopyPaste);
76 }
77 #undef GT_METHOD_NAME
78 
79 #define GT_METHOD_NAME "getText"
getText(GUITestOpStatus & os,QLineEdit * lineEdit)80 QString GTLineEdit::getText(GUITestOpStatus &os, QLineEdit *lineEdit) {
81     Q_UNUSED(os);
82     GT_CHECK_RESULT(NULL != lineEdit, "lineEdit is NULL", "");
83     return lineEdit->text();
84 }
85 #undef GT_METHOD_NAME
86 
87 #define GT_METHOD_NAME "getText"
getText(GUITestOpStatus & os,const QString & lineEditName,QWidget * parent)88 QString GTLineEdit::getText(GUITestOpStatus &os, const QString &lineEditName, QWidget *parent) {
89     return getText(os, GTWidget::findExactWidget<QLineEdit *>(os, lineEditName, parent));
90 }
91 #undef GT_METHOD_NAME
92 
93 #define GT_METHOD_NAME "clear"
clear(GUITestOpStatus & os,QLineEdit * lineEdit)94 void GTLineEdit::clear(GUITestOpStatus &os, QLineEdit *lineEdit) {
95     GT_CHECK(lineEdit != NULL, "lineEdit is NULL");
96 
97     GTWidget::setFocus(os, lineEdit);
98 
99     GTKeyboardUtils::selectAll();
100     GTGlobals::sleep(100);
101     GTKeyboardDriver::keyClick(Qt::Key_Delete);
102     GTGlobals::sleep(100);
103     GTThread::waitForMainThread();
104 
105     QString s = lineEdit->text();
106     GT_CHECK(s.isEmpty() == true, "Can't clear text, lineEdit is not empty");
107 }
108 #undef GT_METHOD_NAME
109 
110 #define GT_METHOD_NAME "pasteClipboard"
pasteClipboard(GUITestOpStatus & os,QLineEdit * lineEdit,PasteMethod pasteMethod)111 void GTLineEdit::pasteClipboard(GUITestOpStatus &os, QLineEdit *lineEdit, PasteMethod pasteMethod) {
112     GT_CHECK(lineEdit != NULL, "lineEdit is NULL");
113 
114     clear(os, lineEdit);
115     switch (pasteMethod) {
116         case Mouse:
117             os.setError("GTLineEdit::pasteClipboard: Not implemented: Paste by mouse");
118             break;
119 
120         default:
121         case Shortcut:
122             GTKeyboardUtils::paste();
123             break;
124     }
125 
126     GTGlobals::sleep(500);
127 }
128 #undef GT_METHOD_NAME
129 
130 #define GT_METHOD_NAME "checkTextSize"
checkTextSize(GUITestOpStatus & os,QLineEdit * lineEdit)131 void GTLineEdit::checkTextSize(GUITestOpStatus &os, QLineEdit *lineEdit) {
132     GT_CHECK(lineEdit != NULL, "lineEdit is NULL");
133 
134     QMargins lineEditMargins = lineEdit->textMargins();
135     QFontMetrics fontMetrics = lineEdit->fontMetrics();
136     int textWidth = lineEditMargins.left() + lineEditMargins.right() + fontMetrics.width(lineEdit->text());
137     int rectWidth = lineEdit->rect().width();
138 
139     GT_CHECK(textWidth <= rectWidth, "GTLineEdit::checkTextSize: Text is not inside LineEdit's rect");
140 }
141 #undef GT_METHOD_NAME
142 
143 #define GT_METHOD_NAME "checkText"
checkText(GUITestOpStatus & os,QLineEdit * lineEdit,const QString & expectedText)144 void GTLineEdit::checkText(GUITestOpStatus &os, QLineEdit *lineEdit, const QString &expectedText) {
145     Q_UNUSED(os);
146     GT_CHECK(NULL != lineEdit, "Line edit is NULL");
147     GT_CHECK(expectedText == lineEdit->text(), QString("The text differs: expect '%1', got '%2'").arg(expectedText).arg(lineEdit->text()));
148 }
149 #undef GT_METHOD_NAME
150 
151 #define GT_METHOD_NAME "checkText"
checkText(GUITestOpStatus & os,const QString & lineEditName,const QWidget * const parent,const QString & expectedText)152 void GTLineEdit::checkText(GUITestOpStatus &os, const QString &lineEditName, const QWidget *const parent, const QString &expectedText) {
153     checkText(os, GTWidget::findExactWidget<QLineEdit *>(os, lineEditName, parent), expectedText);
154 }
155 #undef GT_METHOD_NAME
156 
157 #define GT_METHOD_NAME "copyText"
copyText(GUITestOpStatus & os,QLineEdit * lineEdit)158 QString GTLineEdit::copyText(GUITestOpStatus &os, QLineEdit *lineEdit) {
159     GT_CHECK_RESULT(lineEdit != NULL, "lineEdit is NULL", QString());
160     return lineEdit->text();
161 }
162 #undef GT_METHOD_NAME
163 
164 #define GT_METHOD_NAME "tryToSetText"
tryToSetText(GUITestOpStatus & os,QLineEdit * lineEdit,const QString & str)165 bool GTLineEdit::tryToSetText(GUITestOpStatus &os, QLineEdit *lineEdit, const QString &str) {
166     GT_CHECK_RESULT(lineEdit != NULL, "lineEdit is NULL", false);
167     GTWidget::setFocus(os, lineEdit);
168     if (lineEdit->text() == str) {
169         return true;
170     }
171 
172     if (!lineEdit->text().isEmpty()) {
173         clear(os, lineEdit);
174     }
175 
176     GTKeyboardDriver::keySequence(str);
177     GTGlobals::sleep(500);
178 
179     QString s = lineEdit->text();
180     return s == str;
181 }
182 #undef GT_METHOD_NAME
183 
184 #undef GT_CLASS_NAME
185 
186 }  // namespace HI
187