Changeset View
Changeset View
Standalone View
Standalone View
autotests/kurlrequestertest.cpp
Show All 16 Lines | 1 | /* This file is part of the KDE Frameworks | |||
---|---|---|---|---|---|
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include <kurlrequester.h> | 21 | #include <kurlrequester.h> | ||
22 | #include <kfilewidget.h> | 22 | #include <kfilewidget.h> | ||
23 | #include <kdiroperator.h> | 23 | #include <kdiroperator.h> | ||
24 | 24 | | |||
25 | #include <KComboBox> | ||||
26 | | ||||
25 | #include <QDebug> | 27 | #include <QDebug> | ||
26 | #include <QLineEdit> | 28 | #include <QLineEdit> | ||
27 | #include <QSignalSpy> | 29 | #include <QSignalSpy> | ||
28 | #include <QTemporaryFile> | 30 | #include <QTemporaryFile> | ||
29 | #include <QTest> | 31 | #include <QTest> | ||
30 | 32 | | |||
31 | /* | 33 | /* | ||
32 | IMPORTANT: | 34 | IMPORTANT: | ||
33 | Because this unittest interacts with the file dialog, | 35 | Because this unittest interacts with the file dialog, | ||
34 | remember to run it both with plugins/platformthemes/KDEPlasmaPlatformTheme.so (to use KFileWidget) | 36 | remember to run it both with plugins/platformthemes/KDEPlasmaPlatformTheme.so (to use KFileWidget) | ||
35 | and without it (to use the builtin QFileDialog code) | 37 | and without it (to use the builtin QFileDialog code) | ||
36 | */ | 38 | */ | ||
37 | 39 | | |||
38 | class KUrlRequesterTest : public QObject | 40 | class KUrlRequesterTest : public QObject | ||
39 | { | 41 | { | ||
40 | Q_OBJECT | 42 | Q_OBJECT | ||
41 | private Q_SLOTS: | 43 | private Q_SLOTS: | ||
42 | void initTestCase(); | 44 | void initTestCase(); | ||
43 | void testUrlRequester(); | 45 | void testUrlRequester(); | ||
44 | void testComboRequester(); | 46 | void testComboRequester(); | ||
47 | void testComboRequester_data(); | ||||
45 | 48 | | |||
46 | private: | 49 | private: | ||
47 | bool createTestFile(const QString &fileName) { | 50 | bool createTestFile(const QString &fileName) { | ||
48 | QFile file(fileName); | 51 | QFile file(fileName); | ||
49 | if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { | 52 | if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { | ||
50 | return false; | 53 | return false; | ||
51 | } | 54 | } | ||
52 | file.write("Hello world\n"); | 55 | file.write("Hello world\n"); | ||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Line(s) | 80 | { | |||
134 | qApp->sendEvent(fw ? static_cast<QWidget *>(fw) : static_cast<QWidget *>(fileDialog), &keyPressEv); | 137 | qApp->sendEvent(fw ? static_cast<QWidget *>(fw) : static_cast<QWidget *>(fileDialog), &keyPressEv); | ||
135 | QCOMPARE(fileDialog->result(), static_cast<int>(QDialog::Accepted)); | 138 | QCOMPARE(fileDialog->result(), static_cast<int>(QDialog::Accepted)); | ||
136 | QCOMPARE(fileDialog->selectedFiles(), QStringList() << filePath2); | 139 | QCOMPARE(fileDialog->selectedFiles(), QStringList() << filePath2); | ||
137 | QCOMPARE(req.url().toLocalFile(), filePath2); | 140 | QCOMPARE(req.url().toLocalFile(), filePath2); | ||
138 | } | 141 | } | ||
139 | 142 | | |||
140 | void KUrlRequesterTest::testComboRequester() | 143 | void KUrlRequesterTest::testComboRequester() | ||
141 | { | 144 | { | ||
145 | QFETCH(bool, editable); | ||||
146 | | ||||
142 | KUrlComboRequester req; | 147 | KUrlComboRequester req; | ||
148 | req.show(); | ||||
149 | | ||||
143 | QList<QLineEdit *> lineEdits = req.findChildren<QLineEdit *>(); | 150 | QList<QLineEdit *> lineEdits = req.findChildren<QLineEdit *>(); | ||
144 | QVERIFY(lineEdits.isEmpty()); // no lineedits, only a readonly combo | 151 | QVERIFY(lineEdits.isEmpty()); // no lineedits, only a readonly combo | ||
152 | | ||||
153 | QSignalSpy textSpy(&req, &KUrlComboRequester::textChanged); | ||||
154 | QSignalSpy editSpy(&req, &KUrlComboRequester::textEdited); | ||||
155 | QSignalSpy returnSpy(&req, QOverload<>::of(&KUrlComboRequester::returnPressed)); | ||||
156 | QSignalSpy returnWithTextSpy(&req, QOverload<const QString&>::of(&KUrlComboRequester::returnPressed)); | ||||
157 | | ||||
158 | QVERIFY(!req.comboBox()->isEditable()); | ||||
159 | if (editable) { | ||||
160 | req.comboBox()->setEditable(true); | ||||
161 | | ||||
162 | const auto text = QStringLiteral("foobar"); | ||||
163 | QTest::keyClicks(req.comboBox(), text, Qt::NoModifier); | ||||
164 | QCOMPARE(textSpy.size(), text.size()); | ||||
165 | QCOMPARE(editSpy.size(), text.size()); | ||||
166 | QCOMPARE(textSpy.last().first().toString(), text); | ||||
167 | QCOMPARE(editSpy.last().first().toString(), text); | ||||
168 | | ||||
169 | QCOMPARE(returnSpy.size(), 0); | ||||
170 | QCOMPARE(returnWithTextSpy.size(), 0); | ||||
171 | QTest::keyEvent(QTest::Click, req.comboBox(), Qt::Key_Return); | ||||
172 | QCOMPARE(returnSpy.size(), 1); | ||||
173 | QCOMPARE(returnWithTextSpy.size(), 1); | ||||
174 | QCOMPARE(returnWithTextSpy.last().first().toString(), text); | ||||
175 | } else { | ||||
176 | const auto url1 = QUrl("file:///foo/bar/1"); | ||||
177 | const auto url2 = QUrl("file:///foo/bar/2"); | ||||
178 | req.comboBox()->addUrl(url1); | ||||
179 | QCOMPARE(textSpy.size(), 1); | ||||
180 | QCOMPARE(textSpy.last().first().toUrl(), url1); | ||||
181 | | ||||
182 | req.comboBox()->addUrl(url2); | ||||
183 | QCOMPARE(textSpy.size(), 1); | ||||
184 | | ||||
185 | QTest::keyEvent(QTest::Click, req.comboBox(), Qt::Key_Down); | ||||
186 | QCOMPARE(textSpy.size(), 2); | ||||
dfaure: Please remove this line though. | |||||
187 | QCOMPARE(textSpy.last().first().toUrl(), url2); | ||||
188 | | ||||
189 | // only editable combo boxes get the edit and return signals emitted | ||||
190 | QCOMPARE(editSpy.size(), 0); | ||||
191 | QCOMPARE(returnSpy.size(), 0); | ||||
192 | QCOMPARE(returnWithTextSpy.size(), 0); | ||||
193 | } | ||||
194 | } | ||||
195 | | ||||
196 | void KUrlRequesterTest::testComboRequester_data() | ||||
197 | { | ||||
198 | QTest::addColumn<bool>("editable"); | ||||
199 | | ||||
200 | QTest::newRow("read-only") << false; | ||||
201 | QTest::newRow("editable") << true; | ||||
145 | } | 202 | } | ||
146 | 203 | | |||
147 | QTEST_MAIN(KUrlRequesterTest) | 204 | QTEST_MAIN(KUrlRequesterTest) | ||
148 | #include "kurlrequestertest.moc" | 205 | #include "kurlrequestertest.moc" | ||
dfaure: Then use QTest::keyClick to send key events to the widget? | |||||
I still don't get it to work. This is my currently latest attempt: void KUrlRequesterTest::testComboEditableRequester() { KUrlComboRequester req; QSignalSpy textSpy(&req, &KUrlComboRequester::textChanged); QSignalSpy editSpy(&req, &KUrlComboRequester::textEdited); QSignalSpy returnSpy(&req, static_cast<void (KUrlComboRequester::*)()>(&KUrlComboRequester::returnPressed)); QSignalSpy returnWithTextSpy(&req, static_cast<void (KUrlComboRequester::*)(const QString&)>(&KUrlComboRequester::returnPressed)); QVERIFY(!req.comboBox()->isEditable()); req.comboBox()->setEditable(true); req.show(); QSignalSpy comboTextSpy(req.comboBox(), &QComboBox::currentTextChanged); QSignalSpy comboEditSpy(req.comboBox(), &QComboBox::editTextChanged); auto* lineEdit = req.comboBox()->lineEdit(); QVERIFY(lineEdit); QSignalSpy lineTextSpy(lineEdit, &QLineEdit::textChanged); QSignalSpy lineEditSpy(lineEdit, &QLineEdit::textEdited); // FIXME: this still doesn't emit any signal, but it works in practice when actually interacting with the widget? QTest::keyClicks(lineEdit, QStringLiteral("foobar"), Qt::NoModifier, 100); QVERIFY(lineTextSpy.wait()); QCOMPARE(lineTextSpy.first().first().toString(), QStringLiteral("foobar")); QVERIFY(lineEditSpy.wait()); QCOMPARE(lineEditSpy.first().first().toString(), QStringLiteral("foobar")); } Note that the widget (which I now show) actually shows the text getting written via the QTest::keyClicks method. But none of the signals is actually getting emitted... I'm flabbergasted. Looking at the QLineEdit test code, it doesn't seem to actually contain a positive test for an of isEditable... I feel like I'm missing something fundamental here. Note that I can see the signals just fine in GammaRay when I interact with an editable combo box :-/ mwolff: I still don't get it to work. This is my currently latest attempt:
```
void KUrlRequesterTest… |
Please remove this line though.