Accessibility Mac: Cache Accessible Elements and Notify about changes

The big change is that we now keep the id objects representing accessibles
around so that they are persistent for ATs.
This improves performance of Mac accessibility significantly.

This is required for notifications which are now sent so that many things work much better,
for example the VoiceOver focus follows the keyboard focus.

The parent element in QCocoaAccessibleElement was removed, we can
dynamically access it more reliably.

Change-Id: I686d212f40d28b392dcc22f16f3c3430f08bdc98
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
This commit is contained in:
Frederik Gladhorn 2014-03-20 16:38:36 +01:00 committed by The Qt Project
parent 7537a4605a
commit ccdfe354a6
14 changed files with 342 additions and 73 deletions

View File

@ -16,4 +16,6 @@ contains(QT_CONFIG, accessibility) {
HEADERS += accessible/qaccessiblebridge.h
SOURCES += accessible/qaccessiblebridge.cpp
OBJECTIVE_SOURCES += accessible/qaccessiblecache_mac.mm
}

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
@ -591,8 +591,6 @@ QAccessible::RootObjectHandler QAccessible::installRootObjectHandler(RootObjectH
return old;
}
Q_GLOBAL_STATIC(QAccessibleCache, qAccessibleCache)
/*!
If a QAccessibleInterface implementation exists for the given \a object,
this function returns a pointer to the implementation; otherwise it
@ -616,8 +614,8 @@ QAccessibleInterface *QAccessible::queryAccessibleInterface(QObject *object)
if (!object)
return 0;
if (Id id = qAccessibleCache->objectToId.value(object))
return qAccessibleCache->interfaceForId(id);
if (Id id = QAccessibleCache::instance()->objectToId.value(object))
return QAccessibleCache::instance()->interfaceForId(id);
// Create a QAccessibleInterface for the object class. Start by the most
// derived class and walk up the class hierarchy.
@ -629,8 +627,8 @@ QAccessibleInterface *QAccessible::queryAccessibleInterface(QObject *object)
for (int i = qAccessibleFactories()->count(); i > 0; --i) {
InterfaceFactory factory = qAccessibleFactories()->at(i - 1);
if (QAccessibleInterface *iface = factory(cn, object)) {
qAccessibleCache->insert(object, iface);
Q_ASSERT(qAccessibleCache->objectToId.contains(object));
QAccessibleCache::instance()->insert(object, iface);
Q_ASSERT(QAccessibleCache::instance()->objectToId.contains(object));
return iface;
}
}
@ -652,8 +650,8 @@ QAccessibleInterface *QAccessible::queryAccessibleInterface(QObject *object)
if (factory) {
QAccessibleInterface *result = factory->create(cn, object);
if (result) { // Need this condition because of QDesktopScreenWidget
qAccessibleCache->insert(object, result);
Q_ASSERT(qAccessibleCache->objectToId.contains(object));
QAccessibleCache::instance()->insert(object, result);
Q_ASSERT(QAccessibleCache::instance()->objectToId.contains(object));
}
return result;
}
@ -665,8 +663,8 @@ QAccessibleInterface *QAccessible::queryAccessibleInterface(QObject *object)
#ifndef QT_NO_ACCESSIBILITY
if (object == qApp) {
QAccessibleInterface *appInterface = new QAccessibleApplication;
qAccessibleCache->insert(object, appInterface);
Q_ASSERT(qAccessibleCache->objectToId.contains(qApp));
QAccessibleCache::instance()->insert(object, appInterface);
Q_ASSERT(QAccessibleCache::instance()->objectToId.contains(qApp));
return appInterface;
}
#endif
@ -691,7 +689,7 @@ QAccessibleInterface *QAccessible::queryAccessibleInterface(QObject *object)
QAccessible::Id QAccessible::registerAccessibleInterface(QAccessibleInterface *iface)
{
Q_ASSERT(iface);
return qAccessibleCache->insert(iface->object(), iface);
return QAccessibleCache::instance()->insert(iface->object(), iface);
}
/*!
@ -701,7 +699,7 @@ QAccessible::Id QAccessible::registerAccessibleInterface(QAccessibleInterface *i
*/
void QAccessible::deleteAccessibleInterface(Id id)
{
qAccessibleCache->deleteInterface(id);
QAccessibleCache::instance()->deleteInterface(id);
}
/*!
@ -709,7 +707,7 @@ void QAccessible::deleteAccessibleInterface(Id id)
*/
QAccessible::Id QAccessible::uniqueId(QAccessibleInterface *iface)
{
Id id = qAccessibleCache->idToInterface.key(iface);
Id id = QAccessibleCache::instance()->idToInterface.key(iface);
if (!id)
id = registerAccessibleInterface(iface);
return id;
@ -722,7 +720,7 @@ QAccessible::Id QAccessible::uniqueId(QAccessibleInterface *iface)
*/
QAccessibleInterface *QAccessible::accessibleInterface(Id id)
{
return qAccessibleCache->idToInterface.value(id);
return QAccessibleCache::instance()->idToInterface.value(id);
}

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
@ -49,6 +49,13 @@ QT_BEGIN_NAMESPACE
\brief Maintains a cache of accessible interfaces.
*/
Q_GLOBAL_STATIC(QAccessibleCache, qAccessibleCache)
QAccessibleCache *QAccessibleCache::instance()
{
return qAccessibleCache;
}
/*
The ID is always in the range [INT_MAX+1, UINT_MAX].
This makes it easy on windows to reserve the positive integer range
@ -113,6 +120,10 @@ void QAccessibleCache::deleteInterface(QAccessible::Id id, QObject *obj)
if (obj)
objectToId.remove(obj);
delete iface;
#ifdef Q_OS_MACX
removeCocoaElement(id);
#endif
}
QT_END_NAMESPACE

View File

@ -0,0 +1,67 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qaccessiblecache_p.h"
#ifdef Q_OS_OSX
QT_BEGIN_NAMESPACE
void QAccessibleCache::insertElement(QAccessible::Id axid, QCocoaAccessibleElement *element) const
{
cocoaElements[axid] = element;
}
void QAccessibleCache::removeCocoaElement(QAccessible::Id axid)
{
QCocoaAccessibleElement *element = elementForId(axid);
[element invalidate];
cocoaElements.remove(axid);
}
QCocoaAccessibleElement *QAccessibleCache::elementForId(QAccessible::Id axid) const
{
return cocoaElements.value(axid);
}
QT_END_NAMESPACE
#endif

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
@ -42,24 +42,42 @@
#ifndef QACCESSIBLECACHE_P
#define QACCESSIBLECACHE_P
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/qglobal.h>
#include <QtCore/qobject.h>
#include <QtCore/qhash.h>
#include "qaccessible.h"
Q_FORWARD_DECLARE_OBJC_CLASS(QCocoaAccessibleElement);
QT_BEGIN_NAMESPACE
class QAccessibleCache :public QObject
class Q_GUI_EXPORT QAccessibleCache :public QObject
{
Q_OBJECT
public:
static QAccessibleCache *instance();
QAccessibleInterface *interfaceForId(QAccessible::Id id) const;
QAccessible::Id insert(QObject *object, QAccessibleInterface *iface) const;
void deleteInterface(QAccessible::Id id, QObject *obj = 0);
#ifdef Q_OS_OSX
QCocoaAccessibleElement *elementForId(QAccessible::Id axid) const;
void insertElement(QAccessible::Id axid, QCocoaAccessibleElement *element) const;
#endif
private Q_SLOTS:
void objectDestroyed(QObject *obj);
@ -69,6 +87,11 @@ private:
mutable QHash<QAccessible::Id, QAccessibleInterface *> idToInterface;
mutable QHash<QObject *, QAccessible::Id> objectToId;
#ifdef Q_OS_OSX
void removeCocoaElement(QAccessible::Id axid);
mutable QHash<QAccessible::Id, QCocoaAccessibleElement *> cocoaElements;
#endif
friend class QAccessible;
friend class QAccessibleInterface;
};

View File

@ -79,7 +79,7 @@ namespace QCocoaAccessible {
NSString *macRole(QAccessibleInterface *interface);
bool shouldBeIgnored(QAccessibleInterface *interface);
NSArray *unignoredChildren(id parentObject, QAccessibleInterface *interface);
NSArray *unignoredChildren(QAccessibleInterface *interface);
NSString *getTranslatedAction(const QString &qtAction);
NSMutableArray *createTranslatedActionsList(const QStringList &qtActions);
QString translateAction(NSString *nsAction);

View File

@ -55,19 +55,31 @@ QCocoaAccessibility::~QCocoaAccessibility()
void QCocoaAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
{
QAccessible::Id interfaceId = event->uniqueId();
if (!interfaceId)
QCocoaAccessibleElement *element = [QCocoaAccessibleElement elementWithId: event->uniqueId()];
if (!element) {
qWarning() << "QCocoaAccessibility::notifyAccessibilityUpdate: invalid element";
return;
}
switch (event->type()) {
case QAccessible::ValueChanged:
case QAccessible::TextInserted :
case QAccessible::TextRemoved :
case QAccessible::TextUpdated : {
QCocoaAccessibleElement *element = [QCocoaAccessibleElement createElementWithId : interfaceId parent : nil];
[element autorelease];
NSAccessibilityPostNotification(element, NSAccessibilityValueChangedNotification);
break; }
case QAccessible::Focus: {
NSAccessibilityPostNotification(element, NSAccessibilityFocusedUIElementChangedNotification);
break;
}
case QAccessible::StateChanged:
case QAccessible::ValueChanged:
case QAccessible::TextInserted:
case QAccessible::TextRemoved:
case QAccessible::TextUpdated:
NSAccessibilityPostNotification(element, NSAccessibilityValueChangedNotification);
break;
case QAccessible::TextCaretMoved:
case QAccessible::TextSelectionChanged:
NSAccessibilityPostNotification(element, NSAccessibilitySelectedTextChangedNotification);
break;
case QAccessible::NameChanged:
NSAccessibilityPostNotification(element, NSAccessibilityTitleChangedNotification);
break;
default:
break;
}
@ -218,7 +230,7 @@ bool shouldBeIgnored(QAccessibleInterface *interface)
return false;
}
NSArray *unignoredChildren(id parentObject, QAccessibleInterface *interface)
NSArray *unignoredChildren(QAccessibleInterface *interface)
{
int numKids = interface->childCount();
// qDebug() << "Children for: " << axid << iface << " are: " << numKids;
@ -231,9 +243,12 @@ NSArray *unignoredChildren(id parentObject, QAccessibleInterface *interface)
QAccessible::Id childId = QAccessible::uniqueId(child);
//qDebug() << " kid: " << childId << child;
QCocoaAccessibleElement *element = [QCocoaAccessibleElement createElementWithId:childId parent:parentObject];
[kids addObject: element];
[element release];
QCocoaAccessibleElement *element = [QCocoaAccessibleElement elementWithId: childId];
if (element)
[kids addObject: element];
else
qWarning() << "QCocoaAccessibility: invalid child";
}
return NSAccessibilityUnignoredChildren(kids);
}

View File

@ -52,12 +52,11 @@
@interface QCocoaAccessibleElement : NSObject {
NSString *role;
NSObject *parent;
QAccessible::Id axid;
}
- (id)initWithId:(QAccessible::Id)anId parent:(id)aParent;
+ (QCocoaAccessibleElement *)createElementWithId:(QAccessible::Id)anId parent:(id)aParent;
- (id)initWithId:(QAccessible::Id)anId;
+ (QCocoaAccessibleElement *)elementWithId:(QAccessible::Id)anId;
@end

View File

@ -41,6 +41,8 @@
#include "qcocoaaccessibilityelement.h"
#include "qcocoaaccessibility.h"
#include "qcocoahelpers.h"
#include "qcocoawindow.h"
#include "private/qaccessiblecache_p.h"
#include <QtGui/qaccessible.h>
@ -48,7 +50,7 @@
@implementation QCocoaAccessibleElement
- (id)initWithId:(QAccessible::Id)anId parent:(id)aParent
- (id)initWithId:(QAccessible::Id)anId
{
Q_ASSERT((int)anId < 0);
self = [super init];
@ -57,15 +59,35 @@
QAccessibleInterface *iface = QAccessible::accessibleInterface(axid);
Q_ASSERT(iface);
role = QCocoaAccessible::macRole(iface);
parent = aParent;
}
return self;
}
+ (QCocoaAccessibleElement *)createElementWithId:(QAccessible::Id)anId parent:(id)aParent
+ (id)elementWithId:(QAccessible::Id)anId
{
return [[self alloc] initWithId:anId parent:aParent];
Q_ASSERT(anId);
if (!anId)
return nil;
QAccessibleCache *cache = QAccessibleCache::instance();
QCocoaAccessibleElement *element = cache->elementForId(anId);
if (!element) {
QAccessibleInterface *iface = QAccessible::accessibleInterface(anId);
Q_ASSERT(iface);
if (!iface)
return nil;
element = [[self alloc] initWithId:anId];
cache->insertElement(anId, element);
}
return element;
}
- (void)invalidate {
axid = 0;
NSAccessibilityPostNotification(self, NSAccessibilityUIElementDestroyedNotification);
[self release];
}
- (void)dealloc {
@ -98,6 +120,10 @@
return [NSNumber numberWithInt: newlines];
}
- (BOOL) accessibilityNotifiesWhenDestroyed {
return YES;
}
- (NSArray *)accessibilityAttributeNames {
static NSArray *defaultAttributes = nil;
@ -145,6 +171,26 @@
return [attributes autorelease];
}
- (id)parentElement {
QAccessibleInterface *iface = QAccessible::accessibleInterface(axid);
if (!iface)
return nil;
if (QWindow *window = iface->window()) {
QCocoaWindow *win = static_cast<QCocoaWindow*>(window->handle());
return win->qtView();
}
QAccessibleInterface *parent = iface->parent();
if (!parent) {
qWarning() << "INVALID PARENT FOR INTERFACE: " << iface;
return nil;
}
QAccessible::Id parentId = QAccessible::uniqueId(parent);
return [QCocoaAccessibleElement elementWithId: parentId];
}
- (id)accessibilityAttributeValue:(NSString *)attribute {
QAccessibleInterface *iface = QAccessible::accessibleInterface(axid);
if (!iface) {
@ -157,19 +203,19 @@
} else if ([attribute isEqualToString:NSAccessibilityRoleDescriptionAttribute]) {
return NSAccessibilityRoleDescription(role, nil);
} else if ([attribute isEqualToString:NSAccessibilityChildrenAttribute]) {
return QCocoaAccessible::unignoredChildren(self, iface);
return QCocoaAccessible::unignoredChildren(iface);
} else if ([attribute isEqualToString:NSAccessibilityFocusedAttribute]) {
// Just check if the app thinks we're focused.
id focusedElement = [NSApp accessibilityAttributeValue:NSAccessibilityFocusedUIElementAttribute];
return [NSNumber numberWithBool:[focusedElement isEqual:self]];
} else if ([attribute isEqualToString:NSAccessibilityParentAttribute]) {
return NSAccessibilityUnignoredAncestor(parent);
return NSAccessibilityUnignoredAncestor([self parentElement]);
} else if ([attribute isEqualToString:NSAccessibilityWindowAttribute]) {
// We're in the same window as our parent.
return [parent accessibilityAttributeValue:NSAccessibilityWindowAttribute];
return [[self parentElement] accessibilityAttributeValue:NSAccessibilityWindowAttribute];
} else if ([attribute isEqualToString:NSAccessibilityTopLevelUIElementAttribute]) {
// We're in the same top level element as our parent.
return [parent accessibilityAttributeValue:NSAccessibilityTopLevelUIElementAttribute];
return [[self parentElement] accessibilityAttributeValue:NSAccessibilityTopLevelUIElementAttribute];
} else if ([attribute isEqualToString:NSAccessibilityPositionAttribute]) {
QPoint qtPosition = iface->rect().topLeft();
QSize qtSize = iface->rect().size();
@ -403,20 +449,26 @@
return NSAccessibilityUnignoredAncestor(self);
}
QAccessibleInterface *childInterface = iface->childAt(point.x, qt_mac_flipYCoordinate(point.y));
int y = qt_mac_flipYCoordinate(point.y);
QAccessibleInterface *childInterface = iface->childAt(point.x, y);
// No child found, meaning we hit this element.
if (!childInterface) {
// qDebug() << "Hit test returns: " << id << iface;
if (!childInterface)
return NSAccessibilityUnignoredAncestor(self);
}
// find the deepest child at the point
QAccessibleInterface *childOfChildInterface = 0;
do {
childOfChildInterface = childInterface->childAt(point.x, y);
if (childOfChildInterface)
childInterface = childOfChildInterface;
} while (childOfChildInterface);
QAccessible::Id childId = QAccessible::uniqueId(childInterface);
// hit a child, forward to child accessible interface.
QCocoaAccessibleElement *accessibleElement = [QCocoaAccessibleElement createElementWithId:childId parent:self];
[accessibleElement autorelease];
return [accessibleElement accessibilityHitTest:point];
QCocoaAccessibleElement *accessibleElement = [QCocoaAccessibleElement elementWithId:childId];
if (accessibleElement)
return NSAccessibilityUnignoredAncestor(accessibleElement);
return NSAccessibilityUnignoredAncestor(self);
}
- (id)accessibilityFocusedUIElement {
@ -426,17 +478,15 @@
qWarning() << "FocusedUIElement for INVALID";
return nil;
}
QAccessibleInterface *childInterface = iface->focusChild();
if (childInterface) {
QAccessible::Id childAxid = QAccessible::uniqueId(childInterface);
// FIXME: parent could be wrong
QCocoaAccessibleElement *accessibleElement = [QCocoaAccessibleElement createElementWithId:childAxid parent:self];
[accessibleElement autorelease];
return accessibleElement;
QCocoaAccessibleElement *accessibleElement = [QCocoaAccessibleElement elementWithId:childAxid];
return NSAccessibilityUnignoredAncestor(accessibleElement);
}
// no focus found
return nil;
return NSAccessibilityUnignoredAncestor(self);
}
@end

View File

@ -59,8 +59,7 @@
return nil;
QAccessible::Id childId = QAccessible::uniqueId(m_window->accessibleRoot());
QCocoaAccessibleElement *child = [QCocoaAccessibleElement createElementWithId: childId parent: self];
return [child autorelease];
return [QCocoaAccessibleElement elementWithId: childId];
}
// The QNSView is a container that the user does not interact directly with:

View File

@ -16,6 +16,7 @@ SUBDIRS=\
networkselftest \
qaccessibility \
qaccessibilitylinux \
qaccessibilitymac \
qcomplextext \
qfocusevent \
qnetworkaccessmanager_and_qprogressdialog \

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -82,6 +82,8 @@ private slots:
void singleWidgetTest();
void lineEditTest();
void hierarchyTest();
void notificationsTest();
private:
AccessibleTestWindow *m_window;
};
@ -123,6 +125,7 @@ void tst_QAccessibilityMac::lineEditTest()
m_window->addWidget(lineEdit);
QVERIFY(QTest::qWaitForWindowExposed(m_window));
QCoreApplication::processEvents();
QVERIFY(testLineEdit());
}
@ -148,5 +151,13 @@ void tst_QAccessibilityMac::hierarchyTest()
QVERIFY(testHierarchy(w));
}
void tst_QAccessibilityMac::notificationsTest()
{
if (!macNativeAccessibilityEnabled())
return;
QVERIFY(notifications(m_window));
}
QTEST_MAIN(tst_QAccessibilityMac)
#include "tst_qaccessibilitymac.moc"

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -42,11 +42,13 @@
#include <QtCore/QPair>
#include <QtWidgets/QWidget>
#pragma once // Yeah, it's deprecated in general, but it's standard practice for Mac OS X.
QT_USE_NAMESPACE
bool macNativeAccessibilityEnabled();
bool trusted();
bool testLineEdit();
bool testHierarchy(QWidget *w);
bool singleWidget();
bool notifications(QWidget *w);

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -46,12 +46,15 @@
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qlineedit.h>
#include <QtWidgets/qpushbutton.h>
#include <QtWidgets>
#include <QtTest>
#include <unistd.h>
#import <Cocoa/Cocoa.h>
#import <ApplicationServices/ApplicationServices.h>
QT_USE_NAMESPACE
bool macNativeAccessibilityEnabled()
{
bool enabled = AXAPIEnabled();
@ -76,6 +79,10 @@ bool trusted()
@interface TestAXObject : NSObject
{
AXUIElementRef reference;
NSString *_role;
NSString *_description;
NSString *_value;
CGRect _rect;
}
@property (readonly) NSString *role;
@property (readonly) NSString *description;
@ -84,7 +91,14 @@ bool trusted()
@end
@implementation TestAXObject
@synthesize role = _role;
@synthesize description = _description;
@synthesize value = _value;
@synthesize rect = _rect;
- (id) initWithAXUIElementRef: (AXUIElementRef) ref {
if ( self = [super init] ) {
reference = ref;
AXUIElementCopyAttributeValue(ref, kAXRoleAttribute, (CFTypeRef*)&_role);
@ -103,7 +117,7 @@ bool trusted()
- (AXUIElementRef) ref { return reference; }
- (void) print {
NSLog(@"Accessible Object role: '%@', description: '%@', value: '%@', rect: '%@'", self.role, self.description, self.value, NSStringFromRect(self.rect));
NSLog(@" Children: %ld", [self.childList count]);
NSLog(@" Children: %ld", [[self childList] count]);
}
- (NSArray*) windowList
@ -163,10 +177,10 @@ bool trusted()
bool singleWidget()
{
QLineEdit le;
le.setText("button");
le.show();
EXPECT(QTest::qWaitForWindowExposed(&le));
QLineEdit *le = new QLineEdit();
le->setText("button");
le->show();
EXPECT(QTest::qWaitForWindowExposed(le));
QCoreApplication::processEvents();
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
@ -179,8 +193,16 @@ bool singleWidget()
EXPECT(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef: windowRef];
AXUIElementRef lineEdit = [window findDirectChildByRole: kAXTextFieldRole];
EXPECT(lineEdit != nil);
AXUIElementRef lineEditRef = [window findDirectChildByRole: kAXTextFieldRole];
EXPECT(lineEditRef != nil);
TestAXObject *lineEdit = [[TestAXObject alloc] initWithAXUIElementRef: lineEditRef];
EXPECT([[lineEdit value] isEqualToString:@"button"]);
// Access invalid reference, should return empty value
delete le;
QCoreApplication::processEvents();
TestAXObject *lineEditInvalid = [[TestAXObject alloc] initWithAXUIElementRef: lineEditRef];
EXPECT([[lineEditInvalid value] length] == 0);
return true;
}
@ -207,6 +229,10 @@ bool testLineEdit()
// height of window includes title bar
EXPECT([window rect].size.height >= 400);
NSString *windowTitle;
AXUIElementCopyAttributeValue(windowRef, kAXTitleAttribute, (CFTypeRef*)&windowTitle);
EXPECT([windowTitle isEqualToString:@"Test window"]);
// children of window:
AXUIElementRef lineEdit = [window findDirectChildByRole: kAXTextFieldRole];
EXPECT(lineEdit != nil);
@ -270,3 +296,68 @@ bool testHierarchy(QWidget *w)
return true;
}
QVector<int> notificationList;
void observerCallback(AXObserverRef /*observer*/, AXUIElementRef /*element*/, CFStringRef notification, void *)
{
if ([(NSString*)notification isEqualToString: NSAccessibilityFocusedUIElementChangedNotification])
notificationList.append(QAccessible::Focus);
else if ([(NSString*)notification isEqualToString: NSAccessibilityValueChangedNotification])
notificationList.append(QAccessible::ValueChanged);
else
notificationList.append(-1);
}
bool notifications(QWidget *w)
{
QLineEdit *le1 = new QLineEdit(w);
QLineEdit *le2 = new QLineEdit(w);
w->layout()->addWidget(le1);
w->layout()->addWidget(le2);
QCoreApplication::processEvents();
QTest::qWait(100);
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
EXPECT(appObject);
NSArray *windowList = [appObject windowList];
// one window
EXPECT([windowList count] == 1);
AXUIElementRef windowRef = (AXUIElementRef) [windowList objectAtIndex: 0];
EXPECT(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef: windowRef];
AXUIElementRef lineEdit1 = [window findDirectChildByRole: kAXTextFieldRole];
EXPECT(lineEdit1 != nil);
AXObserverRef observer = 0;
AXError err = AXObserverCreate(getpid(), observerCallback, &observer);
EXPECT(!err);
AXObserverAddNotification(observer, appObject.ref, kAXFocusedUIElementChangedNotification, 0);
AXObserverAddNotification(observer, lineEdit1, kAXValueChangedNotification, 0);
CFRunLoopAddSource( [[NSRunLoop currentRunLoop] getCFRunLoop], AXObserverGetRunLoopSource(observer), kCFRunLoopDefaultMode);
EXPECT(notificationList.length() == 0);
le2->setFocus();
QCoreApplication::processEvents();
EXPECT(notificationList.length() == 1);
EXPECT(notificationList.at(0) == QAccessible::Focus);
le1->setFocus();
QCoreApplication::processEvents();
EXPECT(notificationList.length() == 2);
EXPECT(notificationList.at(1) == QAccessible::Focus);
le1->setText("hello");
QCoreApplication::processEvents();
EXPECT(notificationList.length() == 3);
EXPECT(notificationList.at(2) == QAccessible::ValueChanged);
le1->setText("foo");
QCoreApplication::processEvents();
EXPECT(notificationList.length() == 4);
EXPECT(notificationList.at(3) == QAccessible::ValueChanged);
return true;
}