Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
#import "RunnerSynthesizedGesture.h"
#import "RunnerSynthesizedTextEntry.h"
#import "RunnerXCTestEventBridge.h"
#import "RunnerAXModalScopeCapture.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <XCTest/XCTest.h>

#import "RunnerAXSnapshotBridge.h"

NS_ASSUME_NONNULL_BEGIN

typedef NS_CLOSED_ENUM(NSInteger, RunnerAXModalScopeCaptureStatus) {
RunnerAXModalScopeCaptureStatusCaptured,
/// The AX client cannot serve element snapshot requests or cannot identify their elements.
RunnerAXModalScopeCaptureStatusUnsupported,
RunnerAXModalScopeCaptureStatusClientUnavailable,
RunnerAXModalScopeCaptureStatusTargetUnresolved,
RunnerAXModalScopeCaptureStatusRequestFailed,
RunnerAXModalScopeCaptureStatusDeadlineExceeded,
};

/// One node of a modal-scope tree. `element` is the node's AX identity,
/// `pid:elementOrHash.elementID`. One element can appear under two parents in one tree, so a node
/// is the pair (`element`, `parentElement`).
@interface RunnerAXModalScopeNode : NSObject
@property(nonatomic, copy, readonly) NSString *element;
@property(nonatomic, copy, readonly, nullable) NSString *parentElement;
/// The parent's position in the tree's `nodes`, or -1 for the root.
@property(nonatomic, assign, readonly) NSInteger parentIndex;
@property(nonatomic, assign, readonly) NSInteger elementType;
@property(nonatomic, copy, readonly) NSString *identifier;
@property(nonatomic, copy, readonly) NSString *label;
@property(nonatomic, assign, readonly) CGRect frame;
@property(nonatomic, copy, readonly, nullable) NSString *viewClass;
@property(nonatomic, copy, readonly, nullable) NSString *viewControllerClass;
@end

/// One tree read, in preorder: each node's parent comes before it.
@interface RunnerAXModalScopeTree : NSObject
@property(nonatomic, copy, readonly) NSArray<RunnerAXModalScopeNode *> *nodes;
/// The node cap or the depth cap clipped the tree.
@property(nonatomic, assign, readonly) BOOL truncated;
@property(nonatomic, assign, readonly) NSTimeInterval duration;
@end

/// Three reads of one app, in this order: the tree with `snapshotKeyHonorModalViews`, which leaves
/// out what a modal view hides from VoiceOver; the whole tree; and the honor-modal tree again. The
/// trees are set only when `status` is captured.
@interface RunnerAXModalScopeCapture : NSObject
@property(nonatomic, assign, readonly) RunnerAXModalScopeCaptureStatus status;
/// The AX error text of a failed request, for diagnostics only.
@property(nonatomic, copy, readonly, nullable) NSString *failure;
@property(nonatomic, strong, readonly, nullable) RunnerAXModalScopeTree *plain;
@property(nonatomic, strong, readonly, nullable) RunnerAXModalScopeTree *honorModal;
@property(nonatomic, strong, readonly, nullable) RunnerAXModalScopeTree *confirmingHonorModal;
@end

@interface RunnerAXSnapshotBridge (ModalScope)

/// Reads the modal-scope trees of the application. `includeClassNames` adds the view and view
/// controller class of each node. The capture fails when `deadline` passes before the last read
/// ends.
+ (RunnerAXModalScopeCapture *)modalScopeCaptureForApplication:(XCUIApplication *)application
maxDepth:(NSInteger)maxDepth
maxNodes:(NSInteger)maxNodes
includeClassNames:(BOOL)includeClassNames
deadline:(NSDate *)deadline;

/// One read's tree from its root element snapshot, or nil when a node's element has no AX
/// identity. Exposed for the runner unit bundle: the cap rules are only executable with fabricated
/// snapshots.
+ (nullable RunnerAXModalScopeTree *)modalScopeTreeFromSnapshot:(id)root
maxDepth:(NSInteger)maxDepth
maxNodes:(NSInteger)maxNodes
includeClassNames:(BOOL)includeClassNames;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
#import "RunnerAXModalScopeCapture.h"

#import <objc/message.h>

#import "RunnerAXSnapshotBridge+Internal.h"

// The request parameter that makes the AX server answer with the tree VoiceOver reads: content a
// modal view hides is left out. A server that does not know it ignores it silently, so a capture
// cannot tell "no modal" from "parameter ignored"; only a known modal can.
static NSString *const RunnerAXHonorModalViewsParameter = @"snapshotKeyHonorModalViews";

// Requesting this attribute makes the server fill `additionalAttributes` with the view class
// (5004) and, on a view controller's view, the view controller class (5042).
static NSString *const RunnerAXViewControllerClassNameAttribute = @"XC_kAXXCAttributeViewControllerClassName";
static const NSInteger RunnerAXViewClassAdditionalAttribute = 5004;
static const NSInteger RunnerAXViewControllerClassAdditionalAttribute = 5042;

typedef unsigned long long (*RunnerAXUnsignedLongLongMsgSend)(id, SEL);

@interface RunnerAXModalScopeNode ()
@property(nonatomic, copy, readwrite) NSString *element;
@property(nonatomic, copy, readwrite, nullable) NSString *parentElement;
@property(nonatomic, assign, readwrite) NSInteger parentIndex;
@property(nonatomic, assign, readwrite) NSInteger elementType;
@property(nonatomic, copy, readwrite) NSString *identifier;
@property(nonatomic, copy, readwrite) NSString *label;
@property(nonatomic, assign, readwrite) CGRect frame;
@property(nonatomic, copy, readwrite, nullable) NSString *viewClass;
@property(nonatomic, copy, readwrite, nullable) NSString *viewControllerClass;
@end

@implementation RunnerAXModalScopeNode
@end

@interface RunnerAXModalScopeTree ()
@property(nonatomic, copy, readwrite) NSArray<RunnerAXModalScopeNode *> *nodes;
@property(nonatomic, assign, readwrite) BOOL truncated;
@property(nonatomic, assign, readwrite) NSTimeInterval duration;
@end

@implementation RunnerAXModalScopeTree
@end

@interface RunnerAXModalScopeCapture ()
@property(nonatomic, assign, readwrite) RunnerAXModalScopeCaptureStatus status;
@property(nonatomic, copy, readwrite, nullable) NSString *failure;
@property(nonatomic, strong, readwrite, nullable) RunnerAXModalScopeTree *plain;
@property(nonatomic, strong, readwrite, nullable) RunnerAXModalScopeTree *honorModal;
@property(nonatomic, strong, readwrite, nullable) RunnerAXModalScopeTree *confirmingHonorModal;
@end

@implementation RunnerAXModalScopeCapture

+ (instancetype)captureWithStatus:(RunnerAXModalScopeCaptureStatus)status failure:(nullable NSString *)failure
{
RunnerAXModalScopeCapture *capture = [[self alloc] init];
capture.status = status;
capture.failure = failure;
return capture;
}

@end

@implementation RunnerAXSnapshotBridge (ModalScope)

+ (RunnerAXModalScopeCapture *)modalScopeCaptureForApplication:(XCUIApplication *)application
maxDepth:(NSInteger)maxDepth
maxNodes:(NSInteger)maxNodes
includeClassNames:(BOOL)includeClassNames
deadline:(NSDate *)deadline
{
@try {
id axClient = [self accessibilityClient];
if (nil == axClient) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusClientUnavailable
failure:nil];
}
if (![axClient respondsToSelector:NSSelectorFromString(@"requestSnapshotForElement:attributes:parameters:error:")]) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusUnsupported
failure:nil];
}
id target = [self accessibilityApplicationForApplication:application axClient:axClient];
if (nil == target) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusTargetUnresolved
failure:nil];
}
NSMutableArray *attributes = [[self attributesForKeyPaths:@[ @"elementType", @"identifier", @"label", @"frame", @"children" ]
consumedSuffixes:@[ @"ElementType", @"Identifier", @"Label", @"Frame" ]]
mutableCopy];
if (includeClassNames) {
[attributes addObject:RunnerAXViewControllerClassNameAttribute];
}

// The second honor-modal read confirms the first: a modal that comes or goes between the reads
// changes the honor-modal tree, while the plain tree alone could still look like a superset.
NSArray<NSNumber *> *readsHonorModalViews = @[ @YES, @NO, @YES ];
NSMutableArray<RunnerAXModalScopeTree *> *trees = [NSMutableArray array];
for (NSNumber *honorModalViews in readsHonorModalViews) {
if (deadline.timeIntervalSinceNow <= 0) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusDeadlineExceeded
failure:nil];
}
NSDate *start = [NSDate date];
NSError *error = nil;
NSMutableDictionary *parameters = [self snapshotParametersForClient:axClient maxDepth:maxDepth maxNodes:maxNodes];
parameters[RunnerAXHonorModalViewsParameter] = honorModalViews;
id root = [self rootSnapshotFromClient:axClient
target:target
attributes:attributes
parameters:parameters
error:&error];
if (nil == root) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusRequestFailed
failure:error.localizedDescription];
}
RunnerAXModalScopeTree *tree = [self modalScopeTreeFromSnapshot:root
maxDepth:maxDepth
maxNodes:maxNodes
includeClassNames:includeClassNames];
if (nil == tree) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusUnsupported
failure:nil];
}
tree.duration = -start.timeIntervalSinceNow;
[trees addObject:tree];
}
if (deadline.timeIntervalSinceNow <= 0) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusDeadlineExceeded
failure:nil];
}
RunnerAXModalScopeCapture *capture = [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusCaptured
failure:nil];
capture.honorModal = trees[0];
capture.plain = trees[1];
capture.confirmingHonorModal = trees[2];
return capture;
} @catch (NSException *exception) {
return [RunnerAXModalScopeCapture captureWithStatus:RunnerAXModalScopeCaptureStatusRequestFailed
failure:exception.reason ?: exception.name];
}
}

+ (nullable RunnerAXModalScopeTree *)modalScopeTreeFromSnapshot:(id)root
maxDepth:(NSInteger)maxDepth
maxNodes:(NSInteger)maxNodes
includeClassNames:(BOOL)includeClassNames
{
NSMutableArray<RunnerAXModalScopeNode *> *nodes = [NSMutableArray array];
BOOL truncated = NO;
if (![self appendModalScopeSnapshot:root
parent:nil
parentIndex:-1
depth:0
maxDepth:maxDepth
maxNodes:maxNodes
includeClassNames:includeClassNames
to:nodes
truncated:&truncated]) {
return nil;
}
RunnerAXModalScopeTree *tree = [[RunnerAXModalScopeTree alloc] init];
tree.nodes = nodes;
tree.truncated = truncated;
return tree;
}

// Returns NO when a node's element has no AX identity.
+ (BOOL)appendModalScopeSnapshot:(id)snapshot
parent:(nullable RunnerAXModalScopeNode *)parent
parentIndex:(NSInteger)parentIndex
depth:(NSInteger)depth
maxDepth:(NSInteger)maxDepth
maxNodes:(NSInteger)maxNodes
includeClassNames:(BOOL)includeClassNames
to:(NSMutableArray<RunnerAXModalScopeNode *> *)nodes
truncated:(BOOL *)truncated
{
if ((NSInteger)nodes.count >= maxNodes) {
*truncated = YES;
return YES;
}
NSString *element = [self modalScopeIdentityForElement:[self accessibilityElementForSnapshot:snapshot]];
if (nil == element) {
return NO;
}
RunnerAXModalScopeNode *node = [[RunnerAXModalScopeNode alloc] init];
node.element = element;
node.parentElement = parent.element;
node.parentIndex = parentIndex;
node.elementType = [[self numberValueForKey:@"elementType" snapshot:snapshot] integerValue];
node.identifier = [self stringValueForKey:@"identifier" snapshot:snapshot] ?: @"";
node.label = [self stringValueForKey:@"label" snapshot:snapshot] ?: @"";
node.frame = [self frameForSnapshot:snapshot];
if (includeClassNames) {
id additional = nil;
@try {
additional = [snapshot valueForKey:@"additionalAttributes"];
} @catch (NSException *exception) {
additional = nil;
}
if ([additional isKindOfClass:NSDictionary.class]) {
id viewClass = additional[@(RunnerAXViewClassAdditionalAttribute)];
id viewControllerClass = additional[@(RunnerAXViewControllerClassAdditionalAttribute)];
node.viewClass = [viewClass isKindOfClass:NSString.class] ? viewClass : nil;
node.viewControllerClass = [viewControllerClass isKindOfClass:NSString.class] ? viewControllerClass : nil;
}
}
NSInteger index = (NSInteger)nodes.count;
[nodes addObject:node];
Comment thread
thymikee marked this conversation as resolved.
Resolved
// The request bounds the server's reply only per element (maxChildren, maxArrayCount) and per
// level (maxDepth), not in total. The total cap is this count of kept nodes. It also covers an
// element the server clipped at maxChildren, because that element's children alone reach
// maxNodes. A tree that reaches either cap may be missing nodes.
if ((NSInteger)nodes.count >= maxNodes || depth >= maxDepth - 1) {
*truncated = YES;
return YES;
}
for (id child in [self childrenForSnapshot:snapshot]) {
if (![self appendModalScopeSnapshot:child
parent:node
parentIndex:index
depth:depth + 1
maxDepth:maxDepth
maxNodes:maxNodes
includeClassNames:includeClassNames
to:nodes
truncated:truncated]) {
return NO;
}
}
return YES;
}

+ (nullable NSString *)modalScopeIdentityForElement:(nullable id)element
{
SEL elementOrHash = NSSelectorFromString(@"elementOrHash");
SEL elementID = NSSelectorFromString(@"elementID");
if (nil == element || ![element respondsToSelector:elementOrHash] || ![element respondsToSelector:elementID]) {
return nil;
}
NSInteger processIdentifier = [self integerFrom:element selectorName:@"processIdentifier"];
if (processIdentifier <= 0) {
return nil;
}
RunnerAXUnsignedLongLongMsgSend send = (RunnerAXUnsignedLongLongMsgSend)objc_msgSend;
return [NSString stringWithFormat:@"%ld:%llu.%llu", (long)processIdentifier, send(element, elementOrHash),
send(element, elementID)];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#import "RunnerAXSnapshotBridge.h"

NS_ASSUME_NONNULL_BEGIN

/// The bridge's client, target, request, and attribute plumbing, shared with the other AX reads
/// so each private call has one implementation.
@interface RunnerAXSnapshotBridge ()

+ (nullable id)accessibilityApplicationForApplication:(XCUIApplication *)application axClient:(id)axClient;

/// The client's default snapshot parameters with the depth and node caps applied.
+ (NSMutableDictionary *)snapshotParametersForClient:(id)axClient
maxDepth:(NSInteger)maxDepth
maxNodes:(NSInteger)maxNodes;

/// The root element snapshot of one request, or nil with `error` set.
+ (nullable id)rootSnapshotFromClient:(id)axClient
target:(id)target
attributes:(NSArray *)attributes
parameters:(NSDictionary *)parameters
error:(NSError **)error;

/// Maps snapshot keypaths to AX attributes, keeping only attributes whose name ends with one of
/// `consumedSuffixes`.
+ (NSArray *)attributesForKeyPaths:(NSArray<NSString *> *)keyPaths
consumedSuffixes:(NSArray<NSString *> *)consumedSuffixes;

+ (nullable id)accessibilityElementForSnapshot:(id)snapshot;
+ (NSArray *)childrenForSnapshot:(id)snapshot;
+ (nullable NSNumber *)numberValueForKey:(NSString *)key snapshot:(id)snapshot;
+ (nullable NSString *)stringValueForKey:(NSString *)key snapshot:(id)snapshot;
+ (CGRect)frameForSnapshot:(id)snapshot;
+ (nullable id)objectFrom:(id)target selectorName:(NSString *)selectorName;
+ (NSInteger)integerFrom:(id)target selectorName:(NSString *)selectorName;

@end

NS_ASSUME_NONNULL_END
Loading
Loading