Merge remote-tracking branch 'gerrit/master' into containers
[profile/ivi/qtbase.git] / tests / auto / other / qaccessibility / tst_qaccessibility.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44 #include <QtGui>
45 #include <QtWidgets>
46 #include <math.h>
47
48 #if defined(Q_OS_WIN) && defined(interface)
49 #   undef interface
50 #endif
51
52
53 #include "QtTest/qtestaccessible.h"
54
55 #if defined(Q_OS_WINCE)
56 extern "C" bool SystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni);
57 #define SPI_GETPLATFORMTYPE 257
58 inline bool IsValidCEPlatform() {
59     wchar_t tszPlatform[64];
60     if (SystemParametersInfo(SPI_GETPLATFORMTYPE, sizeof(tszPlatform) / sizeof(*tszPlatform), tszPlatform, 0)) {
61         QString platform = QString::fromWCharArray(tszPlatform);
62         if ((platform == QLatin1String("PocketPC")) || (platform == QLatin1String("Smartphone")))
63             return false;
64     }
65     return true;
66 }
67 #endif
68
69 typedef QSharedPointer<QAccessibleInterface> QAIPtr;
70
71 static inline bool verifyChild(QWidget *child, QAccessibleInterface *interface,
72                                int index, const QRect &domain)
73 {
74     if (!child) {
75         qWarning("tst_QAccessibility::verifyChild: null pointer to child.");
76         return false;
77     }
78
79     if (!interface) {
80         qWarning("tst_QAccessibility::verifyChild: null pointer to interface.");
81         return false;
82     }
83
84     // Verify that we get a valid QAccessibleInterface for the child.
85     QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(child);
86     if (!childInterface) {
87         qWarning("tst_QAccessibility::verifyChild: Failed to retrieve interface for child.");
88         return false;
89     }
90
91     // QAccessibleInterface::indexOfChild():
92     // Verify that indexOfChild() returns an index equal to the index passed in
93     int indexFromIndexOfChild = interface->indexOfChild(childInterface);
94     if (indexFromIndexOfChild != index) {
95         qWarning("tst_QAccessibility::verifyChild (indexOfChild()):");
96         qWarning() << "Expected:" << index;
97         qWarning() << "Actual:  " << indexFromIndexOfChild;
98         return false;
99     }
100
101     // Navigate to child, compare its object and role with the interface from queryAccessibleInterface(child).
102     QAccessibleInterface *navigatedChildInterface = interface->child(index);
103     if (navigatedChildInterface == 0)
104         return false;
105
106     const QRect rectFromInterface = navigatedChildInterface->rect();
107     delete navigatedChildInterface;
108
109     // QAccessibleInterface::childAt():
110     // Calculate global child position and check that the interface
111     // returns the correct index for that position.
112     QPoint globalChildPos = child->mapToGlobal(QPoint(0, 0));
113     QAccessibleInterface *childAtInterface = interface->childAt(globalChildPos.x(), globalChildPos.y());
114     if (!childAtInterface) {
115         qWarning("tst_QAccessibility::verifyChild (childAt()):");
116         qWarning() << "Expected:" << childInterface;
117         qWarning() << "Actual:  no child";
118         return false;
119     }
120     if (childAtInterface->object() != childInterface->object()) {
121         qWarning("tst_QAccessibility::verifyChild (childAt()):");
122         qWarning() << "Expected:" << childInterface;
123         qWarning() << "Actual:  " << childAtInterface;
124         return false;
125     }
126     delete childInterface;
127     delete childAtInterface;
128
129     // QAccessibleInterface::rect():
130     // Calculate global child geometry and check that the interface
131     // returns a QRect which is equal to the calculated QRect.
132     const QRect expectedGlobalRect = QRect(globalChildPos, child->size());
133     if (expectedGlobalRect != rectFromInterface) {
134         qWarning("tst_QAccessibility::verifyChild (rect()):");
135         qWarning() << "Expected:" << expectedGlobalRect;
136         qWarning() << "Actual:  " << rectFromInterface;
137         return false;
138     }
139
140     // Verify that the child is within its domain.
141     if (!domain.contains(rectFromInterface)) {
142         qWarning("tst_QAccessibility::verifyChild: Child is not within its domain.");
143         return false;
144     }
145
146     return true;
147 }
148
149 static inline int indexOfChild(QAccessibleInterface *parentInterface, QWidget *childWidget)
150 {
151     if (!parentInterface || !childWidget)
152         return -1;
153     QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(childWidget);
154     if (!childInterface)
155         return -1;
156     int index = parentInterface->indexOfChild(childInterface);
157     delete childInterface;
158     return index;
159 }
160
161 #define EXPECT(cond) \
162     do { \
163         if (!errorAt && !(cond)) { \
164             errorAt = __LINE__; \
165             qWarning("level: %d, middle: %d, role: %d (%s)", treelevel, middle, iface->role(), #cond); \
166         } \
167     } while (0)
168
169 static int verifyHierarchy(QAccessibleInterface *iface)
170 {
171     int errorAt = 0;
172     static int treelevel = 0;   // for error diagnostics
173     QAccessibleInterface *middleChild, *if2;
174     middleChild = 0;
175     ++treelevel;
176     int middle = iface->childCount()/2 + 1;
177     if (iface->childCount() >= 2) {
178         middleChild = iface->child(middle - 1);
179     }
180     for (int i = 0; i < iface->childCount() && !errorAt; ++i) {
181         if2 = iface->child(i);
182         EXPECT(if2 != 0);
183         // navigate Ancestor...
184         QAccessibleInterface *parent = if2->parent();
185         EXPECT(iface->object() == parent->object());
186         delete parent;
187
188             // navigate Sibling...
189 //            if (middleChild) {
190 //                entry = if2->navigate(QAccessible::Sibling, middle, &if3);
191 //                EXPECT(entry == 0 && if3->object() == middleChild->object());
192 //                if (entry == 0)
193 //                    delete if3;
194 //                EXPECT(iface->indexOfChild(middleChild) == middle);
195 //            }
196
197         // verify children...
198         if (!errorAt)
199             errorAt = verifyHierarchy(if2);
200         delete if2;
201     }
202     delete middleChild;
203
204     --treelevel;
205     return errorAt;
206 }
207
208 QRect childRect(QAccessibleInterface *iface, int index = 0)
209 {
210     QAccessibleInterface *child = iface->child(index);
211     QRect rect = child->rect();
212     delete child;
213     return rect;
214 }
215
216 class tst_QAccessibility : public QObject
217 {
218     Q_OBJECT
219 public:
220     tst_QAccessibility();
221     virtual ~tst_QAccessibility();
222
223 public slots:
224     void initTestCase();
225     void cleanupTestCase();
226     void init();
227     void cleanup();
228 private slots:
229     void eventTest();
230     void customWidget();
231     void deletedWidget();
232
233     void statesStructTest();
234     void navigateHierarchy();
235     void sliderTest();
236     void textAttributes();
237     void hideShowTest();
238
239     void actionTest();
240
241     void applicationTest();
242     void mainWindowTest();
243     void buttonTest();
244     void scrollBarTest();
245     void tabTest();
246     void tabWidgetTest();
247     void menuTest();
248     void spinBoxTest();
249     void doubleSpinBoxTest();
250     void textEditTest();
251     void textBrowserTest();
252     void mdiAreaTest();
253     void mdiSubWindowTest();
254     void lineEditTest();
255     void workspaceTest();
256     void dialogButtonBoxTest();
257     void dialTest();
258     void rubberBandTest();
259     void abstractScrollAreaTest();
260     void scrollAreaTest();
261
262     void listTest();
263     void treeTest();
264     void tableTest();
265
266     void calendarWidgetTest();
267     void dockWidgetTest();
268     void comboBoxTest();
269     void accessibleName();
270     void labelTest();
271     void accelerators();
272
273 protected slots:
274     void onClicked();
275 private:
276     int click_count;
277 };
278
279 const double Q_PI = 3.14159265358979323846;
280
281 QString eventName(const int ev)
282 {
283     switch(ev) {
284     case 0x0001: return "SoundPlayed";
285     case 0x0002: return "Alert";
286     case 0x0003: return "ForegroundChanged";
287     case 0x0004: return "MenuStart";
288     case 0x0005: return "MenuEnd";
289     case 0x0006: return "PopupMenuStart";
290     case 0x0007: return "PopupMenuEnd";
291     case 0x000C: return "ContextHelpStart";
292     case 0x000D: return "ContextHelpEnd";
293     case 0x000E: return "DragDropStart";
294     case 0x000F: return "DragDropEnd";
295     case 0x0010: return "DialogStart";
296     case 0x0011: return "DialogEnd";
297     case 0x0012: return "ScrollingStart";
298     case 0x0013: return "ScrollingEnd";
299     case 0x0018: return "MenuCommand";
300
301     case 0x0116: return "TableModelChanged";
302     case 0x011B: return "TextCaretMoved";
303
304     case 0x8000: return "ObjectCreated";
305     case 0x8001: return "ObjectDestroyed";
306     case 0x8002: return "ObjectShow";
307     case 0x8003: return "ObjectHide";
308     case 0x8004: return "ObjectReorder";
309     case 0x8005: return "Focus";
310     case 0x8006: return "Selection";
311     case 0x8007: return "SelectionAdd";
312     case 0x8008: return "SelectionRemove";
313     case 0x8009: return "SelectionWithin";
314     case 0x800A: return "StateChanged";
315     case 0x800B: return "LocationChanged";
316     case 0x800C: return "NameChanged";
317     case 0x800D: return "DescriptionChanged";
318     case 0x800E: return "ValueChanged";
319     case 0x800F: return "ParentChanged";
320     case 0x80A0: return "HelpChanged";
321     case 0x80B0: return "DefaultActionChanged";
322     case 0x80C0: return "AcceleratorChanged";
323     default: return "Unknown Event";
324     }
325 }
326
327 QAccessible::State state(QWidget * const widget)
328 {
329     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
330     if (!iface)
331         qWarning() << "Cannot get QAccessibleInterface for widget";
332     QAccessible::State state = (iface ? iface->state() : QAccessible::State());
333     delete iface;
334     return state;
335 }
336
337 class QtTestAccessibleWidget: public QWidget
338 {
339     Q_OBJECT
340 public:
341     QtTestAccessibleWidget(QWidget *parent, const char *name): QWidget(parent)
342     {
343         setObjectName(name);
344         QPalette pal;
345         pal.setColor(backgroundRole(), Qt::black);//black is beautiful
346         setPalette(pal);
347         setFixedSize(5, 5);
348     }
349 };
350
351 class QtTestAccessibleWidgetIface: public QAccessibleWidget
352 {
353 public:
354     QtTestAccessibleWidgetIface(QtTestAccessibleWidget *w): QAccessibleWidget(w) {}
355     QString text(QAccessible::Text t) const
356     {
357         if (t == QAccessible::Help)
358             return QString::fromLatin1("Help yourself");
359         return QAccessibleWidget::text(t);
360     }
361     static QAccessibleInterface *ifaceFactory(const QString &key, QObject *o)
362     {
363         if (key == "QtTestAccessibleWidget")
364             return new QtTestAccessibleWidgetIface(static_cast<QtTestAccessibleWidget*>(o));
365         return 0;
366     }
367 };
368
369 tst_QAccessibility::tst_QAccessibility()
370 {
371     click_count = 0;
372 }
373
374 tst_QAccessibility::~tst_QAccessibility()
375 {
376 }
377
378 void tst_QAccessibility::onClicked()
379 {
380     click_count++;
381 }
382
383 void tst_QAccessibility::initTestCase()
384 {
385     QTestAccessibility::initialize();
386     QAccessible::installFactory(QtTestAccessibleWidgetIface::ifaceFactory);
387 }
388
389 void tst_QAccessibility::cleanupTestCase()
390 {
391     QTestAccessibility::cleanup();
392 }
393
394 void tst_QAccessibility::init()
395 {
396     QTestAccessibility::clearEvents();
397 }
398
399 void tst_QAccessibility::cleanup()
400 {
401     const EventList list = QTestAccessibility::events();
402     if (!list.isEmpty()) {
403         qWarning("%d accessibility event(s) were not handled in testfunction '%s':", list.count(),
404                  QString(QTest::currentTestFunction()).toAscii().constData());
405         for (int i = 0; i < list.count(); ++i)
406             qWarning(" %d: Object: %p Event: '%s' (%d) Child: %d", i + 1, list.at(i).object,
407                      eventName(list.at(i).event).toAscii().constData(), list.at(i).event, list.at(i).child);
408     }
409     QTestAccessibility::clearEvents();
410 }
411
412 void tst_QAccessibility::eventTest()
413 {
414     QPushButton* button = new QPushButton(0);
415     button->setObjectName(QString("Olaf"));
416
417     button->show();
418     QVERIFY_EVENT(button, 0, QAccessible::ObjectShow);
419     button->setFocus(Qt::MouseFocusReason);
420     QTestAccessibility::clearEvents();
421     QTest::mouseClick(button, Qt::LeftButton, 0);
422     QVERIFY_EVENT(button, 0, QAccessible::StateChanged);
423     QVERIFY_EVENT(button, 0, QAccessible::StateChanged);
424
425     button->setAccessibleName("Olaf the second");
426     QVERIFY_EVENT(button, 0, QAccessible::NameChanged);
427     button->setAccessibleDescription("This is a button labeled Olaf");
428     QVERIFY_EVENT(button, 0, QAccessible::DescriptionChanged);
429
430     button->hide();
431     QVERIFY_EVENT(button, 0, QAccessible::ObjectHide);
432
433     delete button;
434 }
435
436 void tst_QAccessibility::customWidget()
437 {
438     QtTestAccessibleWidget* widget = new QtTestAccessibleWidget(0, "Heinz");
439
440     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
441     QVERIFY(iface != 0);
442     QVERIFY(iface->isValid());
443     QCOMPARE(iface->object(), (QObject*)widget);
444     QCOMPARE(iface->object()->objectName(), QString("Heinz"));
445     QCOMPARE(iface->text(QAccessible::Help), QString("Help yourself"));
446
447     delete iface;
448     delete widget;
449 }
450
451 void tst_QAccessibility::deletedWidget()
452 {
453     QtTestAccessibleWidget *widget = new QtTestAccessibleWidget(0, "Ralf");
454     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
455     QVERIFY(iface != 0);
456     QVERIFY(iface->isValid());
457     QCOMPARE(iface->object(), (QObject*)widget);
458
459     delete widget;
460     widget = 0;
461     QVERIFY(!iface->isValid());
462     delete iface;
463 }
464
465 void tst_QAccessibility::statesStructTest()
466 {
467     QAccessible::State s1;
468     QVERIFY(s1.disabled == 0);
469     QVERIFY(s1.focusable == 0);
470     QVERIFY(s1.modal == 0);
471
472     QAccessible::State s2;
473     QVERIFY(s2 == s1);
474     s2.busy = true;
475     QVERIFY(!(s2 == s1));
476     s1.busy = true;
477     QVERIFY(s2 == s1);
478     s1 = QAccessible::State();
479     QVERIFY(!(s2 == s1));
480     s1 = s2;
481     QVERIFY(s2 == s1);
482     QVERIFY(s1.busy == 1);
483 }
484
485 void tst_QAccessibility::sliderTest()
486 {
487     {
488     QSlider *slider = new QSlider(0);
489     slider->setObjectName(QString("Slidy"));
490     slider->show();
491     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(slider);
492     QVERIFY(iface != 0);
493     QVERIFY(iface->isValid());
494
495     QCOMPARE(iface->childCount(), 0);
496     QCOMPARE(iface->role(), QAccessible::Slider);
497
498     QAccessibleValueInterface *valueIface = iface->valueInterface();
499     QVERIFY(valueIface != 0);
500     QCOMPARE(valueIface->minimumValue().toInt(), slider->minimum());
501     QCOMPARE(valueIface->maximumValue().toInt(), slider->maximum());
502     slider->setValue(50);
503     QCOMPARE(valueIface->currentValue().toInt(), slider->value());
504     slider->setValue(0);
505     QCOMPARE(valueIface->currentValue().toInt(), slider->value());
506     slider->setValue(100);
507     QCOMPARE(valueIface->currentValue().toInt(), slider->value());
508     valueIface->setCurrentValue(77);
509     QCOMPARE(77, slider->value());
510
511     delete iface;
512     delete slider;
513     }
514     QTestAccessibility::clearEvents();
515 }
516
517 void tst_QAccessibility::navigateHierarchy()
518 {
519     {
520     QWidget *w = new QWidget(0);
521     w->setObjectName(QString("Hans"));
522     w->show();
523     QWidget *w1 = new QWidget(w);
524     w1->setObjectName(QString("1"));
525     w1->show();
526     QWidget *w2 = new QWidget(w);
527     w2->setObjectName(QString("2"));
528     w2->show();
529     QWidget *w3 = new QWidget(w);
530     w3->setObjectName(QString("3"));
531     w3->show();
532     QWidget *w31 = new QWidget(w3);
533     w31->setObjectName(QString("31"));
534     w31->show();
535
536     QAccessibleInterface *target = 0;
537     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(w);
538     QVERIFY(iface != 0);
539     QVERIFY(iface->isValid());
540
541     target = iface->child(14);
542     QVERIFY(target == 0);
543     target = iface->child(-1);
544     QVERIFY(target == 0);
545     target = iface->child(0);
546     QAccessibleInterface *interfaceW1 = iface->child(0);
547     QVERIFY(target);
548     QVERIFY(target->isValid());
549     QCOMPARE(target->object(), (QObject*)w1);
550     QVERIFY(interfaceW1 != 0);
551     QVERIFY(interfaceW1->isValid());
552     QCOMPARE(interfaceW1->object(), (QObject*)w1);
553     delete interfaceW1;
554     delete iface; iface = 0;
555
556     iface = QAccessible::queryAccessibleInterface(w);
557     target = iface->child(2);
558     QVERIFY(target != 0);
559     QVERIFY(target->isValid());
560     QCOMPARE(target->object(), (QObject*)w3);
561     delete iface; iface = 0;
562
563
564     iface = target->child(1);
565     QCOMPARE(iface, (QAccessibleInterface*)0);
566     iface = target->child(0);
567     QVERIFY(iface != 0);
568     QVERIFY(iface->isValid());
569     QCOMPARE(iface->object(), (QObject*)w31);
570
571     iface = QAccessible::queryAccessibleInterface(w);
572     QAccessibleInterface *acc3 = iface->child(2);
573     target = acc3->child(0);
574     delete acc3;
575     delete iface;
576     QCOMPARE(target->object(), (QObject*)w31);
577
578     iface = target->parent();
579     QVERIFY(iface != 0);
580     QVERIFY(iface->isValid());
581     QCOMPARE(iface->object(), (QObject*)w3);
582     delete iface; iface = 0;
583     delete target; target = 0;
584
585     delete w;
586     }
587     QTestAccessibility::clearEvents();
588 }
589
590 #define QSETCOMPARE(thetypename, elements, otherelements) \
591     QCOMPARE((QSet<thetypename>() << elements), (QSet<thetypename>() << otherelements))
592
593 static QWidget *createWidgets()
594 {
595     QWidget *w = new QWidget();
596
597     QHBoxLayout *box = new QHBoxLayout(w);
598
599     int i = 0;
600     box->addWidget(new QComboBox(w));
601     box->addWidget(new QPushButton(QString::fromAscii("widget text %1").arg(i++), w));
602     box->addWidget(new QHeaderView(Qt::Vertical, w));
603     box->addWidget(new QTreeView(w));
604     box->addWidget(new QTreeWidget(w));
605     box->addWidget(new QListView(w));
606     box->addWidget(new QListWidget(w));
607     box->addWidget(new QTableView(w));
608     box->addWidget(new QTableWidget(w));
609     box->addWidget(new QCalendarWidget(w));
610     box->addWidget(new QDialogButtonBox(w));
611     box->addWidget(new QGroupBox(QString::fromAscii("widget text %1").arg(i++), w));
612     box->addWidget(new QFrame(w));
613     box->addWidget(new QLineEdit(QString::fromAscii("widget text %1").arg(i++), w));
614     box->addWidget(new QProgressBar(w));
615     box->addWidget(new QTabWidget(w));
616     box->addWidget(new QCheckBox(QString::fromAscii("widget text %1").arg(i++), w));
617     box->addWidget(new QRadioButton(QString::fromAscii("widget text %1").arg(i++), w));
618     box->addWidget(new QDial(w));
619     box->addWidget(new QScrollBar(w));
620     box->addWidget(new QSlider(w));
621     box->addWidget(new QDateTimeEdit(w));
622     box->addWidget(new QDoubleSpinBox(w));
623     box->addWidget(new QSpinBox(w));
624     box->addWidget(new QLabel(QString::fromAscii("widget text %1").arg(i++), w));
625     box->addWidget(new QLCDNumber(w));
626     box->addWidget(new QStackedWidget(w));
627     box->addWidget(new QToolBox(w));
628     box->addWidget(new QLabel(QString::fromAscii("widget text %1").arg(i++), w));
629     box->addWidget(new QTextEdit(QString::fromAscii("widget text %1").arg(i++), w));
630
631     /* Not in the list
632      * QAbstractItemView, QGraphicsView, QScrollArea,
633      * QToolButton, QDockWidget, QFocusFrame, QMainWindow, QMenu, QMenuBar, QSizeGrip, QSplashScreen, QSplitterHandle,
634      * QStatusBar, QSvgWidget, QTabBar, QToolBar, QWorkspace, QSplitter
635      */
636     return w;
637 }
638
639 void tst_QAccessibility::accessibleName()
640 {
641     QWidget *toplevel = createWidgets();
642     toplevel->show();
643 #if defined(Q_OS_UNIX)
644     QCoreApplication::processEvents();
645     QTest::qWait(100);
646 #endif
647     QLayout *lout = toplevel->layout();
648     for (int i = 0; i < lout->count(); i++) {
649         QLayoutItem *item = lout->itemAt(i);
650         QWidget *child = item->widget();
651
652         QString name = tr("Widget Name %1").arg(i);
653         child->setAccessibleName(name);
654         QAccessibleInterface *acc = QAccessible::queryAccessibleInterface(child);
655         QCOMPARE(acc->text(QAccessible::Name), name);
656
657         QString desc = tr("Widget Description %1").arg(i);
658         child->setAccessibleDescription(desc);
659         QCOMPARE(acc->text(QAccessible::Description), desc);
660
661     }
662
663     delete toplevel;
664     QTestAccessibility::clearEvents();
665 }
666
667 void tst_QAccessibility::textAttributes()
668 {
669     QTextEdit textEdit;
670     int startOffset;
671     int endOffset;
672     QString attributes;
673     QString text("<html><head></head><body>"
674                  "Hello, <b>this</b> is an <i><b>example</b> text</i>."
675                  "<span style=\"font-family: monospace\">Multiple fonts are used.</span>"
676                  "Multiple <span style=\"font-size: 8pt\">text sizes</span> are used."
677                  "Let's give some color to <span style=\"color:#f0f1f2; background-color:#14f01e\">Qt</span>."
678                  "</body></html>");
679
680     textEdit.setText(text);
681     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&textEdit);
682
683     QAccessibleTextInterface *textInterface=interface->textInterface();
684
685     QVERIFY(textInterface);
686     QCOMPARE(textInterface->characterCount(), 112);
687
688     attributes = textInterface->attributes(10, &startOffset, &endOffset);
689     QCOMPARE(startOffset, 7);
690     QCOMPARE(endOffset, 11);
691     attributes.prepend(';');
692     QVERIFY(attributes.contains(QLatin1String(";font-weight:bold;")));
693
694     attributes = textInterface->attributes(18, &startOffset, &endOffset);
695     QCOMPARE(startOffset, 18);
696     QCOMPARE(endOffset, 25);
697     attributes.prepend(';');
698     QVERIFY(attributes.contains(QLatin1String(";font-weight:bold;")));
699     QVERIFY(attributes.contains(QLatin1String(";font-style:italic;")));
700
701     attributes = textInterface->attributes(34, &startOffset, &endOffset);
702     QCOMPARE(startOffset, 31);
703     QCOMPARE(endOffset, 55);
704     attributes.prepend(';');
705     QVERIFY(attributes.contains(QLatin1String(";font-family:\"monospace\";")));
706
707     attributes = textInterface->attributes(65, &startOffset, &endOffset);
708     QCOMPARE(startOffset, 64);
709     QCOMPARE(endOffset, 74);
710     attributes.prepend(';');
711     QVERIFY(attributes.contains(QLatin1String(";font-size:8pt;")));
712
713     attributes = textInterface->attributes(110, &startOffset, &endOffset);
714     QCOMPARE(startOffset, 109);
715     QCOMPARE(endOffset, 111);
716     attributes.prepend(';');
717     QVERIFY(attributes.contains(QLatin1String(";background-color:rgb(20,240,30);")));
718     QVERIFY(attributes.contains(QLatin1String(";color:rgb(240,241,242);")));
719 }
720
721 void tst_QAccessibility::hideShowTest()
722 {
723     QWidget * const window = new QWidget();
724     QWidget * const child = new QWidget(window);
725
726     QVERIFY(state(window).invisible);
727     QVERIFY(state(child).invisible);
728
729     QTestAccessibility::clearEvents();
730
731     // show() and veryfy that both window and child are not invisible and get ObjectShow events.
732     window->show();
733     QVERIFY(!state(window).invisible);
734     QVERIFY(!state(child).invisible);
735     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(window, 0, QAccessible::ObjectShow)));
736     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(child, 0, QAccessible::ObjectShow)));
737     QTestAccessibility::clearEvents();
738
739     // hide() and veryfy that both window and child are invisible and get ObjectHide events.
740     window->hide();
741     QVERIFY(state(window).invisible);
742     QVERIFY(state(child).invisible);
743     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(window, 0, QAccessible::ObjectHide)));
744     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(child, 0, QAccessible::ObjectHide)));
745     QTestAccessibility::clearEvents();
746
747     delete window;
748     QTestAccessibility::clearEvents();
749 }
750
751
752 void tst_QAccessibility::actionTest()
753 {
754     {
755     QCOMPARE(QAccessibleActionInterface::pressAction(), QString(QStringLiteral("Press")));
756
757     QWidget *widget = new QWidget;
758     widget->setFocusPolicy(Qt::NoFocus);
759     widget->show();
760
761     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(widget);
762     QVERIFY(interface);
763     QVERIFY(interface->isValid());
764     QAccessibleActionInterface *actions = interface->actionInterface();
765     QVERIFY(actions);
766
767     // no actions by default, except when focusable
768     QCOMPARE(actions->actionNames(), QStringList());
769     widget->setFocusPolicy(Qt::StrongFocus);
770     QCOMPARE(actions->actionNames(), QStringList(QAccessibleActionInterface::setFocusAction()));
771
772     delete interface;
773     delete widget;
774     }
775     QTestAccessibility::clearEvents();
776
777     {
778     QPushButton *button = new QPushButton;
779     button->show();
780     button->clearFocus();
781     QCOMPARE(button->hasFocus(), false);
782     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(button);
783     QAccessibleActionInterface *actions = interface->actionInterface();
784     QVERIFY(actions);
785
786     // Make sure the "primary action" press comes first!
787     QCOMPARE(actions->actionNames(), QStringList() << QAccessibleActionInterface::pressAction() << QAccessibleActionInterface::setFocusAction());
788
789     actions->doAction(QAccessibleActionInterface::setFocusAction());
790     QTest::qWait(500);
791     QCOMPARE(button->hasFocus(), true);
792
793     connect(button, SIGNAL(clicked()), this, SLOT(onClicked()));
794     QCOMPARE(click_count, 0);
795     actions->doAction(QAccessibleActionInterface::pressAction());
796     QTest::qWait(500);
797     QCOMPARE(click_count, 1);
798
799     delete interface;
800     delete button;
801     }
802     QTestAccessibility::clearEvents();
803 }
804
805 void tst_QAccessibility::applicationTest()
806 {
807     QLatin1String name = QLatin1String("My Name");
808     qApp->setApplicationName(name);
809     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(qApp);
810     QCOMPARE(interface->text(QAccessible::Name), name);
811     QCOMPARE(interface->role(), QAccessible::Application);
812     delete interface;
813 }
814
815 void tst_QAccessibility::mainWindowTest()
816 {
817     QMainWindow *mw = new QMainWindow;
818     mw->resize(300, 200);
819     mw->show(); // triggers layout
820
821     QLatin1String name = QLatin1String("I am the main window");
822     mw->setWindowTitle(name);
823     QTest::qWaitForWindowShown(mw);
824     QVERIFY_EVENT(mw, 0, QAccessible::ObjectShow);
825
826     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(mw);
827     QCOMPARE(interface->text(QAccessible::Name), name);
828     QCOMPARE(interface->role(), QAccessible::Window);
829     delete interface;
830     delete mw;
831     QTestAccessibility::clearEvents();
832 }
833
834 class CounterButton : public QPushButton {
835     Q_OBJECT
836 public:
837     CounterButton(const QString& name, QWidget* parent)
838         : QPushButton(name, parent), clickCount(0)
839     {
840         connect(this, SIGNAL(clicked(bool)), SLOT(incClickCount()));
841     }
842     int clickCount;
843 public Q_SLOTS:
844     void incClickCount() {
845         ++clickCount;
846     }
847 };
848
849 void tst_QAccessibility::buttonTest()
850 {
851     QWidget window;
852     window.setLayout(new QVBoxLayout);
853
854     // Standard push button
855     CounterButton pushButton("Ok", &window);
856
857     // toggle button
858     QPushButton toggleButton("Toggle", &window);
859     toggleButton.setCheckable(true);
860
861     // standard checkbox
862     QCheckBox checkBox("Check me!", &window);
863
864     // tristate checkbox
865     QCheckBox tristate("Tristate!", &window);
866     tristate.setTristate(true);
867
868     // radiobutton
869     QRadioButton radio("Radio me!", &window);
870
871     // standard toolbutton
872     QToolButton toolbutton(&window);
873     toolbutton.setText("Tool");
874     toolbutton.setMinimumSize(20,20);
875
876     // standard toolbutton
877     QToolButton toggletool(&window);
878     toggletool.setCheckable(true);
879     toggletool.setText("Toggle");
880     toggletool.setMinimumSize(20,20);
881
882     // test push button
883     QAccessibleInterface* interface = QAccessible::queryAccessibleInterface(&pushButton);
884     QAccessibleActionInterface* actionInterface = interface->actionInterface();
885     QVERIFY(actionInterface != 0);
886     QCOMPARE(interface->role(), QAccessible::PushButton);
887
888     // buttons only have a click action
889     QCOMPARE(actionInterface->actionNames().size(), 2);
890     QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::pressAction() << QAccessibleActionInterface::setFocusAction());
891     QCOMPARE(pushButton.clickCount, 0);
892     actionInterface->doAction(QAccessibleActionInterface::pressAction());
893     QTest::qWait(500);
894     QCOMPARE(pushButton.clickCount, 1);
895     delete interface;
896
897     // test toggle button
898     interface = QAccessible::queryAccessibleInterface(&toggleButton);
899     actionInterface = interface->actionInterface();
900     QCOMPARE(interface->role(), QAccessible::CheckBox);
901     QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
902     QCOMPARE(actionInterface->localizedActionDescription(QAccessibleActionInterface::checkAction()), QString("Checks the checkbox"));
903     QVERIFY(!toggleButton.isChecked());
904     QVERIFY(!interface->state().checked);
905     actionInterface->doAction(QAccessibleActionInterface::checkAction());
906     QTest::qWait(500);
907     QVERIFY(toggleButton.isChecked());
908     QCOMPARE(actionInterface->actionNames().at(0), QAccessibleActionInterface::uncheckAction());
909     QVERIFY(interface->state().checked);
910     delete interface;
911
912     {
913     // test menu push button
914     QAction *foo = new QAction("Foo", 0);
915     foo->setShortcut(QKeySequence("Ctrl+F"));
916     QMenu *menu = new QMenu();
917     menu->addAction(foo);
918     QPushButton menuButton;
919     menuButton.setMenu(menu);
920     menuButton.show();
921     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&menuButton);
922     QCOMPARE(interface->role(), QAccessible::ButtonMenu);
923     QVERIFY(interface->state().hasPopup);
924     QCOMPARE(interface->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction() << QAccessibleActionInterface::setFocusAction());
925     // showing the menu enters a new event loop...
926 //    interface->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
927 //    QTest::qWait(500);
928     delete interface;
929     delete menu;
930     }
931
932     // test check box
933     interface = QAccessible::queryAccessibleInterface(&checkBox);
934     actionInterface = interface->actionInterface();
935     QCOMPARE(interface->role(), QAccessible::CheckBox);
936     QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
937     QVERIFY(!interface->state().checked);
938     actionInterface->doAction(QAccessibleActionInterface::checkAction());
939     QTest::qWait(500);
940     QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::uncheckAction() << QAccessibleActionInterface::setFocusAction());
941     QVERIFY(interface->state().checked);
942     QVERIFY(checkBox.isChecked());
943     delete interface;
944
945     // test radiobutton
946     interface = QAccessible::queryAccessibleInterface(&radio);
947     actionInterface = interface->actionInterface();
948     QCOMPARE(interface->role(), QAccessible::RadioButton);
949     QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
950     QVERIFY(!interface->state().checked);
951     actionInterface->doAction(QAccessibleActionInterface::checkAction());
952     QTest::qWait(500);
953     QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
954     QVERIFY(interface->state().checked);
955     QVERIFY(checkBox.isChecked());
956     delete interface;
957
958 //    // test standard toolbutton
959 //    QVERIFY(QAccessible::queryAccessibleInterface(&toolbutton, &test));
960 //    QCOMPARE(test->role(), QAccessible::PushButton);
961 //    QCOMPARE(test->defaultAction(0), QAccessible::Press);
962 //    QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Press"));
963 //    QCOMPARE(test->state(), (int)QAccessible::Normal);
964 //    test->release();
965
966 //    // toggle tool button
967 //    QVERIFY(QAccessible::queryAccessibleInterface(&toggletool, &test));
968 //    QCOMPARE(test->role(), QAccessible::CheckBox);
969 //    QCOMPARE(test->defaultAction(0), QAccessible::Press);
970 //    QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Check"));
971 //    QCOMPARE(test->state(), (int)QAccessible::Normal);
972 //    QVERIFY(test->doAction(QAccessible::Press, 0));
973 //    QTest::qWait(500);
974 //    QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Uncheck"));
975 //    QCOMPARE(test->state(), (int)QAccessible::Checked);
976 //    test->release();
977
978 //    // test menu toolbutton
979 //    QVERIFY(QAccessible::queryAccessibleInterface(&menuToolButton, &test));
980 //    QCOMPARE(test->role(), QAccessible::ButtonMenu);
981 //    QCOMPARE(test->defaultAction(0), 1);
982 //    QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Open"));
983 //    QCOMPARE(test->state(), (int)QAccessible::HasPopup);
984 //    QCOMPARE(test->actionCount(0), 1);
985 //    QCOMPARE(test->actionText(QAccessible::Press, QAccessible::Name, 0), QString("Press"));
986 //    test->release();
987
988 //    // test split menu toolbutton
989 //    QVERIFY(QAccessible::queryAccessibleInterface(&splitToolButton, &test));
990 //    QCOMPARE(test->childCount(), 2);
991 //    QCOMPARE(test->role(), QAccessible::ButtonDropDown);
992 //    QCOMPARE(test->role(1), QAccessible::PushButton);
993 //    QCOMPARE(test->role(2), QAccessible::ButtonMenu);
994 //    QCOMPARE(test->defaultAction(0), QAccessible::Press);
995 //    QCOMPARE(test->defaultAction(1), QAccessible::Press);
996 //    QCOMPARE(test->defaultAction(2), QAccessible::Press);
997 //    QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Press"));
998 //    QCOMPARE(test->state(), (int)QAccessible::HasPopup);
999 //    QCOMPARE(test->actionCount(0), 1);
1000 //    QCOMPARE(test->actionText(1, QAccessible::Name, 0), QString("Open"));
1001 //    QCOMPARE(test->actionText(test->defaultAction(1), QAccessible::Name, 1), QString("Press"));
1002 //    QCOMPARE(test->state(1), (int)QAccessible::Normal);
1003 //    QCOMPARE(test->actionText(test->defaultAction(2), QAccessible::Name, 2), QString("Open"));
1004 //    QCOMPARE(test->state(2), (int)QAccessible::HasPopup);
1005 //    test->release();
1006
1007     QTestAccessibility::clearEvents();
1008 }
1009
1010 void tst_QAccessibility::scrollBarTest()
1011 {
1012     QScrollBar *scrollBar  = new QScrollBar(Qt::Horizontal);
1013     QAccessibleInterface * const scrollBarInterface = QAccessible::queryAccessibleInterface(scrollBar);
1014     QVERIFY(scrollBarInterface);
1015     QVERIFY(scrollBarInterface->state().invisible);
1016     scrollBar->resize(200, 50);
1017     scrollBar->show();
1018     QVERIFY(!scrollBarInterface->state().invisible);
1019     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(scrollBar, 0, QAccessible::ObjectShow)));
1020     QTestAccessibility::clearEvents();
1021
1022     scrollBar->hide();
1023     QVERIFY(scrollBarInterface->state().invisible);
1024     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(scrollBar, 0, QAccessible::ObjectHide)));
1025     QTestAccessibility::clearEvents();
1026
1027     // Test that the left/right subcontrols are set to unavailable when the scrollBar is at the minimum/maximum.
1028     scrollBar->show();
1029     scrollBar->setMinimum(11);
1030     scrollBar->setMaximum(111);
1031
1032     QAccessibleValueInterface *valueIface = scrollBarInterface->valueInterface();
1033     QVERIFY(valueIface != 0);
1034     QCOMPARE(valueIface->minimumValue().toInt(), scrollBar->minimum());
1035     QCOMPARE(valueIface->maximumValue().toInt(), scrollBar->maximum());
1036     scrollBar->setValue(50);
1037     QCOMPARE(valueIface->currentValue().toInt(), scrollBar->value());
1038     scrollBar->setValue(0);
1039     QCOMPARE(valueIface->currentValue().toInt(), scrollBar->value());
1040     scrollBar->setValue(100);
1041     QCOMPARE(valueIface->currentValue().toInt(), scrollBar->value());
1042     valueIface->setCurrentValue(77);
1043     QCOMPARE(77, scrollBar->value());
1044
1045     const QRect scrollBarRect = scrollBarInterface->rect();
1046     QVERIFY(scrollBarRect.isValid());
1047
1048     delete scrollBarInterface;
1049     delete scrollBar;
1050
1051     QTestAccessibility::clearEvents();
1052 }
1053
1054 void tst_QAccessibility::tabTest()
1055 {
1056     QTabBar *tabBar = new QTabBar();
1057     tabBar->show();
1058
1059     QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(tabBar);
1060     QVERIFY(interface);
1061     QCOMPARE(interface->childCount(), 2);
1062
1063     // Test that the Invisible bit for the navigation buttons gets set
1064     // and cleared correctly.
1065     QAccessibleInterface *leftButton = interface->child(0);
1066     QCOMPARE(leftButton->role(), QAccessible::PushButton);
1067     QVERIFY(leftButton->state().invisible);
1068     delete leftButton;
1069
1070     const int lots = 5;
1071     for (int i = 0; i < lots; ++i)
1072         tabBar->addTab("Foo");
1073
1074     QAccessibleInterface *child1 = interface->child(0);
1075     QAccessibleInterface *child2 = interface->child(1);
1076     QVERIFY(child1);
1077     QCOMPARE(child1->role(), QAccessible::PageTab);
1078     QVERIFY(child2);
1079     QCOMPARE(child2->role(), QAccessible::PageTab);
1080
1081     QVERIFY((child1->state().invisible) == false);
1082     tabBar->hide();
1083
1084     QCoreApplication::processEvents();
1085     QTest::qWait(100);
1086
1087     QVERIFY(child1->state().invisible);
1088
1089     tabBar->show();
1090     tabBar->setCurrentIndex(0);
1091
1092     // Test that sending a focus action to a tab does not select it.
1093 //    child2->doAction(QAccessible::Focus, 2, QVariantList());
1094     QCOMPARE(tabBar->currentIndex(), 0);
1095
1096     // Test that sending a press action to a tab selects it.
1097     QVERIFY(child2->actionInterface());
1098     QCOMPARE(child2->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1099     QCOMPARE(tabBar->currentIndex(), 0);
1100     child2->actionInterface()->doAction(QAccessibleActionInterface::pressAction());
1101     QCOMPARE(tabBar->currentIndex(), 1);
1102
1103     delete tabBar;
1104     delete interface;
1105     delete child1;
1106     delete child2;
1107     QTestAccessibility::clearEvents();
1108 }
1109
1110 void tst_QAccessibility::tabWidgetTest()
1111 {
1112     QTabWidget *tabWidget = new QTabWidget();
1113     tabWidget->show();
1114
1115     // the interface for the tab is just a container for tabbar and stacked widget
1116     QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(tabWidget);
1117     QVERIFY(interface);
1118     QCOMPARE(interface->childCount(), 2);
1119     QCOMPARE(interface->role(), QAccessible::Client);
1120
1121     // Create pages, check navigation
1122     QLabel *label1 = new QLabel("Page 1", tabWidget);
1123     tabWidget->addTab(label1, "Tab 1");
1124     QLabel *label2 = new QLabel("Page 2", tabWidget);
1125     tabWidget->addTab(label2, "Tab 2");
1126
1127     QCOMPARE(interface->childCount(), 2);
1128
1129     QAccessibleInterface* tabBarInterface = 0;
1130     // there is no special logic to sort the children, so the contents will be 1, the tab bar 2
1131     tabBarInterface = interface->child(1);
1132     QVERIFY(tabBarInterface);
1133     QCOMPARE(tabBarInterface->childCount(), 4);
1134     QCOMPARE(tabBarInterface->role(), QAccessible::PageTabList);
1135
1136     QAccessibleInterface* tabButton1Interface = tabBarInterface->child(0);
1137     QVERIFY(tabButton1Interface);
1138     QCOMPARE(tabButton1Interface->role(), QAccessible::PageTab);
1139     QCOMPARE(tabButton1Interface->text(QAccessible::Name), QLatin1String("Tab 1"));
1140
1141     QAccessibleInterface* tabButton2Interface = tabBarInterface->child(1);
1142     QVERIFY(tabButton1Interface);
1143     QCOMPARE(tabButton2Interface->role(), QAccessible::PageTab);
1144     QCOMPARE(tabButton2Interface->text(QAccessible::Name), QLatin1String("Tab 2"));
1145
1146     QAccessibleInterface* tabButtonLeft = tabBarInterface->child(2);
1147     QVERIFY(tabButtonLeft);
1148     QCOMPARE(tabButtonLeft->role(), QAccessible::PushButton);
1149     QCOMPARE(tabButtonLeft->text(QAccessible::Name), QLatin1String("Scroll Left"));
1150
1151     QAccessibleInterface* tabButtonRight = tabBarInterface->child(3);
1152     QVERIFY(tabButtonRight);
1153     QCOMPARE(tabButtonRight->role(), QAccessible::PushButton);
1154     QCOMPARE(tabButtonRight->text(QAccessible::Name), QLatin1String("Scroll Right"));
1155     delete tabButton1Interface;
1156     delete tabButton2Interface;
1157     delete tabButtonLeft;
1158     delete tabButtonRight;
1159
1160     QAccessibleInterface* stackWidgetInterface = interface->child(0);
1161     QVERIFY(stackWidgetInterface);
1162     QCOMPARE(stackWidgetInterface->childCount(), 2);
1163     QCOMPARE(stackWidgetInterface->role(), QAccessible::LayeredPane);
1164
1165     QAccessibleInterface* stackChild1Interface = stackWidgetInterface->child(0);
1166     QVERIFY(stackChild1Interface);
1167 #ifndef Q_CC_INTEL
1168     QCOMPARE(stackChild1Interface->childCount(), 0);
1169 #endif
1170     QCOMPARE(stackChild1Interface->role(), QAccessible::StaticText);
1171     QCOMPARE(stackChild1Interface->text(QAccessible::Name), QLatin1String("Page 1"));
1172     QCOMPARE(label1, stackChild1Interface->object());
1173
1174     // Navigation in stack widgets should be consistent
1175     QAccessibleInterface* parent = stackChild1Interface->parent();
1176     QVERIFY(parent);
1177 #ifndef Q_CC_INTEL
1178     QCOMPARE(parent->childCount(), 2);
1179 #endif
1180     QCOMPARE(parent->role(), QAccessible::LayeredPane);
1181     delete parent;
1182
1183     QAccessibleInterface* stackChild2Interface = stackWidgetInterface->child(1);
1184     QVERIFY(stackChild2Interface);
1185     QCOMPARE(stackChild2Interface->childCount(), 0);
1186     QCOMPARE(stackChild2Interface->role(), QAccessible::StaticText);
1187     QCOMPARE(label2, stackChild2Interface->object());
1188     QCOMPARE(label2->text(), stackChild2Interface->text(QAccessible::Name));
1189
1190     parent = stackChild2Interface->parent();
1191     QVERIFY(parent);
1192 #ifndef Q_CC_INTEL
1193     QCOMPARE(parent->childCount(), 2);
1194 #endif
1195     QCOMPARE(parent->role(), QAccessible::LayeredPane);
1196     delete parent;
1197
1198     delete tabBarInterface;
1199     delete stackChild1Interface;
1200     delete stackChild2Interface;
1201     delete stackWidgetInterface;
1202     delete interface;
1203     delete tabWidget;
1204     QTestAccessibility::clearEvents();
1205 }
1206
1207 void tst_QAccessibility::menuTest()
1208 {
1209     {
1210     QMainWindow mw;
1211     mw.resize(300, 200);
1212     QMenu *file = mw.menuBar()->addMenu("&File");
1213     QMenu *fileNew = file->addMenu("&New...");
1214     fileNew->menuAction()->setShortcut(tr("Ctrl+N"));
1215     fileNew->addAction("Text file");
1216     fileNew->addAction("Image file");
1217     file->addAction("&Open")->setShortcut(tr("Ctrl+O"));
1218     file->addAction("&Save")->setShortcut(tr("Ctrl+S"));
1219     file->addSeparator();
1220     file->addAction("E&xit")->setShortcut(tr("Alt+F4"));
1221
1222     QMenu *edit = mw.menuBar()->addMenu("&Edit");
1223     edit->addAction("&Undo")->setShortcut(tr("Ctrl+Z"));
1224     edit->addAction("&Redo")->setShortcut(tr("Ctrl+Y"));
1225     edit->addSeparator();
1226     edit->addAction("Cu&t")->setShortcut(tr("Ctrl+X"));
1227     edit->addAction("&Copy")->setShortcut(tr("Ctrl+C"));
1228     edit->addAction("&Paste")->setShortcut(tr("Ctrl+V"));
1229     edit->addAction("&Delete")->setShortcut(tr("Del"));
1230     edit->addSeparator();
1231     edit->addAction("Pr&operties");
1232
1233     mw.menuBar()->addSeparator();
1234
1235     QMenu *help = mw.menuBar()->addMenu("&Help");
1236     help->addAction("&Contents");
1237     help->addAction("&About");
1238
1239     mw.menuBar()->addAction("Action!");
1240
1241     mw.show(); // triggers layout
1242     QTest::qWait(100);
1243
1244     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(mw.menuBar());
1245     QCOMPARE(verifyHierarchy(interface),  0);
1246
1247     QVERIFY(interface);
1248     QCOMPARE(interface->childCount(), 5);
1249     QCOMPARE(interface->role(), QAccessible::MenuBar);
1250
1251     QAccessibleInterface *iFile = interface->child(0);
1252     QAccessibleInterface *iEdit = interface->child(1);
1253     QAccessibleInterface *iSeparator = interface->child(2);
1254     QAccessibleInterface *iHelp = interface->child(3);
1255     QAccessibleInterface *iAction = interface->child(4);
1256
1257     QCOMPARE(iFile->role(), QAccessible::MenuItem);
1258     QCOMPARE(iEdit->role(), QAccessible::MenuItem);
1259     QCOMPARE(iSeparator->role(), QAccessible::Separator);
1260     QCOMPARE(iHelp->role(), QAccessible::MenuItem);
1261     QCOMPARE(iAction->role(), QAccessible::MenuItem);
1262 #ifndef Q_OS_MAC
1263 #ifdef Q_OS_WINCE
1264     if (!IsValidCEPlatform())
1265         QSKIP("Tests do not work on Mobile platforms due to native menus");
1266 #endif
1267     QCOMPARE(mw.mapFromGlobal(interface->rect().topLeft()), mw.menuBar()->geometry().topLeft());
1268     QCOMPARE(interface->rect().size(), mw.menuBar()->size());
1269
1270     QVERIFY(interface->rect().contains(iFile->rect()));
1271     QVERIFY(interface->rect().contains(iEdit->rect()));
1272     // QVERIFY(interface->rect().contains(childSeparator->rect())); //separator might be invisible
1273     QVERIFY(interface->rect().contains(iHelp->rect()));
1274     QVERIFY(interface->rect().contains(iAction->rect()));
1275 #endif
1276
1277     QCOMPARE(iFile->text(QAccessible::Name), QString("File"));
1278     QCOMPARE(iEdit->text(QAccessible::Name), QString("Edit"));
1279     QCOMPARE(iSeparator->text(QAccessible::Name), QString());
1280     QCOMPARE(iHelp->text(QAccessible::Name), QString("Help"));
1281     QCOMPARE(iAction->text(QAccessible::Name), QString("Action!"));
1282
1283 // TODO: Currently not working, task to fix is #100019.
1284 #ifndef Q_OS_MAC
1285     QCOMPARE(iFile->text(QAccessible::Accelerator), tr("Alt+F"));
1286     QCOMPARE(iEdit->text(QAccessible::Accelerator), tr("Alt+E"));
1287     QCOMPARE(iSeparator->text(QAccessible::Accelerator), QString());
1288     QCOMPARE(iHelp->text(QAccessible::Accelerator), tr("Alt+H"));
1289     QCOMPARE(iAction->text(QAccessible::Accelerator), QString());
1290 #endif
1291
1292     QVERIFY(iFile->actionInterface());
1293
1294     QCOMPARE(iFile->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
1295     QCOMPARE(iSeparator->actionInterface()->actionNames(), QStringList());
1296     QCOMPARE(iHelp->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
1297     QCOMPARE(iAction->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1298
1299     bool menuFade = qApp->isEffectEnabled(Qt::UI_FadeMenu);
1300     int menuFadeDelay = 300;
1301     iFile->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1302     if(menuFade)
1303         QTest::qWait(menuFadeDelay);
1304     QVERIFY(file->isVisible() && !edit->isVisible() && !help->isVisible());
1305     iEdit->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1306     if(menuFade)
1307         QTest::qWait(menuFadeDelay);
1308     QVERIFY(!file->isVisible() && edit->isVisible() && !help->isVisible());
1309     iHelp->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1310     if(menuFade)
1311         QTest::qWait(menuFadeDelay);
1312     QVERIFY(!file->isVisible() && !edit->isVisible() && help->isVisible());
1313     iAction->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1314     if(menuFade)
1315         QTest::qWait(menuFadeDelay);
1316     QVERIFY(!file->isVisible() && !edit->isVisible() && !help->isVisible());
1317
1318     QVERIFY(interface->actionInterface());
1319     QCOMPARE(interface->actionInterface()->actionNames(), QStringList());
1320     delete interface;
1321     interface = QAccessible::queryAccessibleInterface(file);
1322     QCOMPARE(interface->childCount(), 5);
1323     QCOMPARE(interface->role(), QAccessible::PopupMenu);
1324
1325     QAccessibleInterface *iFileNew = interface->child(0);
1326     QAccessibleInterface *iFileOpen = interface->child(1);
1327     QAccessibleInterface *iFileSave = interface->child(2);
1328     QAccessibleInterface *iFileSeparator = interface->child(3);
1329     QAccessibleInterface *iFileExit = interface->child(4);
1330
1331     QCOMPARE(iFileNew->role(), QAccessible::MenuItem);
1332     QCOMPARE(iFileOpen->role(), QAccessible::MenuItem);
1333     QCOMPARE(iFileSave->role(), QAccessible::MenuItem);
1334     QCOMPARE(iFileSeparator->role(), QAccessible::Separator);
1335     QCOMPARE(iFileExit->role(), QAccessible::MenuItem);
1336     QCOMPARE(iFileNew->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
1337     QCOMPARE(iFileOpen->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1338     QCOMPARE(iFileSave->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1339     QCOMPARE(iFileSeparator->actionInterface()->actionNames(), QStringList());
1340     QCOMPARE(iFileExit->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1341
1342     QAccessibleInterface *iface = 0;
1343     QAccessibleInterface *iface2 = 0;
1344
1345     // traverse siblings with navigate(Sibling, ...)
1346     iface = interface->child(0);
1347     QVERIFY(iface);
1348     QCOMPARE(iface->role(), QAccessible::MenuItem);
1349
1350     QAccessible::Role fileRoles[5] = {
1351         QAccessible::MenuItem,
1352         QAccessible::MenuItem,
1353         QAccessible::MenuItem,
1354         QAccessible::Separator,
1355         QAccessible::MenuItem
1356     };
1357     for (int child = 0; child < 5; ++child) {
1358         iface2 = interface->child(child);
1359         QVERIFY(iface2);
1360         QCOMPARE(iface2->role(), fileRoles[child]);
1361         delete iface2;
1362     }
1363     delete iface;
1364
1365     // "New" item
1366     iface = interface->child(0);
1367     QVERIFY(iface);
1368     QCOMPARE(iface->role(), QAccessible::MenuItem);
1369
1370     // "New" menu
1371     iface2 = iface->child(0);
1372     delete iface;
1373     iface = iface2;
1374     QVERIFY(iface);
1375     QCOMPARE(iface->role(), QAccessible::PopupMenu);
1376
1377     // "Text file" menu item
1378     iface2 = iface->child(0);
1379     delete iface;
1380     iface = iface2;
1381     QVERIFY(iface);
1382     QCOMPARE(iface->role(), QAccessible::MenuItem);
1383
1384     delete iface;
1385
1386     // move mouse pointer away, since that might influence the
1387     // subsequent tests
1388     QTest::mouseMove(&mw, QPoint(-1, -1));
1389     QTest::qWait(100);
1390     if (menuFade)
1391         QTest::qWait(menuFadeDelay);
1392
1393     iFile->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1394     iFileNew->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1395
1396     QVERIFY(file->isVisible());
1397     QVERIFY(fileNew->isVisible());
1398     QVERIFY(!edit->isVisible());
1399     QVERIFY(!help->isVisible());
1400
1401     QTestAccessibility::clearEvents();
1402     mw.hide();
1403
1404     delete iFile;
1405     delete iFileNew;
1406     delete iFileOpen;
1407     delete iFileSave;
1408     delete iFileSeparator;
1409     delete iFileExit;
1410
1411     // Do not crash if the menu don't have a parent
1412     QMenu *menu = new QMenu;
1413     menu->addAction(QLatin1String("one"));
1414     menu->addAction(QLatin1String("two"));
1415     menu->addAction(QLatin1String("three"));
1416     iface = QAccessible::queryAccessibleInterface(menu);
1417     iface2 = iface->parent();
1418     QVERIFY(iface2);
1419     QCOMPARE(iface2->role(), QAccessible::Application);
1420     // caused a *crash*
1421     iface2->state();
1422     delete iface2;
1423     delete iface;
1424     delete menu;
1425
1426     }
1427     QTestAccessibility::clearEvents();
1428 }
1429
1430 void tst_QAccessibility::spinBoxTest()
1431 {
1432     QSpinBox * const spinBox = new QSpinBox();
1433     spinBox->setValue(3);
1434     spinBox->show();
1435
1436     QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(spinBox);
1437     QVERIFY(interface);
1438     QCOMPARE(interface->role(), QAccessible::SpinBox);
1439
1440     const QRect widgetRect = spinBox->geometry();
1441     const QRect accessibleRect = interface->rect();
1442     QCOMPARE(accessibleRect, widgetRect);
1443     QCOMPARE(interface->text(QAccessible::Value), QLatin1String("3"));
1444
1445     // one child, the line edit
1446     const int numChildren = interface->childCount();
1447     QCOMPARE(numChildren, 1);
1448     QAccessibleInterface *lineEdit = interface->child(0);
1449
1450     QCOMPARE(lineEdit->role(), QAccessible::EditableText);
1451     QCOMPARE(lineEdit->text(QAccessible::Value), QLatin1String("3"));
1452     delete lineEdit;
1453
1454     QVERIFY(interface->valueInterface());
1455     QCOMPARE(interface->valueInterface()->currentValue().toInt(), 3);
1456     interface->valueInterface()->setCurrentValue(23);
1457     QCOMPARE(interface->valueInterface()->currentValue().toInt(), 23);
1458     QCOMPARE(spinBox->value(), 23);
1459
1460     spinBox->setFocus();
1461     QTestAccessibility::clearEvents();
1462     QTest::keyPress(spinBox, Qt::Key_Up);
1463     QTest::qWait(200);
1464     EventList events = QTestAccessibility::events();
1465     QTestAccessibilityEvent expectedEvent(spinBox, 0, (int)QAccessible::ValueChanged);
1466     QVERIFY(events.contains(expectedEvent));
1467     delete spinBox;
1468     QTestAccessibility::clearEvents();
1469 }
1470
1471 void tst_QAccessibility::doubleSpinBoxTest()
1472 {
1473     QDoubleSpinBox *doubleSpinBox = new QDoubleSpinBox;
1474     doubleSpinBox->show();
1475
1476     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(doubleSpinBox);
1477     QVERIFY(interface);
1478
1479     const QRect widgetRect = doubleSpinBox->geometry();
1480     const QRect accessibleRect = interface->rect();
1481     QCOMPARE(accessibleRect, widgetRect);
1482
1483     // Test that we get valid rects for all the spinbox child interfaces.
1484     const int numChildren = interface->childCount();
1485     for (int i = 0; i < numChildren; ++i) {
1486         QAccessibleInterface *childIface = interface->child(i);
1487         const QRect childRect = childIface->rect();
1488         QVERIFY(childRect.isValid());
1489         delete childIface;
1490     }
1491
1492     delete doubleSpinBox;
1493     QTestAccessibility::clearEvents();
1494 }
1495
1496 void tst_QAccessibility::textEditTest()
1497 {
1498     {
1499     QTextEdit edit;
1500     int startOffset;
1501     int endOffset;
1502     QString text = "hello world\nhow are you today?\n";
1503     edit.setText(text);
1504     edit.show();
1505
1506     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&edit);
1507     QCOMPARE(iface->text(QAccessible::Value), text);
1508     QCOMPARE(iface->textInterface()->textAtOffset(8, QAccessible2::WordBoundary, &startOffset, &endOffset), QString("world"));
1509     QCOMPARE(startOffset, 6);
1510     QCOMPARE(endOffset, 11);
1511     QCOMPARE(iface->textInterface()->textAtOffset(14, QAccessible2::LineBoundary, &startOffset, &endOffset), QString("how are you today?"));
1512     QCOMPARE(startOffset, 12);
1513     QCOMPARE(endOffset, 30);
1514     QCOMPARE(iface->textInterface()->characterCount(), 31);
1515     QFontMetrics fm(edit.font());
1516     QCOMPARE(iface->textInterface()->characterRect(0, QAccessible2::RelativeToParent).size(), QSize(fm.width("h"), fm.height()));
1517     QCOMPARE(iface->textInterface()->characterRect(5, QAccessible2::RelativeToParent).size(), QSize(fm.width(" "), fm.height()));
1518     QCOMPARE(iface->textInterface()->characterRect(6, QAccessible2::RelativeToParent).size(), QSize(fm.width("w"), fm.height()));
1519     }
1520     QTestAccessibility::clearEvents();
1521 }
1522
1523 void tst_QAccessibility::textBrowserTest()
1524 {
1525     {
1526     QTextBrowser textBrowser;
1527     QString text = QLatin1String("Hello world\nhow are you today?\n");
1528     textBrowser.setText(text);
1529     textBrowser.show();
1530
1531     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&textBrowser);
1532     QVERIFY(iface);
1533     QCOMPARE(iface->role(), QAccessible::StaticText);
1534     QCOMPARE(iface->text(QAccessible::Value), text);
1535     int startOffset;
1536     int endOffset;
1537     QCOMPARE(iface->textInterface()->textAtOffset(8, QAccessible2::WordBoundary, &startOffset, &endOffset), QString("world"));
1538     QCOMPARE(startOffset, 6);
1539     QCOMPARE(endOffset, 11);
1540     QCOMPARE(iface->textInterface()->textAtOffset(14, QAccessible2::LineBoundary, &startOffset, &endOffset), QString("how are you today?"));
1541     QCOMPARE(startOffset, 12);
1542     QCOMPARE(endOffset, 30);
1543     QCOMPARE(iface->textInterface()->characterCount(), 31);
1544     }
1545     QTestAccessibility::clearEvents();
1546 }
1547
1548 void tst_QAccessibility::mdiAreaTest()
1549 {
1550     {
1551     QMdiArea mdiArea;
1552     mdiArea.resize(400,300);
1553     mdiArea.show();
1554     const int subWindowCount = 3;
1555     for (int i = 0; i < subWindowCount; ++i)
1556         mdiArea.addSubWindow(new QWidget, Qt::Dialog)->show();
1557
1558     QList<QMdiSubWindow *> subWindows = mdiArea.subWindowList();
1559     QCOMPARE(subWindows.count(), subWindowCount);
1560
1561     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&mdiArea);
1562     QVERIFY(interface);
1563     QCOMPARE(interface->childCount(), subWindowCount);
1564
1565     }
1566     QTestAccessibility::clearEvents();
1567 }
1568
1569 void tst_QAccessibility::mdiSubWindowTest()
1570 {
1571     {
1572     QMdiArea mdiArea;
1573     mdiArea.show();
1574     qApp->setActiveWindow(&mdiArea);
1575 #if defined(Q_OS_UNIX)
1576     QCoreApplication::processEvents();
1577     QTest::qWait(150);
1578 #endif
1579
1580     const int subWindowCount =  5;
1581     for (int i = 0; i < subWindowCount; ++i) {
1582         QMdiSubWindow *window = mdiArea.addSubWindow(new QPushButton("QAccessibilityTest"));
1583         window->show();
1584         // Parts of this test requires that the sub windows are placed next
1585         // to each other. In order to achieve that QMdiArea must have
1586         // a width which is larger than subWindow->width() * subWindowCount.
1587         if (i == 0) {
1588             int minimumWidth = window->width() * subWindowCount + 20;
1589             mdiArea.resize(mdiArea.size().expandedTo(QSize(minimumWidth, 0)));
1590 #if defined(Q_OS_UNIX)
1591             QCoreApplication::processEvents();
1592             QTest::qWait(100);
1593 #endif
1594         }
1595     }
1596
1597     QList<QMdiSubWindow *> subWindows = mdiArea.subWindowList();
1598     QCOMPARE(subWindows.count(), subWindowCount);
1599
1600     QMdiSubWindow *testWindow = subWindows.at(3);
1601     QVERIFY(testWindow);
1602     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(testWindow);
1603
1604     // childCount
1605     QVERIFY(interface);
1606     QCOMPARE(interface->childCount(), 1);
1607
1608     // setText / text
1609     QCOMPARE(interface->text(QAccessible::Name), QString());
1610     testWindow->setWindowTitle(QLatin1String("ReplaceMe"));
1611     QCOMPARE(interface->text(QAccessible::Name), QLatin1String("ReplaceMe"));
1612     interface->setText(QAccessible::Name, QLatin1String("TitleSetOnWindow"));
1613     QCOMPARE(interface->text(QAccessible::Name), QLatin1String("TitleSetOnWindow"));
1614
1615     mdiArea.setActiveSubWindow(testWindow);
1616
1617     // state
1618     QAccessible::State state;
1619     state.focusable = true;
1620     state.focused = true;
1621     state.movable = true;
1622     state.sizeable = true;
1623
1624     QCOMPARE(interface->state(), state);
1625     const QRect originalGeometry = testWindow->geometry();
1626     testWindow->showMaximized();
1627     state.sizeable = false;
1628     state.movable = false;
1629     QCOMPARE(interface->state(), state);
1630     testWindow->showNormal();
1631     testWindow->move(-10, 0);
1632     QVERIFY(interface->state().offscreen);
1633     testWindow->setVisible(false);
1634     QVERIFY(interface->state().invisible);
1635     testWindow->setVisible(true);
1636     testWindow->setEnabled(false);
1637     QVERIFY(interface->state().disabled);
1638     testWindow->setEnabled(true);
1639     qApp->setActiveWindow(&mdiArea);
1640     mdiArea.setActiveSubWindow(testWindow);
1641     testWindow->setFocus();
1642     QVERIFY(testWindow->isAncestorOf(qApp->focusWidget()));
1643     QVERIFY(interface->state().focused);
1644     testWindow->setGeometry(originalGeometry);
1645
1646     // rect
1647     const QPoint globalPos = testWindow->mapToGlobal(QPoint(0, 0));
1648     QCOMPARE(interface->rect(), QRect(globalPos, testWindow->size()));
1649     testWindow->hide();
1650     QCOMPARE(interface->rect(), QRect());
1651     QCOMPARE(childRect(interface), QRect());
1652     testWindow->showMinimized();
1653     QCOMPARE(childRect(interface), QRect());
1654     testWindow->showNormal();
1655     testWindow->widget()->hide();
1656     QCOMPARE(childRect(interface), QRect());
1657     testWindow->widget()->show();
1658     const QRect widgetGeometry = testWindow->contentsRect();
1659     const QPoint globalWidgetPos = QPoint(globalPos.x() + widgetGeometry.x(),
1660                                           globalPos.y() + widgetGeometry.y());
1661     QCOMPARE(childRect(interface), QRect(globalWidgetPos, widgetGeometry.size()));
1662
1663     // childAt
1664     QCOMPARE(interface->childAt(-10, 0), static_cast<QAccessibleInterface*>(0));
1665     QCOMPARE(interface->childAt(globalPos.x(), globalPos.y()), static_cast<QAccessibleInterface*>(0));
1666     QAccessibleInterface *child = interface->childAt(globalWidgetPos.x(), globalWidgetPos.y());
1667     QCOMPARE(child->role(), QAccessible::PushButton);
1668     QCOMPARE(child->text(QAccessible::Name), QString("QAccessibilityTest"));
1669     delete child;
1670     testWindow->widget()->hide();
1671     QCOMPARE(interface->childAt(globalWidgetPos.x(), globalWidgetPos.y()), static_cast<QAccessibleInterface*>(0));
1672
1673     }
1674     QTestAccessibility::clearEvents();
1675 }
1676
1677 void tst_QAccessibility::lineEditTest()
1678 {
1679     QLineEdit *le = new QLineEdit;
1680     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(le);
1681     QVERIFY(iface);
1682     le->show();
1683
1684     QApplication::processEvents();
1685     QCOMPARE(iface->childCount(), 0);
1686     QVERIFY(iface->state().sizeable);
1687     QVERIFY(iface->state().movable);
1688     QCOMPARE(bool(iface->state().focusable), le->isActiveWindow());
1689     QVERIFY(iface->state().selectable);
1690     QVERIFY(iface->state().hasPopup);
1691     QCOMPARE(bool(iface->state().focused), le->hasFocus());
1692
1693     QString secret(QLatin1String("secret"));
1694     le->setText(secret);
1695     le->setEchoMode(QLineEdit::Normal);
1696     QVERIFY(!(iface->state().passwordEdit));
1697     QCOMPARE(iface->text(QAccessible::Value), secret);
1698     le->setEchoMode(QLineEdit::NoEcho);
1699     QVERIFY(iface->state().passwordEdit);
1700     QVERIFY(iface->text(QAccessible::Value).isEmpty());
1701     le->setEchoMode(QLineEdit::Password);
1702     QVERIFY(iface->state().passwordEdit);
1703     QVERIFY(iface->text(QAccessible::Value).isEmpty());
1704     le->setEchoMode(QLineEdit::PasswordEchoOnEdit);
1705     QVERIFY(iface->state().passwordEdit);
1706     QVERIFY(iface->text(QAccessible::Value).isEmpty());
1707     le->setEchoMode(QLineEdit::Normal);
1708     QVERIFY(!(iface->state().passwordEdit));
1709     QCOMPARE(iface->text(QAccessible::Value), secret);
1710
1711     QWidget *toplevel = new QWidget;
1712     le->setParent(toplevel);
1713     toplevel->show();
1714     QApplication::processEvents();
1715     QVERIFY(!(iface->state().sizeable));
1716     QVERIFY(!(iface->state().movable));
1717     QCOMPARE(bool(iface->state().focusable), le->isActiveWindow());
1718     QVERIFY(iface->state().selectable);
1719     QVERIFY(iface->state().hasPopup);
1720     QCOMPARE(bool(iface->state().focused), le->hasFocus());
1721
1722     QLineEdit *le2 = new QLineEdit(toplevel);
1723     le2->show();
1724     QTest::qWait(100);
1725     le2->activateWindow();
1726     QTest::qWait(100);
1727     le->setFocus(Qt::TabFocusReason);
1728     QTestAccessibility::clearEvents();
1729     le2->setFocus(Qt::TabFocusReason);
1730     QTRY_VERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(le2, 0, QAccessible::Focus)));
1731
1732     le->setText(QLatin1String("500"));
1733     le->setValidator(new QIntValidator());
1734     iface->setText(QAccessible::Value, QLatin1String("This text is not a number"));
1735     QCOMPARE(le->text(), QLatin1String("500"));
1736
1737     delete iface;
1738     delete le;
1739     delete le2;
1740     QTestAccessibility::clearEvents();
1741
1742     // IA2
1743     QString cite = "I always pass on good advice. It is the only thing to do with it. It is never of any use to oneself. --Oscar Wilde";
1744     QLineEdit *le3 = new QLineEdit(cite, toplevel);
1745     iface = QAccessible::queryAccessibleInterface(le3);
1746     QAccessibleTextInterface* textIface = iface->textInterface();
1747     le3->deselect();
1748     le3->setCursorPosition(3);
1749     QCOMPARE(textIface->cursorPosition(), 3);
1750     QTRY_VERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(le3, 0, QAccessible::TextCaretMoved)));
1751     QCOMPARE(textIface->selectionCount(), 0);
1752     QTestAccessibility::clearEvents();
1753
1754     int start, end;
1755     QCOMPARE(textIface->text(0, 8), QString::fromLatin1("I always"));
1756     QCOMPARE(textIface->textAtOffset(0, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("I"));
1757     QCOMPARE(start, 0);
1758     QCOMPARE(end, 1);
1759     QCOMPARE(textIface->textBeforeOffset(0, QAccessible2::CharBoundary,&start,&end), QString());
1760     QCOMPARE(textIface->textAfterOffset(0, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1(" "));
1761     QCOMPARE(start, 1);
1762     QCOMPARE(end, 2);
1763
1764     QCOMPARE(textIface->textAtOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("a"));
1765     QCOMPARE(start, 5);
1766     QCOMPARE(end, 6);
1767     QCOMPARE(textIface->textBeforeOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("w"));
1768     QCOMPARE(textIface->textAfterOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("y"));
1769
1770     QCOMPARE(textIface->textAtOffset(5, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("always"));
1771     QCOMPARE(start, 2);
1772     QCOMPARE(end, 8);
1773
1774     QCOMPARE(textIface->textAtOffset(2, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("always"));
1775     QCOMPARE(textIface->textAtOffset(7, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("always"));
1776     QCOMPARE(textIface->textAtOffset(8, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1(" "));
1777     QCOMPARE(textIface->textAtOffset(25, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("advice"));
1778     QCOMPARE(textIface->textAtOffset(92, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("oneself"));
1779
1780     QCOMPARE(textIface->textBeforeOffset(5, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1(" "));
1781     QCOMPARE(textIface->textAfterOffset(5, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1(" "));
1782     QCOMPARE(textIface->textAtOffset(5, QAccessible2::SentenceBoundary,&start,&end), QString::fromLatin1("I always pass on good advice. "));
1783     QCOMPARE(start, 0);
1784     QCOMPARE(end, 30);
1785
1786     QCOMPARE(textIface->textBeforeOffset(40, QAccessible2::SentenceBoundary,&start,&end), QString::fromLatin1("I always pass on good advice. "));
1787     QCOMPARE(textIface->textAfterOffset(5, QAccessible2::SentenceBoundary,&start,&end), QString::fromLatin1("It is the only thing to do with it. "));
1788
1789     QCOMPARE(textIface->textAtOffset(5, QAccessible2::ParagraphBoundary,&start,&end), cite);
1790     QCOMPARE(start, 0);
1791     QCOMPARE(end, cite.length());
1792     QCOMPARE(textIface->textAtOffset(5, QAccessible2::LineBoundary,&start,&end), cite);
1793     QCOMPARE(textIface->textAtOffset(5, QAccessible2::NoBoundary,&start,&end), cite);
1794
1795     delete iface;
1796     delete toplevel;
1797     QTestAccessibility::clearEvents();
1798 }
1799
1800 void tst_QAccessibility::workspaceTest()
1801 {
1802     {
1803     QWorkspace workspace;
1804     workspace.resize(400,300);
1805     workspace.show();
1806     const int subWindowCount =  3;
1807     for (int i = 0; i < subWindowCount; ++i) {
1808         QWidget *window = workspace.addWindow(new QWidget);
1809         if (i > 0)
1810             window->move(window->x() + 1, window->y());
1811         window->show();
1812         window->resize(70, window->height());
1813     }
1814
1815     QWidgetList subWindows = workspace.windowList();
1816     QCOMPARE(subWindows.count(), subWindowCount);
1817
1818     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&workspace);
1819     QVERIFY(interface);
1820     QCOMPARE(interface->childCount(), subWindowCount);
1821
1822     }
1823     QTestAccessibility::clearEvents();
1824 }
1825
1826 bool accessibleInterfaceLeftOf(const QAccessibleInterface *a1, const QAccessibleInterface *a2)
1827 {
1828     return a1->rect().x() < a2->rect().x();
1829 }
1830
1831 bool accessibleInterfaceAbove(const QAccessibleInterface *a1, const QAccessibleInterface *a2)
1832 {
1833     return a1->rect().y() < a2->rect().y();
1834 }
1835
1836 void tst_QAccessibility::dialogButtonBoxTest()
1837 {
1838     {
1839     QDialogButtonBox box(QDialogButtonBox::Reset |
1840                          QDialogButtonBox::Help |
1841                          QDialogButtonBox::Ok, Qt::Horizontal);
1842
1843
1844     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&box);
1845     QVERIFY(iface);
1846     box.show();
1847 #if defined(Q_OS_UNIX)
1848     QCoreApplication::processEvents();
1849     QTest::qWait(100);
1850 #endif
1851
1852     QApplication::processEvents();
1853     QCOMPARE(iface->childCount(), 3);
1854     QCOMPARE(iface->role(), QAccessible::Grouping);
1855     QStringList actualOrder;
1856     QAccessibleInterface *child;
1857     child = iface->child(0);
1858     QCOMPARE(child->role(), QAccessible::PushButton);
1859
1860     QVector<QAccessibleInterface *> buttons;
1861     for (int i = 0; i < iface->childCount(); ++i)
1862         buttons <<  iface->child(i);
1863
1864     qSort(buttons.begin(), buttons.end(), accessibleInterfaceLeftOf);
1865
1866     for (int i = 0; i < buttons.count(); ++i)
1867         actualOrder << buttons.at(i)->text(QAccessible::Name);
1868
1869     QStringList expectedOrder;
1870     QDialogButtonBox::ButtonLayout btnlout =
1871         QDialogButtonBox::ButtonLayout(QApplication::style()->styleHint(QStyle::SH_DialogButtonLayout));
1872     switch (btnlout) {
1873     case QDialogButtonBox::WinLayout:
1874         expectedOrder << QDialogButtonBox::tr("Reset")
1875                       << QDialogButtonBox::tr("OK")
1876                       << QDialogButtonBox::tr("Help");
1877         break;
1878     case QDialogButtonBox::GnomeLayout:
1879     case QDialogButtonBox::KdeLayout:
1880     case QDialogButtonBox::MacLayout:
1881         expectedOrder << QDialogButtonBox::tr("Help")
1882                       << QDialogButtonBox::tr("Reset")
1883                       << QDialogButtonBox::tr("OK");
1884         break;
1885     }
1886     QCOMPARE(actualOrder, expectedOrder);
1887     delete iface;
1888     QApplication::processEvents();
1889     QTestAccessibility::clearEvents();
1890     }
1891
1892     {
1893     QDialogButtonBox box(QDialogButtonBox::Reset |
1894                          QDialogButtonBox::Help |
1895                          QDialogButtonBox::Ok, Qt::Horizontal);
1896
1897
1898     // Test up and down navigation
1899     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&box);
1900     QVERIFY(iface);
1901     box.setOrientation(Qt::Vertical);
1902     box.show();
1903 #if defined(Q_OS_UNIX)
1904     QCoreApplication::processEvents();
1905     QTest::qWait(100);
1906 #endif
1907
1908     QApplication::processEvents();
1909     QStringList actualOrder;
1910
1911     QVector<QAccessibleInterface *> buttons;
1912     for (int i = 0; i < iface->childCount(); ++i)
1913         buttons <<  iface->child(i);
1914
1915     qSort(buttons.begin(), buttons.end(), accessibleInterfaceAbove);
1916
1917     for (int i = 0; i < buttons.count(); ++i)
1918         actualOrder << buttons.at(i)->text(QAccessible::Name);
1919
1920     QStringList expectedOrder;
1921     expectedOrder << QDialogButtonBox::tr("OK")
1922                   << QDialogButtonBox::tr("Reset")
1923                   << QDialogButtonBox::tr("Help");
1924
1925     QCOMPARE(actualOrder, expectedOrder);
1926     delete iface;
1927     QApplication::processEvents();
1928
1929     }
1930     QTestAccessibility::clearEvents();
1931 }
1932
1933 void tst_QAccessibility::dialTest()
1934 {
1935     {
1936     QDial dial;
1937     dial.setMinimum(23);
1938     dial.setMaximum(121);
1939     dial.setValue(42);
1940     QCOMPARE(dial.value(), 42);
1941     dial.show();
1942
1943     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&dial);
1944     QVERIFY(interface);
1945     QCOMPARE(interface->childCount(), 0);
1946
1947     QCOMPARE(interface->text(QAccessible::Value), QString::number(dial.value()));
1948     QCOMPARE(interface->rect(), dial.geometry());
1949
1950     QAccessibleValueInterface *valueIface = interface->valueInterface();
1951     QVERIFY(valueIface != 0);
1952     QCOMPARE(valueIface->minimumValue().toInt(), dial.minimum());
1953     QCOMPARE(valueIface->maximumValue().toInt(), dial.maximum());
1954     QCOMPARE(valueIface->currentValue().toInt(), 42);
1955     dial.setValue(50);
1956     QCOMPARE(valueIface->currentValue().toInt(), dial.value());
1957     dial.setValue(0);
1958     QCOMPARE(valueIface->currentValue().toInt(), dial.value());
1959     dial.setValue(100);
1960     QCOMPARE(valueIface->currentValue().toInt(), dial.value());
1961     valueIface->setCurrentValue(77);
1962     QCOMPARE(77, dial.value());
1963     }
1964     QTestAccessibility::clearEvents();
1965 }
1966
1967 void tst_QAccessibility::rubberBandTest()
1968 {
1969     QRubberBand rubberBand(QRubberBand::Rectangle);
1970     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&rubberBand);
1971     QVERIFY(interface);
1972     QCOMPARE(interface->role(), QAccessible::Border);
1973     delete interface;
1974     QTestAccessibility::clearEvents();
1975 }
1976
1977 void tst_QAccessibility::abstractScrollAreaTest()
1978 {
1979     {
1980     QAbstractScrollArea abstractScrollArea;
1981
1982     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&abstractScrollArea);
1983     QVERIFY(interface);
1984     QVERIFY(!interface->rect().isValid());
1985     QCOMPARE(interface->childAt(200, 200), static_cast<QAccessibleInterface*>(0));
1986
1987     abstractScrollArea.resize(400, 400);
1988     abstractScrollArea.show();
1989 #if defined(Q_OS_UNIX)
1990     QCoreApplication::processEvents();
1991     QTest::qWait(100);
1992 #endif
1993     const QRect globalGeometry = QRect(abstractScrollArea.mapToGlobal(QPoint(0, 0)),
1994                                        abstractScrollArea.size());
1995
1996     // Viewport.
1997     QCOMPARE(interface->childCount(), 1);
1998     QWidget *viewport = abstractScrollArea.viewport();
1999     QVERIFY(viewport);
2000     QVERIFY(verifyChild(viewport, interface, 0, globalGeometry));
2001
2002     // Horizontal scrollBar.
2003     abstractScrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2004     QCOMPARE(interface->childCount(), 2);
2005     QWidget *horizontalScrollBar = abstractScrollArea.horizontalScrollBar();
2006     QWidget *horizontalScrollBarContainer = horizontalScrollBar->parentWidget();
2007     QVERIFY(verifyChild(horizontalScrollBarContainer, interface, 1, globalGeometry));
2008
2009     // Horizontal scrollBar widgets.
2010     QLabel *secondLeftLabel = new QLabel(QLatin1String("L2"));
2011     abstractScrollArea.addScrollBarWidget(secondLeftLabel, Qt::AlignLeft);
2012     QCOMPARE(interface->childCount(), 2);
2013
2014     QLabel *firstLeftLabel = new QLabel(QLatin1String("L1"));
2015     abstractScrollArea.addScrollBarWidget(firstLeftLabel, Qt::AlignLeft);
2016     QCOMPARE(interface->childCount(), 2);
2017
2018     QLabel *secondRightLabel = new QLabel(QLatin1String("R2"));
2019     abstractScrollArea.addScrollBarWidget(secondRightLabel, Qt::AlignRight);
2020     QCOMPARE(interface->childCount(), 2);
2021
2022     QLabel *firstRightLabel = new QLabel(QLatin1String("R1"));
2023     abstractScrollArea.addScrollBarWidget(firstRightLabel, Qt::AlignRight);
2024     QCOMPARE(interface->childCount(), 2);
2025
2026     // Vertical scrollBar.
2027     abstractScrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2028     QCOMPARE(interface->childCount(), 3);
2029     QWidget *verticalScrollBar = abstractScrollArea.verticalScrollBar();
2030     QWidget *verticalScrollBarContainer = verticalScrollBar->parentWidget();
2031     QVERIFY(verifyChild(verticalScrollBarContainer, interface, 2, globalGeometry));
2032
2033     // Vertical scrollBar widgets.
2034     QLabel *secondTopLabel = new QLabel(QLatin1String("T2"));
2035     abstractScrollArea.addScrollBarWidget(secondTopLabel, Qt::AlignTop);
2036     QCOMPARE(interface->childCount(), 3);
2037
2038     QLabel *firstTopLabel = new QLabel(QLatin1String("T1"));
2039     abstractScrollArea.addScrollBarWidget(firstTopLabel, Qt::AlignTop);
2040     QCOMPARE(interface->childCount(), 3);
2041
2042     QLabel *secondBottomLabel = new QLabel(QLatin1String("B2"));
2043     abstractScrollArea.addScrollBarWidget(secondBottomLabel, Qt::AlignBottom);
2044     QCOMPARE(interface->childCount(), 3);
2045
2046     QLabel *firstBottomLabel = new QLabel(QLatin1String("B1"));
2047     abstractScrollArea.addScrollBarWidget(firstBottomLabel, Qt::AlignBottom);
2048     QCOMPARE(interface->childCount(), 3);
2049
2050     // CornerWidget.
2051     abstractScrollArea.setCornerWidget(new QLabel(QLatin1String("C")));
2052     QCOMPARE(interface->childCount(), 4);
2053     QWidget *cornerWidget = abstractScrollArea.cornerWidget();
2054     QVERIFY(verifyChild(cornerWidget, interface, 3, globalGeometry));
2055
2056     QCOMPARE(verifyHierarchy(interface), 0);
2057
2058     delete interface;
2059     }
2060
2061     QTestAccessibility::clearEvents();
2062 }
2063
2064 void tst_QAccessibility::scrollAreaTest()
2065 {
2066     {
2067     QScrollArea scrollArea;
2068     scrollArea.show();
2069 #if defined(Q_OS_UNIX)
2070     QCoreApplication::processEvents();
2071     QTest::qWait(100);
2072 #endif
2073     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&scrollArea);
2074     QVERIFY(interface);
2075     QCOMPARE(interface->childCount(), 1); // The viewport.
2076     delete interface;
2077     }
2078     QTestAccessibility::clearEvents();
2079 }
2080
2081 void tst_QAccessibility::listTest()
2082 {
2083     {
2084     QListWidget *listView = new QListWidget;
2085     listView->addItem("Oslo");
2086     listView->addItem("Berlin");
2087     listView->addItem("Brisbane");
2088     listView->resize(400,400);
2089     listView->show();
2090     QTest::qWait(1); // Need this for indexOfchild to work.
2091     QCoreApplication::processEvents();
2092     QTest::qWait(100);
2093
2094     QAIPtr iface = QAIPtr(QAccessible::queryAccessibleInterface(listView));
2095     QCOMPARE(verifyHierarchy(iface.data()), 0);
2096
2097     QCOMPARE((int)iface->role(), (int)QAccessible::List);
2098     QCOMPARE(iface->childCount(), 3);
2099
2100     {
2101     QAIPtr child1 = QAIPtr(iface->child(0));
2102     QVERIFY(child1);
2103     QCOMPARE(iface->indexOfChild(child1.data()), 0);
2104     QCOMPARE(child1->text(QAccessible::Name), QString("Oslo"));
2105     QCOMPARE(child1->role(), QAccessible::ListItem);
2106
2107     QAIPtr child2 = QAIPtr(iface->child(1));
2108     QVERIFY(child2);
2109     QCOMPARE(iface->indexOfChild(child2.data()), 1);
2110     QCOMPARE(child2->text(QAccessible::Name), QString("Berlin"));
2111
2112     QAIPtr child3 = QAIPtr(iface->child(2));
2113     QVERIFY(child3);
2114     QCOMPARE(iface->indexOfChild(child3.data()), 2);
2115     QCOMPARE(child3->text(QAccessible::Name), QString("Brisbane"));
2116     }
2117     QTestAccessibility::clearEvents();
2118
2119     // Check for events
2120     QTest::mouseClick(listView->viewport(), Qt::LeftButton, 0, listView->visualItemRect(listView->item(1)).center());
2121     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 2, QAccessible::Selection)));
2122     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 2, QAccessible::Focus)));
2123     QTest::mouseClick(listView->viewport(), Qt::LeftButton, 0, listView->visualItemRect(listView->item(2)).center());
2124     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 3, QAccessible::Selection)));
2125     QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 3, QAccessible::Focus)));
2126
2127     listView->addItem("Munich");
2128     QCOMPARE(iface->childCount(), 4);
2129
2130     // table 2
2131     QAccessibleTableInterface *table2 = iface->tableInterface();
2132     QVERIFY(table2);
2133     QCOMPARE(table2->columnCount(), 1);
2134     QCOMPARE(table2->rowCount(), 4);
2135     QAIPtr cell1 = QAIPtr(table2->cellAt(0,0));
2136     QVERIFY(cell1);
2137     QCOMPARE(cell1->text(QAccessible::Name), QString("Oslo"));
2138
2139     QAIPtr cell4 = QAIPtr(table2->cellAt(3,0));
2140     QVERIFY(cell4);
2141     QCOMPARE(cell4->text(QAccessible::Name), QString("Munich"));
2142     QCOMPARE(cell4->role(), QAccessible::ListItem);
2143
2144     QAccessibleTableCellInterface *cellInterface = cell4->tableCellInterface();
2145     QVERIFY(cellInterface);
2146     QCOMPARE(cellInterface->rowIndex(), 3);
2147     QCOMPARE(cellInterface->columnIndex(), 0);
2148     QCOMPARE(cellInterface->rowExtent(), 1);
2149     QCOMPARE(cellInterface->columnExtent(), 1);
2150     QCOMPARE(cellInterface->rowHeaderCells(), QList<QAccessibleInterface*>());
2151     QCOMPARE(cellInterface->columnHeaderCells(), QList<QAccessibleInterface*>());
2152
2153     QCOMPARE(QAIPtr(cellInterface->table())->object(), listView);
2154
2155     listView->clearSelection();
2156     QVERIFY(!(cell4->state().expandable));
2157     QVERIFY( (cell4->state().selectable));
2158     QVERIFY(!(cell4->state().selected));
2159     table2->selectRow(3);
2160     QCOMPARE(listView->selectedItems().size(), 1);
2161     QCOMPARE(listView->selectedItems().at(0)->text(), QLatin1String("Munich"));
2162     QVERIFY(cell4->state().selected);
2163     QVERIFY(cellInterface->isSelected());
2164
2165     QVERIFY(table2->cellAt(-1, 0) == 0);
2166     QVERIFY(table2->cellAt(0, -1) == 0);
2167     QVERIFY(table2->cellAt(0, 1) == 0);
2168     QVERIFY(table2->cellAt(4, 0) == 0);
2169
2170     delete listView;
2171     }
2172     QTestAccessibility::clearEvents();
2173 }
2174
2175 void tst_QAccessibility::treeTest()
2176 {
2177     QTreeWidget *treeView = new QTreeWidget;
2178     treeView->setColumnCount(2);
2179     QTreeWidgetItem *header = new QTreeWidgetItem;
2180     header->setText(0, "Artist");
2181     header->setText(1, "Work");
2182     treeView->setHeaderItem(header);
2183
2184     QTreeWidgetItem *root1 = new QTreeWidgetItem;
2185     root1->setText(0, "Spain");
2186     treeView->addTopLevelItem(root1);
2187
2188     QTreeWidgetItem *item1 = new QTreeWidgetItem;
2189     item1->setText(0, "Picasso");
2190     item1->setText(1, "Guernica");
2191     root1->addChild(item1);
2192
2193     QTreeWidgetItem *item2 = new QTreeWidgetItem;
2194     item2->setText(0, "Tapies");
2195     item2->setText(1, "Ambrosia");
2196     root1->addChild(item2);
2197
2198     QTreeWidgetItem *root2 = new QTreeWidgetItem;
2199     root2->setText(0, "Austria");
2200     treeView->addTopLevelItem(root2);
2201
2202     QTreeWidgetItem *item3 = new QTreeWidgetItem;
2203     item3->setText(0, "Klimt");
2204     item3->setText(1, "The Kiss");
2205     root2->addChild(item3);
2206
2207     treeView->resize(400,400);
2208     treeView->show();
2209
2210     QCoreApplication::processEvents();
2211     QTest::qWait(100);
2212
2213     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(treeView);
2214     QCOMPARE(verifyHierarchy(iface), 0);
2215
2216     QCOMPARE((int)iface->role(), (int)QAccessible::Tree);
2217     // header and 2 rows (the others are not expanded, thus not visible)
2218     QCOMPARE(iface->childCount(), 6);
2219
2220     QAccessibleInterface *header1 = 0;
2221     header1 = iface->child(0);
2222     QVERIFY(header1);
2223     QCOMPARE(iface->indexOfChild(header1), 0);
2224     QCOMPARE(header1->text(QAccessible::Name), QString("Artist"));
2225     QCOMPARE(header1->role(), QAccessible::ColumnHeader);
2226     delete header1;
2227
2228     QAccessibleInterface *child1 = 0;
2229     child1 = iface->child(2);
2230     QVERIFY(child1);
2231     QCOMPARE(iface->indexOfChild(child1), 2);
2232     QCOMPARE(child1->text(QAccessible::Name), QString("Spain"));
2233     QCOMPARE(child1->role(), QAccessible::TreeItem);
2234     QVERIFY(!(child1->state().expanded));
2235     delete child1;
2236
2237     QAccessibleInterface *child2 = 0;
2238     child2 = iface->child(4);
2239     QVERIFY(child2);
2240     QCOMPARE(iface->indexOfChild(child2), 4);
2241     QCOMPARE(child2->text(QAccessible::Name), QString("Austria"));
2242     delete child2;
2243
2244     QTestAccessibility::clearEvents();
2245
2246     // table 2
2247     QAccessibleTableInterface *table2 = iface->tableInterface();
2248     QVERIFY(table2);
2249     QCOMPARE(table2->columnCount(), 2);
2250     QCOMPARE(table2->rowCount(), 2);
2251     QAccessibleInterface *cell1;
2252     QVERIFY(cell1 = table2->cellAt(0,0));
2253     QCOMPARE(cell1->text(QAccessible::Name), QString("Spain"));
2254     QAccessibleInterface *cell2;
2255     QVERIFY(cell2 = table2->cellAt(1,0));
2256     QCOMPARE(cell2->text(QAccessible::Name), QString("Austria"));
2257     QCOMPARE(cell2->role(), QAccessible::TreeItem);
2258     QCOMPARE(cell2->tableCellInterface()->rowIndex(), 1);
2259     QCOMPARE(cell2->tableCellInterface()->columnIndex(), 0);
2260     QVERIFY(cell2->state().expandable);
2261     QCOMPARE(iface->indexOfChild(cell2), 4);
2262     QVERIFY(!(cell2->state().expanded));
2263     QCOMPARE(table2->columnDescription(1), QString("Work"));
2264     delete cell2;
2265     delete cell1;
2266
2267     treeView->expandAll();
2268
2269     // Need this for indexOfchild to work.
2270     QCoreApplication::processEvents();
2271     QTest::qWait(100);
2272
2273     QCOMPARE(table2->columnCount(), 2);
2274     QCOMPARE(table2->rowCount(), 5);
2275     cell1 = table2->cellAt(1,0);
2276     QCOMPARE(cell1->text(QAccessible::Name), QString("Picasso"));
2277     QCOMPARE(iface->indexOfChild(cell1), 4); // 2 header + 2 for root item
2278
2279     cell2 = table2->cellAt(4,0);
2280     QCOMPARE(cell2->text(QAccessible::Name), QString("Klimt"));
2281     QCOMPARE(cell2->role(), QAccessible::TreeItem);
2282     QCOMPARE(cell2->tableCellInterface()->rowIndex(), 4);
2283     QCOMPARE(cell2->tableCellInterface()->columnIndex(), 0);
2284     QVERIFY(!(cell2->state().expandable));
2285     QCOMPARE(iface->indexOfChild(cell2), 10);
2286
2287     QCOMPARE(table2->columnDescription(0), QString("Artist"));
2288     QCOMPARE(table2->columnDescription(1), QString("Work"));
2289
2290     delete iface;
2291     QTestAccessibility::clearEvents();
2292 }
2293
2294 void tst_QAccessibility::tableTest()
2295 {
2296     QTableWidget *tableView = new QTableWidget(3, 3);
2297     tableView->setColumnCount(3);
2298     QStringList hHeader;
2299     hHeader << "h1" << "h2" << "h3";
2300     tableView->setHorizontalHeaderLabels(hHeader);
2301
2302     QStringList vHeader;
2303     vHeader << "v1" << "v2" << "v3";
2304     tableView->setVerticalHeaderLabels(vHeader);
2305
2306     for (int i = 0; i<9; ++i) {
2307         QTableWidgetItem *item = new QTableWidgetItem;
2308         item->setText(QString::number(i/3) + QString(".") + QString::number(i%3));
2309         tableView->setItem(i/3, i%3, item);
2310     }
2311
2312     tableView->resize(600,600);
2313     tableView->show();
2314     QTest::qWait(1); // Need this for indexOfchild to work.
2315     QCoreApplication::processEvents();
2316     QTest::qWait(100);
2317
2318     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(tableView);
2319     QCOMPARE(verifyHierarchy(iface), 0);
2320
2321     QCOMPARE((int)iface->role(), (int)QAccessible::Table);
2322     // header and 2 rows (the others are not expanded, thus not visible)
2323     QCOMPARE(iface->childCount(), 9+3+3+1); // cell+headers+topleft button
2324
2325     QAccessibleInterface *cornerButton = iface->child(0);
2326     QVERIFY(cornerButton);
2327     QCOMPARE(iface->indexOfChild(cornerButton), 0);
2328     QCOMPARE(cornerButton->role(), QAccessible::Pane);
2329     delete cornerButton;
2330
2331     QAccessibleInterface *child1 = iface->child(2);
2332     QVERIFY(child1);
2333     QCOMPARE(iface->indexOfChild(child1), 2);
2334     QCOMPARE(child1->text(QAccessible::Name), QString("h2"));
2335     QCOMPARE(child1->role(), QAccessible::ColumnHeader);
2336     QVERIFY(!(child1->state().expanded));
2337     delete child1;
2338
2339     QAccessibleInterface *child2 = iface->child(10);
2340     QVERIFY(child2);
2341     QCOMPARE(iface->indexOfChild(child2), 10);
2342     QCOMPARE(child2->text(QAccessible::Name), QString("1.1"));
2343     QAccessibleTableCellInterface *cell2Iface = child2->tableCellInterface();
2344     QCOMPARE(cell2Iface->rowIndex(), 1);
2345     QCOMPARE(cell2Iface->columnIndex(), 1);
2346     delete child2;
2347
2348     QAccessibleInterface *child3 = iface->child(11);
2349     QCOMPARE(iface->indexOfChild(child3), 11);
2350     QCOMPARE(child3->text(QAccessible::Name), QString("1.2"));
2351     delete child3;
2352
2353     QTestAccessibility::clearEvents();
2354
2355     // table 2
2356     QAccessibleTableInterface *table2 = iface->tableInterface();
2357     QVERIFY(table2);
2358     QCOMPARE(table2->columnCount(), 3);
2359     QCOMPARE(table2->rowCount(), 3);
2360     QAccessibleInterface *cell1;
2361     QVERIFY(cell1 = table2->cellAt(0,0));
2362     QCOMPARE(cell1->text(QAccessible::Name), QString("0.0"));
2363     QCOMPARE(iface->indexOfChild(cell1), 5);
2364
2365     QAccessibleInterface *cell2;
2366     QVERIFY(cell2 = table2->cellAt(0,1));
2367     QCOMPARE(cell2->text(QAccessible::Name), QString("0.1"));
2368     QCOMPARE(cell2->role(), QAccessible::Cell);
2369     QCOMPARE(cell2->tableCellInterface()->rowIndex(), 0);
2370     QCOMPARE(cell2->tableCellInterface()->columnIndex(), 1);
2371     QCOMPARE(iface->indexOfChild(cell2), 6);
2372     delete cell2;
2373
2374     QAccessibleInterface *cell3;
2375     QVERIFY(cell3 = table2->cellAt(1,2));
2376     QCOMPARE(cell3->text(QAccessible::Name), QString("1.2"));
2377     QCOMPARE(cell3->role(), QAccessible::Cell);
2378     QCOMPARE(cell3->tableCellInterface()->rowIndex(), 1);
2379     QCOMPARE(cell3->tableCellInterface()->columnIndex(), 2);
2380     QCOMPARE(iface->indexOfChild(cell3), 11);
2381     delete cell3;
2382
2383     QCOMPARE(table2->columnDescription(0), QString("h1"));
2384     QCOMPARE(table2->columnDescription(1), QString("h2"));
2385     QCOMPARE(table2->columnDescription(2), QString("h3"));
2386     QCOMPARE(table2->rowDescription(0), QString("v1"));
2387     QCOMPARE(table2->rowDescription(1), QString("v2"));
2388     QCOMPARE(table2->rowDescription(2), QString("v3"));
2389
2390     delete iface;
2391
2392     delete tableView;
2393
2394     QTestAccessibility::clearEvents();
2395 }
2396
2397 void tst_QAccessibility::calendarWidgetTest()
2398 {
2399 #ifndef QT_NO_CALENDARWIDGET
2400     {
2401     QCalendarWidget calendarWidget;
2402
2403     QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&calendarWidget);
2404     QVERIFY(interface);
2405     QCOMPARE(interface->role(), QAccessible::Table);
2406     QVERIFY(!interface->rect().isValid());
2407     QCOMPARE(interface->childAt(200, 200), static_cast<QAccessibleInterface*>(0));
2408
2409     calendarWidget.resize(400, 300);
2410     calendarWidget.show();
2411 #if defined(Q_OS_UNIX)
2412     QCoreApplication::processEvents();
2413     QTest::qWait(100);
2414 #endif
2415
2416     // 1 = navigationBar, 2 = view.
2417     QCOMPARE(interface->childCount(), 2);
2418
2419     const QRect globalGeometry = QRect(calendarWidget.mapToGlobal(QPoint(0, 0)),
2420                                        calendarWidget.size());
2421     QCOMPARE(interface->rect(), globalGeometry);
2422
2423     QWidget *navigationBar = 0;
2424     foreach (QObject *child, calendarWidget.children()) {
2425         if (child->objectName() == QLatin1String("qt_calendar_navigationbar")) {
2426             navigationBar = static_cast<QWidget *>(child);
2427             break;
2428         }
2429     }
2430     QVERIFY(navigationBar);
2431     QVERIFY(verifyChild(navigationBar, interface, 0, globalGeometry));
2432
2433     QAbstractItemView *calendarView = 0;
2434     foreach (QObject *child, calendarWidget.children()) {
2435         if (child->objectName() == QLatin1String("qt_calendar_calendarview")) {
2436             calendarView = static_cast<QAbstractItemView *>(child);
2437             break;
2438         }
2439     }
2440     QVERIFY(calendarView);
2441     QVERIFY(verifyChild(calendarView, interface, 1, globalGeometry));
2442
2443     // Hide navigation bar.
2444     calendarWidget.setNavigationBarVisible(false);
2445     QCOMPARE(interface->childCount(), 1);
2446     QVERIFY(!navigationBar->isVisible());
2447
2448     QVERIFY(verifyChild(calendarView, interface, 0, globalGeometry));
2449
2450     // Show navigation bar.
2451     calendarWidget.setNavigationBarVisible(true);
2452     QCOMPARE(interface->childCount(), 2);
2453     QVERIFY(navigationBar->isVisible());
2454
2455     // Navigate to the navigation bar via Child.
2456     QAccessibleInterface *navigationBarInterface = interface->child(0);
2457     QVERIFY(navigationBarInterface);
2458     QCOMPARE(navigationBarInterface->object(), (QObject*)navigationBar);
2459
2460     // Navigate to the view via Child.
2461     QAccessibleInterface *calendarViewInterface = interface->child(1);
2462     QVERIFY(calendarViewInterface);
2463     QCOMPARE(calendarViewInterface->object(), (QObject*)calendarView);
2464
2465     QVERIFY(!interface->child(-1));
2466
2467     // In order for geometric navigation to work they must share the same parent
2468     QCOMPARE(navigationBarInterface->parent()->object(), calendarViewInterface->parent()->object());
2469     QVERIFY(navigationBarInterface->rect().bottom() < calendarViewInterface->rect().top());
2470     delete calendarViewInterface;
2471     calendarViewInterface = 0;
2472     delete navigationBarInterface;
2473     navigationBarInterface = 0;
2474
2475     }
2476     QTestAccessibility::clearEvents();
2477 #endif // QT_NO_CALENDARWIDGET
2478 }
2479
2480 void tst_QAccessibility::dockWidgetTest()
2481 {
2482 #ifndef QT_NO_DOCKWIDGET
2483     // Set up a proper main window with two dock widgets
2484     QMainWindow *mw = new QMainWindow();
2485     QFrame *central = new QFrame(mw);
2486     mw->setCentralWidget(central);
2487     QMenuBar *mb = new QMenuBar(mw);
2488     mb->addAction(tr("&File"));
2489     mw->setMenuBar(mb);
2490
2491     QDockWidget *dock1 = new QDockWidget(mw);
2492     mw->addDockWidget(Qt::LeftDockWidgetArea, dock1);
2493     QPushButton *pb1 = new QPushButton(tr("Push me"), dock1);
2494     dock1->setWidget(pb1);
2495
2496     QDockWidget *dock2 = new QDockWidget(mw);
2497     mw->addDockWidget(Qt::BottomDockWidgetArea, dock2);
2498     QPushButton *pb2 = new QPushButton(tr("Push me"), dock2);
2499     dock2->setWidget(pb2);
2500
2501     mw->resize(600,400);
2502     mw->show();
2503 #if defined(Q_OS_UNIX)
2504     QCoreApplication::processEvents();
2505     QTest::qWait(100);
2506 #endif
2507
2508     QAccessibleInterface *accMainWindow = QAccessible::queryAccessibleInterface(mw);
2509     // 4 children: menu bar, dock1, dock2, and central widget
2510     QCOMPARE(accMainWindow->childCount(), 4);
2511     QAccessibleInterface *accDock1 = 0;
2512     for (int i = 0; i < 4; ++i) {
2513         accDock1 = accMainWindow->child(i);
2514         if (accMainWindow->role() == QAccessible::Window) {
2515             if (accDock1 && qobject_cast<QDockWidget*>(accDock1->object()) == dock1) {
2516                 break;
2517             } else {
2518                 delete accDock1;
2519             }
2520         }
2521     }
2522     QVERIFY(accDock1);
2523     QCOMPARE(accDock1->role(), QAccessible::Window);
2524
2525     QAccessibleInterface *dock1TitleBar = accDock1->child(0);
2526     QCOMPARE(dock1TitleBar->role(), QAccessible::TitleBar);
2527     QVERIFY(accDock1->rect().contains(dock1TitleBar->rect()));
2528     delete dock1TitleBar;
2529
2530     QPoint globalPos = dock1->mapToGlobal(QPoint(0,0));
2531     globalPos.rx()+=5;  //### query style
2532     globalPos.ry()+=5;
2533     QAccessibleInterface *childAt = accDock1->childAt(globalPos.x(), globalPos.y());    //###
2534     QCOMPARE(childAt->role(), QAccessible::TitleBar);
2535     int index = accDock1->indexOfChild(childAt);
2536     delete childAt;
2537     QAccessibleInterface *accTitleBar = accDock1->child(index);
2538
2539     QCOMPARE(accTitleBar->role(), QAccessible::TitleBar);
2540     QCOMPARE(accDock1->indexOfChild(accTitleBar), 0);
2541     QAccessibleInterface *acc;
2542     acc = accTitleBar->parent();
2543     QVERIFY(acc);
2544     QCOMPARE(acc->role(), QAccessible::Window);
2545
2546
2547     delete accTitleBar;
2548     delete accDock1;
2549     delete pb1;
2550     delete pb2;
2551     delete dock1;
2552     delete dock2;
2553     delete mw;
2554     QTestAccessibility::clearEvents();
2555 #endif // QT_NO_DOCKWIDGET
2556 }
2557
2558 void tst_QAccessibility::comboBoxTest()
2559 {
2560 #if defined(Q_OS_WINCE)
2561     if (!IsValidCEPlatform())
2562         QSKIP("Test skipped on Windows Mobile test hardware");
2563 #endif
2564     { // not editable combobox
2565     QComboBox combo;
2566     combo.addItems(QStringList() << "one" << "two" << "three");
2567     combo.show();
2568     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&combo);
2569     QCOMPARE(verifyHierarchy(iface), 0);
2570
2571     QCOMPARE(iface->role(), QAccessible::ComboBox);
2572     QCOMPARE(iface->childCount(), 1);
2573
2574 #ifdef Q_OS_UNIX
2575     QCOMPARE(iface->text(QAccessible::Name), QLatin1String("one"));
2576 #endif
2577     QCOMPARE(iface->text(QAccessible::Value), QLatin1String("one"));
2578     combo.setCurrentIndex(2);
2579 #ifdef Q_OS_UNIX
2580     QCOMPARE(iface->text(QAccessible::Name), QLatin1String("three"));
2581 #endif
2582     QCOMPARE(iface->text(QAccessible::Value), QLatin1String("three"));
2583
2584     QAccessibleInterface *listIface = iface->child(0);
2585     QCOMPARE(listIface->role(), QAccessible::List);
2586     QCOMPARE(listIface->childCount(), 3);
2587
2588     QVERIFY(!combo.view()->isVisible());
2589     QVERIFY(iface->actionInterface());
2590     QCOMPARE(iface->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
2591     iface->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
2592     QVERIFY(combo.view()->isVisible());
2593
2594     delete iface;
2595     }
2596
2597     { // editable combobox
2598     QComboBox editableCombo;
2599     editableCombo.show();
2600     editableCombo.setEditable(true);
2601     editableCombo.addItems(QStringList() << "foo" << "bar" << "baz");
2602
2603     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&editableCombo);
2604     QCOMPARE(verifyHierarchy(iface), 0);
2605
2606     QCOMPARE(iface->role(), QAccessible::ComboBox);
2607     QCOMPARE(iface->childCount(), 2);
2608
2609     QAccessibleInterface *listIface = iface->child(0);
2610     QCOMPARE(listIface->role(), QAccessible::List);
2611     QAccessibleInterface *editIface = iface->child(1);
2612     QCOMPARE(editIface->role(), QAccessible::EditableText);
2613
2614     delete listIface;
2615     delete editIface;
2616     delete iface;
2617     }
2618
2619     QTestAccessibility::clearEvents();
2620 }
2621
2622 void tst_QAccessibility::labelTest()
2623 {
2624     QString text = "Hello World";
2625     QLabel *label = new QLabel(text);
2626     label->show();
2627
2628 #if defined(Q_OS_UNIX)
2629     QCoreApplication::processEvents();
2630 #endif
2631     QTest::qWait(100);
2632
2633     QAccessibleInterface *acc_label = QAccessible::queryAccessibleInterface(label);
2634     QVERIFY(acc_label);
2635
2636     QCOMPARE(acc_label->text(QAccessible::Name), text);
2637
2638     delete acc_label;
2639     delete label;
2640     QTestAccessibility::clearEvents();
2641
2642     QPixmap testPixmap(50, 50);
2643     testPixmap.fill();
2644
2645     QLabel imageLabel;
2646     imageLabel.setPixmap(testPixmap);
2647     imageLabel.setToolTip("Test Description");
2648
2649     acc_label = QAccessible::queryAccessibleInterface(&imageLabel);
2650     QVERIFY(acc_label);
2651
2652     QAccessibleImageInterface *imageInterface = acc_label->imageInterface();
2653     QVERIFY(imageInterface);
2654
2655     QCOMPARE(imageInterface->imageSize(), testPixmap.size());
2656     QCOMPARE(imageInterface->imageDescription(), QString::fromLatin1("Test Description"));
2657     QCOMPARE(imageInterface->imagePosition(QAccessible2::RelativeToParent), imageLabel.geometry());
2658
2659     delete acc_label;
2660
2661     QTestAccessibility::clearEvents();
2662 }
2663
2664 void tst_QAccessibility::accelerators()
2665 {
2666     QWidget *window = new QWidget;
2667     QHBoxLayout *lay = new QHBoxLayout(window);
2668     QLabel *label = new QLabel(tr("&Line edit"), window);
2669     QLineEdit *le = new QLineEdit(window);
2670     lay->addWidget(label);
2671     lay->addWidget(le);
2672     label->setBuddy(le);
2673
2674     window->show();
2675
2676     QAccessibleInterface *accLineEdit = QAccessible::queryAccessibleInterface(le);
2677     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("L"));
2678     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("L"));
2679     label->setText(tr("Q &"));
2680     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2681     label->setText(tr("Q &&"));
2682     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2683     label->setText(tr("Q && A"));
2684     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2685     label->setText(tr("Q &&&A"));
2686     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("A"));
2687     label->setText(tr("Q &&A"));
2688     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2689
2690 #if !defined(QT_NO_DEBUG) && !defined(Q_OS_MAC)
2691     QTest::ignoreMessage(QtWarningMsg, "QKeySequence::mnemonic: \"Q &A&B\" contains multiple occurrences of '&'");
2692 #endif
2693     label->setText(tr("Q &A&B"));
2694     QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("A"));
2695
2696 #if defined(Q_OS_UNIX)
2697     QCoreApplication::processEvents();
2698 #endif
2699     QTest::qWait(100);
2700     delete window;
2701     QTestAccessibility::clearEvents();
2702 }
2703
2704 QTEST_MAIN(tst_QAccessibility)
2705 #include "tst_qaccessibility.moc"