1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
43 #include <QtCore/qglobal.h>
45 # include <QtCore/qt_windows.h>
47 # include <servprov.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>
57 #include <QtTest/QtTest>
61 #include <qpa/qplatformnativeinterface.h>
63 #if defined(Q_OS_WIN) && defined(interface)
68 #include "QtTest/qtestaccessible.h"
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")))
84 typedef QSharedPointer<QAccessibleInterface> QAIPtr;
86 static inline bool verifyChild(QWidget *child, QAccessibleInterface *interface,
87 int index, const QRect &domain)
90 qWarning("tst_QAccessibility::verifyChild: null pointer to child.");
95 qWarning("tst_QAccessibility::verifyChild: null pointer to interface.");
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.");
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;
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)
121 const QRect rectFromInterface = navigatedChildInterface->rect();
122 delete navigatedChildInterface;
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";
135 if (childAtInterface->object() != childInterface->object()) {
136 qWarning("tst_QAccessibility::verifyChild (childAt()):");
137 qWarning() << "Expected:" << childInterface;
138 qWarning() << "Actual: " << childAtInterface;
141 delete childInterface;
142 delete childAtInterface;
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;
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.");
164 static inline int indexOfChild(QAccessibleInterface *parentInterface, QWidget *childWidget)
166 if (!parentInterface || !childWidget)
168 QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(childWidget);
171 int index = parentInterface->indexOfChild(childInterface);
172 delete childInterface;
176 #define EXPECT(cond) \
178 if (!errorAt && !(cond)) { \
179 errorAt = __LINE__; \
180 qWarning("level: %d, middle: %d, role: %d (%s)", treelevel, middle, iface->role(), #cond); \
184 static int verifyHierarchy(QAccessibleInterface *iface)
187 static int treelevel = 0; // for error diagnostics
188 QAccessibleInterface *middleChild, *if2;
191 int middle = iface->childCount()/2 + 1;
192 if (iface->childCount() >= 2) {
193 middleChild = iface->child(middle - 1);
195 for (int i = 0; i < iface->childCount() && !errorAt; ++i) {
196 if2 = iface->child(i);
198 // navigate Ancestor...
199 QAccessibleInterface *parent = if2->parent();
200 EXPECT(iface->object() == parent->object());
203 // navigate Sibling...
204 // if (middleChild) {
205 // entry = if2->navigate(QAccessible::Sibling, middle, &if3);
206 // EXPECT(entry == 0 && if3->object() == middleChild->object());
209 // EXPECT(iface->indexOfChild(middleChild) == middle);
212 // verify children...
214 errorAt = verifyHierarchy(if2);
223 QRect childRect(QAccessibleInterface *iface, int index = 0)
225 QAccessibleInterface *child = iface->child(index);
226 QRect rect = child->rect();
231 class tst_QAccessibility : public QObject
235 tst_QAccessibility();
236 virtual ~tst_QAccessibility();
240 void cleanupTestCase();
246 void deletedWidget();
248 void statesStructTest();
249 void navigateHierarchy();
251 void textAttributes();
256 void applicationTest();
257 void mainWindowTest();
259 void scrollBarTest();
261 void tabWidgetTest();
264 void doubleSpinBoxTest();
266 void textBrowserTest();
268 void mdiSubWindowTest();
271 void dialogButtonBoxTest();
273 void rubberBandTest();
274 void abstractScrollAreaTest();
275 void scrollAreaTest();
281 void calendarWidgetTest();
282 void dockWidgetTest();
284 void accessibleName();
295 const double Q_PI = 3.14159265358979323846;
297 QAccessible::State state(QWidget * const widget)
299 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
301 qWarning() << "Cannot get QAccessibleInterface for widget";
302 QAccessible::State state = (iface ? iface->state() : QAccessible::State());
307 class QtTestAccessibleWidget: public QWidget
311 QtTestAccessibleWidget(QWidget *parent, const char *name): QWidget(parent)
315 pal.setColor(backgroundRole(), Qt::black);//black is beautiful
321 class QtTestAccessibleWidgetIface: public QAccessibleWidget
324 QtTestAccessibleWidgetIface(QtTestAccessibleWidget *w): QAccessibleWidget(w) {}
325 QString text(QAccessible::Text t) const
327 if (t == QAccessible::Help)
328 return QString::fromLatin1("Help yourself");
329 return QAccessibleWidget::text(t);
331 static QAccessibleInterface *ifaceFactory(const QString &key, QObject *o)
333 if (key == "QtTestAccessibleWidget")
334 return new QtTestAccessibleWidgetIface(static_cast<QtTestAccessibleWidget*>(o));
339 tst_QAccessibility::tst_QAccessibility()
344 tst_QAccessibility::~tst_QAccessibility()
348 void tst_QAccessibility::onClicked()
353 void tst_QAccessibility::initTestCase()
355 QTestAccessibility::initialize();
356 QAccessible::installFactory(QtTestAccessibleWidgetIface::ifaceFactory);
359 void tst_QAccessibility::cleanupTestCase()
361 QTestAccessibility::cleanup();
364 void tst_QAccessibility::init()
366 QTestAccessibility::clearEvents();
369 void tst_QAccessibility::cleanup()
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());
379 QTestAccessibility::clearEvents();
382 void tst_QAccessibility::eventTest()
384 QPushButton* button = new QPushButton(0);
385 button->setObjectName(QString("Olaf"));
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);
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);
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));
410 void tst_QAccessibility::customWidget()
412 QtTestAccessibleWidget* widget = new QtTestAccessibleWidget(0, "Heinz");
414 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
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"));
425 void tst_QAccessibility::deletedWidget()
427 QtTestAccessibleWidget *widget = new QtTestAccessibleWidget(0, "Ralf");
428 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
430 QVERIFY(iface->isValid());
431 QCOMPARE(iface->object(), (QObject*)widget);
435 QVERIFY(!iface->isValid());
439 void tst_QAccessibility::statesStructTest()
441 QAccessible::State s1;
442 QVERIFY(s1.disabled == 0);
443 QVERIFY(s1.focusable == 0);
444 QVERIFY(s1.modal == 0);
446 QAccessible::State s2;
449 QVERIFY(!(s2 == s1));
452 s1 = QAccessible::State();
453 QVERIFY(!(s2 == s1));
456 QVERIFY(s1.busy == 1);
459 void tst_QAccessibility::sliderTest()
462 QSlider *slider = new QSlider(0);
463 slider->setObjectName(QString("Slidy"));
465 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(slider);
467 QVERIFY(iface->isValid());
469 QCOMPARE(iface->childCount(), 0);
470 QCOMPARE(iface->role(), QAccessible::Slider);
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());
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());
488 QTestAccessibility::clearEvents();
491 void tst_QAccessibility::navigateHierarchy()
494 QWidget *w = new QWidget(0);
495 w->setObjectName(QString("Hans"));
497 QWidget *w1 = new QWidget(w);
498 w1->setObjectName(QString("1"));
500 QWidget *w2 = new QWidget(w);
501 w2->setObjectName(QString("2"));
503 QWidget *w3 = new QWidget(w);
504 w3->setObjectName(QString("3"));
506 QWidget *w31 = new QWidget(w3);
507 w31->setObjectName(QString("31"));
510 QAccessibleInterface *target = 0;
511 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(w);
513 QVERIFY(iface->isValid());
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);
522 QVERIFY(target->isValid());
523 QCOMPARE(target->object(), (QObject*)w1);
524 QVERIFY(interfaceW1 != 0);
525 QVERIFY(interfaceW1->isValid());
526 QCOMPARE(interfaceW1->object(), (QObject*)w1);
528 delete iface; iface = 0;
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;
538 iface = target->child(1);
539 QCOMPARE(iface, (QAccessibleInterface*)0);
540 iface = target->child(0);
542 QVERIFY(iface->isValid());
543 QCOMPARE(iface->object(), (QObject*)w31);
545 iface = QAccessible::queryAccessibleInterface(w);
546 QAccessibleInterface *acc3 = iface->child(2);
547 target = acc3->child(0);
550 QCOMPARE(target->object(), (QObject*)w31);
552 iface = target->parent();
554 QVERIFY(iface->isValid());
555 QCOMPARE(iface->object(), (QObject*)w3);
556 delete iface; iface = 0;
557 delete target; target = 0;
561 QTestAccessibility::clearEvents();
564 #define QSETCOMPARE(thetypename, elements, otherelements) \
565 QCOMPARE((QSet<thetypename>() << elements), (QSet<thetypename>() << otherelements))
567 static QWidget *createWidgets()
569 QWidget *w = new QWidget();
571 QHBoxLayout *box = new QHBoxLayout(w);
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));
606 * QAbstractItemView, QGraphicsView, QScrollArea,
607 * QToolButton, QDockWidget, QFocusFrame, QMainWindow, QMenu, QMenuBar, QSizeGrip, QSplashScreen, QSplitterHandle,
608 * QStatusBar, QSvgWidget, QTabBar, QToolBar, QSplitter
613 void tst_QAccessibility::accessibleName()
615 QWidget *toplevel = createWidgets();
617 #if defined(Q_OS_UNIX)
618 QCoreApplication::processEvents();
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();
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);
631 QString desc = tr("Widget Description %1").arg(i);
632 child->setAccessibleDescription(desc);
633 QCOMPARE(acc->text(QAccessible::Description), desc);
638 QTestAccessibility::clearEvents();
641 void tst_QAccessibility::textAttributes()
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>."
654 textEdit.setText(text);
655 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&textEdit);
657 QAccessibleTextInterface *textInterface=interface->textInterface();
659 QVERIFY(textInterface);
660 QCOMPARE(textInterface->characterCount(), 112);
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;")));
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;")));
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\";")));
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;")));
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);")));
695 void tst_QAccessibility::hideShowTest()
697 QWidget * const window = new QWidget();
698 QWidget * const child = new QWidget(window);
700 QVERIFY(state(window).invisible);
701 QVERIFY(state(child).invisible);
703 QTestAccessibility::clearEvents();
705 // show() and veryfy that both window and child are not invisible and get ObjectShow events.
707 QVERIFY(!state(window).invisible);
708 QVERIFY(!state(child).invisible);
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();
716 // hide() and veryfy that both window and child are invisible and get ObjectHide events.
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();
727 QTestAccessibility::clearEvents();
731 void tst_QAccessibility::actionTest()
734 QCOMPARE(QAccessibleActionInterface::pressAction(), QString(QStringLiteral("Press")));
736 QWidget *widget = new QWidget;
737 widget->setFocusPolicy(Qt::NoFocus);
740 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(widget);
742 QVERIFY(interface->isValid());
743 QAccessibleActionInterface *actions = interface->actionInterface();
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()));
754 QTestAccessibility::clearEvents();
757 QPushButton *button = new QPushButton;
759 QVERIFY(QTest::qWaitForWindowExposed(button));
760 button->clearFocus();
761 QCOMPARE(button->hasFocus(), false);
762 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(button);
763 QAccessibleActionInterface *actions = interface->actionInterface();
766 // Make sure the "primary action" press comes first!
767 QCOMPARE(actions->actionNames(), QStringList() << QAccessibleActionInterface::pressAction() << QAccessibleActionInterface::setFocusAction());
769 actions->doAction(QAccessibleActionInterface::setFocusAction());
771 QCOMPARE(button->hasFocus(), true);
773 connect(button, SIGNAL(clicked()), this, SLOT(onClicked()));
774 QCOMPARE(click_count, 0);
775 actions->doAction(QAccessibleActionInterface::pressAction());
777 QCOMPARE(click_count, 1);
782 QTestAccessibility::clearEvents();
785 void tst_QAccessibility::applicationTest()
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);
795 void tst_QAccessibility::mainWindowTest()
798 QMainWindow *mw = new QMainWindow;
799 mw->resize(300, 200);
800 mw->show(); // triggers layout
801 qApp->setActiveWindow(mw);
803 QLatin1String name = QLatin1String("I am the main window");
804 mw->setWindowTitle(name);
805 QVERIFY(QTest::qWaitForWindowActive(mw));
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));
815 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(mw);
816 QCOMPARE(iface->text(QAccessible::Name), name);
817 QCOMPARE(iface->role(), QAccessible::Window);
818 QVERIFY(iface->state().active);
824 QTestAccessibility::clearEvents();
828 window.setGeometry(80, 80, 40, 40);
830 QTRY_VERIFY(QGuiApplication::focusWindow() == &window);
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);
837 QAccessible::State activeState;
838 activeState.active = true;
839 QAccessibleStateChangeEvent active(&window, activeState);
840 QVERIFY_EVENT(&active);
843 child.setParent(&window);
844 child.setGeometry(10, 10, 20, 20);
847 child.requestActivateWindow();
848 QTRY_VERIFY(QGuiApplication::focusWindow() == &child);
850 QAccessibleStateChangeEvent deactivate(&window, activeState);
851 QVERIFY_EVENT(&deactivate); // deactivation of parent
853 QAccessibleStateChangeEvent activeChild(&child, activeState);
854 QVERIFY_EVENT(&activeChild);
858 class CounterButton : public QPushButton {
861 CounterButton(const QString& name, QWidget* parent)
862 : QPushButton(name, parent), clickCount(0)
864 connect(this, SIGNAL(clicked(bool)), SLOT(incClickCount()));
868 void incClickCount() {
873 static QAccessibleInterface *relatedInterface(QAccessibleInterface *iface, QAccessible::RelationFlag flag)
875 typedef QPair<QAccessibleInterface *, QAccessible::Relation> RelationPair;
876 QVector<RelationPair> rels = iface->relations(flag);
878 for (int i = 1; i < rels.count(); ++i)
879 delete rels.at(i).first;
881 return rels.value(0).first;
884 void tst_QAccessibility::buttonTest()
887 window.setLayout(new QVBoxLayout);
889 // Standard push button
890 CounterButton pushButton("Ok", &window);
893 QPushButton toggleButton("Toggle", &window);
894 toggleButton.setCheckable(true);
897 QCheckBox checkBox("Check me!", &window);
900 QCheckBox tristate("Tristate!", &window);
901 tristate.setTristate(true);
904 QRadioButton radio("Radio me!", &window);
906 // standard toolbutton
907 QToolButton toolbutton(&window);
908 toolbutton.setText("Tool");
909 toolbutton.setMinimumSize(20,20);
911 // standard toolbutton
912 QToolButton toggletool(&window);
913 toggletool.setCheckable(true);
914 toggletool.setText("Toggle");
915 toggletool.setMinimumSize(20,20);
918 QAccessibleInterface* interface = QAccessible::queryAccessibleInterface(&pushButton);
919 QAccessibleActionInterface* actionInterface = interface->actionInterface();
920 QVERIFY(actionInterface != 0);
921 QCOMPARE(interface->role(), QAccessible::PushButton);
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());
929 QCOMPARE(pushButton.clickCount, 1);
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());
942 QVERIFY(toggleButton.isChecked());
943 QCOMPARE(actionInterface->actionNames().at(0), QAccessibleActionInterface::toggleAction());
944 QVERIFY(interface->state().checked);
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);
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);
968 QTestAccessibility::clearEvents();
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());
979 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::toggleAction() << QAccessibleActionInterface::setFocusAction());
980 QVERIFY(interface->state().checked);
981 QVERIFY(checkBox.isChecked());
982 QAccessible::State st;
984 QAccessibleStateChangeEvent ev(&checkBox, st);
986 checkBox.setChecked(false);
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());
1000 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::toggleAction() << QAccessibleActionInterface::setFocusAction());
1001 QVERIFY(interface->state().checked);
1002 QVERIFY(radio.isChecked());
1003 QAccessible::State st;
1005 QAccessibleStateChangeEvent ev(&radio, st);
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);
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);
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"));
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);
1060 void tst_QAccessibility::scrollBarTest()
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);
1068 QVERIFY(!scrollBarInterface->state().invisible);
1069 QAccessibleEvent show(scrollBar, QAccessible::ObjectShow);
1070 QVERIFY(QTestAccessibility::containsEvent(&show));
1071 QTestAccessibility::clearEvents();
1074 QVERIFY(scrollBarInterface->state().invisible);
1075 QAccessibleEvent hide(scrollBar, QAccessible::ObjectHide);
1076 QVERIFY(QTestAccessibility::containsEvent(&hide));
1077 QTestAccessibility::clearEvents();
1079 // Test that the left/right subcontrols are set to unavailable when the scrollBar is at the minimum/maximum.
1081 scrollBar->setMinimum(11);
1082 scrollBar->setMaximum(111);
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());
1097 const QRect scrollBarRect = scrollBarInterface->rect();
1098 QVERIFY(scrollBarRect.isValid());
1100 delete scrollBarInterface;
1103 QTestAccessibility::clearEvents();
1106 void tst_QAccessibility::tabTest()
1108 QTabBar *tabBar = new QTabBar();
1111 QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(tabBar);
1113 QCOMPARE(interface->childCount(), 2);
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);
1123 for (int i = 0; i < lots; ++i)
1124 tabBar->addTab("Foo");
1126 QAccessibleInterface *child1 = interface->child(0);
1127 QAccessibleInterface *child2 = interface->child(1);
1129 QCOMPARE(child1->role(), QAccessible::PageTab);
1131 QCOMPARE(child2->role(), QAccessible::PageTab);
1133 QVERIFY((child1->state().invisible) == false);
1136 QCoreApplication::processEvents();
1139 QVERIFY(child1->state().invisible);
1142 tabBar->setCurrentIndex(0);
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);
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);
1159 QTestAccessibility::clearEvents();
1162 void tst_QAccessibility::tabWidgetTest()
1164 QTabWidget *tabWidget = new QTabWidget();
1167 // the interface for the tab is just a container for tabbar and stacked widget
1168 QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(tabWidget);
1170 QCOMPARE(interface->childCount(), 2);
1171 QCOMPARE(interface->role(), QAccessible::Client);
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");
1179 QCOMPARE(interface->childCount(), 2);
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);
1188 QAccessibleInterface* tabButton1Interface = tabBarInterface->child(0);
1189 QVERIFY(tabButton1Interface);
1190 QCOMPARE(tabButton1Interface->role(), QAccessible::PageTab);
1191 QCOMPARE(tabButton1Interface->text(QAccessible::Name), QLatin1String("Tab 1"));
1193 QAccessibleInterface* tabButton2Interface = tabBarInterface->child(1);
1194 QVERIFY(tabButton1Interface);
1195 QCOMPARE(tabButton2Interface->role(), QAccessible::PageTab);
1196 QCOMPARE(tabButton2Interface->text(QAccessible::Name), QLatin1String("Tab 2"));
1198 QAccessibleInterface* tabButtonLeft = tabBarInterface->child(2);
1199 QVERIFY(tabButtonLeft);
1200 QCOMPARE(tabButtonLeft->role(), QAccessible::PushButton);
1201 QCOMPARE(tabButtonLeft->text(QAccessible::Name), QLatin1String("Scroll Left"));
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;
1212 QAccessibleInterface* stackWidgetInterface = interface->child(0);
1213 QVERIFY(stackWidgetInterface);
1214 QCOMPARE(stackWidgetInterface->childCount(), 2);
1215 QCOMPARE(stackWidgetInterface->role(), QAccessible::LayeredPane);
1217 QAccessibleInterface* stackChild1Interface = stackWidgetInterface->child(0);
1218 QVERIFY(stackChild1Interface);
1220 QCOMPARE(stackChild1Interface->childCount(), 0);
1222 QCOMPARE(stackChild1Interface->role(), QAccessible::StaticText);
1223 QCOMPARE(stackChild1Interface->text(QAccessible::Name), QLatin1String("Page 1"));
1224 QCOMPARE(label1, stackChild1Interface->object());
1226 // Navigation in stack widgets should be consistent
1227 QAccessibleInterface* parent = stackChild1Interface->parent();
1230 QCOMPARE(parent->childCount(), 2);
1232 QCOMPARE(parent->role(), QAccessible::LayeredPane);
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));
1242 parent = stackChild2Interface->parent();
1245 QCOMPARE(parent->childCount(), 2);
1247 QCOMPARE(parent->role(), QAccessible::LayeredPane);
1250 delete tabBarInterface;
1251 delete stackChild1Interface;
1252 delete stackChild2Interface;
1253 delete stackWidgetInterface;
1256 QTestAccessibility::clearEvents();
1259 void tst_QAccessibility::menuTest()
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"));
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");
1285 mw.menuBar()->addSeparator();
1287 QMenu *help = mw.menuBar()->addMenu("&Help");
1288 help->addAction("&Contents");
1289 help->addAction("&About");
1291 mw.menuBar()->addAction("Action!");
1293 mw.show(); // triggers layout
1296 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(mw.menuBar());
1297 QCOMPARE(verifyHierarchy(interface), 0);
1300 QCOMPARE(interface->childCount(), 5);
1301 QCOMPARE(interface->role(), QAccessible::MenuBar);
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);
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);
1316 if (!IsValidCEPlatform())
1317 QSKIP("Tests do not work on Mobile platforms due to native menus");
1319 QCOMPARE(mw.mapFromGlobal(interface->rect().topLeft()), mw.menuBar()->geometry().topLeft());
1320 QCOMPARE(interface->rect().size(), mw.menuBar()->size());
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()));
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!"));
1335 // TODO: Currently not working, task to fix is #100019.
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());
1344 QVERIFY(iFile->actionInterface());
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());
1351 bool menuFade = qApp->isEffectEnabled(Qt::UI_FadeMenu);
1352 int menuFadeDelay = 300;
1353 iFile->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1355 QTest::qWait(menuFadeDelay);
1356 QVERIFY(file->isVisible() && !edit->isVisible() && !help->isVisible());
1357 iEdit->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1359 QTest::qWait(menuFadeDelay);
1360 QVERIFY(!file->isVisible() && edit->isVisible() && !help->isVisible());
1361 iHelp->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1363 QTest::qWait(menuFadeDelay);
1364 QVERIFY(!file->isVisible() && !edit->isVisible() && help->isVisible());
1365 iAction->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1367 QTest::qWait(menuFadeDelay);
1368 QVERIFY(!file->isVisible() && !edit->isVisible() && !help->isVisible());
1370 QVERIFY(interface->actionInterface());
1371 QCOMPARE(interface->actionInterface()->actionNames(), QStringList());
1373 interface = QAccessible::queryAccessibleInterface(file);
1374 QCOMPARE(interface->childCount(), 5);
1375 QCOMPARE(interface->role(), QAccessible::PopupMenu);
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);
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());
1394 QAccessibleInterface *iface = 0;
1395 QAccessibleInterface *iface2 = 0;
1397 // traverse siblings with navigate(Sibling, ...)
1398 iface = interface->child(0);
1400 QCOMPARE(iface->role(), QAccessible::MenuItem);
1402 QAccessible::Role fileRoles[5] = {
1403 QAccessible::MenuItem,
1404 QAccessible::MenuItem,
1405 QAccessible::MenuItem,
1406 QAccessible::Separator,
1407 QAccessible::MenuItem
1409 for (int child = 0; child < 5; ++child) {
1410 iface2 = interface->child(child);
1412 QCOMPARE(iface2->role(), fileRoles[child]);
1418 iface = interface->child(0);
1420 QCOMPARE(iface->role(), QAccessible::MenuItem);
1423 iface2 = iface->child(0);
1427 QCOMPARE(iface->role(), QAccessible::PopupMenu);
1429 // "Text file" menu item
1430 iface2 = iface->child(0);
1434 QCOMPARE(iface->role(), QAccessible::MenuItem);
1438 // move mouse pointer away, since that might influence the
1440 QTest::mouseMove(&mw, QPoint(-1, -1));
1443 QTest::qWait(menuFadeDelay);
1445 iFile->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1446 iFileNew->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1448 QVERIFY(file->isVisible());
1449 QVERIFY(fileNew->isVisible());
1450 QVERIFY(!edit->isVisible());
1451 QVERIFY(!help->isVisible());
1453 QTestAccessibility::clearEvents();
1460 delete iFileSeparator;
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();
1471 QCOMPARE(iface2->role(), QAccessible::Application);
1479 QTestAccessibility::clearEvents();
1482 void tst_QAccessibility::spinBoxTest()
1484 QSpinBox * const spinBox = new QSpinBox();
1485 spinBox->setValue(3);
1488 QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(spinBox);
1490 QCOMPARE(interface->role(), QAccessible::SpinBox);
1492 const QRect widgetRect = spinBox->geometry();
1493 const QRect accessibleRect = interface->rect();
1494 QCOMPARE(accessibleRect, widgetRect);
1495 QCOMPARE(interface->text(QAccessible::Value), QLatin1String("3"));
1497 // one child, the line edit
1498 const int numChildren = interface->childCount();
1499 QCOMPARE(numChildren, 1);
1500 QAccessibleInterface *lineEdit = interface->child(0);
1502 QCOMPARE(lineEdit->role(), QAccessible::EditableText);
1503 QCOMPARE(lineEdit->text(QAccessible::Value), QLatin1String("3"));
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);
1512 spinBox->setFocus();
1513 QTestAccessibility::clearEvents();
1514 QTest::keyPress(spinBox, Qt::Key_Up);
1516 QAccessibleValueChangeEvent expectedEvent(spinBox, spinBox->value());
1517 QVERIFY(QTestAccessibility::containsEvent(&expectedEvent));
1519 QTestAccessibility::clearEvents();
1522 void tst_QAccessibility::doubleSpinBoxTest()
1524 QDoubleSpinBox *doubleSpinBox = new QDoubleSpinBox;
1525 doubleSpinBox->show();
1527 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(doubleSpinBox);
1530 const QRect widgetRect = doubleSpinBox->geometry();
1531 const QRect accessibleRect = interface->rect();
1532 QCOMPARE(accessibleRect, widgetRect);
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());
1543 delete doubleSpinBox;
1544 QTestAccessibility::clearEvents();
1547 void tst_QAccessibility::textEditTest()
1553 QString text = "hello world\nhow are you today?\n";
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()));
1571 QTestAccessibility::clearEvents();
1574 QTextCursor c = edit.textCursor();
1576 c.setPosition(4, QTextCursor::KeepAnchor);
1577 edit.setTextCursor(c);
1578 QAccessibleTextSelectionEvent sel(&edit, 2, 4);
1579 QVERIFY_EVENT(&sel);
1582 int end = edit.textCursor().position();
1583 sel.setCursorPosition(end);
1584 sel.setSelection(0, end);
1585 QVERIFY_EVENT(&sel);
1587 QTestAccessibility::clearEvents();
1590 void tst_QAccessibility::textBrowserTest()
1593 QTextBrowser textBrowser;
1594 QString text = QLatin1String("Hello world\nhow are you today?\n");
1595 textBrowser.setText(text);
1598 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&textBrowser);
1600 QCOMPARE(iface->role(), QAccessible::StaticText);
1601 QCOMPARE(iface->text(QAccessible::Value), text);
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);
1612 QTestAccessibility::clearEvents();
1615 void tst_QAccessibility::mdiAreaTest()
1619 mdiArea.resize(400,300);
1621 const int subWindowCount = 3;
1622 for (int i = 0; i < subWindowCount; ++i)
1623 mdiArea.addSubWindow(new QWidget, Qt::Dialog)->show();
1625 QList<QMdiSubWindow *> subWindows = mdiArea.subWindowList();
1626 QCOMPARE(subWindows.count(), subWindowCount);
1628 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&mdiArea);
1630 QCOMPARE(interface->childCount(), subWindowCount);
1633 QTestAccessibility::clearEvents();
1636 void tst_QAccessibility::mdiSubWindowTest()
1641 qApp->setActiveWindow(&mdiArea);
1642 QVERIFY(QTest::qWaitForWindowActive(&mdiArea));
1645 const int subWindowCount = 5;
1646 for (int i = 0; i < subWindowCount; ++i) {
1647 QMdiSubWindow *window = mdiArea.addSubWindow(new QPushButton("QAccessibilityTest"));
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.
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();
1662 QList<QMdiSubWindow *> subWindows = mdiArea.subWindowList();
1663 QCOMPARE(subWindows.count(), subWindowCount);
1665 QMdiSubWindow *testWindow = subWindows.at(3);
1666 QVERIFY(testWindow);
1667 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(testWindow);
1671 QCOMPARE(interface->childCount(), 1);
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"));
1680 mdiArea.setActiveSubWindow(testWindow);
1683 QAccessible::State state;
1684 state.focusable = true;
1685 state.focused = true;
1686 state.movable = true;
1687 state.sizeable = true;
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);
1712 const QPoint globalPos = testWindow->mapToGlobal(QPoint(0, 0));
1713 QCOMPARE(interface->rect(), QRect(globalPos, testWindow->size()));
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());
1727 QEXPECT_FAIL("", "QTBUG-22812", Abort);
1729 QCOMPARE(childRect(interface), QRect(globalWidgetPos, widgetGeometry.size()));
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"));
1738 testWindow->widget()->hide();
1739 QCOMPARE(interface->childAt(globalWidgetPos.x(), globalWidgetPos.y()), static_cast<QAccessibleInterface*>(0));
1742 QTestAccessibility::clearEvents();
1745 void tst_QAccessibility::lineEditTest()
1747 QWidget *toplevel = new QWidget;
1749 QLineEdit *le = new QLineEdit;
1750 QAIPtr iface(QAccessible::queryAccessibleInterface(le));
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());
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);
1781 le->setParent(toplevel);
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());
1791 QLineEdit *le2 = new QLineEdit(toplevel);
1794 le2->activateWindow();
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));
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"));
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);
1816 QAIPtr iface(QAccessible::queryAccessibleInterface(le3));
1817 QAccessibleTextInterface* textIface = iface->textInterface();
1819 QTestAccessibility::clearEvents();
1820 le3->setCursorPosition(3);
1821 QCOMPARE(textIface->cursorPosition(), 3);
1823 QAccessibleTextCursorEvent caretEvent(le3, 3);
1824 QTRY_VERIFY(QTestAccessibility::containsEvent(&caretEvent));
1825 QCOMPARE(textIface->selectionCount(), 0);
1826 QTestAccessibility::clearEvents();
1829 QCOMPARE(textIface->text(0, 8), QString::fromLatin1("I always"));
1830 QCOMPARE(textIface->textAtOffset(0, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("I"));
1833 QCOMPARE(textIface->textBeforeOffset(0, QAccessible2::CharBoundary,&start,&end), QString());
1834 QCOMPARE(textIface->textAfterOffset(0, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1(" "));
1838 QCOMPARE(textIface->textAtOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("a"));
1841 QCOMPARE(textIface->textBeforeOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("w"));
1842 QCOMPARE(textIface->textAfterOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("y"));
1844 QCOMPARE(textIface->textAtOffset(5, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("always"));
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"));
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. "));
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. "));
1863 QCOMPARE(textIface->textAtOffset(5, QAccessible2::ParagraphBoundary,&start,&end), cite);
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);
1869 QTestAccessibility::clearEvents();
1873 // Test events: cursor movement, selection, text changes
1874 QString text = "Hello, world";
1875 QLineEdit *lineEdit = new QLineEdit(text, toplevel);
1877 QTestAccessibility::clearEvents();
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);
1887 lineEdit->setSelection(2, 4);
1889 QAccessibleTextSelectionEvent sel(lineEdit, 2, 2+4);
1890 QVERIFY_EVENT(&sel);
1892 lineEdit->selectAll();
1893 sel.setSelection(0, lineEdit->text().length());
1894 sel.setCursorPosition(lineEdit->text().length());
1895 QVERIFY_EVENT(&sel);
1897 lineEdit->setSelection(10, -4);
1898 QCOMPARE(lineEdit->cursorPosition(), 6);
1899 QAccessibleTextSelectionEvent sel2(lineEdit, 6, 10);
1900 sel2.setCursorPosition(6);
1901 QVERIFY_EVENT(&sel2);
1903 lineEdit->deselect();
1904 QAccessibleTextSelectionEvent sel3(lineEdit, -1, -1);
1905 sel3.setCursorPosition(6);
1906 QVERIFY_EVENT(&sel3);
1910 // FIXME: improve redundant updates
1911 QAccessibleTextRemoveEvent remove(lineEdit, 0, text);
1912 QVERIFY_EVENT(&remove);
1914 QAccessibleTextSelectionEvent noSel(lineEdit, -1, -1);
1915 QVERIFY_EVENT(&noSel);
1916 QAccessibleTextCursorEvent cursor(lineEdit, 0);
1917 QVERIFY_EVENT(&cursor);
1919 lineEdit->setText("foo");
1920 cursorEvent.setCursorPosition(3);
1921 QVERIFY_EVENT(&cursorEvent);
1923 QAccessibleTextInsertEvent e(lineEdit, 0, "foo");
1924 QVERIFY(QTestAccessibility::containsEvent(&e));
1926 lineEdit->setText("bar");
1927 QAccessibleTextUpdateEvent update(lineEdit, 0, "foo", "bar");
1928 QVERIFY(QTestAccessibility::containsEvent(&update));
1930 // FIXME check what extra events are around and get rid of them
1931 QTestAccessibility::clearEvents();
1933 QTestEventList keys;
1934 keys.addKeyClick('D');
1935 keys.simulate(lineEdit);
1937 QAccessibleTextInsertEvent insertD(lineEdit, 3, "D");
1938 QVERIFY_EVENT(&insertD);
1940 keys.addKeyClick('E');
1941 keys.simulate(lineEdit);
1943 QAccessibleTextInsertEvent insertE(lineEdit, 4, "E");
1944 QVERIFY(QTestAccessibility::containsEvent(&insertE));
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));
1955 keys.addKeyClick('C');
1956 keys.simulate(lineEdit);
1958 QAccessibleTextInsertEvent insertC(lineEdit, 3, "C");
1959 QVERIFY(QTestAccessibility::containsEvent(&insertC));
1962 keys.addKeyClick('O');
1963 keys.simulate(lineEdit);
1964 QAccessibleTextInsertEvent insertO(lineEdit, 4, "O");
1965 QVERIFY(QTestAccessibility::containsEvent(&insertO));
1968 QTestAccessibility::clearEvents();
1971 void tst_QAccessibility::groupBoxTest()
1974 QGroupBox *groupBox = new QGroupBox();
1975 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(groupBox);
1977 groupBox->setTitle(QLatin1String("Test QGroupBox"));
1979 QAccessibleEvent ev(groupBox, QAccessible::NameChanged);
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);
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);
1999 delete relation.first;
2001 delete rButtonIface;
2007 QGroupBox *groupBox = new QGroupBox();
2008 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(groupBox);
2009 QVERIFY(!iface->state().checkable);
2010 groupBox->setCheckable(true);
2012 groupBox->setChecked(false);
2013 QAccessible::State st;
2015 QAccessibleStateChangeEvent ev(groupBox, st);
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);
2037 bool accessibleInterfaceLeftOf(const QAccessibleInterface *a1, const QAccessibleInterface *a2)
2039 return a1->rect().x() < a2->rect().x();
2042 bool accessibleInterfaceAbove(const QAccessibleInterface *a1, const QAccessibleInterface *a2)
2044 return a1->rect().y() < a2->rect().y();
2047 void tst_QAccessibility::dialogButtonBoxTest()
2050 QDialogButtonBox box(QDialogButtonBox::Reset |
2051 QDialogButtonBox::Help |
2052 QDialogButtonBox::Ok, Qt::Horizontal);
2055 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&box);
2058 #if defined(Q_OS_UNIX)
2059 QCoreApplication::processEvents();
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);
2071 QVector<QAccessibleInterface *> buttons;
2072 for (int i = 0; i < iface->childCount(); ++i)
2073 buttons << iface->child(i);
2075 qSort(buttons.begin(), buttons.end(), accessibleInterfaceLeftOf);
2077 for (int i = 0; i < buttons.count(); ++i)
2078 actualOrder << buttons.at(i)->text(QAccessible::Name);
2080 QStringList expectedOrder;
2081 QDialogButtonBox::ButtonLayout btnlout =
2082 QDialogButtonBox::ButtonLayout(QApplication::style()->styleHint(QStyle::SH_DialogButtonLayout));
2084 case QDialogButtonBox::WinLayout:
2085 expectedOrder << QDialogButtonBox::tr("Reset")
2086 << QDialogButtonBox::tr("OK")
2087 << QDialogButtonBox::tr("Help");
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");
2097 QCOMPARE(actualOrder, expectedOrder);
2099 QApplication::processEvents();
2100 QTestAccessibility::clearEvents();
2104 QDialogButtonBox box(QDialogButtonBox::Reset |
2105 QDialogButtonBox::Help |
2106 QDialogButtonBox::Ok, Qt::Horizontal);
2109 // Test up and down navigation
2110 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&box);
2112 box.setOrientation(Qt::Vertical);
2114 #if defined(Q_OS_UNIX)
2115 QCoreApplication::processEvents();
2119 QApplication::processEvents();
2120 QStringList actualOrder;
2122 QVector<QAccessibleInterface *> buttons;
2123 for (int i = 0; i < iface->childCount(); ++i)
2124 buttons << iface->child(i);
2126 qSort(buttons.begin(), buttons.end(), accessibleInterfaceAbove);
2128 for (int i = 0; i < buttons.count(); ++i)
2129 actualOrder << buttons.at(i)->text(QAccessible::Name);
2131 QStringList expectedOrder;
2132 expectedOrder << QDialogButtonBox::tr("OK")
2133 << QDialogButtonBox::tr("Reset")
2134 << QDialogButtonBox::tr("Help");
2136 QCOMPARE(actualOrder, expectedOrder);
2138 QApplication::processEvents();
2141 QTestAccessibility::clearEvents();
2144 void tst_QAccessibility::dialTest()
2148 dial.setMinimum(23);
2149 dial.setMaximum(121);
2151 QCOMPARE(dial.value(), 42);
2154 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&dial);
2156 QCOMPARE(interface->childCount(), 0);
2158 QCOMPARE(interface->text(QAccessible::Value), QString::number(dial.value()));
2159 QCOMPARE(interface->rect(), dial.geometry());
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);
2167 QCOMPARE(valueIface->currentValue().toInt(), dial.value());
2169 QCOMPARE(valueIface->currentValue().toInt(), dial.value());
2171 QCOMPARE(valueIface->currentValue().toInt(), dial.value());
2172 valueIface->setCurrentValue(77);
2173 QCOMPARE(77, dial.value());
2175 QTestAccessibility::clearEvents();
2178 void tst_QAccessibility::rubberBandTest()
2180 QRubberBand rubberBand(QRubberBand::Rectangle);
2181 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&rubberBand);
2183 QCOMPARE(interface->role(), QAccessible::Border);
2185 QTestAccessibility::clearEvents();
2188 void tst_QAccessibility::abstractScrollAreaTest()
2191 QAbstractScrollArea abstractScrollArea;
2193 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&abstractScrollArea);
2195 QVERIFY(!interface->rect().isValid());
2196 QCOMPARE(interface->childAt(200, 200), static_cast<QAccessibleInterface*>(0));
2198 abstractScrollArea.resize(400, 400);
2199 abstractScrollArea.show();
2200 #if defined(Q_OS_UNIX)
2201 QCoreApplication::processEvents();
2204 const QRect globalGeometry = QRect(abstractScrollArea.mapToGlobal(QPoint(0, 0)),
2205 abstractScrollArea.size());
2208 QCOMPARE(interface->childCount(), 1);
2209 QWidget *viewport = abstractScrollArea.viewport();
2211 QVERIFY(verifyChild(viewport, interface, 0, globalGeometry));
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));
2220 // Horizontal scrollBar widgets.
2221 QLabel *secondLeftLabel = new QLabel(QLatin1String("L2"));
2222 abstractScrollArea.addScrollBarWidget(secondLeftLabel, Qt::AlignLeft);
2223 QCOMPARE(interface->childCount(), 2);
2225 QLabel *firstLeftLabel = new QLabel(QLatin1String("L1"));
2226 abstractScrollArea.addScrollBarWidget(firstLeftLabel, Qt::AlignLeft);
2227 QCOMPARE(interface->childCount(), 2);
2229 QLabel *secondRightLabel = new QLabel(QLatin1String("R2"));
2230 abstractScrollArea.addScrollBarWidget(secondRightLabel, Qt::AlignRight);
2231 QCOMPARE(interface->childCount(), 2);
2233 QLabel *firstRightLabel = new QLabel(QLatin1String("R1"));
2234 abstractScrollArea.addScrollBarWidget(firstRightLabel, Qt::AlignRight);
2235 QCOMPARE(interface->childCount(), 2);
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));
2244 // Vertical scrollBar widgets.
2245 QLabel *secondTopLabel = new QLabel(QLatin1String("T2"));
2246 abstractScrollArea.addScrollBarWidget(secondTopLabel, Qt::AlignTop);
2247 QCOMPARE(interface->childCount(), 3);
2249 QLabel *firstTopLabel = new QLabel(QLatin1String("T1"));
2250 abstractScrollArea.addScrollBarWidget(firstTopLabel, Qt::AlignTop);
2251 QCOMPARE(interface->childCount(), 3);
2253 QLabel *secondBottomLabel = new QLabel(QLatin1String("B2"));
2254 abstractScrollArea.addScrollBarWidget(secondBottomLabel, Qt::AlignBottom);
2255 QCOMPARE(interface->childCount(), 3);
2257 QLabel *firstBottomLabel = new QLabel(QLatin1String("B1"));
2258 abstractScrollArea.addScrollBarWidget(firstBottomLabel, Qt::AlignBottom);
2259 QCOMPARE(interface->childCount(), 3);
2262 abstractScrollArea.setCornerWidget(new QLabel(QLatin1String("C")));
2263 QCOMPARE(interface->childCount(), 4);
2264 QWidget *cornerWidget = abstractScrollArea.cornerWidget();
2265 QVERIFY(verifyChild(cornerWidget, interface, 3, globalGeometry));
2267 QCOMPARE(verifyHierarchy(interface), 0);
2272 QTestAccessibility::clearEvents();
2275 void tst_QAccessibility::scrollAreaTest()
2278 QScrollArea scrollArea;
2280 #if defined(Q_OS_UNIX)
2281 QCoreApplication::processEvents();
2284 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&scrollArea);
2286 QCOMPARE(interface->childCount(), 1); // The viewport.
2289 QTestAccessibility::clearEvents();
2292 void tst_QAccessibility::listTest()
2295 QListWidget *listView = new QListWidget;
2296 listView->addItem("Oslo");
2297 listView->addItem("Berlin");
2298 listView->addItem("Brisbane");
2299 listView->resize(400,400);
2301 QTest::qWait(1); // Need this for indexOfchild to work.
2302 QCoreApplication::processEvents();
2305 QAIPtr iface = QAIPtr(QAccessible::queryAccessibleInterface(listView));
2306 QCOMPARE(verifyHierarchy(iface.data()), 0);
2308 QCOMPARE((int)iface->role(), (int)QAccessible::List);
2309 QCOMPARE(iface->childCount(), 3);
2312 QAIPtr child1 = QAIPtr(iface->child(0));
2314 QCOMPARE(iface->indexOfChild(child1.data()), 0);
2315 QCOMPARE(child1->text(QAccessible::Name), QString("Oslo"));
2316 QCOMPARE(child1->role(), QAccessible::ListItem);
2318 QAIPtr child2 = QAIPtr(iface->child(1));
2320 QCOMPARE(iface->indexOfChild(child2.data()), 1);
2321 QCOMPARE(child2->text(QAccessible::Name), QString("Berlin"));
2323 QAIPtr child3 = QAIPtr(iface->child(2));
2325 QCOMPARE(iface->indexOfChild(child3.data()), 2);
2326 QCOMPARE(child3->text(QAccessible::Name), QString("Brisbane"));
2328 QTestAccessibility::clearEvents();
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());
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));
2347 listView->addItem("Munich");
2348 QCOMPARE(iface->childCount(), 4);
2351 QAccessibleTableInterface *table2 = iface->tableInterface();
2353 QCOMPARE(table2->columnCount(), 1);
2354 QCOMPARE(table2->rowCount(), 4);
2355 QAIPtr cell1 = QAIPtr(table2->cellAt(0,0));
2357 QCOMPARE(cell1->text(QAccessible::Name), QString("Oslo"));
2359 QAIPtr cell4 = QAIPtr(table2->cellAt(3,0));
2361 QCOMPARE(cell4->text(QAccessible::Name), QString("Munich"));
2362 QCOMPARE(cell4->role(), QAccessible::ListItem);
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*>());
2373 QCOMPARE(QAIPtr(cellInterface->table())->object(), listView);
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());
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);
2392 QTestAccessibility::clearEvents();
2395 void tst_QAccessibility::treeTest()
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);
2404 QTreeWidgetItem *root1 = new QTreeWidgetItem;
2405 root1->setText(0, "Spain");
2406 treeView->addTopLevelItem(root1);
2408 QTreeWidgetItem *item1 = new QTreeWidgetItem;
2409 item1->setText(0, "Picasso");
2410 item1->setText(1, "Guernica");
2411 root1->addChild(item1);
2413 QTreeWidgetItem *item2 = new QTreeWidgetItem;
2414 item2->setText(0, "Tapies");
2415 item2->setText(1, "Ambrosia");
2416 root1->addChild(item2);
2418 QTreeWidgetItem *root2 = new QTreeWidgetItem;
2419 root2->setText(0, "Austria");
2420 treeView->addTopLevelItem(root2);
2422 QTreeWidgetItem *item3 = new QTreeWidgetItem;
2423 item3->setText(0, "Klimt");
2424 item3->setText(1, "The Kiss");
2425 root2->addChild(item3);
2427 treeView->resize(400,400);
2430 QCoreApplication::processEvents();
2433 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(treeView);
2434 QCOMPARE(verifyHierarchy(iface), 0);
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);
2440 QAccessibleInterface *header1 = 0;
2441 header1 = iface->child(0);
2443 QCOMPARE(iface->indexOfChild(header1), 0);
2444 QCOMPARE(header1->text(QAccessible::Name), QString("Artist"));
2445 QCOMPARE(header1->role(), QAccessible::ColumnHeader);
2448 QAccessibleInterface *child1 = 0;
2449 child1 = iface->child(2);
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));
2457 QAccessibleInterface *child2 = 0;
2458 child2 = iface->child(4);
2460 QCOMPARE(iface->indexOfChild(child2), 4);
2461 QCOMPARE(child2->text(QAccessible::Name), QString("Austria"));
2464 QTestAccessibility::clearEvents();
2467 QAccessibleTableInterface *table2 = iface->tableInterface();
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"));
2487 treeView->expandAll();
2489 // Need this for indexOfchild to work.
2490 QCoreApplication::processEvents();
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
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);
2507 QCOMPARE(table2->columnDescription(0), QString("Artist"));
2508 QCOMPARE(table2->columnDescription(1), QString("Work"));
2511 QTestAccessibility::clearEvents();
2514 void tst_QAccessibility::tableTest()
2516 QTableWidget *tableView = new QTableWidget(3, 3);
2517 tableView->setColumnCount(3);
2518 QStringList hHeader;
2519 hHeader << "h1" << "h2" << "h3";
2520 tableView->setHorizontalHeaderLabels(hHeader);
2522 QStringList vHeader;
2523 vHeader << "v1" << "v2" << "v3";
2524 tableView->setVerticalHeaderLabels(vHeader);
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);
2532 tableView->resize(600,600);
2534 QTest::qWait(1); // Need this for indexOfchild to work.
2535 QCoreApplication::processEvents();
2538 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(tableView);
2539 QCOMPARE(verifyHierarchy(iface), 0);
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
2545 QAccessibleInterface *cornerButton = iface->child(0);
2546 QVERIFY(cornerButton);
2547 QCOMPARE(iface->indexOfChild(cornerButton), 0);
2548 QCOMPARE(cornerButton->role(), QAccessible::Pane);
2549 delete cornerButton;
2551 QAccessibleInterface *child1 = iface->child(2);
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));
2559 QAccessibleInterface *child2 = iface->child(10);
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);
2568 QAccessibleInterface *child3 = iface->child(11);
2569 QCOMPARE(iface->indexOfChild(child3), 11);
2570 QCOMPARE(child3->text(QAccessible::Name), QString("1.2"));
2573 QTestAccessibility::clearEvents();
2576 QAccessibleTableInterface *table2 = iface->tableInterface();
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);
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);
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);
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"));
2614 QTestAccessibility::clearEvents();
2617 void tst_QAccessibility::calendarWidgetTest()
2619 #ifndef QT_NO_CALENDARWIDGET
2621 QCalendarWidget calendarWidget;
2623 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&calendarWidget);
2625 QCOMPARE(interface->role(), QAccessible::Table);
2626 QVERIFY(!interface->rect().isValid());
2627 QCOMPARE(interface->childAt(200, 200), static_cast<QAccessibleInterface*>(0));
2629 calendarWidget.resize(400, 300);
2630 calendarWidget.show();
2631 #if defined(Q_OS_UNIX)
2632 QCoreApplication::processEvents();
2636 // 1 = navigationBar, 2 = view.
2637 QCOMPARE(interface->childCount(), 2);
2639 const QRect globalGeometry = QRect(calendarWidget.mapToGlobal(QPoint(0, 0)),
2640 calendarWidget.size());
2641 QCOMPARE(interface->rect(), globalGeometry);
2643 QWidget *navigationBar = 0;
2644 foreach (QObject *child, calendarWidget.children()) {
2645 if (child->objectName() == QLatin1String("qt_calendar_navigationbar")) {
2646 navigationBar = static_cast<QWidget *>(child);
2650 QVERIFY(navigationBar);
2651 QVERIFY(verifyChild(navigationBar, interface, 0, globalGeometry));
2653 QAbstractItemView *calendarView = 0;
2654 foreach (QObject *child, calendarWidget.children()) {
2655 if (child->objectName() == QLatin1String("qt_calendar_calendarview")) {
2656 calendarView = static_cast<QAbstractItemView *>(child);
2660 QVERIFY(calendarView);
2661 QVERIFY(verifyChild(calendarView, interface, 1, globalGeometry));
2663 // Hide navigation bar.
2664 calendarWidget.setNavigationBarVisible(false);
2665 QCOMPARE(interface->childCount(), 1);
2666 QVERIFY(!navigationBar->isVisible());
2668 QVERIFY(verifyChild(calendarView, interface, 0, globalGeometry));
2670 // Show navigation bar.
2671 calendarWidget.setNavigationBarVisible(true);
2672 QCOMPARE(interface->childCount(), 2);
2673 QVERIFY(navigationBar->isVisible());
2675 // Navigate to the navigation bar via Child.
2676 QAccessibleInterface *navigationBarInterface = interface->child(0);
2677 QVERIFY(navigationBarInterface);
2678 QCOMPARE(navigationBarInterface->object(), (QObject*)navigationBar);
2680 // Navigate to the view via Child.
2681 QAccessibleInterface *calendarViewInterface = interface->child(1);
2682 QVERIFY(calendarViewInterface);
2683 QCOMPARE(calendarViewInterface->object(), (QObject*)calendarView);
2685 QVERIFY(!interface->child(-1));
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;
2696 QTestAccessibility::clearEvents();
2697 #endif // QT_NO_CALENDARWIDGET
2700 void tst_QAccessibility::dockWidgetTest()
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"));
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);
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);
2721 mw->resize(600,400);
2723 #if defined(Q_OS_UNIX)
2724 QCoreApplication::processEvents();
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) {
2743 QCOMPARE(accDock1->role(), QAccessible::Window);
2745 QAccessibleInterface *dock1TitleBar = accDock1->child(0);
2746 QCOMPARE(dock1TitleBar->role(), QAccessible::TitleBar);
2747 QVERIFY(accDock1->rect().contains(dock1TitleBar->rect()));
2748 delete dock1TitleBar;
2750 QPoint globalPos = dock1->mapToGlobal(QPoint(0,0));
2751 globalPos.rx()+=5; //### query style
2753 QAccessibleInterface *childAt = accDock1->childAt(globalPos.x(), globalPos.y()); //###
2754 QCOMPARE(childAt->role(), QAccessible::TitleBar);
2755 int index = accDock1->indexOfChild(childAt);
2757 QAccessibleInterface *accTitleBar = accDock1->child(index);
2759 QCOMPARE(accTitleBar->role(), QAccessible::TitleBar);
2760 QCOMPARE(accDock1->indexOfChild(accTitleBar), 0);
2761 QAccessibleInterface *acc;
2762 acc = accTitleBar->parent();
2764 QCOMPARE(acc->role(), QAccessible::Window);
2774 QTestAccessibility::clearEvents();
2775 #endif // QT_NO_DOCKWIDGET
2778 void tst_QAccessibility::comboBoxTest()
2780 #if defined(Q_OS_WINCE)
2781 if (!IsValidCEPlatform())
2782 QSKIP("Test skipped on Windows Mobile test hardware");
2784 { // not editable combobox
2786 combo.addItems(QStringList() << "one" << "two" << "three");
2788 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&combo);
2789 QCOMPARE(verifyHierarchy(iface), 0);
2791 QCOMPARE(iface->role(), QAccessible::ComboBox);
2792 QCOMPARE(iface->childCount(), 1);
2795 QCOMPARE(iface->text(QAccessible::Name), QLatin1String("one"));
2797 QCOMPARE(iface->text(QAccessible::Value), QLatin1String("one"));
2798 combo.setCurrentIndex(2);
2800 QCOMPARE(iface->text(QAccessible::Name), QLatin1String("three"));
2802 QCOMPARE(iface->text(QAccessible::Value), QLatin1String("three"));
2804 QAccessibleInterface *listIface = iface->child(0);
2805 QCOMPARE(listIface->role(), QAccessible::List);
2806 QCOMPARE(listIface->childCount(), 3);
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());
2817 { // editable combobox
2818 QComboBox editableCombo;
2819 editableCombo.show();
2820 editableCombo.setEditable(true);
2821 editableCombo.addItems(QStringList() << "foo" << "bar" << "baz");
2823 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&editableCombo);
2824 QCOMPARE(verifyHierarchy(iface), 0);
2826 QCOMPARE(iface->role(), QAccessible::ComboBox);
2827 QCOMPARE(iface->childCount(), 2);
2829 QAccessibleInterface *listIface = iface->child(0);
2830 QCOMPARE(listIface->role(), QAccessible::List);
2831 QAccessibleInterface *editIface = iface->child(1);
2832 QCOMPARE(editIface->role(), QAccessible::EditableText);
2839 QTestAccessibility::clearEvents();
2842 void tst_QAccessibility::labelTest()
2844 QString text = "Hello World";
2845 QLabel *label = new QLabel(text);
2848 #if defined(Q_OS_UNIX)
2849 QCoreApplication::processEvents();
2853 QAccessibleInterface *acc_label = QAccessible::queryAccessibleInterface(label);
2856 QCOMPARE(acc_label->text(QAccessible::Name), text);
2860 QTestAccessibility::clearEvents();
2862 QPixmap testPixmap(50, 50);
2866 imageLabel.setPixmap(testPixmap);
2867 imageLabel.setToolTip("Test Description");
2869 acc_label = QAccessible::queryAccessibleInterface(&imageLabel);
2872 QAccessibleImageInterface *imageInterface = acc_label->imageInterface();
2873 QVERIFY(imageInterface);
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);
2882 QTestAccessibility::clearEvents();
2885 void tst_QAccessibility::accelerators()
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);
2893 label->setBuddy(le);
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());
2911 #if !defined(QT_NO_DEBUG) && !defined(Q_OS_MAC)
2912 QTest::ignoreMessage(QtWarningMsg, "QKeySequence::mnemonic: \"Q &A&B\" contains multiple occurrences of '&'");
2914 label->setText(tr("Q &A&B"));
2915 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("A"));
2917 #if defined(Q_OS_UNIX)
2918 QCoreApplication::processEvents();
2922 QTestAccessibility::clearEvents();
2925 #ifdef QT_SUPPORTS_IACCESSIBLE2
2926 static IUnknown *queryIA2(IAccessible *acc, const IID &iid)
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);
2940 // The control supports IAccessible2.
2941 pService->Release();
2943 return resultInterface;
2947 void tst_QAccessibility::bridgeTest()
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.
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);
2962 QVERIFY(QTest::qWaitForWindowExposed(window));
2965 /**************************************************
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);
2973 // All set, now test the bridge.
2975 pt.x = buttonRect.center().x();
2976 pt.y = buttonRect.center().y();
2977 IAccessible *iaccButton;
2980 HRESULT hr = ::AccessibleObjectFromPoint(pt, &iaccButton, &varChild);
2981 QVERIFY(SUCCEEDED(hr));
2986 // **** Test get_accRole ****
2988 hr = iaccButton->get_accRole(varSELF, &varRole);
2989 QVERIFY(SUCCEEDED(hr));
2991 QCOMPARE(varRole.vt, (VARTYPE)VT_I4);
2992 QCOMPARE(varRole.lVal, (LONG)ROLE_SYSTEM_PUSHBUTTON);
2994 // **** Test accLocation ****
2996 hr = iaccButton->accLocation(&x, &y, &w, &h, varSELF);
2997 QCOMPARE(buttonRect, QRect(x, y, w, h));
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.
3005 /***** Test IAccessibleComponent *****/
3006 IAccessibleComponent *ia2Component = 0;
3007 hr = ia2Button->QueryInterface(IID_IAccessibleComponent, (void**)&ia2Component);
3008 QVERIFY(SUCCEEDED(hr));
3010 hr = ia2Component->get_locationInParent(&x, &y);
3011 QVERIFY(SUCCEEDED(hr));
3012 QCOMPARE(button->pos(), QPoint(x, y));
3013 ia2Component->Release();
3015 /***** Test IAccessibleAction *****/
3016 IAccessibleAction *ia2Action = 0;
3017 hr = ia2Button->QueryInterface(IID_IAccessibleAction, (void**)&ia2Action);
3018 QVERIFY(SUCCEEDED(hr));
3021 ia2Action->nActions(&nActions);
3022 QVERIFY(nActions >= 1); // "Press" and "SetFocus"
3024 ia2Action->get_name(0, &actionName);
3025 QString name((QChar*)actionName);
3026 QCOMPARE(name, QAccessibleActionInterface::pressAction());
3027 ia2Action->Release();
3031 ia2Button->Release();
3034 iaccButton->Release();
3036 /**************************************************
3038 **************************************************/
3039 QWindow *windowHandle = window->windowHandle();
3040 QPlatformNativeInterface *platform = QGuiApplication::platformNativeInterface();
3041 HWND hWnd = (HWND)platform->nativeResourceForWindow("handle", windowHandle);
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));
3049 QCOMPARE(varRole.vt, (VARTYPE)VT_I4);
3050 QCOMPARE(varRole.lVal, (LONG)ROLE_SYSTEM_CLIENT);
3053 hr = iaccWindow->get_accChildCount(&nChildren);
3054 QVERIFY(SUCCEEDED(hr));
3055 QCOMPARE(nChildren, (long)2);
3057 /**************************************************
3059 **************************************************/
3060 // Get the second child (the accessible interface for the text edit)
3061 varChild.vt = VT_I4;
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));
3071 QCOMPARE(varRole.vt, (VARTYPE)VT_I4);
3072 QCOMPARE(varRole.lVal, (LONG)ROLE_SYSTEM_TEXT);
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();
3084 if (IAccessibleText *ia2Text = (IAccessibleText*)queryIA2(iaccTextEdit, IID_IAccessibleText)) {
3086 hr = ia2Text->get_text(12, 15, &txt);
3087 QVERIFY(SUCCEEDED(hr));
3088 QCOMPARE(QString((QChar*)txt), QLatin1String("are"));
3092 iaccTextEdit->Release();
3095 iaccWindow->Release();
3096 QTestAccessibility::clearEvents();
3100 QTEST_MAIN(tst_QAccessibility)
3101 #include "tst_qaccessibility.moc"