-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathASDisplayViewAccessibilityTests.mm
661 lines (527 loc) · 26.3 KB
/
ASDisplayViewAccessibilityTests.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//
// ASDisplayViewAccessibilityTests.mm
// Texture
//
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/_ASDisplayView.h>
#import <AsyncDisplayKit/_ASDisplayViewAccessiblity.h>
#import <AsyncDisplayKit/ASButtonNode.h>
#import <AsyncDisplayKit/ASDisplayNode.h>
#import <AsyncDisplayKit/ASDisplayNode+Beta.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import <AsyncDisplayKit/ASConfiguration.h>
#import <AsyncDisplayKit/ASConfigurationInternal.h>
#import <AsyncDisplayKit/ASScrollNode.h>
#import <AsyncDisplayKit/ASDKViewController.h>
#import <OCMock/OCMock.h>
#import "ASDisplayNodeTestsHelper.h"
#import <WebKit/WebKit.h>
extern void SortAccessibilityElements(NSMutableArray *elements);
@interface ASDisplayViewAccessibilityTests : XCTestCase
@end
@implementation ASDisplayViewAccessibilityTests
- (void)testAccessibilityElementsAccessors
{
// Setup nodes with accessibility info
ASDisplayNode *node = nil;
ASDisplayNode *subnode = nil;
node = [[ASDisplayNode alloc] init];
subnode = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:node];
[window makeKeyAndVisible];
NSString *label = @"foo";
subnode.isAccessibilityElement = YES;
subnode.accessibilityLabel = label;
[node addSubnode:subnode];
XCTAssertEqualObjects([node.view.accessibilityElements.firstObject accessibilityLabel], label);
// NOTE: The following tests will fail unless accessibility is enabled, e.g. by turning the
// accessibility inspector on. See https://github.com/TextureGroup/Texture/pull/1069 for details.
/*XCTAssertEqualObjects([[node.view accessibilityElementAtIndex:0] accessibilityLabel], label);
XCTAssertEqual(node.view.accessibilityElementCount, 1);
XCTAssertEqual([node.view indexOfAccessibilityElement:node.view.accessibilityElements.firstObject], 0);*/
}
- (void)testThatSubnodeAccessibilityLabelAggregationWorks
{
// Setup nodes
ASDisplayNode *node = nil;
ASDisplayNode *innerNode1 = nil;
ASDisplayNode *innerNode2 = nil;
node = [[ASDisplayNode alloc] init];
innerNode1 = [[ASDisplayNode alloc] init];
innerNode2 = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:node];
[window makeKeyAndVisible];
// Initialize nodes with relevant accessibility data
node.isAccessibilityContainer = YES;
innerNode1.accessibilityLabel = @"hello";
innerNode2.accessibilityLabel = @"world";
// Attach the subnodes to the parent node, then ensure their accessibility labels have been'
// aggregated to the parent's accessibility label
[node addSubnode:innerNode1];
[node addSubnode:innerNode2];
XCTAssertEqualObjects([node.view.accessibilityElements.firstObject accessibilityLabel],
@"hello, world", @"Subnode accessibility label aggregation broken %@",
[node.view.accessibilityElements.firstObject accessibilityLabel]);
}
- (void)testThatContainerAccessibilityLabelOverrideStopsAggregation
{
// Setup nodes
ASDisplayNode *node = nil;
ASDisplayNode *innerNode = nil;
node = [[ASDisplayNode alloc] init];
innerNode = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:node];
[window makeKeyAndVisible];
// Initialize nodes with relevant accessibility data
node.isAccessibilityContainer = YES;
node.accessibilityLabel = @"hello";
innerNode.accessibilityLabel = @"world";
// Attach the subnode to the parent node, then ensure the parent's accessibility label does not
// get aggregated with the subnode's label
[node addSubnode:innerNode];
XCTAssertEqualObjects([node.view.accessibilityElements.firstObject accessibilityLabel], @"hello",
@"Container accessibility label override broken %@",
[node.view.accessibilityElements.firstObject accessibilityLabel]);
}
- (void)testAccessibilityLayerBackedContainerWithinAccessibilityContainer
{
ASDisplayNode *container = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:container];
[window makeKeyAndVisible];
container.frame = CGRectMake(50, 50, 200, 600);
container.isAccessibilityContainer = YES;
ASDisplayNode *subContainer = [[ASDisplayNode alloc] init];
subContainer.frame = CGRectMake(50, 50, 200, 600);
subContainer.layerBacked = YES;
subContainer.isAccessibilityContainer = YES;
[container addSubnode:subContainer];
ASTextNode *text1 = [[ASTextNode alloc] init];
text1.attributedText = [[NSAttributedString alloc] initWithString:@"hello"];
text1.frame = CGRectMake(50, 100, 200, 200);
text1.layerBacked = YES;
[subContainer addSubnode:text1];
ASTextNode *text2 = [[ASTextNode alloc] init];
text2.attributedText = [[NSAttributedString alloc] initWithString:@"world"];
text2.frame = CGRectMake(50, 300, 200, 200);
text2.layerBacked = YES;
[subContainer addSubnode:text2];
NSArray<UIAccessibilityElement *> *accessibilityElements = container.view.accessibilityElements;
XCTAssertEqual(accessibilityElements.count, 2);
XCTAssertEqualObjects(accessibilityElements[1].accessibilityLabel, @"hello, world");
}
- (void)testAccessibilityNonLayerbackedNodesOperationInNonContainer
{
ASDisplayNode *container = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:container];
[window makeKeyAndVisible];
container.frame = CGRectMake(50, 50, 200, 600);
container.backgroundColor = [UIColor grayColor];
// Do any additional setup after loading the view, typically from a nib.
ASTextNode *text1 = [[ASTextNode alloc] init];
text1.attributedText = [[NSAttributedString alloc] initWithString:@"hello"];
text1.frame = CGRectMake(50, 100, 200, 200);
[container addSubnode:text1];
[container layoutIfNeeded];
[container.layer displayIfNeeded];
NSArray<UIAccessibilityElement *> *elements = container.view.accessibilityElements;
XCTAssertTrue(elements.count == 1);
XCTAssertTrue([[elements.firstObject accessibilityLabel] isEqualToString:@"hello"]);
ASTextNode *text2 = [[ASTextNode alloc] init];
text2.attributedText = [[NSAttributedString alloc] initWithString:@"world"];
text2.frame = CGRectMake(50, 300, 200, 200);
[container addSubnode:text2];
[container layoutIfNeeded];
[container.layer displayIfNeeded];
ASCATransactionQueueWait(nil);
NSArray<UIAccessibilityElement *> *updatedElements = container.view.accessibilityElements;
XCTAssertTrue(updatedElements.count == 2);
XCTAssertTrue([[updatedElements.firstObject accessibilityLabel] isEqualToString:@"hello"]);
XCTAssertTrue([[updatedElements.lastObject accessibilityLabel] isEqualToString:@"world"]);
ASTextNode *text3 = [[ASTextNode alloc] init];
text3.attributedText = [[NSAttributedString alloc] initWithString:@"!!!!"];
text3.frame = CGRectMake(50, 400, 200, 100);
[text2 addSubnode:text3];
[container layoutIfNeeded];
[container.layer displayIfNeeded];
ASCATransactionQueueWait(nil);
NSArray<UIAccessibilityElement *> *updatedElements2 = container.view.accessibilityElements;
//text3 won't be read out cause it's overshadowed by text2
XCTAssertTrue(updatedElements2.count == 2);
XCTAssertTrue([[updatedElements2.firstObject accessibilityLabel] isEqualToString:@"hello"]);
XCTAssertTrue([[updatedElements2.lastObject accessibilityLabel] isEqualToString:@"world"]);
}
- (void)testAccessibilityElementsAreNilForWrappedWKWebView {
ASDisplayNode *container = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:container];
[window makeKeyAndVisible];
container.frame = CGRectMake(50, 50, 200, 600);
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
[container.view addSubview:webView];
NSString *htmlString =
@"<html>"
@" <head>"
@" <meta name='viewport' content='width=device-width' />"
@" </head>"
@" <body>"
@" <h1>Texture is Awesome!</h1>"
@" <p>Especially when web views inside nodes are accessible.</p>"
@" </body>"
@" </html>";
[webView loadHTMLString:htmlString baseURL:nil];
// Accessibility elements should be nil in this case, because
// WKWebView handles accessibility out of process.
NSArray *accessibilityElements = container.accessibilityElements;
XCTAssertNil(accessibilityElements);
}
#pragma mark -
#pragma mark UIAccessibilityAction Forwarding
- (void)testActionForwarding {
ASDisplayNode *node = [ASDisplayNode new];
UIView *view = node.view;
id mockNode = OCMPartialMock(node);
OCMExpect([mockNode accessibilityActivate]);
[view accessibilityActivate];
OCMExpect([mockNode accessibilityIncrement]);
[view accessibilityIncrement];
OCMExpect([mockNode accessibilityDecrement]);
[view accessibilityDecrement];
OCMExpect([mockNode accessibilityScroll:UIAccessibilityScrollDirectionDown]);
[view accessibilityScroll:UIAccessibilityScrollDirectionDown];
OCMExpect([mockNode accessibilityPerformEscape]);
[view accessibilityPerformEscape];
OCMExpect([mockNode accessibilityPerformMagicTap]);
[view accessibilityPerformMagicTap];
OCMVerifyAll(mockNode);
}
#pragma mark -
#pragma mark AccessibilityElements
// dummy action for a button
- (void)fakeSelector:(id)sender { }
- (void)testThatAccessibilityElementsWorks {
ASDisplayNode *containerNode = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:containerNode];
[window makeKeyAndVisible];
containerNode.frame = CGRectMake(0, 0, 100, 200);
ASTextNode *label = [[ASTextNode alloc] init];
label.attributedText = [[NSAttributedString alloc] initWithString:@"test label"];
label.frame = CGRectMake(0, 0, 100, 20);
ASButtonNode *button = [[ASButtonNode alloc] init];
[button setTitle:@"tap me" withFont:[UIFont systemFontOfSize:17] withColor:nil forState:UIControlStateNormal];
[button addTarget:self action:@selector(fakeSelector:) forControlEvents:ASControlNodeEventTouchUpInside];
button.frame = CGRectMake(0, 25, 100, 20);
[containerNode addSubnode:label];
[containerNode addSubnode:button];
// force load
__unused UIView *view = containerNode.view;
NSArray *elements = [containerNode.view accessibilityElements];
XCTAssertTrue(elements.count == 2);
XCTAssertEqual([elements.firstObject asyncdisplaykit_node], label);
XCTAssertEqual([elements.lastObject asyncdisplaykit_node], button);
}
- (void)testThatAccessibilityElementsOverrideWorks {
ASDisplayNode *containerNode = [[ASDisplayNode alloc] init];
containerNode.frame = CGRectMake(0, 0, 100, 200);
ASTextNode *label = [[ASTextNode alloc] init];
label.attributedText = [[NSAttributedString alloc] initWithString:@"test label"];
label.frame = CGRectMake(0, 0, 100, 20);
ASButtonNode *button = [[ASButtonNode alloc] init];
[button setTitle:@"tap me" withFont:[UIFont systemFontOfSize:17] withColor:nil forState:UIControlStateNormal];
[button addTarget:self action:@selector(fakeSelector:) forControlEvents:ASControlNodeEventTouchUpInside];
button.frame = CGRectMake(0, 25, 100, 20);
[containerNode addSubnode:label];
[containerNode addSubnode:button];
containerNode.accessibilityElements = @[ label ];
// force load
__unused UIView *view = containerNode.view;
NSArray *elements = [containerNode.view accessibilityElements];
XCTAssertTrue(elements.count == 1);
XCTAssertEqual(elements.firstObject, label);
}
- (void)testHiddenAccessibilityElements {
ASDisplayNode *containerNode = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:containerNode];
[window makeKeyAndVisible];
containerNode.frame = CGRectMake(0, 0, 100, 200);
ASTextNode *label = [[ASTextNode alloc] init];
label.attributedText = [[NSAttributedString alloc] initWithString:@"test label"];
label.frame = CGRectMake(0, 0, 100, 20);
ASTextNode *hiddenLabel = [[ASTextNode alloc] init];
hiddenLabel.attributedText = [[NSAttributedString alloc] initWithString:@"hidden label"];
hiddenLabel.frame = CGRectMake(0, 24, 100, 20);
hiddenLabel.hidden = YES;
[containerNode addSubnode:label];
[containerNode addSubnode:hiddenLabel];
// force load
__unused UIView *view = containerNode.view;
NSArray *elements = [containerNode.view accessibilityElements];
XCTAssertTrue(elements.count == 1);
XCTAssertEqual(elements.firstObject, label.view);
}
- (void)testTransparentAccessibilityElements {
ASDisplayNode *containerNode = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:containerNode];
[window makeKeyAndVisible];
containerNode.frame = CGRectMake(0, 0, 100, 200);
ASTextNode *label = [[ASTextNode alloc] init];
label.attributedText = [[NSAttributedString alloc] initWithString:@"test label"];
label.frame = CGRectMake(0, 0, 100, 20);
ASTextNode *hiddenLabel = [[ASTextNode alloc] init];
hiddenLabel.attributedText = [[NSAttributedString alloc] initWithString:@"hidden label"];
hiddenLabel.frame = CGRectMake(0, 24, 100, 20);
hiddenLabel.alpha = 0.0;
[containerNode addSubnode:label];
[containerNode addSubnode:hiddenLabel];
// force load
__unused UIView *view = containerNode.view;
NSArray *elements = [containerNode.view accessibilityElements];
XCTAssertTrue(elements.count == 1);
XCTAssertEqual(elements.firstObject, label.view);
}
- (void)testAccessibilityElementsNotInAppWindow {
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
ASDKViewController *vc = [[ASDKViewController alloc] initWithNode:node];
window.rootViewController = vc;
[window makeKeyAndVisible];
[window layoutIfNeeded];
CGSize windowSize = window.frame.size;
ASTextNode *label = [[ASTextNode alloc] init];
label.attributedText = [[NSAttributedString alloc] initWithString:@"on screen"];
label.frame = CGRectMake(0, 0, 100, 20);
ASTextNode *partiallyOnScreenNodeY = [[ASTextNode alloc] init];
partiallyOnScreenNodeY.attributedText = [[NSAttributedString alloc] initWithString:@"partially on screen y"];
partiallyOnScreenNodeY.frame = CGRectMake(0, windowSize.height - 10, 100, 20);
ASTextNode *partiallyOnScreenNodeX = [[ASTextNode alloc] init];
partiallyOnScreenNodeX.attributedText = [[NSAttributedString alloc] initWithString:@"partially on screen x"];
partiallyOnScreenNodeX.frame = CGRectMake(windowSize.width - 10, 100, 100, 20);
ASTextNode *offScreenNodeY = [[ASTextNode alloc] init];
offScreenNodeY.attributedText = [[NSAttributedString alloc] initWithString:@"off screen y"];
offScreenNodeY.frame = CGRectMake(0, windowSize.height + 10, 100, 20);
ASTextNode *offScreenNodeX = [[ASTextNode alloc] init];
offScreenNodeX.attributedText = [[NSAttributedString alloc] initWithString:@"off screen x"];
offScreenNodeX.frame = CGRectMake(windowSize.width + 1, 200, 100, 20);
ASTextNode *offScreenNode = [[ASTextNode alloc] init];
offScreenNode.attributedText = [[NSAttributedString alloc] initWithString:@"off screen"];
offScreenNode.frame = CGRectMake(windowSize.width + 1, windowSize.height + 1, 100, 20);
[node addSubnode:label];
[node addSubnode:partiallyOnScreenNodeY];
[node addSubnode:partiallyOnScreenNodeX];
[node addSubnode:offScreenNodeY];
[node addSubnode:offScreenNodeX];
[node addSubnode:offScreenNode];
NSArray *elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 3);
XCTAssertTrue([elements containsObject:label.view]);
XCTAssertTrue([elements containsObject:partiallyOnScreenNodeX.view]);
XCTAssertTrue([elements containsObject:partiallyOnScreenNodeY.view]);
}
- (void)testAccessibilityElementsNotInAppWindowButInScrollView {
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
ASScrollNode *node = [[ASScrollNode alloc] init];
node.automaticallyManagesSubnodes = YES;
ASDKViewController *vc = [[ASDKViewController alloc] initWithNode:node];
window.rootViewController = vc;
[window makeKeyAndVisible];
[window layoutIfNeeded];
CGSize windowSize = window.frame.size;
node.view.contentSize = CGSizeMake(window.frame.size.width, window.frame.size.height * 2.0);
ASTextNode *label = [[ASTextNode alloc] init];
label.attributedText = [[NSAttributedString alloc] initWithString:@"on screen"];
label.frame = CGRectMake(0, 0, 100, 20);
ASTextNode *partiallyOnScreenNodeY = [[ASTextNode alloc] init];
partiallyOnScreenNodeY.attributedText = [[NSAttributedString alloc] initWithString:@"partially on screen y"];
partiallyOnScreenNodeY.frame = CGRectMake(0, windowSize.height - 10, 100, 20);
ASTextNode *partiallyOnScreenNodeX = [[ASTextNode alloc] init];
partiallyOnScreenNodeX.attributedText = [[NSAttributedString alloc] initWithString:@"partially on screen x"];
partiallyOnScreenNodeX.frame = CGRectMake(windowSize.width - 10, 100, 100, 20);
ASTextNode *offScreenNodeY = [[ASTextNode alloc] init];
offScreenNodeY.attributedText = [[NSAttributedString alloc] initWithString:@"off screen y"];
offScreenNodeY.frame = CGRectMake(0, windowSize.height + 10, 100, 20);
ASTextNode *offScreenNodeX = [[ASTextNode alloc] init];
offScreenNodeX.attributedText = [[NSAttributedString alloc] initWithString:@"off screen x"];
offScreenNodeX.frame = CGRectMake(windowSize.width + 1, 200, 100, 20);
ASTextNode *offScreenNode = [[ASTextNode alloc] init];
offScreenNode.attributedText = [[NSAttributedString alloc] initWithString:@"off screen"];
offScreenNode.frame = CGRectMake(windowSize.width + 1, windowSize.height + 1, 100, 20);
[node addSubnode:label];
[node addSubnode:partiallyOnScreenNodeY];
[node addSubnode:partiallyOnScreenNodeX];
[node addSubnode:offScreenNodeY];
[node addSubnode:offScreenNodeX];
[node addSubnode:offScreenNode];
NSArray *elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 6);
XCTAssertTrue([elements containsObject:label.view]);
XCTAssertTrue([elements containsObject:partiallyOnScreenNodeX.view]);
XCTAssertTrue([elements containsObject:partiallyOnScreenNodeY.view]);
XCTAssertTrue([elements containsObject:offScreenNodeY.view]);
XCTAssertTrue([elements containsObject:offScreenNodeX.view]);
XCTAssertTrue([elements containsObject:offScreenNode.view]);
}
- (void)testAccessibilitySort {
ASDisplayNode *node1 = [[ASDisplayNode alloc] init];
node1.accessibilityFrame = CGRectMake(0, 0, 50, 200);
ASDisplayNode *node2 = [[ASDisplayNode alloc] init];
node2.accessibilityFrame = CGRectMake(0, 0, 100, 200);
ASDisplayNode *node3 = [[ASDisplayNode alloc] init];
node3.accessibilityFrame = CGRectMake(0, 1, 100, 200);
ASDisplayNode *node4 = [[ASDisplayNode alloc] init];
node4.accessibilityFrame = CGRectMake(1, 1, 100, 200);
NSMutableArray *elements = [@[node2, node4, node3, node1] mutableCopy];
SortAccessibilityElements(elements);
XCTAssertEqual(elements[0], node1);
XCTAssertEqual(elements[1], node2);
XCTAssertEqual(elements[2], node3);
XCTAssertEqual(elements[3], node4);
}
- (void)testCustomAccessibilitySort {
// silly custom sorter that puts items with the largest height first.
setUserDefinedAccessibilitySortComparator(^NSComparisonResult(NSObject *a, NSObject *b) {
if (a.accessibilityFrame.size.height == b.accessibilityFrame.size.height) {
return NSOrderedSame;
}
return a.accessibilityFrame.size.height > b.accessibilityFrame.size.height ? NSOrderedAscending : NSOrderedDescending;
});
ASDisplayNode *node1 = [[ASDisplayNode alloc] init];
node1.accessibilityFrame = CGRectMake(0, 0, 50, 300);
ASDisplayNode *node2 = [[ASDisplayNode alloc] init];
node2.accessibilityFrame = CGRectMake(0, 0, 100, 250);
ASDisplayNode *node3 = [[ASDisplayNode alloc] init];
node3.accessibilityFrame = CGRectMake(0, 0, 100, 200);
ASDisplayNode *node4 = [[ASDisplayNode alloc] init];
node4.accessibilityFrame = CGRectMake(0, 0, 100, 150);
NSMutableArray *elements = [@[node2, node4, node3, node1] mutableCopy];
SortAccessibilityElements(elements);
XCTAssertEqual(elements[0], node1);
XCTAssertEqual(elements[1], node2);
XCTAssertEqual(elements[2], node3);
XCTAssertEqual(elements[3], node4);
}
- (void)testSubnodeIsModal {
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
ASDKViewController *vc = [[ASDKViewController alloc] initWithNode:node];
window.rootViewController = vc;
[window makeKeyAndVisible];
[window layoutIfNeeded];
ASTextNode *label1 = [[ASTextNode alloc] init];
label1.attributedText = [[NSAttributedString alloc] initWithString:@"label1"];
label1.frame = CGRectMake(10, 80, 300, 20);
[node addSubnode:label1];
ASTextNode *label2 = [[ASTextNode alloc] init];
label2.attributedText = [[NSAttributedString alloc] initWithString:@"label2"];
label2.frame = CGRectMake(10, CGRectGetMaxY(label1.frame) + 8, 300, 20);
[node addSubnode:label2];
ASDisplayNode *modalNode = [[ASDisplayNode alloc] init];
modalNode.frame = CGRectInset(CGRectUnion(label1.frame, label2.frame), -8, -8);
// This is kind of cheating. When voice over is activated, the modal node will end up reporting that it
// has 1 accessibilityElement. But getting that to happen in a unit test doesn't seem possible.
id modalMock = OCMPartialMock(modalNode);
OCMStub([modalMock accessibilityElementCount]).andReturn(1);
[node addSubnode:modalMock];
ASTextNode *label3 = [[ASTextNode alloc] init];
label3.attributedText = [[NSAttributedString alloc] initWithString:@"label6"];
label3.frame = CGRectMake(8, 4, 200, 20);
[modalNode addSubnode:label3];
modalNode.accessibilityViewIsModal = YES;
NSArray *elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 1);
XCTAssertTrue([elements containsObject:modalNode.view]);
}
- (void)testMultipleSubnodesAreModal {
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
ASDKViewController *vc = [[ASDKViewController alloc] initWithNode:node];
window.rootViewController = vc;
[window makeKeyAndVisible];
[window layoutIfNeeded];
ASTextNode *label1 = [[ASTextNode alloc] init];
label1.attributedText = [[NSAttributedString alloc] initWithString:@"label1"];
label1.frame = CGRectMake(10, 80, 300, 20);
[node addSubnode:label1];
ASTextNode *label2 = [[ASTextNode alloc] init];
label2.attributedText = [[NSAttributedString alloc] initWithString:@"label2"];
label2.frame = CGRectMake(10, CGRectGetMaxY(label1.frame) + 8, 300, 20);
[node addSubnode:label2];
ASDisplayNode *modalNode1 = [[ASDisplayNode alloc] init];
modalNode1.frame = CGRectInset(CGRectUnion(label1.frame, label2.frame), -8, -8);
// This is kind of cheating. When voice over is activated, the modal node will end up reporting that it
// has 1 accessibilityElement. But getting that to happen in a unit test doesn't seem possible.
id modalMock1 = OCMPartialMock(modalNode1);
OCMStub([modalMock1 accessibilityElementCount]).andReturn(1);
ASTextNode *label3 = [[ASTextNode alloc] init];
label3.attributedText = [[NSAttributedString alloc] initWithString:@"label6"];
label3.frame = CGRectMake(8, 4, 200, 20);
[modalNode1 addSubnode:label3];
modalNode1.accessibilityViewIsModal = YES;
ASDisplayNode *modalNode2 = [[ASDisplayNode alloc] init];
modalNode2.frame = CGRectOffset(modalNode1.frame, 0, modalNode1.frame.size.height + 10);
id modalMock2 = OCMPartialMock(modalNode2);
OCMStub([modalMock2 accessibilityElementCount]).andReturn(1);
ASTextNode *label4 = [[ASTextNode alloc] init];
label4.attributedText = [[NSAttributedString alloc] initWithString:@"label6"];
label4.frame = CGRectMake(8, 4, 200, 20);
[modalNode2 addSubnode:label4];
modalNode2.accessibilityViewIsModal = YES;
// add modalNode1 last, and assert that it is the one that appears in accessibilityElements
// (UIKit uses the last modal subview in subviews as the modal element).
[node addSubnode:modalMock2];
[node addSubnode:modalMock1];
NSArray *elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 1);
XCTAssertTrue([elements containsObject:modalNode1.view]);
// let's change which node is modal and make sure the elements get updated.
modalNode1.accessibilityViewIsModal = NO;
elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 1);
XCTAssertTrue([elements containsObject:modalNode2.view]);
}
- (void)testAccessibilityElementsHidden {
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
ASDKViewController *vc = [[ASDKViewController alloc] initWithNode:node];
window.rootViewController = vc;
[window makeKeyAndVisible];
[window layoutIfNeeded];
ASTextNode *label1 = [[ASTextNode alloc] init];
label1.attributedText = [[NSAttributedString alloc] initWithString:@"on screen"];
label1.frame = CGRectMake(0, 0, 100, 20);
ASTextNode *label2 = [[ASTextNode alloc] init];
label2.attributedText = [[NSAttributedString alloc] initWithString:@"partially on screen y"];
label2.frame = CGRectMake(0, 20, 100, 20);
[node addSubnode:label1];
[node addSubnode:label2];
NSArray *elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 2);
XCTAssertTrue([elements containsObject:label1.view]);
XCTAssertTrue([elements containsObject:label2.view]);
node.accessibilityElementsHidden = YES;
elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 0);
node.accessibilityElementsHidden = NO;
elements = [node.view accessibilityElements];
XCTAssertTrue(elements.count == 2);
XCTAssertTrue([elements containsObject:label1.view]);
XCTAssertTrue([elements containsObject:label2.view]);
}
@end