Changeset View
Changeset View
Standalone View
Standalone View
autotests/klineedit_unittest.cpp
Show First 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | 31 | private Q_SLOTS: | |||
---|---|---|---|---|---|
54 | void testTextEditedSignals() | 54 | void testTextEditedSignals() | ||
55 | { | 55 | { | ||
56 | KLineEdit w; | 56 | KLineEdit w; | ||
57 | QVERIFY(!w.isModified()); | 57 | QVERIFY(!w.isModified()); | ||
58 | 58 | | |||
59 | // setText emits textChanged and userTextChanged, but not textEdited | 59 | // setText emits textChanged and userTextChanged, but not textEdited | ||
60 | QSignalSpy textChangedSpy(&w, SIGNAL(textChanged(QString))); | 60 | QSignalSpy textChangedSpy(&w, SIGNAL(textChanged(QString))); | ||
61 | QSignalSpy textEditedSpy(&w, SIGNAL(textEdited(QString))); | 61 | QSignalSpy textEditedSpy(&w, SIGNAL(textEdited(QString))); | ||
62 | #ifndef KCOMPLETION_NO_DEPRECATED | 62 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
63 | QSignalSpy userTextChangedSpy(&w, SIGNAL(userTextChanged(QString))); | 63 | QSignalSpy userTextChangedSpy(&w, SIGNAL(userTextChanged(QString))); | ||
64 | #endif | 64 | #endif | ||
65 | w.setText(QStringLiteral("Hello worl")); | 65 | w.setText(QStringLiteral("Hello worl")); | ||
66 | #ifndef KCOMPLETION_NO_DEPRECATED | 66 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
67 | QCOMPARE(userTextChangedSpy.count(), 1); | 67 | QCOMPARE(userTextChangedSpy.count(), 1); | ||
68 | QCOMPARE(userTextChangedSpy[0][0].toString(), w.text()); | 68 | QCOMPARE(userTextChangedSpy[0][0].toString(), w.text()); | ||
69 | #endif | 69 | #endif | ||
70 | QCOMPARE(textChangedSpy.count(), 1); | 70 | QCOMPARE(textChangedSpy.count(), 1); | ||
71 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | 71 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | ||
72 | QCOMPARE(textEditedSpy.count(), 0); | 72 | QCOMPARE(textEditedSpy.count(), 0); | ||
73 | QVERIFY(!w.isModified()); | 73 | QVERIFY(!w.isModified()); | ||
74 | #ifndef KCOMPLETION_NO_DEPRECATED | 74 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
75 | userTextChangedSpy.clear(); | 75 | userTextChangedSpy.clear(); | ||
76 | #endif | 76 | #endif | ||
77 | textChangedSpy.clear(); | 77 | textChangedSpy.clear(); | ||
78 | textEditedSpy.clear(); | 78 | textEditedSpy.clear(); | ||
79 | 79 | | |||
80 | // calling clear should emit textChanged and userTextChanged, but not textEdited | 80 | // calling clear should emit textChanged and userTextChanged, but not textEdited | ||
81 | w.clear(); | 81 | w.clear(); | ||
82 | QCOMPARE(textChangedSpy.count(),1); | 82 | QCOMPARE(textChangedSpy.count(),1); | ||
83 | QCOMPARE(textEditedSpy.count(),0); | 83 | QCOMPARE(textEditedSpy.count(),0); | ||
84 | #ifndef KCOMPLETION_NO_DEPRECATED | 84 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
85 | QCOMPARE(userTextChangedSpy.count(),1); | 85 | QCOMPARE(userTextChangedSpy.count(),1); | ||
86 | #endif | 86 | #endif | ||
87 | 87 | | |||
88 | //if text box is already empty, calling clear() shouldn't emit | 88 | //if text box is already empty, calling clear() shouldn't emit | ||
89 | // any more signals | 89 | // any more signals | ||
90 | w.clear(); | 90 | w.clear(); | ||
91 | QCOMPARE(textChangedSpy.count(),1); | 91 | QCOMPARE(textChangedSpy.count(),1); | ||
92 | QCOMPARE(textEditedSpy.count(),0); | 92 | QCOMPARE(textEditedSpy.count(),0); | ||
93 | #ifndef KCOMPLETION_NO_DEPRECATED | 93 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
94 | QCOMPARE(userTextChangedSpy.count(),1); | 94 | QCOMPARE(userTextChangedSpy.count(),1); | ||
95 | #endif | 95 | #endif | ||
96 | 96 | | |||
97 | //set the text back for further tests below | 97 | //set the text back for further tests below | ||
98 | w.setText(QStringLiteral("Hello worl")); | 98 | w.setText(QStringLiteral("Hello worl")); | ||
99 | #ifndef KCOMPLETION_NO_DEPRECATED | 99 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
100 | userTextChangedSpy.clear(); | 100 | userTextChangedSpy.clear(); | ||
101 | #endif | 101 | #endif | ||
102 | textChangedSpy.clear(); | 102 | textChangedSpy.clear(); | ||
103 | textEditedSpy.clear(); | 103 | textEditedSpy.clear(); | ||
104 | 104 | | |||
105 | // typing emits all three signals | 105 | // typing emits all three signals | ||
106 | QTest::keyClick(&w, Qt::Key_D); | 106 | QTest::keyClick(&w, Qt::Key_D); | ||
107 | QCOMPARE(w.text(), QString::fromLatin1("Hello world")); | 107 | QCOMPARE(w.text(), QString::fromLatin1("Hello world")); | ||
108 | #ifndef KCOMPLETION_NO_DEPRECATED | 108 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
109 | QCOMPARE(userTextChangedSpy.count(), 1); | 109 | QCOMPARE(userTextChangedSpy.count(), 1); | ||
110 | QCOMPARE(userTextChangedSpy[0][0].toString(), w.text()); | 110 | QCOMPARE(userTextChangedSpy[0][0].toString(), w.text()); | ||
111 | #endif | 111 | #endif | ||
112 | QCOMPARE(textChangedSpy.count(), 1); | 112 | QCOMPARE(textChangedSpy.count(), 1); | ||
113 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | 113 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | ||
114 | QCOMPARE(textEditedSpy.count(), 1); | 114 | QCOMPARE(textEditedSpy.count(), 1); | ||
115 | QCOMPARE(textEditedSpy[0][0].toString(), w.text()); | 115 | QCOMPARE(textEditedSpy[0][0].toString(), w.text()); | ||
116 | QVERIFY(w.isModified()); | 116 | QVERIFY(w.isModified()); | ||
117 | 117 | | |||
118 | w.setText(QStringLiteral("K")); // prepare for next test | 118 | w.setText(QStringLiteral("K")); // prepare for next test | ||
119 | #ifndef KCOMPLETION_NO_DEPRECATED | 119 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
120 | userTextChangedSpy.clear(); | 120 | userTextChangedSpy.clear(); | ||
121 | #endif | 121 | #endif | ||
122 | textChangedSpy.clear(); | 122 | textChangedSpy.clear(); | ||
123 | textEditedSpy.clear(); | 123 | textEditedSpy.clear(); | ||
124 | QVERIFY(!w.isModified()); | 124 | QVERIFY(!w.isModified()); | ||
125 | 125 | | |||
126 | // the suggestion from auto completion emits textChanged but not userTextChanged nor textEdited | 126 | // the suggestion from auto completion emits textChanged but not userTextChanged nor textEdited | ||
127 | w.setCompletionMode(KCompletion::CompletionAuto); | 127 | w.setCompletionMode(KCompletion::CompletionAuto); | ||
128 | KCompletion completion; | 128 | KCompletion completion; | ||
129 | completion.setSoundsEnabled(false); | 129 | completion.setSoundsEnabled(false); | ||
130 | QStringList items; | 130 | QStringList items; | ||
131 | items << QStringLiteral("KDE is cool") << QStringLiteral("KDE is really cool"); | 131 | items << QStringLiteral("KDE is cool") << QStringLiteral("KDE is really cool"); | ||
132 | completion.setItems(items); | 132 | completion.setItems(items); | ||
133 | w.setCompletionObject(&completion); | 133 | w.setCompletionObject(&completion); | ||
134 | 134 | | |||
135 | w.doCompletion(w.text()); | 135 | w.doCompletion(w.text()); | ||
136 | QCOMPARE(w.text(), items.at(0)); | 136 | QCOMPARE(w.text(), items.at(0)); | ||
137 | 137 | | |||
138 | #ifndef KCOMPLETION_NO_DEPRECATED | 138 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
139 | QCOMPARE(userTextChangedSpy.count(), 0); | 139 | QCOMPARE(userTextChangedSpy.count(), 0); | ||
140 | #endif | 140 | #endif | ||
141 | QCOMPARE(textChangedSpy.count(), 1); | 141 | QCOMPARE(textChangedSpy.count(), 1); | ||
142 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | 142 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | ||
143 | QCOMPARE(textEditedSpy.count(), 0); | 143 | QCOMPARE(textEditedSpy.count(), 0); | ||
144 | QVERIFY(!w.isModified()); | 144 | QVERIFY(!w.isModified()); | ||
145 | #ifndef KCOMPLETION_NO_DEPRECATED | 145 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
146 | userTextChangedSpy.clear(); | 146 | userTextChangedSpy.clear(); | ||
147 | #endif | 147 | #endif | ||
148 | textChangedSpy.clear(); | 148 | textChangedSpy.clear(); | ||
149 | textEditedSpy.clear(); | 149 | textEditedSpy.clear(); | ||
150 | 150 | | |||
151 | // accepting the completion suggestion now emits all three signals too | 151 | // accepting the completion suggestion now emits all three signals too | ||
152 | QTest::keyClick(&w, Qt::Key_Right); | 152 | QTest::keyClick(&w, Qt::Key_Right); | ||
153 | QCOMPARE(w.text(), items.at(0)); | 153 | QCOMPARE(w.text(), items.at(0)); | ||
154 | 154 | | |||
155 | #ifndef KCOMPLETION_NO_DEPRECATED | 155 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
156 | QCOMPARE(userTextChangedSpy.count(), 1); | 156 | QCOMPARE(userTextChangedSpy.count(), 1); | ||
157 | QCOMPARE(userTextChangedSpy[0][0].toString(), w.text()); | 157 | QCOMPARE(userTextChangedSpy[0][0].toString(), w.text()); | ||
158 | #endif | 158 | #endif | ||
159 | QCOMPARE(textChangedSpy.count(), 1); | 159 | QCOMPARE(textChangedSpy.count(), 1); | ||
160 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | 160 | QCOMPARE(textChangedSpy[0][0].toString(), w.text()); | ||
161 | QCOMPARE(textEditedSpy.count(), 1); | 161 | QCOMPARE(textEditedSpy.count(), 1); | ||
162 | QCOMPARE(textEditedSpy[0][0].toString(), w.text()); | 162 | QCOMPARE(textEditedSpy[0][0].toString(), w.text()); | ||
163 | QVERIFY(w.isModified()); | 163 | QVERIFY(w.isModified()); | ||
164 | #ifndef KCOMPLETION_NO_DEPRECATED | 164 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
165 | userTextChangedSpy.clear(); | 165 | userTextChangedSpy.clear(); | ||
166 | #endif | 166 | #endif | ||
167 | textChangedSpy.clear(); | 167 | textChangedSpy.clear(); | ||
168 | textEditedSpy.clear(); | 168 | textEditedSpy.clear(); | ||
169 | 169 | | |||
170 | // Now with popup completion | 170 | // Now with popup completion | ||
171 | w.setCompletionMode(KCompletion::CompletionPopup); | 171 | w.setCompletionMode(KCompletion::CompletionPopup); | ||
172 | w.setText(QStringLiteral("KDE")); | 172 | w.setText(QStringLiteral("KDE")); | ||
173 | QVERIFY(!w.isModified()); | 173 | QVERIFY(!w.isModified()); | ||
174 | #ifndef KCOMPLETION_NO_DEPRECATED | 174 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
175 | userTextChangedSpy.clear(); | 175 | userTextChangedSpy.clear(); | ||
176 | #endif | 176 | #endif | ||
177 | textChangedSpy.clear(); | 177 | textChangedSpy.clear(); | ||
178 | textEditedSpy.clear(); | 178 | textEditedSpy.clear(); | ||
179 | w.doCompletion(w.text()); // popup appears | 179 | w.doCompletion(w.text()); // popup appears | ||
180 | QCOMPARE(w.text(), QString::fromLatin1("KDE")); | 180 | QCOMPARE(w.text(), QString::fromLatin1("KDE")); | ||
181 | #ifndef KCOMPLETION_NO_DEPRECATED | 181 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
182 | QCOMPARE(textChangedSpy.count() + userTextChangedSpy.count() + textEditedSpy.count(), 0); | 182 | QCOMPARE(textChangedSpy.count() + userTextChangedSpy.count() + textEditedSpy.count(), 0); | ||
183 | #else | 183 | #else | ||
184 | QCOMPARE(textChangedSpy.count() + textEditedSpy.count(), 0); | 184 | QCOMPARE(textChangedSpy.count() + textEditedSpy.count(), 0); | ||
185 | #endif | 185 | #endif | ||
186 | w.completionBox()->down(); // select 1st item | 186 | w.completionBox()->down(); // select 1st item | ||
187 | QCOMPARE(w.text(), items.at(0)); | 187 | QCOMPARE(w.text(), items.at(0)); | ||
188 | QVERIFY(w.isModified()); | 188 | QVERIFY(w.isModified()); | ||
189 | w.completionBox()->down(); // select 2nd item | 189 | w.completionBox()->down(); // select 2nd item | ||
190 | QCOMPARE(w.text(), items.at(1)); | 190 | QCOMPARE(w.text(), items.at(1)); | ||
191 | 191 | | |||
192 | // Selecting an item in the popup completion changes the lineedit text | 192 | // Selecting an item in the popup completion changes the lineedit text | ||
193 | // and emits textChanged and userTextChanged, but not textEdited. | 193 | // and emits textChanged and userTextChanged, but not textEdited. | ||
194 | #ifndef KCOMPLETION_NO_DEPRECATED | 194 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
195 | QCOMPARE(userTextChangedSpy.count(), 2); | 195 | QCOMPARE(userTextChangedSpy.count(), 2); | ||
196 | #endif | 196 | #endif | ||
197 | QCOMPARE(textChangedSpy.count(), 2); | 197 | QCOMPARE(textChangedSpy.count(), 2); | ||
198 | QCOMPARE(textEditedSpy.count(), 0); | 198 | QCOMPARE(textEditedSpy.count(), 0); | ||
199 | #ifndef KCOMPLETION_NO_DEPRECATED | 199 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
200 | userTextChangedSpy.clear(); | 200 | userTextChangedSpy.clear(); | ||
201 | #endif | 201 | #endif | ||
202 | textChangedSpy.clear(); | 202 | textChangedSpy.clear(); | ||
203 | textEditedSpy.clear(); | 203 | textEditedSpy.clear(); | ||
204 | 204 | | |||
205 | QTest::keyClick(&w, Qt::Key_Enter); // activate | 205 | QTest::keyClick(&w, Qt::Key_Enter); // activate | ||
206 | QVERIFY(!w.completionBox()->isVisible()); | 206 | QVERIFY(!w.completionBox()->isVisible()); | ||
207 | QCOMPARE(w.text(), items.at(1)); | 207 | QCOMPARE(w.text(), items.at(1)); | ||
208 | QVERIFY(w.isModified()); | 208 | QVERIFY(w.isModified()); | ||
209 | // Nothing else happens, the text was already set in the lineedit | 209 | // Nothing else happens, the text was already set in the lineedit | ||
210 | QCOMPARE(textChangedSpy.count(), 0); | 210 | QCOMPARE(textChangedSpy.count(), 0); | ||
211 | QCOMPARE(textEditedSpy.count(), 0); | 211 | QCOMPARE(textEditedSpy.count(), 0); | ||
212 | #ifndef KCOMPLETION_NO_DEPRECATED | 212 | #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 5) | ||
213 | QCOMPARE(userTextChangedSpy.count(), 0); | 213 | QCOMPARE(userTextChangedSpy.count(), 0); | ||
214 | #endif | 214 | #endif | ||
215 | 215 | | |||
216 | // Now when using the mouse in the popup completion | 216 | // Now when using the mouse in the popup completion | ||
217 | w.setText(QStringLiteral("KDE")); | 217 | w.setText(QStringLiteral("KDE")); | ||
218 | w.doCompletion(w.text()); // popup appears | 218 | w.doCompletion(w.text()); // popup appears | ||
219 | QCOMPARE(w.text(), QString::fromLatin1("KDE")); | 219 | QCOMPARE(w.text(), QString::fromLatin1("KDE")); | ||
220 | // Selecting an item in the popup completion changes the lineedit text and emits all 3 signals | 220 | // Selecting an item in the popup completion changes the lineedit text and emits all 3 signals | ||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |