Allow QML URLs to contain pre-encoded octets
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeaccessibility / tst_qdeclarativeaccessibility.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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 QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44 #include "QtTest/qtestaccessible.h"
45
46 #include <QtGui/qaccessible.h>
47
48 #include <QtQuick1/qdeclarativeview.h>
49 #include <QtQuick/qquickview.h>
50 #include <QtQuick/qquickitem.h>
51
52 #include <QtDeclarative/qdeclarativeengine.h>
53 #include <QtDeclarative/qdeclarativeproperty.h>
54 #include <private/qdeclarativeaccessibleattached_p.h>
55
56 #include "../../shared/util.h"
57
58
59 typedef QSharedPointer<QAccessibleInterface> QAI;
60
61
62 static inline bool verifyChild(QWidget *child, QAccessibleInterface *iface,
63                                int index, const QRect &domain)
64 {
65     if (!child) {
66         qWarning("tst_QAccessibility::verifyChild: null pointer to child.");
67         return false;
68     }
69
70     if (!iface) {
71         qWarning("tst_QAccessibility::verifyChild: null pointer to interface.");
72         return false;
73     }
74
75     // Verify that we get a valid QAccessibleInterface for the child.
76     QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(child);
77     if (!childInterface) {
78         qWarning("tst_QAccessibility::verifyChild: Failed to retrieve interface for child.");
79         return false;
80     }
81
82     // QAccessibleInterface::indexOfChild():
83     // Verify that indexOfChild() returns an index equal to the index passed in
84     int indexFromIndexOfChild = iface->indexOfChild(childInterface);
85     delete childInterface;
86     if (indexFromIndexOfChild != index) {
87         qWarning("tst_QAccessibility::verifyChild (indexOfChild()):");
88         qWarning() << "Expected:" << index;
89         qWarning() << "Actual:  " << indexFromIndexOfChild;
90         return false;
91     }
92
93     // Navigate to child, compare its object and role with the interface from queryAccessibleInterface(child).
94     QAccessibleInterface *navigatedChildInterface = iface->child(index - 1);
95     if (navigatedChildInterface == 0)
96         return false;
97
98     const QRect rectFromInterface = navigatedChildInterface->rect();
99     delete navigatedChildInterface;
100
101     // QAccessibleInterface::childAt():
102     // Calculate global child position and check that the interface
103     // returns the correct index for that position.
104     QPoint globalChildPos = child->mapToGlobal(QPoint(0, 0));
105     QAccessibleInterface *childAtInterface = iface->childAt(globalChildPos.x(), globalChildPos.y());
106     if (!childAtInterface) {
107         qWarning("tst_QAccessibility::verifyChild (childAt()):");
108         qWarning() << "Expected:" << childInterface;
109         qWarning() << "Actual:  no child";
110         return false;
111     }
112     if (childAtInterface->object() != childInterface->object()) {
113         qWarning("tst_QAccessibility::verifyChild (childAt()):");
114         qWarning() << "Expected:" << childInterface;
115         qWarning() << "Actual:  " << childAtInterface;
116         return false;
117     }
118     delete childInterface;
119     delete childAtInterface;
120
121     // Verify that the child is within its domain.
122     if (!domain.contains(rectFromInterface)) {
123         qWarning("tst_QAccessibility::verifyChild: Child is not within its domain.");
124         return false;
125     }
126
127     return true;
128 }
129
130 static inline int indexOfChild(QAccessibleInterface *parentInterface, QWidget *childWidget)
131 {
132     if (!parentInterface || !childWidget)
133         return -1;
134     QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(childWidget);
135     if (!childInterface)
136         return -1;
137     int index = parentInterface->indexOfChild(childInterface);
138     delete childInterface;
139     return index;
140 }
141
142 #define EXPECT(cond) \
143     do { \
144         if (!errorAt && !(cond)) { \
145             errorAt = __LINE__; \
146             qWarning("level: %d, middle: %d, role: %d (%s)", treelevel, middle, iface->role(), #cond); \
147         } \
148     } while (0)
149
150 static int verifyHierarchy(QAccessibleInterface *iface)
151 {
152     int errorAt = 0;
153     static int treelevel = 0;   // for error diagnostics
154     QAccessibleInterface *middleChild, *if2;
155     middleChild = 0;
156     ++treelevel;
157     int middle = iface->childCount()/2 + 1;
158     if (iface->childCount() >= 2) {
159         middleChild = iface->child(middle - 1);
160     }
161     for (int i = 0; i < iface->childCount() && !errorAt; ++i) {
162         if2 = iface->child(i);
163         EXPECT(if2 != 0);
164         // navigate Ancestor...
165         QAccessibleInterface *parent = if2->parent();
166         EXPECT(iface->object() == parent->object());
167         delete parent;
168
169             // navigate Sibling...
170 //            if (middleChild) {
171 //                entry = if2->navigate(QAccessible::Sibling, middle, &if3);
172 //                EXPECT(entry == 0 && if3->object() == middleChild->object());
173 //                if (entry == 0)
174 //                    delete if3;
175 //                EXPECT(iface->indexOfChild(middleChild) == middle);
176 //            }
177
178         // verify children...
179         if (!errorAt)
180             errorAt = verifyHierarchy(if2);
181         delete if2;
182     }
183     delete middleChild;
184
185     --treelevel;
186     return errorAt;
187 }
188
189
190 //TESTED_FILES=
191
192 class tst_QDeclarativeAccessibility : public QDeclarativeDataTest
193 {
194     Q_OBJECT
195 public:
196     tst_QDeclarativeAccessibility();
197     virtual ~tst_QDeclarativeAccessibility();
198
199 private slots:
200     void commonTests_data();
201     void commonTests();
202
203     void declarativeAttachedProperties();
204     void basicPropertiesTest();
205     void hitTest();
206     void checkableTest();
207 };
208
209 tst_QDeclarativeAccessibility::tst_QDeclarativeAccessibility()
210 {
211
212 }
213
214 tst_QDeclarativeAccessibility::~tst_QDeclarativeAccessibility()
215 {
216
217 }
218
219 void tst_QDeclarativeAccessibility::commonTests_data()
220 {
221     QTest::addColumn<QString>("accessibleRoleFileName");
222
223     QTest::newRow("StaticText") << SRCDIR "/data/statictext.qml";
224     QTest::newRow("PushButton") << SRCDIR "/data/pushbutton.qml";
225 }
226
227 void tst_QDeclarativeAccessibility::commonTests()
228 {
229     QFETCH(QString, accessibleRoleFileName);
230
231     qDebug() << "testing" << accessibleRoleFileName;
232
233     QQuickView *view = new QQuickView();
234 //    view->setFixedSize(240,320);
235     view->setSource(QUrl::fromLocalFile(accessibleRoleFileName));
236     view->show();
237 //    view->setFocus();
238     QVERIFY(view->rootObject() != 0);
239
240     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(view);
241     QVERIFY(iface);
242
243     delete iface;
244     delete view;
245 }
246
247
248
249 QString eventName(const int ev)
250 {
251     switch (ev) {
252     case 0x0001: return "SoundPlayed";
253     case 0x0002: return "Alert";
254     case 0x0003: return "ForegroundChanged";
255     case 0x0004: return "MenuStart";
256     case 0x0005: return "MenuEnd";
257     case 0x0006: return "PopupMenuStart";
258     case 0x0007: return "PopupMenuEnd";
259     case 0x000C: return "ContextHelpStart";
260     case 0x000D: return "ContextHelpEnd";
261     case 0x000E: return "DragDropStart";
262     case 0x000F: return "DragDropEnd";
263     case 0x0010: return "DialogStart";
264     case 0x0011: return "DialogEnd";
265     case 0x0012: return "ScrollingStart";
266     case 0x0013: return "ScrollingEnd";
267     case 0x0018: return "MenuCommand";
268     case 0x8000: return "ObjectCreated";
269     case 0x8001: return "ObjectDestroyed";
270     case 0x8002: return "ObjectShow";
271     case 0x8003: return "ObjectHide";
272     case 0x8004: return "ObjectReorder";
273     case 0x8005: return "Focus";
274     case 0x8006: return "Selection";
275     case 0x8007: return "SelectionAdd";
276     case 0x8008: return "SelectionRemove";
277     case 0x8009: return "SelectionWithin";
278     case 0x800A: return "StateChanged";
279     case 0x800B: return "LocationChanged";
280     case 0x800C: return "NameChanged";
281     case 0x800D: return "DescriptionChanged";
282     case 0x800E: return "ValueChanged";
283     case 0x800F: return "ParentChanged";
284     case 0x80A0: return "HelpChanged";
285     case 0x80B0: return "DefaultActionChanged";
286     case 0x80C0: return "AcceleratorChanged";
287     default: return "Unknown Event";
288     }
289 }
290
291 void tst_QDeclarativeAccessibility::declarativeAttachedProperties()
292 {
293     {
294         QDeclarativeEngine engine;
295         QDeclarativeComponent component(&engine);
296         component.setData("import QtQuick 1.1\nItem {\n"
297                                 "}", QUrl());
298         QObject *object = component.create();
299         QVERIFY(object != 0);
300
301         QObject *attachedObject = QDeclarativeAccessibleAttached::attachedProperties(object);
302         QCOMPARE(attachedObject, static_cast<QObject*>(0));
303         delete object;
304     }
305
306     // Attached property
307     {
308         QObject parent;
309         QDeclarativeAccessibleAttached *attachedObj = new QDeclarativeAccessibleAttached(&parent);
310
311         attachedObj->name();
312
313         QVariant pp = attachedObj->property("name");
314         QDeclarativeEngine engine;
315         QDeclarativeComponent component(&engine);
316         component.setData("import QtQuick 1.1\nItem {\n"
317                                 "Accessible.role: Accessible.Button\n"
318                                 "}", QUrl());
319         QObject *object = component.create();
320         QVERIFY(object != 0);
321
322         QObject *attachedObject = QDeclarativeAccessibleAttached::attachedProperties(object);
323         QVERIFY(attachedObject);
324         if (attachedObject) {
325             QVariant p = attachedObject->property("role");
326             QCOMPARE(p.isNull(), false);
327             QCOMPARE(p.toInt(), int(QAccessible::PushButton));
328             p = attachedObject->property("name");
329             QCOMPARE(p.isNull(), true);
330             p = attachedObject->property("description");
331             QCOMPARE(p.isNull(), true);
332         }
333         delete object;
334     }
335
336     // Attached property
337     {
338         QDeclarativeEngine engine;
339         QDeclarativeComponent component(&engine);
340         component.setData("import QtQuick 1.1\nItem {\n"
341                                 "Accessible.role: Accessible.Button\n"
342                                 "Accessible.name: \"Donald\"\n"
343                                 "Accessible.description: \"Duck\"\n"
344                                 "}", QUrl());
345         QObject *object = component.create();
346         QVERIFY(object != 0);
347
348         QObject *attachedObject = QDeclarativeAccessibleAttached::attachedProperties(object);
349         QVERIFY(attachedObject);
350         if (attachedObject) {
351             QVariant p = attachedObject->property("role");
352             QCOMPARE(p.isNull(), false);
353             QCOMPARE(p.toInt(), int(QAccessible::PushButton));
354             p = attachedObject->property("name");
355             QCOMPARE(p.isNull(), false);
356             QCOMPARE(p.toString(), QLatin1String("Donald"));
357             p = attachedObject->property("description");
358             QCOMPARE(p.isNull(), false);
359             QCOMPARE(p.toString(), QLatin1String("Duck"));
360         }
361         delete object;
362     }
363 }
364
365
366 void tst_QDeclarativeAccessibility::basicPropertiesTest()
367 {
368     QAI app = QAI(QAccessible::queryAccessibleInterface(qApp));
369     QCOMPARE(app->childCount(), 0);
370
371     QQuickView *canvas = new QQuickView();
372     canvas->setSource(testFileUrl("statictext.qml"));
373     canvas->show();
374     QCOMPARE(app->childCount(), 1);
375
376     QAI iface = QAI(QAccessible::queryAccessibleInterface(canvas));
377     QVERIFY(iface.data());
378     QCOMPARE(iface->childCount(), 1);
379
380     QAI item = QAI(iface->child(0));
381     QVERIFY(item.data());
382     QCOMPARE(item->childCount(), 2);
383     QCOMPARE(item->rect().size(), QSize(400, 400));
384     QCOMPARE(item->role(), QAccessible::Pane);
385     QCOMPARE(iface->indexOfChild(item.data()), 0);
386
387     QAI text = QAI(item->child(0));
388     QVERIFY(text.data());
389     QCOMPARE(text->childCount(), 0);
390
391     QCOMPARE(text->text(QAccessible::Name), QLatin1String("Hello Accessibility"));
392     QCOMPARE(text->rect().size(), QSize(200, 50));
393     QCOMPARE(text->rect().x(), item->rect().x() + 100);
394     QCOMPARE(text->rect().y(), item->rect().y() + 20);
395     QCOMPARE(text->role(), QAccessible::StaticText);
396     QCOMPARE(item->indexOfChild(text.data()), 0);
397
398     QAI text2 = QAI(item->child(1));
399     QVERIFY(text2.data());
400     QCOMPARE(text2->childCount(), 0);
401
402     QCOMPARE(text2->text(QAccessible::Name), QLatin1String("The Hello 2 accessible text"));
403     QCOMPARE(text2->rect().size(), QSize(100, 40));
404     QCOMPARE(text2->rect().x(), item->rect().x() + 100);
405     QCOMPARE(text2->rect().y(), item->rect().y() + 40);
406     QCOMPARE(text2->role(), QAccessible::StaticText);
407     QCOMPARE(item->indexOfChild(text2.data()), 1);
408
409     QCOMPARE(iface->indexOfChild(text2.data()), -1);
410     QCOMPARE(text2->indexOfChild(item.data()), -1);
411
412     delete canvas;
413 }
414
415 QAI topLevelChildAt(QAccessibleInterface *iface, int x, int y)
416 {
417     QAI child = QAI(iface->childAt(x, y));
418     if (!child)
419         return QAI();
420
421     QAI childOfChild;
422     while (childOfChild = QAI(child->childAt(x, y))) {
423         child = childOfChild;
424     }
425     return child;
426 }
427
428 void tst_QDeclarativeAccessibility::hitTest()
429 {
430     QQuickView *canvas = new QQuickView;
431     canvas->setSource(testFileUrl("hittest.qml"));
432     canvas->show();
433
434     QAI iface = QAI(QAccessible::queryAccessibleInterface(canvas));
435     QVERIFY(iface.data());
436     QAI rootItem = QAI(iface->child(0));
437     QRect rootRect = rootItem->rect();
438
439     // hit the root item
440     QAI itemHit(iface->childAt(rootRect.x() + 200, rootRect.y() + 50));
441     QVERIFY(itemHit);
442     QCOMPARE(rootRect, itemHit->rect());
443
444     // hit rect1
445     QAI rect1(rootItem->child(1));
446     QRect rect1Rect = rect1->rect();
447     itemHit = QAI(rootItem->childAt(rect1Rect.x() + 10, rect1Rect.y() + 10));
448     QVERIFY(itemHit);
449     QCOMPARE(rect1Rect, itemHit->rect());
450     QCOMPARE(itemHit->text(QAccessible::Name), QLatin1String("rect1"));
451
452     // should also work from top level (app)
453     QAI app(QAccessible::queryAccessibleInterface(qApp));
454     QAI itemHit2(topLevelChildAt(app.data(), rect1Rect.x() + 10, rect1Rect.y() + 10));
455     QVERIFY(itemHit2);
456     QCOMPARE(itemHit2->rect(), rect1Rect);
457     QCOMPARE(itemHit2->text(QAccessible::Name), QLatin1String("rect1"));
458
459     // hit rect201
460     QAI rect2(rootItem->child(2));
461     QAI rect20(rect2->child(1));
462     QAI rect201(rect20->child(2));
463     QVERIFY(rect201);
464
465     QRect rect201Rect = rect201->rect();
466     itemHit = QAI(iface->childAt(rect201Rect.x() + 20, rect201Rect.y() + 20));
467     QVERIFY(itemHit);
468     QCOMPARE(itemHit->rect(), rect201Rect);
469     QCOMPARE(itemHit->text(QAccessible::Name), QLatin1String("rect201"));
470
471     delete canvas;
472 }
473
474 void tst_QDeclarativeAccessibility::checkableTest()
475 {
476     QQuickView *canvas = new QQuickView();
477     canvas->setSource(testFileUrl("checkbuttons.qml"));
478     canvas->show();
479
480     QAI iface = QAI(QAccessible::queryAccessibleInterface(canvas));
481     QVERIFY(iface.data());
482     QAI root = QAI(iface->child(0));
483
484     QAI button1 = QAI(root->child(0));
485     QCOMPARE(button1->role(), QAccessible::Button);
486     QVERIFY(!(button1->state().checked));
487     QAI button2 = QAI(root->child(1));
488     QVERIFY(!(button2->state().checked));
489     QAI button3 = QAI(root->child(2));
490     QVERIFY(button3->state().checked);
491
492     QAI checkBox1 = QAI(root->child(3));
493     QCOMPARE(checkBox1->role(), QAccessible::CheckBox);
494     QVERIFY((checkBox1->state().checked));
495     QAI checkBox2 = QAI(root->child(4));
496     QVERIFY(!(checkBox2->state().checked));
497 }
498
499 QTEST_MAIN(tst_QDeclarativeAccessibility)
500
501 #include "tst_qdeclarativeaccessibility.moc"