|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 #include <qtest.h> |
|
42 #include <QSignalSpy> |
|
43 #include <QtDeclarative/qdeclarativeengine.h> |
|
44 #include <QtDeclarative/qdeclarativecomponent.h> |
|
45 #include <QtDeclarative/qdeclarativeview.h> |
|
46 #include <private/qdeclarativerectangle_p.h> |
|
47 #include <private/qdeclarativetextedit_p.h> |
|
48 #include <private/qdeclarativetext_p.h> |
|
49 #include <QtDeclarative/private/qdeclarativefocusscope_p.h> |
|
50 |
|
51 |
|
52 class tst_qdeclarativefocusscope : public QObject |
|
53 { |
|
54 Q_OBJECT |
|
55 public: |
|
56 tst_qdeclarativefocusscope() {} |
|
57 |
|
58 template<typename T> |
|
59 T *findItem(QGraphicsObject *parent, const QString &id); |
|
60 |
|
61 private slots: |
|
62 void basic(); |
|
63 void nested(); |
|
64 void noFocus(); |
|
65 void textEdit(); |
|
66 void forceFocus(); |
|
67 }; |
|
68 |
|
69 /* |
|
70 Find an item with the specified id. |
|
71 */ |
|
72 template<typename T> |
|
73 T *tst_qdeclarativefocusscope::findItem(QGraphicsObject *parent, const QString &objectName) |
|
74 { |
|
75 const QMetaObject &mo = T::staticMetaObject; |
|
76 QList<QGraphicsItem *> children = parent->childItems(); |
|
77 for (int i = 0; i < children.count(); ++i) { |
|
78 QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(children.at(i)->toGraphicsObject()); |
|
79 if (item) { |
|
80 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) { |
|
81 return static_cast<T*>(item); |
|
82 } |
|
83 item = findItem<T>(item, objectName); |
|
84 if (item) |
|
85 return static_cast<T*>(item); |
|
86 } |
|
87 } |
|
88 return 0; |
|
89 } |
|
90 |
|
91 void tst_qdeclarativefocusscope::basic() |
|
92 { |
|
93 QDeclarativeView *view = new QDeclarativeView; |
|
94 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test.qml")); |
|
95 |
|
96 QDeclarativeRectangle *item0 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item0")); |
|
97 QDeclarativeRectangle *item1 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item1")); |
|
98 QDeclarativeRectangle *item2 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item2")); |
|
99 QDeclarativeRectangle *item3 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item3")); |
|
100 QVERIFY(item0 != 0); |
|
101 QVERIFY(item1 != 0); |
|
102 QVERIFY(item2 != 0); |
|
103 QVERIFY(item3 != 0); |
|
104 |
|
105 view->show(); |
|
106 qApp->setActiveWindow(view); |
|
107 qApp->processEvents(); |
|
108 |
|
109 #ifdef Q_WS_X11 |
|
110 // to be safe and avoid failing setFocus with window managers |
|
111 qt_x11_wait_for_window_manager(view); |
|
112 #endif |
|
113 |
|
114 QVERIFY(view->hasFocus()); |
|
115 QVERIFY(view->scene()->hasFocus()); |
|
116 QVERIFY(item0->wantsFocus() == true); |
|
117 QVERIFY(item1->hasFocus() == true); |
|
118 QVERIFY(item2->hasFocus() == false); |
|
119 QVERIFY(item3->hasFocus() == false); |
|
120 |
|
121 QTest::keyClick(view, Qt::Key_Right); |
|
122 QVERIFY(item0->wantsFocus() == true); |
|
123 QVERIFY(item1->hasFocus() == false); |
|
124 QVERIFY(item2->hasFocus() == true); |
|
125 QVERIFY(item3->hasFocus() == false); |
|
126 |
|
127 QTest::keyClick(view, Qt::Key_Down); |
|
128 QVERIFY(item0->wantsFocus() == false); |
|
129 QVERIFY(item1->hasFocus() == false); |
|
130 QVERIFY(item2->hasFocus() == false); |
|
131 QVERIFY(item3->hasFocus() == true); |
|
132 |
|
133 delete view; |
|
134 } |
|
135 |
|
136 void tst_qdeclarativefocusscope::nested() |
|
137 { |
|
138 QDeclarativeView *view = new QDeclarativeView; |
|
139 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test2.qml")); |
|
140 |
|
141 QDeclarativeFocusScope *item1 = findItem<QDeclarativeFocusScope>(view->rootObject(), QLatin1String("item1")); |
|
142 QDeclarativeFocusScope *item2 = findItem<QDeclarativeFocusScope>(view->rootObject(), QLatin1String("item2")); |
|
143 QDeclarativeFocusScope *item3 = findItem<QDeclarativeFocusScope>(view->rootObject(), QLatin1String("item3")); |
|
144 QDeclarativeFocusScope *item4 = findItem<QDeclarativeFocusScope>(view->rootObject(), QLatin1String("item4")); |
|
145 QDeclarativeFocusScope *item5 = findItem<QDeclarativeFocusScope>(view->rootObject(), QLatin1String("item5")); |
|
146 QVERIFY(item1 != 0); |
|
147 QVERIFY(item2 != 0); |
|
148 QVERIFY(item3 != 0); |
|
149 QVERIFY(item4 != 0); |
|
150 QVERIFY(item5 != 0); |
|
151 |
|
152 view->show(); |
|
153 qApp->setActiveWindow(view); |
|
154 qApp->processEvents(); |
|
155 |
|
156 #ifdef Q_WS_X11 |
|
157 // to be safe and avoid failing setFocus with window managers |
|
158 qt_x11_wait_for_window_manager(view); |
|
159 #endif |
|
160 |
|
161 QVERIFY(view->hasFocus()); |
|
162 QVERIFY(view->scene()->hasFocus()); |
|
163 |
|
164 QVERIFY(item1->wantsFocus() == true); |
|
165 QVERIFY(item1->hasFocus() == false); |
|
166 QVERIFY(item2->wantsFocus() == true); |
|
167 QVERIFY(item2->hasFocus() == false); |
|
168 QVERIFY(item3->wantsFocus() == true); |
|
169 QVERIFY(item3->hasFocus() == false); |
|
170 QVERIFY(item4->wantsFocus() == true); |
|
171 QVERIFY(item4->hasFocus() == false); |
|
172 QVERIFY(item5->wantsFocus() == true); |
|
173 QVERIFY(item5->hasFocus() == true); |
|
174 delete view; |
|
175 } |
|
176 |
|
177 void tst_qdeclarativefocusscope::noFocus() |
|
178 { |
|
179 QDeclarativeView *view = new QDeclarativeView; |
|
180 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test4.qml")); |
|
181 |
|
182 QDeclarativeRectangle *item0 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item0")); |
|
183 QDeclarativeRectangle *item1 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item1")); |
|
184 QDeclarativeRectangle *item2 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item2")); |
|
185 QDeclarativeRectangle *item3 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item3")); |
|
186 QVERIFY(item0 != 0); |
|
187 QVERIFY(item1 != 0); |
|
188 QVERIFY(item2 != 0); |
|
189 QVERIFY(item3 != 0); |
|
190 |
|
191 view->show(); |
|
192 qApp->setActiveWindow(view); |
|
193 qApp->processEvents(); |
|
194 |
|
195 #ifdef Q_WS_X11 |
|
196 // to be safe and avoid failing setFocus with window managers |
|
197 qt_x11_wait_for_window_manager(view); |
|
198 #endif |
|
199 |
|
200 QVERIFY(view->hasFocus()); |
|
201 QVERIFY(view->scene()->hasFocus()); |
|
202 QVERIFY(item0->wantsFocus() == false); |
|
203 QVERIFY(item1->hasFocus() == false); |
|
204 QVERIFY(item2->hasFocus() == false); |
|
205 QVERIFY(item3->hasFocus() == false); |
|
206 |
|
207 QTest::keyClick(view, Qt::Key_Right); |
|
208 QVERIFY(item0->wantsFocus() == false); |
|
209 QVERIFY(item1->hasFocus() == false); |
|
210 QVERIFY(item2->hasFocus() == false); |
|
211 QVERIFY(item3->hasFocus() == false); |
|
212 |
|
213 QTest::keyClick(view, Qt::Key_Down); |
|
214 QVERIFY(item0->wantsFocus() == false); |
|
215 QVERIFY(item1->hasFocus() == false); |
|
216 QVERIFY(item2->hasFocus() == false); |
|
217 QVERIFY(item3->hasFocus() == false); |
|
218 |
|
219 delete view; |
|
220 } |
|
221 |
|
222 void tst_qdeclarativefocusscope::textEdit() |
|
223 { |
|
224 QDeclarativeView *view = new QDeclarativeView; |
|
225 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test5.qml")); |
|
226 |
|
227 QDeclarativeRectangle *item0 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item0")); |
|
228 QDeclarativeTextEdit *item1 = findItem<QDeclarativeTextEdit>(view->rootObject(), QLatin1String("item1")); |
|
229 QDeclarativeRectangle *item2 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item2")); |
|
230 QDeclarativeTextEdit *item3 = findItem<QDeclarativeTextEdit>(view->rootObject(), QLatin1String("item3")); |
|
231 QVERIFY(item0 != 0); |
|
232 QVERIFY(item1 != 0); |
|
233 QVERIFY(item2 != 0); |
|
234 QVERIFY(item3 != 0); |
|
235 |
|
236 view->show(); |
|
237 qApp->setActiveWindow(view); |
|
238 qApp->processEvents(); |
|
239 |
|
240 #ifdef Q_WS_X11 |
|
241 // to be safe and avoid failing setFocus with window managers |
|
242 qt_x11_wait_for_window_manager(view); |
|
243 #endif |
|
244 |
|
245 QVERIFY(view->hasFocus()); |
|
246 QVERIFY(view->scene()->hasFocus()); |
|
247 QVERIFY(item0->wantsFocus() == true); |
|
248 QVERIFY(item1->hasFocus() == true); |
|
249 QVERIFY(item2->hasFocus() == false); |
|
250 QVERIFY(item3->hasFocus() == false); |
|
251 |
|
252 QTest::keyClick(view, Qt::Key_Right); |
|
253 QVERIFY(item0->wantsFocus() == true); |
|
254 QVERIFY(item1->hasFocus() == true); |
|
255 QVERIFY(item2->hasFocus() == false); |
|
256 QVERIFY(item3->hasFocus() == false); |
|
257 |
|
258 QTest::keyClick(view, Qt::Key_Right); |
|
259 QTest::keyClick(view, Qt::Key_Right); |
|
260 QTest::keyClick(view, Qt::Key_Right); |
|
261 QTest::keyClick(view, Qt::Key_Right); |
|
262 QTest::keyClick(view, Qt::Key_Right); |
|
263 QVERIFY(item0->wantsFocus() == true); |
|
264 QVERIFY(item1->hasFocus() == false); |
|
265 QVERIFY(item2->hasFocus() == true); |
|
266 QVERIFY(item3->hasFocus() == false); |
|
267 |
|
268 QTest::keyClick(view, Qt::Key_Down); |
|
269 QVERIFY(item0->wantsFocus() == false); |
|
270 QVERIFY(item1->hasFocus() == false); |
|
271 QVERIFY(item2->hasFocus() == false); |
|
272 QVERIFY(item3->hasFocus() == true); |
|
273 |
|
274 delete view; |
|
275 } |
|
276 |
|
277 void tst_qdeclarativefocusscope::forceFocus() |
|
278 { |
|
279 QDeclarativeView *view = new QDeclarativeView; |
|
280 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/forcefocus.qml")); |
|
281 |
|
282 QDeclarativeRectangle *item0 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item0")); |
|
283 QDeclarativeRectangle *item1 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item1")); |
|
284 QDeclarativeRectangle *item2 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item2")); |
|
285 QDeclarativeRectangle *item3 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item3")); |
|
286 QDeclarativeRectangle *item4 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item4")); |
|
287 QDeclarativeRectangle *item5 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item5")); |
|
288 QVERIFY(item0 != 0); |
|
289 QVERIFY(item1 != 0); |
|
290 QVERIFY(item2 != 0); |
|
291 QVERIFY(item3 != 0); |
|
292 QVERIFY(item4 != 0); |
|
293 QVERIFY(item5 != 0); |
|
294 |
|
295 view->show(); |
|
296 qApp->setActiveWindow(view); |
|
297 qApp->processEvents(); |
|
298 |
|
299 #ifdef Q_WS_X11 |
|
300 // to be safe and avoid failing setFocus with window managers |
|
301 qt_x11_wait_for_window_manager(view); |
|
302 #endif |
|
303 |
|
304 QVERIFY(view->hasFocus()); |
|
305 QVERIFY(view->scene()->hasFocus()); |
|
306 QVERIFY(item0->wantsFocus() == true); |
|
307 QVERIFY(item1->hasFocus() == true); |
|
308 QVERIFY(item2->hasFocus() == false); |
|
309 QVERIFY(item3->wantsFocus() == false); |
|
310 QVERIFY(item4->hasFocus() == false); |
|
311 QVERIFY(item5->hasFocus() == false); |
|
312 |
|
313 QTest::keyClick(view, Qt::Key_4); |
|
314 QVERIFY(item0->wantsFocus() == true); |
|
315 QVERIFY(item1->hasFocus() == true); |
|
316 QVERIFY(item2->hasFocus() == false); |
|
317 QVERIFY(item3->wantsFocus() == false); |
|
318 QVERIFY(item4->hasFocus() == false); |
|
319 QVERIFY(item5->hasFocus() == false); |
|
320 |
|
321 QTest::keyClick(view, Qt::Key_5); |
|
322 QVERIFY(item0->wantsFocus() == false); |
|
323 QVERIFY(item1->hasFocus() == false); |
|
324 QVERIFY(item2->hasFocus() == false); |
|
325 QVERIFY(item3->wantsFocus() == true); |
|
326 QVERIFY(item4->hasFocus() == false); |
|
327 QVERIFY(item5->hasFocus() == true); |
|
328 |
|
329 delete view; |
|
330 } |
|
331 |
|
332 |
|
333 QTEST_MAIN(tst_qdeclarativefocusscope) |
|
334 |
|
335 #include "tst_qdeclarativefocusscope.moc" |