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 <QtTest/QtTest>
48 #if defined(Q_OS_WIN) && defined(interface)
53 #include "QtTest/qtestaccessible.h"
55 #if defined(Q_OS_WINCE)
56 extern "C" bool SystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni);
57 #define SPI_GETPLATFORMTYPE 257
58 inline bool IsValidCEPlatform() {
59 wchar_t tszPlatform[64];
60 if (SystemParametersInfo(SPI_GETPLATFORMTYPE, sizeof(tszPlatform) / sizeof(*tszPlatform), tszPlatform, 0)) {
61 QString platform = QString::fromWCharArray(tszPlatform);
62 if ((platform == QLatin1String("PocketPC")) || (platform == QLatin1String("Smartphone")))
69 typedef QSharedPointer<QAccessibleInterface> QAIPtr;
71 static inline bool verifyChild(QWidget *child, QAccessibleInterface *interface,
72 int index, const QRect &domain)
75 qWarning("tst_QAccessibility::verifyChild: null pointer to child.");
80 qWarning("tst_QAccessibility::verifyChild: null pointer to interface.");
84 // Verify that we get a valid QAccessibleInterface for the child.
85 QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(child);
86 if (!childInterface) {
87 qWarning("tst_QAccessibility::verifyChild: Failed to retrieve interface for child.");
91 // QAccessibleInterface::indexOfChild():
92 // Verify that indexOfChild() returns an index equal to the index passed in
93 int indexFromIndexOfChild = interface->indexOfChild(childInterface);
94 if (indexFromIndexOfChild != index) {
95 qWarning("tst_QAccessibility::verifyChild (indexOfChild()):");
96 qWarning() << "Expected:" << index;
97 qWarning() << "Actual: " << indexFromIndexOfChild;
101 // Navigate to child, compare its object and role with the interface from queryAccessibleInterface(child).
102 QAccessibleInterface *navigatedChildInterface = interface->child(index);
103 if (navigatedChildInterface == 0)
106 const QRect rectFromInterface = navigatedChildInterface->rect();
107 delete navigatedChildInterface;
109 // QAccessibleInterface::childAt():
110 // Calculate global child position and check that the interface
111 // returns the correct index for that position.
112 QPoint globalChildPos = child->mapToGlobal(QPoint(0, 0));
113 QAccessibleInterface *childAtInterface = interface->childAt(globalChildPos.x(), globalChildPos.y());
114 if (!childAtInterface) {
115 qWarning("tst_QAccessibility::verifyChild (childAt()):");
116 qWarning() << "Expected:" << childInterface;
117 qWarning() << "Actual: no child";
120 if (childAtInterface->object() != childInterface->object()) {
121 qWarning("tst_QAccessibility::verifyChild (childAt()):");
122 qWarning() << "Expected:" << childInterface;
123 qWarning() << "Actual: " << childAtInterface;
126 delete childInterface;
127 delete childAtInterface;
129 // QAccessibleInterface::rect():
130 // Calculate global child geometry and check that the interface
131 // returns a QRect which is equal to the calculated QRect.
132 const QRect expectedGlobalRect = QRect(globalChildPos, child->size());
133 if (expectedGlobalRect != rectFromInterface) {
134 qWarning("tst_QAccessibility::verifyChild (rect()):");
135 qWarning() << "Expected:" << expectedGlobalRect;
136 qWarning() << "Actual: " << rectFromInterface;
140 // Verify that the child is within its domain.
141 if (!domain.contains(rectFromInterface)) {
142 qWarning("tst_QAccessibility::verifyChild: Child is not within its domain.");
149 static inline int indexOfChild(QAccessibleInterface *parentInterface, QWidget *childWidget)
151 if (!parentInterface || !childWidget)
153 QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(childWidget);
156 int index = parentInterface->indexOfChild(childInterface);
157 delete childInterface;
161 #define EXPECT(cond) \
163 if (!errorAt && !(cond)) { \
164 errorAt = __LINE__; \
165 qWarning("level: %d, middle: %d, role: %d (%s)", treelevel, middle, iface->role(), #cond); \
169 static int verifyHierarchy(QAccessibleInterface *iface)
172 static int treelevel = 0; // for error diagnostics
173 QAccessibleInterface *middleChild, *if2;
176 int middle = iface->childCount()/2 + 1;
177 if (iface->childCount() >= 2) {
178 middleChild = iface->child(middle - 1);
180 for (int i = 0; i < iface->childCount() && !errorAt; ++i) {
181 if2 = iface->child(i);
183 // navigate Ancestor...
184 QAccessibleInterface *parent = if2->parent();
185 EXPECT(iface->object() == parent->object());
188 // navigate Sibling...
189 // if (middleChild) {
190 // entry = if2->navigate(QAccessible::Sibling, middle, &if3);
191 // EXPECT(entry == 0 && if3->object() == middleChild->object());
194 // EXPECT(iface->indexOfChild(middleChild) == middle);
197 // verify children...
199 errorAt = verifyHierarchy(if2);
208 QRect childRect(QAccessibleInterface *iface, int index = 0)
210 QAccessibleInterface *child = iface->child(index);
211 QRect rect = child->rect();
216 class tst_QAccessibility : public QObject
220 tst_QAccessibility();
221 virtual ~tst_QAccessibility();
225 void cleanupTestCase();
231 void deletedWidget();
233 void statesStructTest();
234 void navigateHierarchy();
236 void textAttributes();
241 void applicationTest();
242 void mainWindowTest();
244 void scrollBarTest();
246 void tabWidgetTest();
249 void doubleSpinBoxTest();
251 void textBrowserTest();
253 void mdiSubWindowTest();
255 void workspaceTest();
256 void dialogButtonBoxTest();
258 void rubberBandTest();
259 void abstractScrollAreaTest();
260 void scrollAreaTest();
266 void calendarWidgetTest();
267 void dockWidgetTest();
269 void accessibleName();
279 const double Q_PI = 3.14159265358979323846;
281 QString eventName(const int ev)
284 case 0x0001: return "SoundPlayed";
285 case 0x0002: return "Alert";
286 case 0x0003: return "ForegroundChanged";
287 case 0x0004: return "MenuStart";
288 case 0x0005: return "MenuEnd";
289 case 0x0006: return "PopupMenuStart";
290 case 0x0007: return "PopupMenuEnd";
291 case 0x000C: return "ContextHelpStart";
292 case 0x000D: return "ContextHelpEnd";
293 case 0x000E: return "DragDropStart";
294 case 0x000F: return "DragDropEnd";
295 case 0x0010: return "DialogStart";
296 case 0x0011: return "DialogEnd";
297 case 0x0012: return "ScrollingStart";
298 case 0x0013: return "ScrollingEnd";
299 case 0x0018: return "MenuCommand";
301 case 0x0116: return "TableModelChanged";
302 case 0x011B: return "TextCaretMoved";
304 case 0x8000: return "ObjectCreated";
305 case 0x8001: return "ObjectDestroyed";
306 case 0x8002: return "ObjectShow";
307 case 0x8003: return "ObjectHide";
308 case 0x8004: return "ObjectReorder";
309 case 0x8005: return "Focus";
310 case 0x8006: return "Selection";
311 case 0x8007: return "SelectionAdd";
312 case 0x8008: return "SelectionRemove";
313 case 0x8009: return "SelectionWithin";
314 case 0x800A: return "StateChanged";
315 case 0x800B: return "LocationChanged";
316 case 0x800C: return "NameChanged";
317 case 0x800D: return "DescriptionChanged";
318 case 0x800E: return "ValueChanged";
319 case 0x800F: return "ParentChanged";
320 case 0x80A0: return "HelpChanged";
321 case 0x80B0: return "DefaultActionChanged";
322 case 0x80C0: return "AcceleratorChanged";
323 default: return "Unknown Event";
327 QAccessible::State state(QWidget * const widget)
329 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
331 qWarning() << "Cannot get QAccessibleInterface for widget";
332 QAccessible::State state = (iface ? iface->state() : QAccessible::State());
337 class QtTestAccessibleWidget: public QWidget
341 QtTestAccessibleWidget(QWidget *parent, const char *name): QWidget(parent)
345 pal.setColor(backgroundRole(), Qt::black);//black is beautiful
351 class QtTestAccessibleWidgetIface: public QAccessibleWidget
354 QtTestAccessibleWidgetIface(QtTestAccessibleWidget *w): QAccessibleWidget(w) {}
355 QString text(QAccessible::Text t) const
357 if (t == QAccessible::Help)
358 return QString::fromLatin1("Help yourself");
359 return QAccessibleWidget::text(t);
361 static QAccessibleInterface *ifaceFactory(const QString &key, QObject *o)
363 if (key == "QtTestAccessibleWidget")
364 return new QtTestAccessibleWidgetIface(static_cast<QtTestAccessibleWidget*>(o));
369 tst_QAccessibility::tst_QAccessibility()
374 tst_QAccessibility::~tst_QAccessibility()
378 void tst_QAccessibility::onClicked()
383 void tst_QAccessibility::initTestCase()
385 QTestAccessibility::initialize();
386 QAccessible::installFactory(QtTestAccessibleWidgetIface::ifaceFactory);
389 void tst_QAccessibility::cleanupTestCase()
391 QTestAccessibility::cleanup();
394 void tst_QAccessibility::init()
396 QTestAccessibility::clearEvents();
399 void tst_QAccessibility::cleanup()
401 const EventList list = QTestAccessibility::events();
402 if (!list.isEmpty()) {
403 qWarning("%d accessibility event(s) were not handled in testfunction '%s':", list.count(),
404 QString(QTest::currentTestFunction()).toAscii().constData());
405 for (int i = 0; i < list.count(); ++i)
406 qWarning(" %d: Object: %p Event: '%s' (%d) Child: %d", i + 1, list.at(i).object,
407 eventName(list.at(i).event).toAscii().constData(), list.at(i).event, list.at(i).child);
409 QTestAccessibility::clearEvents();
412 void tst_QAccessibility::eventTest()
414 QPushButton* button = new QPushButton(0);
415 button->setObjectName(QString("Olaf"));
418 QVERIFY_EVENT(button, 0, QAccessible::ObjectShow);
419 button->setFocus(Qt::MouseFocusReason);
420 QTestAccessibility::clearEvents();
421 QTest::mouseClick(button, Qt::LeftButton, 0);
422 QVERIFY_EVENT(button, 0, QAccessible::StateChanged);
423 QVERIFY_EVENT(button, 0, QAccessible::StateChanged);
425 button->setAccessibleName("Olaf the second");
426 QVERIFY_EVENT(button, 0, QAccessible::NameChanged);
427 button->setAccessibleDescription("This is a button labeled Olaf");
428 QVERIFY_EVENT(button, 0, QAccessible::DescriptionChanged);
431 QVERIFY_EVENT(button, 0, QAccessible::ObjectHide);
436 void tst_QAccessibility::customWidget()
438 QtTestAccessibleWidget* widget = new QtTestAccessibleWidget(0, "Heinz");
440 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
442 QVERIFY(iface->isValid());
443 QCOMPARE(iface->object(), (QObject*)widget);
444 QCOMPARE(iface->object()->objectName(), QString("Heinz"));
445 QCOMPARE(iface->text(QAccessible::Help), QString("Help yourself"));
451 void tst_QAccessibility::deletedWidget()
453 QtTestAccessibleWidget *widget = new QtTestAccessibleWidget(0, "Ralf");
454 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(widget);
456 QVERIFY(iface->isValid());
457 QCOMPARE(iface->object(), (QObject*)widget);
461 QVERIFY(!iface->isValid());
465 void tst_QAccessibility::statesStructTest()
467 QAccessible::State s1;
468 QVERIFY(s1.disabled == 0);
469 QVERIFY(s1.focusable == 0);
470 QVERIFY(s1.modal == 0);
472 QAccessible::State s2;
475 QVERIFY(!(s2 == s1));
478 s1 = QAccessible::State();
479 QVERIFY(!(s2 == s1));
482 QVERIFY(s1.busy == 1);
485 void tst_QAccessibility::sliderTest()
488 QSlider *slider = new QSlider(0);
489 slider->setObjectName(QString("Slidy"));
491 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(slider);
493 QVERIFY(iface->isValid());
495 QCOMPARE(iface->childCount(), 0);
496 QCOMPARE(iface->role(), QAccessible::Slider);
498 QAccessibleValueInterface *valueIface = iface->valueInterface();
499 QVERIFY(valueIface != 0);
500 QCOMPARE(valueIface->minimumValue().toInt(), slider->minimum());
501 QCOMPARE(valueIface->maximumValue().toInt(), slider->maximum());
502 slider->setValue(50);
503 QCOMPARE(valueIface->currentValue().toInt(), slider->value());
505 QCOMPARE(valueIface->currentValue().toInt(), slider->value());
506 slider->setValue(100);
507 QCOMPARE(valueIface->currentValue().toInt(), slider->value());
508 valueIface->setCurrentValue(77);
509 QCOMPARE(77, slider->value());
514 QTestAccessibility::clearEvents();
517 void tst_QAccessibility::navigateHierarchy()
520 QWidget *w = new QWidget(0);
521 w->setObjectName(QString("Hans"));
523 QWidget *w1 = new QWidget(w);
524 w1->setObjectName(QString("1"));
526 QWidget *w2 = new QWidget(w);
527 w2->setObjectName(QString("2"));
529 QWidget *w3 = new QWidget(w);
530 w3->setObjectName(QString("3"));
532 QWidget *w31 = new QWidget(w3);
533 w31->setObjectName(QString("31"));
536 QAccessibleInterface *target = 0;
537 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(w);
539 QVERIFY(iface->isValid());
541 target = iface->child(14);
542 QVERIFY(target == 0);
543 target = iface->child(-1);
544 QVERIFY(target == 0);
545 target = iface->child(0);
546 QAccessibleInterface *interfaceW1 = iface->child(0);
548 QVERIFY(target->isValid());
549 QCOMPARE(target->object(), (QObject*)w1);
550 QVERIFY(interfaceW1 != 0);
551 QVERIFY(interfaceW1->isValid());
552 QCOMPARE(interfaceW1->object(), (QObject*)w1);
554 delete iface; iface = 0;
556 iface = QAccessible::queryAccessibleInterface(w);
557 target = iface->child(2);
558 QVERIFY(target != 0);
559 QVERIFY(target->isValid());
560 QCOMPARE(target->object(), (QObject*)w3);
561 delete iface; iface = 0;
564 iface = target->child(1);
565 QCOMPARE(iface, (QAccessibleInterface*)0);
566 iface = target->child(0);
568 QVERIFY(iface->isValid());
569 QCOMPARE(iface->object(), (QObject*)w31);
571 iface = QAccessible::queryAccessibleInterface(w);
572 QAccessibleInterface *acc3 = iface->child(2);
573 target = acc3->child(0);
576 QCOMPARE(target->object(), (QObject*)w31);
578 iface = target->parent();
580 QVERIFY(iface->isValid());
581 QCOMPARE(iface->object(), (QObject*)w3);
582 delete iface; iface = 0;
583 delete target; target = 0;
587 QTestAccessibility::clearEvents();
590 #define QSETCOMPARE(thetypename, elements, otherelements) \
591 QCOMPARE((QSet<thetypename>() << elements), (QSet<thetypename>() << otherelements))
593 static QWidget *createWidgets()
595 QWidget *w = new QWidget();
597 QHBoxLayout *box = new QHBoxLayout(w);
600 box->addWidget(new QComboBox(w));
601 box->addWidget(new QPushButton(QString::fromAscii("widget text %1").arg(i++), w));
602 box->addWidget(new QHeaderView(Qt::Vertical, w));
603 box->addWidget(new QTreeView(w));
604 box->addWidget(new QTreeWidget(w));
605 box->addWidget(new QListView(w));
606 box->addWidget(new QListWidget(w));
607 box->addWidget(new QTableView(w));
608 box->addWidget(new QTableWidget(w));
609 box->addWidget(new QCalendarWidget(w));
610 box->addWidget(new QDialogButtonBox(w));
611 box->addWidget(new QGroupBox(QString::fromAscii("widget text %1").arg(i++), w));
612 box->addWidget(new QFrame(w));
613 box->addWidget(new QLineEdit(QString::fromAscii("widget text %1").arg(i++), w));
614 box->addWidget(new QProgressBar(w));
615 box->addWidget(new QTabWidget(w));
616 box->addWidget(new QCheckBox(QString::fromAscii("widget text %1").arg(i++), w));
617 box->addWidget(new QRadioButton(QString::fromAscii("widget text %1").arg(i++), w));
618 box->addWidget(new QDial(w));
619 box->addWidget(new QScrollBar(w));
620 box->addWidget(new QSlider(w));
621 box->addWidget(new QDateTimeEdit(w));
622 box->addWidget(new QDoubleSpinBox(w));
623 box->addWidget(new QSpinBox(w));
624 box->addWidget(new QLabel(QString::fromAscii("widget text %1").arg(i++), w));
625 box->addWidget(new QLCDNumber(w));
626 box->addWidget(new QStackedWidget(w));
627 box->addWidget(new QToolBox(w));
628 box->addWidget(new QLabel(QString::fromAscii("widget text %1").arg(i++), w));
629 box->addWidget(new QTextEdit(QString::fromAscii("widget text %1").arg(i++), w));
632 * QAbstractItemView, QGraphicsView, QScrollArea,
633 * QToolButton, QDockWidget, QFocusFrame, QMainWindow, QMenu, QMenuBar, QSizeGrip, QSplashScreen, QSplitterHandle,
634 * QStatusBar, QSvgWidget, QTabBar, QToolBar, QWorkspace, QSplitter
639 void tst_QAccessibility::accessibleName()
641 QWidget *toplevel = createWidgets();
643 #if defined(Q_OS_UNIX)
644 QCoreApplication::processEvents();
647 QLayout *lout = toplevel->layout();
648 for (int i = 0; i < lout->count(); i++) {
649 QLayoutItem *item = lout->itemAt(i);
650 QWidget *child = item->widget();
652 QString name = tr("Widget Name %1").arg(i);
653 child->setAccessibleName(name);
654 QAccessibleInterface *acc = QAccessible::queryAccessibleInterface(child);
655 QCOMPARE(acc->text(QAccessible::Name), name);
657 QString desc = tr("Widget Description %1").arg(i);
658 child->setAccessibleDescription(desc);
659 QCOMPARE(acc->text(QAccessible::Description), desc);
664 QTestAccessibility::clearEvents();
667 void tst_QAccessibility::textAttributes()
673 QString text("<html><head></head><body>"
674 "Hello, <b>this</b> is an <i><b>example</b> text</i>."
675 "<span style=\"font-family: monospace\">Multiple fonts are used.</span>"
676 "Multiple <span style=\"font-size: 8pt\">text sizes</span> are used."
677 "Let's give some color to <span style=\"color:#f0f1f2; background-color:#14f01e\">Qt</span>."
680 textEdit.setText(text);
681 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&textEdit);
683 QAccessibleTextInterface *textInterface=interface->textInterface();
685 QVERIFY(textInterface);
686 QCOMPARE(textInterface->characterCount(), 112);
688 attributes = textInterface->attributes(10, &startOffset, &endOffset);
689 QCOMPARE(startOffset, 7);
690 QCOMPARE(endOffset, 11);
691 attributes.prepend(';');
692 QVERIFY(attributes.contains(QLatin1String(";font-weight:bold;")));
694 attributes = textInterface->attributes(18, &startOffset, &endOffset);
695 QCOMPARE(startOffset, 18);
696 QCOMPARE(endOffset, 25);
697 attributes.prepend(';');
698 QVERIFY(attributes.contains(QLatin1String(";font-weight:bold;")));
699 QVERIFY(attributes.contains(QLatin1String(";font-style:italic;")));
701 attributes = textInterface->attributes(34, &startOffset, &endOffset);
702 QCOMPARE(startOffset, 31);
703 QCOMPARE(endOffset, 55);
704 attributes.prepend(';');
705 QVERIFY(attributes.contains(QLatin1String(";font-family:\"monospace\";")));
707 attributes = textInterface->attributes(65, &startOffset, &endOffset);
708 QCOMPARE(startOffset, 64);
709 QCOMPARE(endOffset, 74);
710 attributes.prepend(';');
711 QVERIFY(attributes.contains(QLatin1String(";font-size:8pt;")));
713 attributes = textInterface->attributes(110, &startOffset, &endOffset);
714 QCOMPARE(startOffset, 109);
715 QCOMPARE(endOffset, 111);
716 attributes.prepend(';');
717 QVERIFY(attributes.contains(QLatin1String(";background-color:rgb(20,240,30);")));
718 QVERIFY(attributes.contains(QLatin1String(";color:rgb(240,241,242);")));
721 void tst_QAccessibility::hideShowTest()
723 QWidget * const window = new QWidget();
724 QWidget * const child = new QWidget(window);
726 QVERIFY(state(window).invisible);
727 QVERIFY(state(child).invisible);
729 QTestAccessibility::clearEvents();
731 // show() and veryfy that both window and child are not invisible and get ObjectShow events.
733 QVERIFY(!state(window).invisible);
734 QVERIFY(!state(child).invisible);
735 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(window, 0, QAccessible::ObjectShow)));
736 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(child, 0, QAccessible::ObjectShow)));
737 QTestAccessibility::clearEvents();
739 // hide() and veryfy that both window and child are invisible and get ObjectHide events.
741 QVERIFY(state(window).invisible);
742 QVERIFY(state(child).invisible);
743 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(window, 0, QAccessible::ObjectHide)));
744 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(child, 0, QAccessible::ObjectHide)));
745 QTestAccessibility::clearEvents();
748 QTestAccessibility::clearEvents();
752 void tst_QAccessibility::actionTest()
755 QCOMPARE(QAccessibleActionInterface::pressAction(), QString(QStringLiteral("Press")));
757 QWidget *widget = new QWidget;
758 widget->setFocusPolicy(Qt::NoFocus);
761 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(widget);
763 QVERIFY(interface->isValid());
764 QAccessibleActionInterface *actions = interface->actionInterface();
767 // no actions by default, except when focusable
768 QCOMPARE(actions->actionNames(), QStringList());
769 widget->setFocusPolicy(Qt::StrongFocus);
770 QCOMPARE(actions->actionNames(), QStringList(QAccessibleActionInterface::setFocusAction()));
775 QTestAccessibility::clearEvents();
778 QPushButton *button = new QPushButton;
780 button->clearFocus();
781 QCOMPARE(button->hasFocus(), false);
782 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(button);
783 QAccessibleActionInterface *actions = interface->actionInterface();
786 // Make sure the "primary action" press comes first!
787 QCOMPARE(actions->actionNames(), QStringList() << QAccessibleActionInterface::pressAction() << QAccessibleActionInterface::setFocusAction());
789 actions->doAction(QAccessibleActionInterface::setFocusAction());
791 QCOMPARE(button->hasFocus(), true);
793 connect(button, SIGNAL(clicked()), this, SLOT(onClicked()));
794 QCOMPARE(click_count, 0);
795 actions->doAction(QAccessibleActionInterface::pressAction());
797 QCOMPARE(click_count, 1);
802 QTestAccessibility::clearEvents();
805 void tst_QAccessibility::applicationTest()
807 QLatin1String name = QLatin1String("My Name");
808 qApp->setApplicationName(name);
809 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(qApp);
810 QCOMPARE(interface->text(QAccessible::Name), name);
811 QCOMPARE(interface->role(), QAccessible::Application);
815 void tst_QAccessibility::mainWindowTest()
817 QMainWindow *mw = new QMainWindow;
818 mw->resize(300, 200);
819 mw->show(); // triggers layout
821 QLatin1String name = QLatin1String("I am the main window");
822 mw->setWindowTitle(name);
823 QTest::qWaitForWindowShown(mw);
824 QVERIFY_EVENT(mw, 0, QAccessible::ObjectShow);
826 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(mw);
827 QCOMPARE(interface->text(QAccessible::Name), name);
828 QCOMPARE(interface->role(), QAccessible::Window);
831 QTestAccessibility::clearEvents();
834 class CounterButton : public QPushButton {
837 CounterButton(const QString& name, QWidget* parent)
838 : QPushButton(name, parent), clickCount(0)
840 connect(this, SIGNAL(clicked(bool)), SLOT(incClickCount()));
844 void incClickCount() {
849 void tst_QAccessibility::buttonTest()
852 window.setLayout(new QVBoxLayout);
854 // Standard push button
855 CounterButton pushButton("Ok", &window);
858 QPushButton toggleButton("Toggle", &window);
859 toggleButton.setCheckable(true);
862 QCheckBox checkBox("Check me!", &window);
865 QCheckBox tristate("Tristate!", &window);
866 tristate.setTristate(true);
869 QRadioButton radio("Radio me!", &window);
871 // standard toolbutton
872 QToolButton toolbutton(&window);
873 toolbutton.setText("Tool");
874 toolbutton.setMinimumSize(20,20);
876 // standard toolbutton
877 QToolButton toggletool(&window);
878 toggletool.setCheckable(true);
879 toggletool.setText("Toggle");
880 toggletool.setMinimumSize(20,20);
883 QAccessibleInterface* interface = QAccessible::queryAccessibleInterface(&pushButton);
884 QAccessibleActionInterface* actionInterface = interface->actionInterface();
885 QVERIFY(actionInterface != 0);
886 QCOMPARE(interface->role(), QAccessible::PushButton);
888 // buttons only have a click action
889 QCOMPARE(actionInterface->actionNames().size(), 2);
890 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::pressAction() << QAccessibleActionInterface::setFocusAction());
891 QCOMPARE(pushButton.clickCount, 0);
892 actionInterface->doAction(QAccessibleActionInterface::pressAction());
894 QCOMPARE(pushButton.clickCount, 1);
897 // test toggle button
898 interface = QAccessible::queryAccessibleInterface(&toggleButton);
899 actionInterface = interface->actionInterface();
900 QCOMPARE(interface->role(), QAccessible::CheckBox);
901 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
902 QCOMPARE(actionInterface->localizedActionDescription(QAccessibleActionInterface::checkAction()), QString("Checks the checkbox"));
903 QVERIFY(!toggleButton.isChecked());
904 QVERIFY(!interface->state().checked);
905 actionInterface->doAction(QAccessibleActionInterface::checkAction());
907 QVERIFY(toggleButton.isChecked());
908 QCOMPARE(actionInterface->actionNames().at(0), QAccessibleActionInterface::uncheckAction());
909 QVERIFY(interface->state().checked);
913 // test menu push button
914 QAction *foo = new QAction("Foo", 0);
915 foo->setShortcut(QKeySequence("Ctrl+F"));
916 QMenu *menu = new QMenu();
917 menu->addAction(foo);
918 QPushButton menuButton;
919 menuButton.setMenu(menu);
921 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&menuButton);
922 QCOMPARE(interface->role(), QAccessible::ButtonMenu);
923 QVERIFY(interface->state().hasPopup);
924 QCOMPARE(interface->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction() << QAccessibleActionInterface::setFocusAction());
925 // showing the menu enters a new event loop...
926 // interface->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
927 // QTest::qWait(500);
933 interface = QAccessible::queryAccessibleInterface(&checkBox);
934 actionInterface = interface->actionInterface();
935 QCOMPARE(interface->role(), QAccessible::CheckBox);
936 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
937 QVERIFY(!interface->state().checked);
938 actionInterface->doAction(QAccessibleActionInterface::checkAction());
940 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::uncheckAction() << QAccessibleActionInterface::setFocusAction());
941 QVERIFY(interface->state().checked);
942 QVERIFY(checkBox.isChecked());
946 interface = QAccessible::queryAccessibleInterface(&radio);
947 actionInterface = interface->actionInterface();
948 QCOMPARE(interface->role(), QAccessible::RadioButton);
949 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
950 QVERIFY(!interface->state().checked);
951 actionInterface->doAction(QAccessibleActionInterface::checkAction());
953 QCOMPARE(actionInterface->actionNames(), QStringList() << QAccessibleActionInterface::checkAction() << QAccessibleActionInterface::setFocusAction());
954 QVERIFY(interface->state().checked);
955 QVERIFY(checkBox.isChecked());
958 // // test standard toolbutton
959 // QVERIFY(QAccessible::queryAccessibleInterface(&toolbutton, &test));
960 // QCOMPARE(test->role(), QAccessible::PushButton);
961 // QCOMPARE(test->defaultAction(0), QAccessible::Press);
962 // QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Press"));
963 // QCOMPARE(test->state(), (int)QAccessible::Normal);
966 // // toggle tool button
967 // QVERIFY(QAccessible::queryAccessibleInterface(&toggletool, &test));
968 // QCOMPARE(test->role(), QAccessible::CheckBox);
969 // QCOMPARE(test->defaultAction(0), QAccessible::Press);
970 // QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Check"));
971 // QCOMPARE(test->state(), (int)QAccessible::Normal);
972 // QVERIFY(test->doAction(QAccessible::Press, 0));
973 // QTest::qWait(500);
974 // QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Uncheck"));
975 // QCOMPARE(test->state(), (int)QAccessible::Checked);
978 // // test menu toolbutton
979 // QVERIFY(QAccessible::queryAccessibleInterface(&menuToolButton, &test));
980 // QCOMPARE(test->role(), QAccessible::ButtonMenu);
981 // QCOMPARE(test->defaultAction(0), 1);
982 // QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Open"));
983 // QCOMPARE(test->state(), (int)QAccessible::HasPopup);
984 // QCOMPARE(test->actionCount(0), 1);
985 // QCOMPARE(test->actionText(QAccessible::Press, QAccessible::Name, 0), QString("Press"));
988 // // test split menu toolbutton
989 // QVERIFY(QAccessible::queryAccessibleInterface(&splitToolButton, &test));
990 // QCOMPARE(test->childCount(), 2);
991 // QCOMPARE(test->role(), QAccessible::ButtonDropDown);
992 // QCOMPARE(test->role(1), QAccessible::PushButton);
993 // QCOMPARE(test->role(2), QAccessible::ButtonMenu);
994 // QCOMPARE(test->defaultAction(0), QAccessible::Press);
995 // QCOMPARE(test->defaultAction(1), QAccessible::Press);
996 // QCOMPARE(test->defaultAction(2), QAccessible::Press);
997 // QCOMPARE(test->actionText(test->defaultAction(0), QAccessible::Name, 0), QString("Press"));
998 // QCOMPARE(test->state(), (int)QAccessible::HasPopup);
999 // QCOMPARE(test->actionCount(0), 1);
1000 // QCOMPARE(test->actionText(1, QAccessible::Name, 0), QString("Open"));
1001 // QCOMPARE(test->actionText(test->defaultAction(1), QAccessible::Name, 1), QString("Press"));
1002 // QCOMPARE(test->state(1), (int)QAccessible::Normal);
1003 // QCOMPARE(test->actionText(test->defaultAction(2), QAccessible::Name, 2), QString("Open"));
1004 // QCOMPARE(test->state(2), (int)QAccessible::HasPopup);
1007 QTestAccessibility::clearEvents();
1010 void tst_QAccessibility::scrollBarTest()
1012 QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal);
1013 QAccessibleInterface * const scrollBarInterface = QAccessible::queryAccessibleInterface(scrollBar);
1014 QVERIFY(scrollBarInterface);
1015 QVERIFY(scrollBarInterface->state().invisible);
1016 scrollBar->resize(200, 50);
1018 QVERIFY(!scrollBarInterface->state().invisible);
1019 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(scrollBar, 0, QAccessible::ObjectShow)));
1020 QTestAccessibility::clearEvents();
1023 QVERIFY(scrollBarInterface->state().invisible);
1024 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(scrollBar, 0, QAccessible::ObjectHide)));
1025 QTestAccessibility::clearEvents();
1027 // Test that the left/right subcontrols are set to unavailable when the scrollBar is at the minimum/maximum.
1029 scrollBar->setMinimum(11);
1030 scrollBar->setMaximum(111);
1032 QAccessibleValueInterface *valueIface = scrollBarInterface->valueInterface();
1033 QVERIFY(valueIface != 0);
1034 QCOMPARE(valueIface->minimumValue().toInt(), scrollBar->minimum());
1035 QCOMPARE(valueIface->maximumValue().toInt(), scrollBar->maximum());
1036 scrollBar->setValue(50);
1037 QCOMPARE(valueIface->currentValue().toInt(), scrollBar->value());
1038 scrollBar->setValue(0);
1039 QCOMPARE(valueIface->currentValue().toInt(), scrollBar->value());
1040 scrollBar->setValue(100);
1041 QCOMPARE(valueIface->currentValue().toInt(), scrollBar->value());
1042 valueIface->setCurrentValue(77);
1043 QCOMPARE(77, scrollBar->value());
1045 const QRect scrollBarRect = scrollBarInterface->rect();
1046 QVERIFY(scrollBarRect.isValid());
1048 delete scrollBarInterface;
1051 QTestAccessibility::clearEvents();
1054 void tst_QAccessibility::tabTest()
1056 QTabBar *tabBar = new QTabBar();
1059 QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(tabBar);
1061 QCOMPARE(interface->childCount(), 2);
1063 // Test that the Invisible bit for the navigation buttons gets set
1064 // and cleared correctly.
1065 QAccessibleInterface *leftButton = interface->child(0);
1066 QCOMPARE(leftButton->role(), QAccessible::PushButton);
1067 QVERIFY(leftButton->state().invisible);
1071 for (int i = 0; i < lots; ++i)
1072 tabBar->addTab("Foo");
1074 QAccessibleInterface *child1 = interface->child(0);
1075 QAccessibleInterface *child2 = interface->child(1);
1077 QCOMPARE(child1->role(), QAccessible::PageTab);
1079 QCOMPARE(child2->role(), QAccessible::PageTab);
1081 QVERIFY((child1->state().invisible) == false);
1084 QCoreApplication::processEvents();
1087 QVERIFY(child1->state().invisible);
1090 tabBar->setCurrentIndex(0);
1092 // Test that sending a focus action to a tab does not select it.
1093 // child2->doAction(QAccessible::Focus, 2, QVariantList());
1094 QCOMPARE(tabBar->currentIndex(), 0);
1096 // Test that sending a press action to a tab selects it.
1097 QVERIFY(child2->actionInterface());
1098 QCOMPARE(child2->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1099 QCOMPARE(tabBar->currentIndex(), 0);
1100 child2->actionInterface()->doAction(QAccessibleActionInterface::pressAction());
1101 QCOMPARE(tabBar->currentIndex(), 1);
1107 QTestAccessibility::clearEvents();
1110 void tst_QAccessibility::tabWidgetTest()
1112 QTabWidget *tabWidget = new QTabWidget();
1115 // the interface for the tab is just a container for tabbar and stacked widget
1116 QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(tabWidget);
1118 QCOMPARE(interface->childCount(), 2);
1119 QCOMPARE(interface->role(), QAccessible::Client);
1121 // Create pages, check navigation
1122 QLabel *label1 = new QLabel("Page 1", tabWidget);
1123 tabWidget->addTab(label1, "Tab 1");
1124 QLabel *label2 = new QLabel("Page 2", tabWidget);
1125 tabWidget->addTab(label2, "Tab 2");
1127 QCOMPARE(interface->childCount(), 2);
1129 QAccessibleInterface* tabBarInterface = 0;
1130 // there is no special logic to sort the children, so the contents will be 1, the tab bar 2
1131 tabBarInterface = interface->child(1);
1132 QVERIFY(tabBarInterface);
1133 QCOMPARE(tabBarInterface->childCount(), 4);
1134 QCOMPARE(tabBarInterface->role(), QAccessible::PageTabList);
1136 QAccessibleInterface* tabButton1Interface = tabBarInterface->child(0);
1137 QVERIFY(tabButton1Interface);
1138 QCOMPARE(tabButton1Interface->role(), QAccessible::PageTab);
1139 QCOMPARE(tabButton1Interface->text(QAccessible::Name), QLatin1String("Tab 1"));
1141 QAccessibleInterface* tabButton2Interface = tabBarInterface->child(1);
1142 QVERIFY(tabButton1Interface);
1143 QCOMPARE(tabButton2Interface->role(), QAccessible::PageTab);
1144 QCOMPARE(tabButton2Interface->text(QAccessible::Name), QLatin1String("Tab 2"));
1146 QAccessibleInterface* tabButtonLeft = tabBarInterface->child(2);
1147 QVERIFY(tabButtonLeft);
1148 QCOMPARE(tabButtonLeft->role(), QAccessible::PushButton);
1149 QCOMPARE(tabButtonLeft->text(QAccessible::Name), QLatin1String("Scroll Left"));
1151 QAccessibleInterface* tabButtonRight = tabBarInterface->child(3);
1152 QVERIFY(tabButtonRight);
1153 QCOMPARE(tabButtonRight->role(), QAccessible::PushButton);
1154 QCOMPARE(tabButtonRight->text(QAccessible::Name), QLatin1String("Scroll Right"));
1155 delete tabButton1Interface;
1156 delete tabButton2Interface;
1157 delete tabButtonLeft;
1158 delete tabButtonRight;
1160 QAccessibleInterface* stackWidgetInterface = interface->child(0);
1161 QVERIFY(stackWidgetInterface);
1162 QCOMPARE(stackWidgetInterface->childCount(), 2);
1163 QCOMPARE(stackWidgetInterface->role(), QAccessible::LayeredPane);
1165 QAccessibleInterface* stackChild1Interface = stackWidgetInterface->child(0);
1166 QVERIFY(stackChild1Interface);
1168 QCOMPARE(stackChild1Interface->childCount(), 0);
1170 QCOMPARE(stackChild1Interface->role(), QAccessible::StaticText);
1171 QCOMPARE(stackChild1Interface->text(QAccessible::Name), QLatin1String("Page 1"));
1172 QCOMPARE(label1, stackChild1Interface->object());
1174 // Navigation in stack widgets should be consistent
1175 QAccessibleInterface* parent = stackChild1Interface->parent();
1178 QCOMPARE(parent->childCount(), 2);
1180 QCOMPARE(parent->role(), QAccessible::LayeredPane);
1183 QAccessibleInterface* stackChild2Interface = stackWidgetInterface->child(1);
1184 QVERIFY(stackChild2Interface);
1185 QCOMPARE(stackChild2Interface->childCount(), 0);
1186 QCOMPARE(stackChild2Interface->role(), QAccessible::StaticText);
1187 QCOMPARE(label2, stackChild2Interface->object());
1188 QCOMPARE(label2->text(), stackChild2Interface->text(QAccessible::Name));
1190 parent = stackChild2Interface->parent();
1193 QCOMPARE(parent->childCount(), 2);
1195 QCOMPARE(parent->role(), QAccessible::LayeredPane);
1198 delete tabBarInterface;
1199 delete stackChild1Interface;
1200 delete stackChild2Interface;
1201 delete stackWidgetInterface;
1204 QTestAccessibility::clearEvents();
1207 void tst_QAccessibility::menuTest()
1211 mw.resize(300, 200);
1212 QMenu *file = mw.menuBar()->addMenu("&File");
1213 QMenu *fileNew = file->addMenu("&New...");
1214 fileNew->menuAction()->setShortcut(tr("Ctrl+N"));
1215 fileNew->addAction("Text file");
1216 fileNew->addAction("Image file");
1217 file->addAction("&Open")->setShortcut(tr("Ctrl+O"));
1218 file->addAction("&Save")->setShortcut(tr("Ctrl+S"));
1219 file->addSeparator();
1220 file->addAction("E&xit")->setShortcut(tr("Alt+F4"));
1222 QMenu *edit = mw.menuBar()->addMenu("&Edit");
1223 edit->addAction("&Undo")->setShortcut(tr("Ctrl+Z"));
1224 edit->addAction("&Redo")->setShortcut(tr("Ctrl+Y"));
1225 edit->addSeparator();
1226 edit->addAction("Cu&t")->setShortcut(tr("Ctrl+X"));
1227 edit->addAction("&Copy")->setShortcut(tr("Ctrl+C"));
1228 edit->addAction("&Paste")->setShortcut(tr("Ctrl+V"));
1229 edit->addAction("&Delete")->setShortcut(tr("Del"));
1230 edit->addSeparator();
1231 edit->addAction("Pr&operties");
1233 mw.menuBar()->addSeparator();
1235 QMenu *help = mw.menuBar()->addMenu("&Help");
1236 help->addAction("&Contents");
1237 help->addAction("&About");
1239 mw.menuBar()->addAction("Action!");
1241 mw.show(); // triggers layout
1244 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(mw.menuBar());
1245 QCOMPARE(verifyHierarchy(interface), 0);
1248 QCOMPARE(interface->childCount(), 5);
1249 QCOMPARE(interface->role(), QAccessible::MenuBar);
1251 QAccessibleInterface *iFile = interface->child(0);
1252 QAccessibleInterface *iEdit = interface->child(1);
1253 QAccessibleInterface *iSeparator = interface->child(2);
1254 QAccessibleInterface *iHelp = interface->child(3);
1255 QAccessibleInterface *iAction = interface->child(4);
1257 QCOMPARE(iFile->role(), QAccessible::MenuItem);
1258 QCOMPARE(iEdit->role(), QAccessible::MenuItem);
1259 QCOMPARE(iSeparator->role(), QAccessible::Separator);
1260 QCOMPARE(iHelp->role(), QAccessible::MenuItem);
1261 QCOMPARE(iAction->role(), QAccessible::MenuItem);
1264 if (!IsValidCEPlatform())
1265 QSKIP("Tests do not work on Mobile platforms due to native menus");
1267 QCOMPARE(mw.mapFromGlobal(interface->rect().topLeft()), mw.menuBar()->geometry().topLeft());
1268 QCOMPARE(interface->rect().size(), mw.menuBar()->size());
1270 QVERIFY(interface->rect().contains(iFile->rect()));
1271 QVERIFY(interface->rect().contains(iEdit->rect()));
1272 // QVERIFY(interface->rect().contains(childSeparator->rect())); //separator might be invisible
1273 QVERIFY(interface->rect().contains(iHelp->rect()));
1274 QVERIFY(interface->rect().contains(iAction->rect()));
1277 QCOMPARE(iFile->text(QAccessible::Name), QString("File"));
1278 QCOMPARE(iEdit->text(QAccessible::Name), QString("Edit"));
1279 QCOMPARE(iSeparator->text(QAccessible::Name), QString());
1280 QCOMPARE(iHelp->text(QAccessible::Name), QString("Help"));
1281 QCOMPARE(iAction->text(QAccessible::Name), QString("Action!"));
1283 // TODO: Currently not working, task to fix is #100019.
1285 QCOMPARE(iFile->text(QAccessible::Accelerator), tr("Alt+F"));
1286 QCOMPARE(iEdit->text(QAccessible::Accelerator), tr("Alt+E"));
1287 QCOMPARE(iSeparator->text(QAccessible::Accelerator), QString());
1288 QCOMPARE(iHelp->text(QAccessible::Accelerator), tr("Alt+H"));
1289 QCOMPARE(iAction->text(QAccessible::Accelerator), QString());
1292 QVERIFY(iFile->actionInterface());
1294 QCOMPARE(iFile->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
1295 QCOMPARE(iSeparator->actionInterface()->actionNames(), QStringList());
1296 QCOMPARE(iHelp->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
1297 QCOMPARE(iAction->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1299 bool menuFade = qApp->isEffectEnabled(Qt::UI_FadeMenu);
1300 int menuFadeDelay = 300;
1301 iFile->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1303 QTest::qWait(menuFadeDelay);
1304 QVERIFY(file->isVisible() && !edit->isVisible() && !help->isVisible());
1305 iEdit->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1307 QTest::qWait(menuFadeDelay);
1308 QVERIFY(!file->isVisible() && edit->isVisible() && !help->isVisible());
1309 iHelp->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1311 QTest::qWait(menuFadeDelay);
1312 QVERIFY(!file->isVisible() && !edit->isVisible() && help->isVisible());
1313 iAction->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1315 QTest::qWait(menuFadeDelay);
1316 QVERIFY(!file->isVisible() && !edit->isVisible() && !help->isVisible());
1318 QVERIFY(interface->actionInterface());
1319 QCOMPARE(interface->actionInterface()->actionNames(), QStringList());
1321 interface = QAccessible::queryAccessibleInterface(file);
1322 QCOMPARE(interface->childCount(), 5);
1323 QCOMPARE(interface->role(), QAccessible::PopupMenu);
1325 QAccessibleInterface *iFileNew = interface->child(0);
1326 QAccessibleInterface *iFileOpen = interface->child(1);
1327 QAccessibleInterface *iFileSave = interface->child(2);
1328 QAccessibleInterface *iFileSeparator = interface->child(3);
1329 QAccessibleInterface *iFileExit = interface->child(4);
1331 QCOMPARE(iFileNew->role(), QAccessible::MenuItem);
1332 QCOMPARE(iFileOpen->role(), QAccessible::MenuItem);
1333 QCOMPARE(iFileSave->role(), QAccessible::MenuItem);
1334 QCOMPARE(iFileSeparator->role(), QAccessible::Separator);
1335 QCOMPARE(iFileExit->role(), QAccessible::MenuItem);
1336 QCOMPARE(iFileNew->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
1337 QCOMPARE(iFileOpen->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1338 QCOMPARE(iFileSave->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1339 QCOMPARE(iFileSeparator->actionInterface()->actionNames(), QStringList());
1340 QCOMPARE(iFileExit->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::pressAction());
1342 QAccessibleInterface *iface = 0;
1343 QAccessibleInterface *iface2 = 0;
1345 // traverse siblings with navigate(Sibling, ...)
1346 iface = interface->child(0);
1348 QCOMPARE(iface->role(), QAccessible::MenuItem);
1350 QAccessible::Role fileRoles[5] = {
1351 QAccessible::MenuItem,
1352 QAccessible::MenuItem,
1353 QAccessible::MenuItem,
1354 QAccessible::Separator,
1355 QAccessible::MenuItem
1357 for (int child = 0; child < 5; ++child) {
1358 iface2 = interface->child(child);
1360 QCOMPARE(iface2->role(), fileRoles[child]);
1366 iface = interface->child(0);
1368 QCOMPARE(iface->role(), QAccessible::MenuItem);
1371 iface2 = iface->child(0);
1375 QCOMPARE(iface->role(), QAccessible::PopupMenu);
1377 // "Text file" menu item
1378 iface2 = iface->child(0);
1382 QCOMPARE(iface->role(), QAccessible::MenuItem);
1386 // move mouse pointer away, since that might influence the
1388 QTest::mouseMove(&mw, QPoint(-1, -1));
1391 QTest::qWait(menuFadeDelay);
1393 iFile->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1394 iFileNew->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
1396 QVERIFY(file->isVisible());
1397 QVERIFY(fileNew->isVisible());
1398 QVERIFY(!edit->isVisible());
1399 QVERIFY(!help->isVisible());
1401 QTestAccessibility::clearEvents();
1408 delete iFileSeparator;
1411 // Do not crash if the menu don't have a parent
1412 QMenu *menu = new QMenu;
1413 menu->addAction(QLatin1String("one"));
1414 menu->addAction(QLatin1String("two"));
1415 menu->addAction(QLatin1String("three"));
1416 iface = QAccessible::queryAccessibleInterface(menu);
1417 iface2 = iface->parent();
1419 QCOMPARE(iface2->role(), QAccessible::Application);
1427 QTestAccessibility::clearEvents();
1430 void tst_QAccessibility::spinBoxTest()
1432 QSpinBox * const spinBox = new QSpinBox();
1433 spinBox->setValue(3);
1436 QAccessibleInterface * const interface = QAccessible::queryAccessibleInterface(spinBox);
1438 QCOMPARE(interface->role(), QAccessible::SpinBox);
1440 const QRect widgetRect = spinBox->geometry();
1441 const QRect accessibleRect = interface->rect();
1442 QCOMPARE(accessibleRect, widgetRect);
1443 QCOMPARE(interface->text(QAccessible::Value), QLatin1String("3"));
1445 // one child, the line edit
1446 const int numChildren = interface->childCount();
1447 QCOMPARE(numChildren, 1);
1448 QAccessibleInterface *lineEdit = interface->child(0);
1450 QCOMPARE(lineEdit->role(), QAccessible::EditableText);
1451 QCOMPARE(lineEdit->text(QAccessible::Value), QLatin1String("3"));
1454 QVERIFY(interface->valueInterface());
1455 QCOMPARE(interface->valueInterface()->currentValue().toInt(), 3);
1456 interface->valueInterface()->setCurrentValue(23);
1457 QCOMPARE(interface->valueInterface()->currentValue().toInt(), 23);
1458 QCOMPARE(spinBox->value(), 23);
1460 spinBox->setFocus();
1461 QTestAccessibility::clearEvents();
1462 QTest::keyPress(spinBox, Qt::Key_Up);
1464 EventList events = QTestAccessibility::events();
1465 QTestAccessibilityEvent expectedEvent(spinBox, 0, (int)QAccessible::ValueChanged);
1466 QVERIFY(events.contains(expectedEvent));
1468 QTestAccessibility::clearEvents();
1471 void tst_QAccessibility::doubleSpinBoxTest()
1473 QDoubleSpinBox *doubleSpinBox = new QDoubleSpinBox;
1474 doubleSpinBox->show();
1476 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(doubleSpinBox);
1479 const QRect widgetRect = doubleSpinBox->geometry();
1480 const QRect accessibleRect = interface->rect();
1481 QCOMPARE(accessibleRect, widgetRect);
1483 // Test that we get valid rects for all the spinbox child interfaces.
1484 const int numChildren = interface->childCount();
1485 for (int i = 0; i < numChildren; ++i) {
1486 QAccessibleInterface *childIface = interface->child(i);
1487 const QRect childRect = childIface->rect();
1488 QVERIFY(childRect.isValid());
1492 delete doubleSpinBox;
1493 QTestAccessibility::clearEvents();
1496 void tst_QAccessibility::textEditTest()
1502 QString text = "hello world\nhow are you today?\n";
1506 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&edit);
1507 QCOMPARE(iface->text(QAccessible::Value), text);
1508 QCOMPARE(iface->textInterface()->textAtOffset(8, QAccessible2::WordBoundary, &startOffset, &endOffset), QString("world"));
1509 QCOMPARE(startOffset, 6);
1510 QCOMPARE(endOffset, 11);
1511 QCOMPARE(iface->textInterface()->textAtOffset(14, QAccessible2::LineBoundary, &startOffset, &endOffset), QString("how are you today?"));
1512 QCOMPARE(startOffset, 12);
1513 QCOMPARE(endOffset, 30);
1514 QCOMPARE(iface->textInterface()->characterCount(), 31);
1515 QFontMetrics fm(edit.font());
1516 QCOMPARE(iface->textInterface()->characterRect(0, QAccessible2::RelativeToParent).size(), QSize(fm.width("h"), fm.height()));
1517 QCOMPARE(iface->textInterface()->characterRect(5, QAccessible2::RelativeToParent).size(), QSize(fm.width(" "), fm.height()));
1518 QCOMPARE(iface->textInterface()->characterRect(6, QAccessible2::RelativeToParent).size(), QSize(fm.width("w"), fm.height()));
1520 QTestAccessibility::clearEvents();
1523 void tst_QAccessibility::textBrowserTest()
1526 QTextBrowser textBrowser;
1527 QString text = QLatin1String("Hello world\nhow are you today?\n");
1528 textBrowser.setText(text);
1531 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&textBrowser);
1533 QCOMPARE(iface->role(), QAccessible::StaticText);
1534 QCOMPARE(iface->text(QAccessible::Value), text);
1537 QCOMPARE(iface->textInterface()->textAtOffset(8, QAccessible2::WordBoundary, &startOffset, &endOffset), QString("world"));
1538 QCOMPARE(startOffset, 6);
1539 QCOMPARE(endOffset, 11);
1540 QCOMPARE(iface->textInterface()->textAtOffset(14, QAccessible2::LineBoundary, &startOffset, &endOffset), QString("how are you today?"));
1541 QCOMPARE(startOffset, 12);
1542 QCOMPARE(endOffset, 30);
1543 QCOMPARE(iface->textInterface()->characterCount(), 31);
1545 QTestAccessibility::clearEvents();
1548 void tst_QAccessibility::mdiAreaTest()
1552 mdiArea.resize(400,300);
1554 const int subWindowCount = 3;
1555 for (int i = 0; i < subWindowCount; ++i)
1556 mdiArea.addSubWindow(new QWidget, Qt::Dialog)->show();
1558 QList<QMdiSubWindow *> subWindows = mdiArea.subWindowList();
1559 QCOMPARE(subWindows.count(), subWindowCount);
1561 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&mdiArea);
1563 QCOMPARE(interface->childCount(), subWindowCount);
1566 QTestAccessibility::clearEvents();
1569 void tst_QAccessibility::mdiSubWindowTest()
1574 qApp->setActiveWindow(&mdiArea);
1575 #if defined(Q_OS_UNIX)
1576 QCoreApplication::processEvents();
1580 const int subWindowCount = 5;
1581 for (int i = 0; i < subWindowCount; ++i) {
1582 QMdiSubWindow *window = mdiArea.addSubWindow(new QPushButton("QAccessibilityTest"));
1584 // Parts of this test requires that the sub windows are placed next
1585 // to each other. In order to achieve that QMdiArea must have
1586 // a width which is larger than subWindow->width() * subWindowCount.
1588 int minimumWidth = window->width() * subWindowCount + 20;
1589 mdiArea.resize(mdiArea.size().expandedTo(QSize(minimumWidth, 0)));
1590 #if defined(Q_OS_UNIX)
1591 QCoreApplication::processEvents();
1597 QList<QMdiSubWindow *> subWindows = mdiArea.subWindowList();
1598 QCOMPARE(subWindows.count(), subWindowCount);
1600 QMdiSubWindow *testWindow = subWindows.at(3);
1601 QVERIFY(testWindow);
1602 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(testWindow);
1606 QCOMPARE(interface->childCount(), 1);
1609 QCOMPARE(interface->text(QAccessible::Name), QString());
1610 testWindow->setWindowTitle(QLatin1String("ReplaceMe"));
1611 QCOMPARE(interface->text(QAccessible::Name), QLatin1String("ReplaceMe"));
1612 interface->setText(QAccessible::Name, QLatin1String("TitleSetOnWindow"));
1613 QCOMPARE(interface->text(QAccessible::Name), QLatin1String("TitleSetOnWindow"));
1615 mdiArea.setActiveSubWindow(testWindow);
1618 QAccessible::State state;
1619 state.focusable = true;
1620 state.focused = true;
1621 state.movable = true;
1622 state.sizeable = true;
1624 QCOMPARE(interface->state(), state);
1625 const QRect originalGeometry = testWindow->geometry();
1626 testWindow->showMaximized();
1627 state.sizeable = false;
1628 state.movable = false;
1629 QCOMPARE(interface->state(), state);
1630 testWindow->showNormal();
1631 testWindow->move(-10, 0);
1632 QVERIFY(interface->state().offscreen);
1633 testWindow->setVisible(false);
1634 QVERIFY(interface->state().invisible);
1635 testWindow->setVisible(true);
1636 testWindow->setEnabled(false);
1637 QVERIFY(interface->state().disabled);
1638 testWindow->setEnabled(true);
1639 qApp->setActiveWindow(&mdiArea);
1640 mdiArea.setActiveSubWindow(testWindow);
1641 testWindow->setFocus();
1642 QVERIFY(testWindow->isAncestorOf(qApp->focusWidget()));
1643 QVERIFY(interface->state().focused);
1644 testWindow->setGeometry(originalGeometry);
1647 const QPoint globalPos = testWindow->mapToGlobal(QPoint(0, 0));
1648 QCOMPARE(interface->rect(), QRect(globalPos, testWindow->size()));
1650 QCOMPARE(interface->rect(), QRect());
1651 QCOMPARE(childRect(interface), QRect());
1652 testWindow->showMinimized();
1653 QCOMPARE(childRect(interface), QRect());
1654 testWindow->showNormal();
1655 testWindow->widget()->hide();
1656 QCOMPARE(childRect(interface), QRect());
1657 testWindow->widget()->show();
1658 const QRect widgetGeometry = testWindow->contentsRect();
1659 const QPoint globalWidgetPos = QPoint(globalPos.x() + widgetGeometry.x(),
1660 globalPos.y() + widgetGeometry.y());
1661 QCOMPARE(childRect(interface), QRect(globalWidgetPos, widgetGeometry.size()));
1664 QCOMPARE(interface->childAt(-10, 0), static_cast<QAccessibleInterface*>(0));
1665 QCOMPARE(interface->childAt(globalPos.x(), globalPos.y()), static_cast<QAccessibleInterface*>(0));
1666 QAccessibleInterface *child = interface->childAt(globalWidgetPos.x(), globalWidgetPos.y());
1667 QCOMPARE(child->role(), QAccessible::PushButton);
1668 QCOMPARE(child->text(QAccessible::Name), QString("QAccessibilityTest"));
1670 testWindow->widget()->hide();
1671 QCOMPARE(interface->childAt(globalWidgetPos.x(), globalWidgetPos.y()), static_cast<QAccessibleInterface*>(0));
1674 QTestAccessibility::clearEvents();
1677 void tst_QAccessibility::lineEditTest()
1679 QLineEdit *le = new QLineEdit;
1680 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(le);
1684 QApplication::processEvents();
1685 QCOMPARE(iface->childCount(), 0);
1686 QVERIFY(iface->state().sizeable);
1687 QVERIFY(iface->state().movable);
1688 QCOMPARE(bool(iface->state().focusable), le->isActiveWindow());
1689 QVERIFY(iface->state().selectable);
1690 QVERIFY(iface->state().hasPopup);
1691 QCOMPARE(bool(iface->state().focused), le->hasFocus());
1693 QString secret(QLatin1String("secret"));
1694 le->setText(secret);
1695 le->setEchoMode(QLineEdit::Normal);
1696 QVERIFY(!(iface->state().passwordEdit));
1697 QCOMPARE(iface->text(QAccessible::Value), secret);
1698 le->setEchoMode(QLineEdit::NoEcho);
1699 QVERIFY(iface->state().passwordEdit);
1700 QVERIFY(iface->text(QAccessible::Value).isEmpty());
1701 le->setEchoMode(QLineEdit::Password);
1702 QVERIFY(iface->state().passwordEdit);
1703 QVERIFY(iface->text(QAccessible::Value).isEmpty());
1704 le->setEchoMode(QLineEdit::PasswordEchoOnEdit);
1705 QVERIFY(iface->state().passwordEdit);
1706 QVERIFY(iface->text(QAccessible::Value).isEmpty());
1707 le->setEchoMode(QLineEdit::Normal);
1708 QVERIFY(!(iface->state().passwordEdit));
1709 QCOMPARE(iface->text(QAccessible::Value), secret);
1711 QWidget *toplevel = new QWidget;
1712 le->setParent(toplevel);
1714 QApplication::processEvents();
1715 QVERIFY(!(iface->state().sizeable));
1716 QVERIFY(!(iface->state().movable));
1717 QCOMPARE(bool(iface->state().focusable), le->isActiveWindow());
1718 QVERIFY(iface->state().selectable);
1719 QVERIFY(iface->state().hasPopup);
1720 QCOMPARE(bool(iface->state().focused), le->hasFocus());
1722 QLineEdit *le2 = new QLineEdit(toplevel);
1725 le2->activateWindow();
1727 le->setFocus(Qt::TabFocusReason);
1728 QTestAccessibility::clearEvents();
1729 le2->setFocus(Qt::TabFocusReason);
1730 QTRY_VERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(le2, 0, QAccessible::Focus)));
1732 le->setText(QLatin1String("500"));
1733 le->setValidator(new QIntValidator());
1734 iface->setText(QAccessible::Value, QLatin1String("This text is not a number"));
1735 QCOMPARE(le->text(), QLatin1String("500"));
1740 QTestAccessibility::clearEvents();
1743 QString cite = "I always pass on good advice. It is the only thing to do with it. It is never of any use to oneself. --Oscar Wilde";
1744 QLineEdit *le3 = new QLineEdit(cite, toplevel);
1745 iface = QAccessible::queryAccessibleInterface(le3);
1746 QAccessibleTextInterface* textIface = iface->textInterface();
1748 le3->setCursorPosition(3);
1749 QCOMPARE(textIface->cursorPosition(), 3);
1750 QTRY_VERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(le3, 0, QAccessible::TextCaretMoved)));
1751 QCOMPARE(textIface->selectionCount(), 0);
1752 QTestAccessibility::clearEvents();
1755 QCOMPARE(textIface->text(0, 8), QString::fromLatin1("I always"));
1756 QCOMPARE(textIface->textAtOffset(0, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("I"));
1759 QCOMPARE(textIface->textBeforeOffset(0, QAccessible2::CharBoundary,&start,&end), QString());
1760 QCOMPARE(textIface->textAfterOffset(0, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1(" "));
1764 QCOMPARE(textIface->textAtOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("a"));
1767 QCOMPARE(textIface->textBeforeOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("w"));
1768 QCOMPARE(textIface->textAfterOffset(5, QAccessible2::CharBoundary,&start,&end), QString::fromLatin1("y"));
1770 QCOMPARE(textIface->textAtOffset(5, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("always"));
1774 QCOMPARE(textIface->textAtOffset(2, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("always"));
1775 QCOMPARE(textIface->textAtOffset(7, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("always"));
1776 QCOMPARE(textIface->textAtOffset(8, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1(" "));
1777 QCOMPARE(textIface->textAtOffset(25, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("advice"));
1778 QCOMPARE(textIface->textAtOffset(92, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1("oneself"));
1780 QCOMPARE(textIface->textBeforeOffset(5, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1(" "));
1781 QCOMPARE(textIface->textAfterOffset(5, QAccessible2::WordBoundary,&start,&end), QString::fromLatin1(" "));
1782 QCOMPARE(textIface->textAtOffset(5, QAccessible2::SentenceBoundary,&start,&end), QString::fromLatin1("I always pass on good advice. "));
1786 QCOMPARE(textIface->textBeforeOffset(40, QAccessible2::SentenceBoundary,&start,&end), QString::fromLatin1("I always pass on good advice. "));
1787 QCOMPARE(textIface->textAfterOffset(5, QAccessible2::SentenceBoundary,&start,&end), QString::fromLatin1("It is the only thing to do with it. "));
1789 QCOMPARE(textIface->textAtOffset(5, QAccessible2::ParagraphBoundary,&start,&end), cite);
1791 QCOMPARE(end, cite.length());
1792 QCOMPARE(textIface->textAtOffset(5, QAccessible2::LineBoundary,&start,&end), cite);
1793 QCOMPARE(textIface->textAtOffset(5, QAccessible2::NoBoundary,&start,&end), cite);
1797 QTestAccessibility::clearEvents();
1800 void tst_QAccessibility::workspaceTest()
1803 QWorkspace workspace;
1804 workspace.resize(400,300);
1806 const int subWindowCount = 3;
1807 for (int i = 0; i < subWindowCount; ++i) {
1808 QWidget *window = workspace.addWindow(new QWidget);
1810 window->move(window->x() + 1, window->y());
1812 window->resize(70, window->height());
1815 QWidgetList subWindows = workspace.windowList();
1816 QCOMPARE(subWindows.count(), subWindowCount);
1818 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&workspace);
1820 QCOMPARE(interface->childCount(), subWindowCount);
1823 QTestAccessibility::clearEvents();
1826 bool accessibleInterfaceLeftOf(const QAccessibleInterface *a1, const QAccessibleInterface *a2)
1828 return a1->rect().x() < a2->rect().x();
1831 bool accessibleInterfaceAbove(const QAccessibleInterface *a1, const QAccessibleInterface *a2)
1833 return a1->rect().y() < a2->rect().y();
1836 void tst_QAccessibility::dialogButtonBoxTest()
1839 QDialogButtonBox box(QDialogButtonBox::Reset |
1840 QDialogButtonBox::Help |
1841 QDialogButtonBox::Ok, Qt::Horizontal);
1844 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&box);
1847 #if defined(Q_OS_UNIX)
1848 QCoreApplication::processEvents();
1852 QApplication::processEvents();
1853 QCOMPARE(iface->childCount(), 3);
1854 QCOMPARE(iface->role(), QAccessible::Grouping);
1855 QStringList actualOrder;
1856 QAccessibleInterface *child;
1857 child = iface->child(0);
1858 QCOMPARE(child->role(), QAccessible::PushButton);
1860 QVector<QAccessibleInterface *> buttons;
1861 for (int i = 0; i < iface->childCount(); ++i)
1862 buttons << iface->child(i);
1864 qSort(buttons.begin(), buttons.end(), accessibleInterfaceLeftOf);
1866 for (int i = 0; i < buttons.count(); ++i)
1867 actualOrder << buttons.at(i)->text(QAccessible::Name);
1869 QStringList expectedOrder;
1870 QDialogButtonBox::ButtonLayout btnlout =
1871 QDialogButtonBox::ButtonLayout(QApplication::style()->styleHint(QStyle::SH_DialogButtonLayout));
1873 case QDialogButtonBox::WinLayout:
1874 expectedOrder << QDialogButtonBox::tr("Reset")
1875 << QDialogButtonBox::tr("OK")
1876 << QDialogButtonBox::tr("Help");
1878 case QDialogButtonBox::GnomeLayout:
1879 case QDialogButtonBox::KdeLayout:
1880 case QDialogButtonBox::MacLayout:
1881 expectedOrder << QDialogButtonBox::tr("Help")
1882 << QDialogButtonBox::tr("Reset")
1883 << QDialogButtonBox::tr("OK");
1886 QCOMPARE(actualOrder, expectedOrder);
1888 QApplication::processEvents();
1889 QTestAccessibility::clearEvents();
1893 QDialogButtonBox box(QDialogButtonBox::Reset |
1894 QDialogButtonBox::Help |
1895 QDialogButtonBox::Ok, Qt::Horizontal);
1898 // Test up and down navigation
1899 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&box);
1901 box.setOrientation(Qt::Vertical);
1903 #if defined(Q_OS_UNIX)
1904 QCoreApplication::processEvents();
1908 QApplication::processEvents();
1909 QStringList actualOrder;
1911 QVector<QAccessibleInterface *> buttons;
1912 for (int i = 0; i < iface->childCount(); ++i)
1913 buttons << iface->child(i);
1915 qSort(buttons.begin(), buttons.end(), accessibleInterfaceAbove);
1917 for (int i = 0; i < buttons.count(); ++i)
1918 actualOrder << buttons.at(i)->text(QAccessible::Name);
1920 QStringList expectedOrder;
1921 expectedOrder << QDialogButtonBox::tr("OK")
1922 << QDialogButtonBox::tr("Reset")
1923 << QDialogButtonBox::tr("Help");
1925 QCOMPARE(actualOrder, expectedOrder);
1927 QApplication::processEvents();
1930 QTestAccessibility::clearEvents();
1933 void tst_QAccessibility::dialTest()
1937 dial.setMinimum(23);
1938 dial.setMaximum(121);
1940 QCOMPARE(dial.value(), 42);
1943 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&dial);
1945 QCOMPARE(interface->childCount(), 0);
1947 QCOMPARE(interface->text(QAccessible::Value), QString::number(dial.value()));
1948 QCOMPARE(interface->rect(), dial.geometry());
1950 QAccessibleValueInterface *valueIface = interface->valueInterface();
1951 QVERIFY(valueIface != 0);
1952 QCOMPARE(valueIface->minimumValue().toInt(), dial.minimum());
1953 QCOMPARE(valueIface->maximumValue().toInt(), dial.maximum());
1954 QCOMPARE(valueIface->currentValue().toInt(), 42);
1956 QCOMPARE(valueIface->currentValue().toInt(), dial.value());
1958 QCOMPARE(valueIface->currentValue().toInt(), dial.value());
1960 QCOMPARE(valueIface->currentValue().toInt(), dial.value());
1961 valueIface->setCurrentValue(77);
1962 QCOMPARE(77, dial.value());
1964 QTestAccessibility::clearEvents();
1967 void tst_QAccessibility::rubberBandTest()
1969 QRubberBand rubberBand(QRubberBand::Rectangle);
1970 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&rubberBand);
1972 QCOMPARE(interface->role(), QAccessible::Border);
1974 QTestAccessibility::clearEvents();
1977 void tst_QAccessibility::abstractScrollAreaTest()
1980 QAbstractScrollArea abstractScrollArea;
1982 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&abstractScrollArea);
1984 QVERIFY(!interface->rect().isValid());
1985 QCOMPARE(interface->childAt(200, 200), static_cast<QAccessibleInterface*>(0));
1987 abstractScrollArea.resize(400, 400);
1988 abstractScrollArea.show();
1989 #if defined(Q_OS_UNIX)
1990 QCoreApplication::processEvents();
1993 const QRect globalGeometry = QRect(abstractScrollArea.mapToGlobal(QPoint(0, 0)),
1994 abstractScrollArea.size());
1997 QCOMPARE(interface->childCount(), 1);
1998 QWidget *viewport = abstractScrollArea.viewport();
2000 QVERIFY(verifyChild(viewport, interface, 0, globalGeometry));
2002 // Horizontal scrollBar.
2003 abstractScrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2004 QCOMPARE(interface->childCount(), 2);
2005 QWidget *horizontalScrollBar = abstractScrollArea.horizontalScrollBar();
2006 QWidget *horizontalScrollBarContainer = horizontalScrollBar->parentWidget();
2007 QVERIFY(verifyChild(horizontalScrollBarContainer, interface, 1, globalGeometry));
2009 // Horizontal scrollBar widgets.
2010 QLabel *secondLeftLabel = new QLabel(QLatin1String("L2"));
2011 abstractScrollArea.addScrollBarWidget(secondLeftLabel, Qt::AlignLeft);
2012 QCOMPARE(interface->childCount(), 2);
2014 QLabel *firstLeftLabel = new QLabel(QLatin1String("L1"));
2015 abstractScrollArea.addScrollBarWidget(firstLeftLabel, Qt::AlignLeft);
2016 QCOMPARE(interface->childCount(), 2);
2018 QLabel *secondRightLabel = new QLabel(QLatin1String("R2"));
2019 abstractScrollArea.addScrollBarWidget(secondRightLabel, Qt::AlignRight);
2020 QCOMPARE(interface->childCount(), 2);
2022 QLabel *firstRightLabel = new QLabel(QLatin1String("R1"));
2023 abstractScrollArea.addScrollBarWidget(firstRightLabel, Qt::AlignRight);
2024 QCOMPARE(interface->childCount(), 2);
2026 // Vertical scrollBar.
2027 abstractScrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2028 QCOMPARE(interface->childCount(), 3);
2029 QWidget *verticalScrollBar = abstractScrollArea.verticalScrollBar();
2030 QWidget *verticalScrollBarContainer = verticalScrollBar->parentWidget();
2031 QVERIFY(verifyChild(verticalScrollBarContainer, interface, 2, globalGeometry));
2033 // Vertical scrollBar widgets.
2034 QLabel *secondTopLabel = new QLabel(QLatin1String("T2"));
2035 abstractScrollArea.addScrollBarWidget(secondTopLabel, Qt::AlignTop);
2036 QCOMPARE(interface->childCount(), 3);
2038 QLabel *firstTopLabel = new QLabel(QLatin1String("T1"));
2039 abstractScrollArea.addScrollBarWidget(firstTopLabel, Qt::AlignTop);
2040 QCOMPARE(interface->childCount(), 3);
2042 QLabel *secondBottomLabel = new QLabel(QLatin1String("B2"));
2043 abstractScrollArea.addScrollBarWidget(secondBottomLabel, Qt::AlignBottom);
2044 QCOMPARE(interface->childCount(), 3);
2046 QLabel *firstBottomLabel = new QLabel(QLatin1String("B1"));
2047 abstractScrollArea.addScrollBarWidget(firstBottomLabel, Qt::AlignBottom);
2048 QCOMPARE(interface->childCount(), 3);
2051 abstractScrollArea.setCornerWidget(new QLabel(QLatin1String("C")));
2052 QCOMPARE(interface->childCount(), 4);
2053 QWidget *cornerWidget = abstractScrollArea.cornerWidget();
2054 QVERIFY(verifyChild(cornerWidget, interface, 3, globalGeometry));
2056 QCOMPARE(verifyHierarchy(interface), 0);
2061 QTestAccessibility::clearEvents();
2064 void tst_QAccessibility::scrollAreaTest()
2067 QScrollArea scrollArea;
2069 #if defined(Q_OS_UNIX)
2070 QCoreApplication::processEvents();
2073 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&scrollArea);
2075 QCOMPARE(interface->childCount(), 1); // The viewport.
2078 QTestAccessibility::clearEvents();
2081 void tst_QAccessibility::listTest()
2084 QListWidget *listView = new QListWidget;
2085 listView->addItem("Oslo");
2086 listView->addItem("Berlin");
2087 listView->addItem("Brisbane");
2088 listView->resize(400,400);
2090 QTest::qWait(1); // Need this for indexOfchild to work.
2091 QCoreApplication::processEvents();
2094 QAIPtr iface = QAIPtr(QAccessible::queryAccessibleInterface(listView));
2095 QCOMPARE(verifyHierarchy(iface.data()), 0);
2097 QCOMPARE((int)iface->role(), (int)QAccessible::List);
2098 QCOMPARE(iface->childCount(), 3);
2101 QAIPtr child1 = QAIPtr(iface->child(0));
2103 QCOMPARE(iface->indexOfChild(child1.data()), 0);
2104 QCOMPARE(child1->text(QAccessible::Name), QString("Oslo"));
2105 QCOMPARE(child1->role(), QAccessible::ListItem);
2107 QAIPtr child2 = QAIPtr(iface->child(1));
2109 QCOMPARE(iface->indexOfChild(child2.data()), 1);
2110 QCOMPARE(child2->text(QAccessible::Name), QString("Berlin"));
2112 QAIPtr child3 = QAIPtr(iface->child(2));
2114 QCOMPARE(iface->indexOfChild(child3.data()), 2);
2115 QCOMPARE(child3->text(QAccessible::Name), QString("Brisbane"));
2117 QTestAccessibility::clearEvents();
2120 QTest::mouseClick(listView->viewport(), Qt::LeftButton, 0, listView->visualItemRect(listView->item(1)).center());
2121 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 2, QAccessible::Selection)));
2122 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 2, QAccessible::Focus)));
2123 QTest::mouseClick(listView->viewport(), Qt::LeftButton, 0, listView->visualItemRect(listView->item(2)).center());
2124 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 3, QAccessible::Selection)));
2125 QVERIFY(QTestAccessibility::events().contains(QTestAccessibilityEvent(listView, 3, QAccessible::Focus)));
2127 listView->addItem("Munich");
2128 QCOMPARE(iface->childCount(), 4);
2131 QAccessibleTableInterface *table2 = iface->tableInterface();
2133 QCOMPARE(table2->columnCount(), 1);
2134 QCOMPARE(table2->rowCount(), 4);
2135 QAIPtr cell1 = QAIPtr(table2->cellAt(0,0));
2137 QCOMPARE(cell1->text(QAccessible::Name), QString("Oslo"));
2139 QAIPtr cell4 = QAIPtr(table2->cellAt(3,0));
2141 QCOMPARE(cell4->text(QAccessible::Name), QString("Munich"));
2142 QCOMPARE(cell4->role(), QAccessible::ListItem);
2144 QAccessibleTableCellInterface *cellInterface = cell4->tableCellInterface();
2145 QVERIFY(cellInterface);
2146 QCOMPARE(cellInterface->rowIndex(), 3);
2147 QCOMPARE(cellInterface->columnIndex(), 0);
2148 QCOMPARE(cellInterface->rowExtent(), 1);
2149 QCOMPARE(cellInterface->columnExtent(), 1);
2150 QCOMPARE(cellInterface->rowHeaderCells(), QList<QAccessibleInterface*>());
2151 QCOMPARE(cellInterface->columnHeaderCells(), QList<QAccessibleInterface*>());
2153 QCOMPARE(QAIPtr(cellInterface->table())->object(), listView);
2155 listView->clearSelection();
2156 QVERIFY(!(cell4->state().expandable));
2157 QVERIFY( (cell4->state().selectable));
2158 QVERIFY(!(cell4->state().selected));
2159 table2->selectRow(3);
2160 QCOMPARE(listView->selectedItems().size(), 1);
2161 QCOMPARE(listView->selectedItems().at(0)->text(), QLatin1String("Munich"));
2162 QVERIFY(cell4->state().selected);
2163 QVERIFY(cellInterface->isSelected());
2165 QVERIFY(table2->cellAt(-1, 0) == 0);
2166 QVERIFY(table2->cellAt(0, -1) == 0);
2167 QVERIFY(table2->cellAt(0, 1) == 0);
2168 QVERIFY(table2->cellAt(4, 0) == 0);
2172 QTestAccessibility::clearEvents();
2175 void tst_QAccessibility::treeTest()
2177 QTreeWidget *treeView = new QTreeWidget;
2178 treeView->setColumnCount(2);
2179 QTreeWidgetItem *header = new QTreeWidgetItem;
2180 header->setText(0, "Artist");
2181 header->setText(1, "Work");
2182 treeView->setHeaderItem(header);
2184 QTreeWidgetItem *root1 = new QTreeWidgetItem;
2185 root1->setText(0, "Spain");
2186 treeView->addTopLevelItem(root1);
2188 QTreeWidgetItem *item1 = new QTreeWidgetItem;
2189 item1->setText(0, "Picasso");
2190 item1->setText(1, "Guernica");
2191 root1->addChild(item1);
2193 QTreeWidgetItem *item2 = new QTreeWidgetItem;
2194 item2->setText(0, "Tapies");
2195 item2->setText(1, "Ambrosia");
2196 root1->addChild(item2);
2198 QTreeWidgetItem *root2 = new QTreeWidgetItem;
2199 root2->setText(0, "Austria");
2200 treeView->addTopLevelItem(root2);
2202 QTreeWidgetItem *item3 = new QTreeWidgetItem;
2203 item3->setText(0, "Klimt");
2204 item3->setText(1, "The Kiss");
2205 root2->addChild(item3);
2207 treeView->resize(400,400);
2210 QCoreApplication::processEvents();
2213 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(treeView);
2214 QCOMPARE(verifyHierarchy(iface), 0);
2216 QCOMPARE((int)iface->role(), (int)QAccessible::Tree);
2217 // header and 2 rows (the others are not expanded, thus not visible)
2218 QCOMPARE(iface->childCount(), 6);
2220 QAccessibleInterface *header1 = 0;
2221 header1 = iface->child(0);
2223 QCOMPARE(iface->indexOfChild(header1), 0);
2224 QCOMPARE(header1->text(QAccessible::Name), QString("Artist"));
2225 QCOMPARE(header1->role(), QAccessible::ColumnHeader);
2228 QAccessibleInterface *child1 = 0;
2229 child1 = iface->child(2);
2231 QCOMPARE(iface->indexOfChild(child1), 2);
2232 QCOMPARE(child1->text(QAccessible::Name), QString("Spain"));
2233 QCOMPARE(child1->role(), QAccessible::TreeItem);
2234 QVERIFY(!(child1->state().expanded));
2237 QAccessibleInterface *child2 = 0;
2238 child2 = iface->child(4);
2240 QCOMPARE(iface->indexOfChild(child2), 4);
2241 QCOMPARE(child2->text(QAccessible::Name), QString("Austria"));
2244 QTestAccessibility::clearEvents();
2247 QAccessibleTableInterface *table2 = iface->tableInterface();
2249 QCOMPARE(table2->columnCount(), 2);
2250 QCOMPARE(table2->rowCount(), 2);
2251 QAccessibleInterface *cell1;
2252 QVERIFY(cell1 = table2->cellAt(0,0));
2253 QCOMPARE(cell1->text(QAccessible::Name), QString("Spain"));
2254 QAccessibleInterface *cell2;
2255 QVERIFY(cell2 = table2->cellAt(1,0));
2256 QCOMPARE(cell2->text(QAccessible::Name), QString("Austria"));
2257 QCOMPARE(cell2->role(), QAccessible::TreeItem);
2258 QCOMPARE(cell2->tableCellInterface()->rowIndex(), 1);
2259 QCOMPARE(cell2->tableCellInterface()->columnIndex(), 0);
2260 QVERIFY(cell2->state().expandable);
2261 QCOMPARE(iface->indexOfChild(cell2), 4);
2262 QVERIFY(!(cell2->state().expanded));
2263 QCOMPARE(table2->columnDescription(1), QString("Work"));
2267 treeView->expandAll();
2269 // Need this for indexOfchild to work.
2270 QCoreApplication::processEvents();
2273 QCOMPARE(table2->columnCount(), 2);
2274 QCOMPARE(table2->rowCount(), 5);
2275 cell1 = table2->cellAt(1,0);
2276 QCOMPARE(cell1->text(QAccessible::Name), QString("Picasso"));
2277 QCOMPARE(iface->indexOfChild(cell1), 4); // 2 header + 2 for root item
2279 cell2 = table2->cellAt(4,0);
2280 QCOMPARE(cell2->text(QAccessible::Name), QString("Klimt"));
2281 QCOMPARE(cell2->role(), QAccessible::TreeItem);
2282 QCOMPARE(cell2->tableCellInterface()->rowIndex(), 4);
2283 QCOMPARE(cell2->tableCellInterface()->columnIndex(), 0);
2284 QVERIFY(!(cell2->state().expandable));
2285 QCOMPARE(iface->indexOfChild(cell2), 10);
2287 QCOMPARE(table2->columnDescription(0), QString("Artist"));
2288 QCOMPARE(table2->columnDescription(1), QString("Work"));
2291 QTestAccessibility::clearEvents();
2294 void tst_QAccessibility::tableTest()
2296 QTableWidget *tableView = new QTableWidget(3, 3);
2297 tableView->setColumnCount(3);
2298 QStringList hHeader;
2299 hHeader << "h1" << "h2" << "h3";
2300 tableView->setHorizontalHeaderLabels(hHeader);
2302 QStringList vHeader;
2303 vHeader << "v1" << "v2" << "v3";
2304 tableView->setVerticalHeaderLabels(vHeader);
2306 for (int i = 0; i<9; ++i) {
2307 QTableWidgetItem *item = new QTableWidgetItem;
2308 item->setText(QString::number(i/3) + QString(".") + QString::number(i%3));
2309 tableView->setItem(i/3, i%3, item);
2312 tableView->resize(600,600);
2314 QTest::qWait(1); // Need this for indexOfchild to work.
2315 QCoreApplication::processEvents();
2318 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(tableView);
2319 QCOMPARE(verifyHierarchy(iface), 0);
2321 QCOMPARE((int)iface->role(), (int)QAccessible::Table);
2322 // header and 2 rows (the others are not expanded, thus not visible)
2323 QCOMPARE(iface->childCount(), 9+3+3+1); // cell+headers+topleft button
2325 QAccessibleInterface *cornerButton = iface->child(0);
2326 QVERIFY(cornerButton);
2327 QCOMPARE(iface->indexOfChild(cornerButton), 0);
2328 QCOMPARE(cornerButton->role(), QAccessible::Pane);
2329 delete cornerButton;
2331 QAccessibleInterface *child1 = iface->child(2);
2333 QCOMPARE(iface->indexOfChild(child1), 2);
2334 QCOMPARE(child1->text(QAccessible::Name), QString("h2"));
2335 QCOMPARE(child1->role(), QAccessible::ColumnHeader);
2336 QVERIFY(!(child1->state().expanded));
2339 QAccessibleInterface *child2 = iface->child(10);
2341 QCOMPARE(iface->indexOfChild(child2), 10);
2342 QCOMPARE(child2->text(QAccessible::Name), QString("1.1"));
2343 QAccessibleTableCellInterface *cell2Iface = child2->tableCellInterface();
2344 QCOMPARE(cell2Iface->rowIndex(), 1);
2345 QCOMPARE(cell2Iface->columnIndex(), 1);
2348 QAccessibleInterface *child3 = iface->child(11);
2349 QCOMPARE(iface->indexOfChild(child3), 11);
2350 QCOMPARE(child3->text(QAccessible::Name), QString("1.2"));
2353 QTestAccessibility::clearEvents();
2356 QAccessibleTableInterface *table2 = iface->tableInterface();
2358 QCOMPARE(table2->columnCount(), 3);
2359 QCOMPARE(table2->rowCount(), 3);
2360 QAccessibleInterface *cell1;
2361 QVERIFY(cell1 = table2->cellAt(0,0));
2362 QCOMPARE(cell1->text(QAccessible::Name), QString("0.0"));
2363 QCOMPARE(iface->indexOfChild(cell1), 5);
2365 QAccessibleInterface *cell2;
2366 QVERIFY(cell2 = table2->cellAt(0,1));
2367 QCOMPARE(cell2->text(QAccessible::Name), QString("0.1"));
2368 QCOMPARE(cell2->role(), QAccessible::Cell);
2369 QCOMPARE(cell2->tableCellInterface()->rowIndex(), 0);
2370 QCOMPARE(cell2->tableCellInterface()->columnIndex(), 1);
2371 QCOMPARE(iface->indexOfChild(cell2), 6);
2374 QAccessibleInterface *cell3;
2375 QVERIFY(cell3 = table2->cellAt(1,2));
2376 QCOMPARE(cell3->text(QAccessible::Name), QString("1.2"));
2377 QCOMPARE(cell3->role(), QAccessible::Cell);
2378 QCOMPARE(cell3->tableCellInterface()->rowIndex(), 1);
2379 QCOMPARE(cell3->tableCellInterface()->columnIndex(), 2);
2380 QCOMPARE(iface->indexOfChild(cell3), 11);
2383 QCOMPARE(table2->columnDescription(0), QString("h1"));
2384 QCOMPARE(table2->columnDescription(1), QString("h2"));
2385 QCOMPARE(table2->columnDescription(2), QString("h3"));
2386 QCOMPARE(table2->rowDescription(0), QString("v1"));
2387 QCOMPARE(table2->rowDescription(1), QString("v2"));
2388 QCOMPARE(table2->rowDescription(2), QString("v3"));
2394 QTestAccessibility::clearEvents();
2397 void tst_QAccessibility::calendarWidgetTest()
2399 #ifndef QT_NO_CALENDARWIDGET
2401 QCalendarWidget calendarWidget;
2403 QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&calendarWidget);
2405 QCOMPARE(interface->role(), QAccessible::Table);
2406 QVERIFY(!interface->rect().isValid());
2407 QCOMPARE(interface->childAt(200, 200), static_cast<QAccessibleInterface*>(0));
2409 calendarWidget.resize(400, 300);
2410 calendarWidget.show();
2411 #if defined(Q_OS_UNIX)
2412 QCoreApplication::processEvents();
2416 // 1 = navigationBar, 2 = view.
2417 QCOMPARE(interface->childCount(), 2);
2419 const QRect globalGeometry = QRect(calendarWidget.mapToGlobal(QPoint(0, 0)),
2420 calendarWidget.size());
2421 QCOMPARE(interface->rect(), globalGeometry);
2423 QWidget *navigationBar = 0;
2424 foreach (QObject *child, calendarWidget.children()) {
2425 if (child->objectName() == QLatin1String("qt_calendar_navigationbar")) {
2426 navigationBar = static_cast<QWidget *>(child);
2430 QVERIFY(navigationBar);
2431 QVERIFY(verifyChild(navigationBar, interface, 0, globalGeometry));
2433 QAbstractItemView *calendarView = 0;
2434 foreach (QObject *child, calendarWidget.children()) {
2435 if (child->objectName() == QLatin1String("qt_calendar_calendarview")) {
2436 calendarView = static_cast<QAbstractItemView *>(child);
2440 QVERIFY(calendarView);
2441 QVERIFY(verifyChild(calendarView, interface, 1, globalGeometry));
2443 // Hide navigation bar.
2444 calendarWidget.setNavigationBarVisible(false);
2445 QCOMPARE(interface->childCount(), 1);
2446 QVERIFY(!navigationBar->isVisible());
2448 QVERIFY(verifyChild(calendarView, interface, 0, globalGeometry));
2450 // Show navigation bar.
2451 calendarWidget.setNavigationBarVisible(true);
2452 QCOMPARE(interface->childCount(), 2);
2453 QVERIFY(navigationBar->isVisible());
2455 // Navigate to the navigation bar via Child.
2456 QAccessibleInterface *navigationBarInterface = interface->child(0);
2457 QVERIFY(navigationBarInterface);
2458 QCOMPARE(navigationBarInterface->object(), (QObject*)navigationBar);
2460 // Navigate to the view via Child.
2461 QAccessibleInterface *calendarViewInterface = interface->child(1);
2462 QVERIFY(calendarViewInterface);
2463 QCOMPARE(calendarViewInterface->object(), (QObject*)calendarView);
2465 QVERIFY(!interface->child(-1));
2467 // In order for geometric navigation to work they must share the same parent
2468 QCOMPARE(navigationBarInterface->parent()->object(), calendarViewInterface->parent()->object());
2469 QVERIFY(navigationBarInterface->rect().bottom() < calendarViewInterface->rect().top());
2470 delete calendarViewInterface;
2471 calendarViewInterface = 0;
2472 delete navigationBarInterface;
2473 navigationBarInterface = 0;
2476 QTestAccessibility::clearEvents();
2477 #endif // QT_NO_CALENDARWIDGET
2480 void tst_QAccessibility::dockWidgetTest()
2482 #ifndef QT_NO_DOCKWIDGET
2483 // Set up a proper main window with two dock widgets
2484 QMainWindow *mw = new QMainWindow();
2485 QFrame *central = new QFrame(mw);
2486 mw->setCentralWidget(central);
2487 QMenuBar *mb = new QMenuBar(mw);
2488 mb->addAction(tr("&File"));
2491 QDockWidget *dock1 = new QDockWidget(mw);
2492 mw->addDockWidget(Qt::LeftDockWidgetArea, dock1);
2493 QPushButton *pb1 = new QPushButton(tr("Push me"), dock1);
2494 dock1->setWidget(pb1);
2496 QDockWidget *dock2 = new QDockWidget(mw);
2497 mw->addDockWidget(Qt::BottomDockWidgetArea, dock2);
2498 QPushButton *pb2 = new QPushButton(tr("Push me"), dock2);
2499 dock2->setWidget(pb2);
2501 mw->resize(600,400);
2503 #if defined(Q_OS_UNIX)
2504 QCoreApplication::processEvents();
2508 QAccessibleInterface *accMainWindow = QAccessible::queryAccessibleInterface(mw);
2509 // 4 children: menu bar, dock1, dock2, and central widget
2510 QCOMPARE(accMainWindow->childCount(), 4);
2511 QAccessibleInterface *accDock1 = 0;
2512 for (int i = 0; i < 4; ++i) {
2513 accDock1 = accMainWindow->child(i);
2514 if (accMainWindow->role() == QAccessible::Window) {
2515 if (accDock1 && qobject_cast<QDockWidget*>(accDock1->object()) == dock1) {
2523 QCOMPARE(accDock1->role(), QAccessible::Window);
2525 QAccessibleInterface *dock1TitleBar = accDock1->child(0);
2526 QCOMPARE(dock1TitleBar->role(), QAccessible::TitleBar);
2527 QVERIFY(accDock1->rect().contains(dock1TitleBar->rect()));
2528 delete dock1TitleBar;
2530 QPoint globalPos = dock1->mapToGlobal(QPoint(0,0));
2531 globalPos.rx()+=5; //### query style
2533 QAccessibleInterface *childAt = accDock1->childAt(globalPos.x(), globalPos.y()); //###
2534 QCOMPARE(childAt->role(), QAccessible::TitleBar);
2535 int index = accDock1->indexOfChild(childAt);
2537 QAccessibleInterface *accTitleBar = accDock1->child(index);
2539 QCOMPARE(accTitleBar->role(), QAccessible::TitleBar);
2540 QCOMPARE(accDock1->indexOfChild(accTitleBar), 0);
2541 QAccessibleInterface *acc;
2542 acc = accTitleBar->parent();
2544 QCOMPARE(acc->role(), QAccessible::Window);
2554 QTestAccessibility::clearEvents();
2555 #endif // QT_NO_DOCKWIDGET
2558 void tst_QAccessibility::comboBoxTest()
2560 #if defined(Q_OS_WINCE)
2561 if (!IsValidCEPlatform())
2562 QSKIP("Test skipped on Windows Mobile test hardware");
2564 { // not editable combobox
2566 combo.addItems(QStringList() << "one" << "two" << "three");
2568 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&combo);
2569 QCOMPARE(verifyHierarchy(iface), 0);
2571 QCOMPARE(iface->role(), QAccessible::ComboBox);
2572 QCOMPARE(iface->childCount(), 1);
2575 QCOMPARE(iface->text(QAccessible::Name), QLatin1String("one"));
2577 QCOMPARE(iface->text(QAccessible::Value), QLatin1String("one"));
2578 combo.setCurrentIndex(2);
2580 QCOMPARE(iface->text(QAccessible::Name), QLatin1String("three"));
2582 QCOMPARE(iface->text(QAccessible::Value), QLatin1String("three"));
2584 QAccessibleInterface *listIface = iface->child(0);
2585 QCOMPARE(listIface->role(), QAccessible::List);
2586 QCOMPARE(listIface->childCount(), 3);
2588 QVERIFY(!combo.view()->isVisible());
2589 QVERIFY(iface->actionInterface());
2590 QCOMPARE(iface->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction());
2591 iface->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
2592 QVERIFY(combo.view()->isVisible());
2597 { // editable combobox
2598 QComboBox editableCombo;
2599 editableCombo.show();
2600 editableCombo.setEditable(true);
2601 editableCombo.addItems(QStringList() << "foo" << "bar" << "baz");
2603 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&editableCombo);
2604 QCOMPARE(verifyHierarchy(iface), 0);
2606 QCOMPARE(iface->role(), QAccessible::ComboBox);
2607 QCOMPARE(iface->childCount(), 2);
2609 QAccessibleInterface *listIface = iface->child(0);
2610 QCOMPARE(listIface->role(), QAccessible::List);
2611 QAccessibleInterface *editIface = iface->child(1);
2612 QCOMPARE(editIface->role(), QAccessible::EditableText);
2619 QTestAccessibility::clearEvents();
2622 void tst_QAccessibility::labelTest()
2624 QString text = "Hello World";
2625 QLabel *label = new QLabel(text);
2628 #if defined(Q_OS_UNIX)
2629 QCoreApplication::processEvents();
2633 QAccessibleInterface *acc_label = QAccessible::queryAccessibleInterface(label);
2636 QCOMPARE(acc_label->text(QAccessible::Name), text);
2640 QTestAccessibility::clearEvents();
2642 QPixmap testPixmap(50, 50);
2646 imageLabel.setPixmap(testPixmap);
2647 imageLabel.setToolTip("Test Description");
2649 acc_label = QAccessible::queryAccessibleInterface(&imageLabel);
2652 QAccessibleImageInterface *imageInterface = acc_label->imageInterface();
2653 QVERIFY(imageInterface);
2655 QCOMPARE(imageInterface->imageSize(), testPixmap.size());
2656 QCOMPARE(imageInterface->imageDescription(), QString::fromLatin1("Test Description"));
2657 QCOMPARE(imageInterface->imagePosition(QAccessible2::RelativeToParent), imageLabel.geometry());
2661 QTestAccessibility::clearEvents();
2664 void tst_QAccessibility::accelerators()
2666 QWidget *window = new QWidget;
2667 QHBoxLayout *lay = new QHBoxLayout(window);
2668 QLabel *label = new QLabel(tr("&Line edit"), window);
2669 QLineEdit *le = new QLineEdit(window);
2670 lay->addWidget(label);
2672 label->setBuddy(le);
2676 QAccessibleInterface *accLineEdit = QAccessible::queryAccessibleInterface(le);
2677 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("L"));
2678 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("L"));
2679 label->setText(tr("Q &"));
2680 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2681 label->setText(tr("Q &&"));
2682 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2683 label->setText(tr("Q && A"));
2684 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2685 label->setText(tr("Q &&&A"));
2686 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("A"));
2687 label->setText(tr("Q &&A"));
2688 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QString());
2690 #if !defined(QT_NO_DEBUG) && !defined(Q_OS_MAC)
2691 QTest::ignoreMessage(QtWarningMsg, "QKeySequence::mnemonic: \"Q &A&B\" contains multiple occurrences of '&'");
2693 label->setText(tr("Q &A&B"));
2694 QCOMPARE(accLineEdit->text(QAccessible::Accelerator), QKeySequence(Qt::ALT).toString(QKeySequence::NativeText) + QLatin1String("A"));
2696 #if defined(Q_OS_UNIX)
2697 QCoreApplication::processEvents();
2701 QTestAccessibility::clearEvents();
2704 QTEST_MAIN(tst_QAccessibility)
2705 #include "tst_qaccessibility.moc"