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