Flutter iOS Embedder
accessibility_bridge_test.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #import <OCMock/OCMock.h>
6 #import <XCTest/XCTest.h>
7 
8 #include <utility>
9 
10 #import "flutter/fml/message_loop.h"
11 #import "flutter/fml/thread.h"
23 
25 
26 @class MockPlatformView;
27 __weak static MockPlatformView* gMockPlatformView = nil;
28 
30 - (id)_accessibilityHitTest:(CGPoint)point withEvent:(UIEvent*)event;
31 @end
32 
33 @interface MockPlatformView : UIView
34 @end
35 @implementation MockPlatformView
36 
37 - (instancetype)init {
38  self = [super init];
39  if (self) {
40  gMockPlatformView = self;
41  }
42  return self;
43 }
44 
45 - (void)dealloc {
46  gMockPlatformView = nil;
47 }
48 
49 @end
50 
52 @property(nonatomic, strong) UIView* view;
53 @end
54 
55 @implementation MockFlutterPlatformView
56 
57 - (instancetype)init {
58  if (self = [super init]) {
59  _view = [[MockPlatformView alloc] init];
60  }
61  return self;
62 }
63 
64 @end
65 
67 @end
68 
69 @implementation MockFlutterPlatformFactory
70 - (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
71  viewIdentifier:(int64_t)viewId
72  arguments:(id _Nullable)args {
73  return [[MockFlutterPlatformView alloc] init];
74 }
75 
76 @end
77 
78 namespace flutter {
79 namespace {
80 class MockDelegate : public PlatformView::Delegate {
81  public:
82  void OnPlatformViewCreated(std::unique_ptr<Surface> surface) override {}
83  void OnPlatformViewDestroyed() override {}
84  void OnPlatformViewScheduleFrame() override {}
85  void OnPlatformViewAddView(int64_t view_id,
86  const ViewportMetrics& viewport_metrics,
87  AddViewCallback callback) override {}
88  void OnPlatformViewRemoveView(int64_t view_id, RemoveViewCallback callback) override {}
89  void OnPlatformViewSendViewFocusEvent(const ViewFocusEvent& event) override {};
90  void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
91  void OnPlatformViewSetViewportMetrics(int64_t view_id, const ViewportMetrics& metrics) override {}
92  const flutter::Settings& OnPlatformViewGetSettings() const override { return settings_; }
93  void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}
94  void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet) override {
95  }
96  HitTestResponse OnPlatformViewHitTest(int64_t view_id, const flutter::PointData offset) override {
97  return {.has_platform_view = false};
98  }
99  void OnPlatformViewDispatchSemanticsAction(int64_t view_id,
100  int32_t node_id,
101  SemanticsAction action,
102  fml::MallocMapping args) override {}
103  void OnPlatformViewSetSemanticsEnabled(bool enabled) override {}
104  void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {}
105  void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture) override {}
106  void OnPlatformViewUnregisterTexture(int64_t texture_id) override {}
107  void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {}
108 
109  void LoadDartDeferredLibrary(intptr_t loading_unit_id,
110  std::unique_ptr<const fml::Mapping> snapshot_data,
111  std::unique_ptr<const fml::Mapping> snapshot_instructions) override {
112  }
113  void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
114  const std::string error_message,
115  bool transient) override {}
116  void UpdateAssetResolverByType(std::unique_ptr<flutter::AssetResolver> updated_asset_resolver,
117  flutter::AssetResolver::AssetResolverType type) override {}
118  std::shared_ptr<fml::BasicTaskRunner> OnPlatformViewGetShutdownSafeIOTaskRunner() const override {
119  return nullptr;
120  }
121 
122  flutter::Settings settings_;
123 };
124 
125 class MockIosDelegate : public AccessibilityBridge::IosDelegate {
126  public:
127  bool IsFlutterViewControllerPresentingModalViewController(
128  FlutterViewController* view_controller) override {
129  return result_IsFlutterViewControllerPresentingModalViewController_;
130  };
131 
132  void PostAccessibilityNotification(UIAccessibilityNotifications notification,
133  id argument) override {
134  if (on_PostAccessibilityNotification_) {
135  on_PostAccessibilityNotification_(notification, argument);
136  }
137  }
138  std::function<void(UIAccessibilityNotifications, id)> on_PostAccessibilityNotification_;
139  bool result_IsFlutterViewControllerPresentingModalViewController_ = false;
140 };
141 } // namespace
142 } // namespace flutter
143 
144 namespace {
145 fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name) {
146  auto thread = std::make_unique<fml::Thread>(name);
147  auto runner = thread->GetTaskRunner();
148  return runner;
149 }
150 
151 void StubLoadedView(id viewController, id view) {
152  OCMStub([viewController view]).andReturn(view);
153  OCMStub([viewController viewIfLoaded]).andReturn(view);
154 }
155 
156 void UpdateRootSemantics(flutter::AccessibilityBridge* bridge, std::string label = "root") {
157  flutter::SemanticsNodeUpdates nodes;
158  flutter::SemanticsNode semantics_node;
159  semantics_node.id = kRootNodeId;
160  semantics_node.label = std::move(label);
161  nodes[kRootNodeId] = semantics_node;
162  flutter::CustomAccessibilityActionUpdates actions;
163  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
164 }
165 } // namespace
166 
167 @interface AccessibilityBridgeTest : XCTestCase
168 @end
169 
170 @implementation AccessibilityBridgeTest
171 
172 - (void)testCreate {
173  flutter::MockDelegate mock_delegate;
174  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
175  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
176  /*platform=*/thread_task_runner,
177  /*raster=*/thread_task_runner,
178  /*ui=*/thread_task_runner,
179  /*io=*/thread_task_runner);
180  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
181  /*delegate=*/mock_delegate,
182  /*platform_views_controller=*/nil,
183  /*task_runners=*/runners,
184  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
185  auto bridge =
186  std::make_unique<flutter::AccessibilityBridge>(/*view=*/nil,
187  /*platform_view=*/platform_view.get(),
188  /*platform_views_controller=*/nil);
189  XCTAssertTrue(bridge.get());
190 }
191 
192 - (void)testSetViewControllerRebindsWhenSameViewControllerLoadsView {
193  flutter::MockDelegate mock_delegate;
194  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
195  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
196  /*platform=*/thread_task_runner,
197  /*raster=*/thread_task_runner,
198  /*ui=*/thread_task_runner,
199  /*io=*/thread_task_runner);
200  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
201  /*delegate=*/mock_delegate,
202  /*platform_views_controller=*/nil,
203  /*task_runners=*/runners,
204  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
205 
206  id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
207  id engine = OCMClassMock([FlutterEngine class]);
208  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
209  id mockFlutterView = OCMClassMock([FlutterView class]);
210  __block UIView* viewIfLoaded = nil;
211  OCMStub([mockFlutterViewController viewIfLoaded]).andDo(^(NSInvocation* invocation) {
212  [invocation setReturnValue:&viewIfLoaded];
213  });
214  OCMStub([mockFlutterViewController engine]).andReturn(engine);
215  OCMStub([engine binaryMessenger]).andReturn(messenger);
216 
217  auto bridge =
218  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
219  /*platform_view=*/platform_view.get(),
220  /*platform_views_controller=*/nil);
221  flutter::AccessibilityBridge* bridge_ptr = bridge.get();
222  UpdateRootSemantics(bridge.get(), "loaded later");
223 
224  viewIfLoaded = mockFlutterView;
225  OCMExpect([mockFlutterView setAccessibilityElements:[OCMArg checkWithBlock:^BOOL(NSArray* value) {
226  if ([value count] != 1) {
227  return NO;
228  }
229  if (![value[0] isKindOfClass:[SemanticsObjectContainer class]]) {
230  return NO;
231  }
232  SemanticsObjectContainer* container = value[0];
233  SemanticsObject* object = container.semanticsObject;
234  return object.uid == kRootNodeId && object.bridge == bridge_ptr &&
235  object.node.label == "loaded later";
236  }]]);
237  bridge->SetViewController(mockFlutterViewController, /*previousView=*/nil);
238  OCMVerifyAll(mockFlutterView);
239 
240  [engine stopMocking];
241 }
242 
243 - (void)testUpdateSemanticsEmpty {
244  flutter::MockDelegate mock_delegate;
245  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
246  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
247  /*platform=*/thread_task_runner,
248  /*raster=*/thread_task_runner,
249  /*ui=*/thread_task_runner,
250  /*io=*/thread_task_runner);
251  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
252  /*delegate=*/mock_delegate,
253  /*platform_views_controller=*/nil,
254  /*task_runners=*/runners,
255  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
256  id mockFlutterView = OCMClassMock([FlutterView class]);
257  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
258  OCMStub([mockFlutterViewController viewIfLoaded]).andReturn(mockFlutterView);
259  OCMReject([mockFlutterView setAccessibilityElements:[OCMArg any]]);
260  auto bridge =
261  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
262  /*platform_view=*/platform_view.get(),
263  /*platform_views_controller=*/nil);
264  flutter::SemanticsNodeUpdates nodes;
265  flutter::CustomAccessibilityActionUpdates actions;
266  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
267  OCMVerifyAll(mockFlutterView);
268  bridge->SetViewController(nil, /*previousView=*/nil);
269 }
270 
271 - (void)testUpdateSemanticsOneNode {
272  flutter::MockDelegate mock_delegate;
273  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
274  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
275  /*platform=*/thread_task_runner,
276  /*raster=*/thread_task_runner,
277  /*ui=*/thread_task_runner,
278  /*io=*/thread_task_runner);
279  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
280  /*delegate=*/mock_delegate,
281  /*platform_views_controller=*/nil,
282  /*task_runners=*/runners,
283  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
284  id mockFlutterView = OCMClassMock([FlutterView class]);
285  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
286  StubLoadedView(mockFlutterViewController, mockFlutterView);
287  std::string label = "some label";
288 
289  __block auto bridge =
290  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
291  /*platform_view=*/platform_view.get(),
292  /*platform_views_controller=*/nil);
293 
294  OCMExpect([mockFlutterView setAccessibilityElements:[OCMArg checkWithBlock:^BOOL(NSArray* value) {
295  if ([value count] != 1) {
296  return NO;
297  } else {
298  SemanticsObjectContainer* container = value[0];
299  SemanticsObject* object = container.semanticsObject;
300  return object.uid == kRootNodeId &&
301  object.bridge == bridge.get() && object.node.label == label;
302  }
303  }]]);
304 
305  flutter::SemanticsNodeUpdates nodes;
306  flutter::SemanticsNode semantics_node;
307  semantics_node.id = kRootNodeId;
308  semantics_node.label = label;
309  nodes[kRootNodeId] = semantics_node;
310  flutter::CustomAccessibilityActionUpdates actions;
311  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
312  OCMVerifyAll(mockFlutterView);
313 }
314 
315 - (void)testSetViewControllerClearsPreviousViewAccessibilityElementsOwnedByBridge {
316  flutter::MockDelegate mock_delegate;
317  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
318  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
319  /*platform=*/thread_task_runner,
320  /*raster=*/thread_task_runner,
321  /*ui=*/thread_task_runner,
322  /*io=*/thread_task_runner);
323  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
324  /*delegate=*/mock_delegate,
325  /*platform_views_controller=*/nil,
326  /*task_runners=*/runners,
327  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
328  UIView* previousView = [[UIView alloc] init];
329  UIView* nextView = [[UIView alloc] init];
330  id previousViewController = OCMClassMock([FlutterViewController class]);
331  id nextViewController = OCMClassMock([FlutterViewController class]);
332  StubLoadedView(previousViewController, previousView);
333  StubLoadedView(nextViewController, nextView);
334 
335  auto bridge =
336  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/previousViewController,
337  /*platform_view=*/platform_view.get(),
338  /*platform_views_controller=*/nil);
339  UpdateRootSemantics(bridge.get(), "owned");
340 
341  XCTAssertNotNil(previousView.accessibilityElements);
342  XCTAssertNil(nextView.accessibilityElements);
343  bridge->SetViewController(nextViewController, static_cast<FlutterView*>(previousView));
344  XCTAssertNil(previousView.accessibilityElements);
345  XCTAssertNotNil(nextView.accessibilityElements);
346 }
347 
348 - (void)testSetViewControllerDoesNotClearPreviousViewElementsOwnedByAnotherBridge {
349  flutter::MockDelegate mock_delegate;
350  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
351  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
352  /*platform=*/thread_task_runner,
353  /*raster=*/thread_task_runner,
354  /*ui=*/thread_task_runner,
355  /*io=*/thread_task_runner);
356  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
357  /*delegate=*/mock_delegate,
358  /*platform_views_controller=*/nil,
359  /*task_runners=*/runners,
360  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
361  UIView* previousView = [[UIView alloc] init];
362  UIView* nextView = [[UIView alloc] init];
363  UIView* otherView = [[UIView alloc] init];
364  id previousViewController = OCMClassMock([FlutterViewController class]);
365  id nextViewController = OCMClassMock([FlutterViewController class]);
366  id otherViewController = OCMClassMock([FlutterViewController class]);
367  StubLoadedView(previousViewController, previousView);
368  StubLoadedView(nextViewController, nextView);
369  StubLoadedView(otherViewController, otherView);
370 
371  auto bridge =
372  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/previousViewController,
373  /*platform_view=*/platform_view.get(),
374  /*platform_views_controller=*/nil);
375  auto other_bridge =
376  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/otherViewController,
377  /*platform_view=*/platform_view.get(),
378  /*platform_views_controller=*/nil);
379  UpdateRootSemantics(bridge.get(), "owned");
380  UpdateRootSemantics(other_bridge.get(), "other");
381  NSArray* otherBridgeElements = otherView.accessibilityElements;
382  XCTAssertNotNil(otherBridgeElements);
383  previousView.accessibilityElements = otherBridgeElements;
384 
385  bridge->SetViewController(nextViewController, static_cast<FlutterView*>(previousView));
386  XCTAssertEqual(previousView.accessibilityElements, otherBridgeElements);
387 }
388 
389 - (void)testSetViewControllerDoesNotClearPreviousViewAccessibilityElementsOwnedByUIKit {
390  flutter::MockDelegate mock_delegate;
391  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
392  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
393  /*platform=*/thread_task_runner,
394  /*raster=*/thread_task_runner,
395  /*ui=*/thread_task_runner,
396  /*io=*/thread_task_runner);
397  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
398  /*delegate=*/mock_delegate,
399  /*platform_views_controller=*/nil,
400  /*task_runners=*/runners,
401  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
402  UIView* previousView = [[UIView alloc] init];
403  UIView* nextView = [[UIView alloc] init];
404  id previousViewController = OCMClassMock([FlutterViewController class]);
405  id nextViewController = OCMClassMock([FlutterViewController class]);
406  StubLoadedView(previousViewController, previousView);
407  StubLoadedView(nextViewController, nextView);
408 
409  auto bridge =
410  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/previousViewController,
411  /*platform_view=*/platform_view.get(),
412  /*platform_views_controller=*/nil);
413  UIAccessibilityElement* nativeElement =
414  [[UIAccessibilityElement alloc] initWithAccessibilityContainer:previousView];
415  NSArray* nativeElements = @[ nativeElement ];
416  previousView.accessibilityElements = nativeElements;
417 
418  bridge->SetViewController(nextViewController, static_cast<FlutterView*>(previousView));
419  XCTAssertEqual(previousView.accessibilityElements, nativeElements);
420 }
421 
422 - (void)testSetViewControllerDoesNotClearMixedAccessibilityElements {
423  flutter::MockDelegate mock_delegate;
424  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
425  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
426  /*platform=*/thread_task_runner,
427  /*raster=*/thread_task_runner,
428  /*ui=*/thread_task_runner,
429  /*io=*/thread_task_runner);
430  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
431  /*delegate=*/mock_delegate,
432  /*platform_views_controller=*/nil,
433  /*task_runners=*/runners,
434  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
435  UIView* previousView = [[UIView alloc] init];
436  UIView* nextView = [[UIView alloc] init];
437  id previousViewController = OCMClassMock([FlutterViewController class]);
438  id nextViewController = OCMClassMock([FlutterViewController class]);
439  StubLoadedView(previousViewController, previousView);
440  StubLoadedView(nextViewController, nextView);
441 
442  auto bridge =
443  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/previousViewController,
444  /*platform_view=*/platform_view.get(),
445  /*platform_views_controller=*/nil);
446  UpdateRootSemantics(bridge.get(), "owned");
447  id bridgeElement = previousView.accessibilityElements.firstObject;
448  UIAccessibilityElement* nativeElement =
449  [[UIAccessibilityElement alloc] initWithAccessibilityContainer:previousView];
450  NSArray* mixedElements = @[ bridgeElement, nativeElement ];
451  previousView.accessibilityElements = mixedElements;
452 
453  bridge->SetViewController(nextViewController, static_cast<FlutterView*>(previousView));
454  XCTAssertEqual(previousView.accessibilityElements, mixedElements);
455  XCTAssertNotNil(nextView.accessibilityElements);
456 }
457 
458 - (void)testIsVoiceOverRunning {
459  flutter::MockDelegate mock_delegate;
460  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
461  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
462  /*platform=*/thread_task_runner,
463  /*raster=*/thread_task_runner,
464  /*ui=*/thread_task_runner,
465  /*io=*/thread_task_runner);
466  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
467  /*delegate=*/mock_delegate,
468  /*platform_views_controller=*/nil,
469  /*task_runners=*/runners,
470  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
471  id mockFlutterView = OCMClassMock([FlutterView class]);
472  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
473  StubLoadedView(mockFlutterViewController, mockFlutterView);
474  OCMStub([mockFlutterViewController isVoiceOverRunning]).andReturn(YES);
475 
476  __block auto bridge =
477  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
478  /*platform_view=*/platform_view.get(),
479  /*platform_views_controller=*/nil);
480 
481  XCTAssertTrue(bridge->isVoiceOverRunning());
482 }
483 
484 - (void)testSemanticsDeallocated {
485  @autoreleasepool {
486  flutter::MockDelegate mock_delegate;
487  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
488  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
489  /*platform=*/thread_task_runner,
490  /*raster=*/thread_task_runner,
491  /*ui=*/thread_task_runner,
492  /*io=*/thread_task_runner);
493 
494  FlutterPlatformViewsController* flutterPlatformViewsController =
495  [[FlutterPlatformViewsController alloc] init];
496  flutterPlatformViewsController.taskRunner =
497  [[FlutterFMLTaskRunner alloc] initWithTaskRunner:thread_task_runner];
498  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
499  /*delegate=*/mock_delegate,
500  /*platform_views_controller=*/flutterPlatformViewsController,
501  /*task_runners=*/runners,
502  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
503  id mockFlutterView = OCMClassMock([FlutterView class]);
504  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
505  StubLoadedView(mockFlutterViewController, mockFlutterView);
506  std::string label = "some label";
507  flutterPlatformViewsController.flutterView = mockFlutterView;
508 
509  MockFlutterPlatformFactory* factory = [[MockFlutterPlatformFactory alloc] init];
510  [flutterPlatformViewsController
511  registerViewFactory:factory
512  withId:@"MockFlutterPlatformView"
513  gestureRecognizersBlockingPolicy:FlutterPlatformViewGestureRecognizersBlockingPolicyEager];
514  FlutterResult result = ^(id result) {
515  };
516  [flutterPlatformViewsController
518  arguments:@{
519  @"id" : @2,
520  @"viewType" : @"MockFlutterPlatformView"
521  }]
522  result:result];
523 
524  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
525  /*view_controller=*/mockFlutterViewController,
526  /*platform_view=*/platform_view.get(),
527  /*platform_views_controller=*/flutterPlatformViewsController);
528 
529  flutter::SemanticsNodeUpdates nodes;
530  flutter::SemanticsNode semantics_node;
531  semantics_node.id = 2;
532  semantics_node.platformViewId = 2;
533  semantics_node.label = label;
534  nodes[kRootNodeId] = semantics_node;
535  flutter::CustomAccessibilityActionUpdates actions;
536  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
537  XCTAssertNotNil(gMockPlatformView);
538  [flutterPlatformViewsController reset];
539  }
540  XCTAssertNil(gMockPlatformView);
541 }
542 
543 - (void)testSemanticsDeallocatedWithoutLoadingView {
544  id engine = OCMClassMock([FlutterEngine class]);
545  FlutterViewController* flutterViewController =
546  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
547  @autoreleasepool {
548  flutter::MockDelegate mock_delegate;
549  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
550  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
551  /*platform=*/thread_task_runner,
552  /*raster=*/thread_task_runner,
553  /*ui=*/thread_task_runner,
554  /*io=*/thread_task_runner);
555 
556  FlutterPlatformViewsController* flutterPlatformViewsController =
557  [[FlutterPlatformViewsController alloc] init];
558  flutterPlatformViewsController.taskRunner =
559  [[FlutterFMLTaskRunner alloc] initWithTaskRunner:thread_task_runner];
560  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
561  /*delegate=*/mock_delegate,
562  /*platform_views_controller=*/flutterPlatformViewsController,
563  /*task_runners=*/runners,
564  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
565 
566  MockFlutterPlatformFactory* factory = [[MockFlutterPlatformFactory alloc] init];
567  [flutterPlatformViewsController
568  registerViewFactory:factory
569  withId:@"MockFlutterPlatformView"
570  gestureRecognizersBlockingPolicy:FlutterPlatformViewGestureRecognizersBlockingPolicyEager];
571  FlutterResult result = ^(id result) {
572  };
573  [flutterPlatformViewsController
575  arguments:@{
576  @"id" : @2,
577  @"viewType" : @"MockFlutterPlatformView"
578  }]
579  result:result];
580 
581  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
582  /*view_controller=*/flutterViewController,
583  /*platform_view=*/platform_view.get(),
584  /*platform_views_controller=*/flutterPlatformViewsController);
585 
586  XCTAssertNotNil(gMockPlatformView);
587  [flutterPlatformViewsController reset];
588  platform_view->NotifyDestroyed();
589  }
590  XCTAssertNil(gMockPlatformView);
591  XCTAssertNil(flutterViewController.viewIfLoaded);
592  [flutterViewController deregisterNotifications];
593 }
594 
595 - (void)testReplacedSemanticsDoesNotCleanupChildren {
596  flutter::MockDelegate mock_delegate;
597  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
598  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
599  /*platform=*/thread_task_runner,
600  /*raster=*/thread_task_runner,
601  /*ui=*/thread_task_runner,
602  /*io=*/thread_task_runner);
603 
604  FlutterPlatformViewsController* flutterPlatformViewsController =
605  [[FlutterPlatformViewsController alloc] init];
606  flutterPlatformViewsController.taskRunner =
607  [[FlutterFMLTaskRunner alloc] initWithTaskRunner:thread_task_runner];
608  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
609  /*delegate=*/mock_delegate,
610  /*platform_views_controller=*/flutterPlatformViewsController,
611  /*task_runners=*/runners,
612  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
613  id engine = OCMClassMock([FlutterEngine class]);
614  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
615  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
616  opaque:YES
617  enableWideGamut:NO];
618  StubLoadedView(mockFlutterViewController, flutterView);
619  std::string label = "some label";
620  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
621  /*view_controller=*/mockFlutterViewController,
622  /*platform_view=*/platform_view.get(),
623  /*platform_views_controller=*/flutterPlatformViewsController);
624  @autoreleasepool {
625  flutter::SemanticsNodeUpdates nodes;
626  flutter::SemanticsNode parent;
627  parent.id = 0;
628  parent.rect = SkRect::MakeXYWH(0, 0, 100, 200);
629  parent.label = "label";
630  parent.value = "value";
631  parent.hint = "hint";
632 
633  flutter::SemanticsNode node;
634  node.id = 1;
635  node.rect = SkRect::MakeXYWH(0, 0, 100, 200);
636  node.label = "label";
637  node.value = "value";
638  node.hint = "hint";
639  node.scrollExtentMax = 100.0;
640  node.scrollPosition = 0.0;
641  parent.childrenInTraversalOrder.push_back(1);
642  parent.childrenInHitTestOrder.push_back(1);
643 
644  flutter::SemanticsNode child;
645  child.id = 2;
646  child.rect = SkRect::MakeXYWH(0, 0, 100, 200);
647  child.label = "label";
648  child.value = "value";
649  child.hint = "hint";
650  node.childrenInTraversalOrder.push_back(2);
651  node.childrenInHitTestOrder.push_back(2);
652 
653  nodes[0] = parent;
654  nodes[1] = node;
655  nodes[2] = child;
656  flutter::CustomAccessibilityActionUpdates actions;
657  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
658 
659  // Add implicit scroll from node 1 to cause replacement.
660  flutter::SemanticsNodeUpdates new_nodes;
661  flutter::SemanticsNode new_node;
662  new_node.id = 1;
663  new_node.rect = SkRect::MakeXYWH(0, 0, 100, 200);
664  new_node.flags.hasImplicitScrolling = true;
665  new_node.actions = flutter::kHorizontalScrollSemanticsActions;
666  new_node.label = "label";
667  new_node.value = "value";
668  new_node.hint = "hint";
669  new_node.scrollExtentMax = 100.0;
670  new_node.scrollPosition = 0.0;
671  new_node.childrenInTraversalOrder.push_back(2);
672  new_node.childrenInHitTestOrder.push_back(2);
673 
674  new_nodes[1] = new_node;
675  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
676  }
677  /// The old node should be deallocated at this moment. Procced to check
678  /// accessibility tree integrity.
679  id rootContainer = flutterView.accessibilityElements[0];
680  XCTAssertTrue([rootContainer accessibilityElementCount] ==
681  2); // one for root, one for scrollable.
682  id scrollableContainer = [rootContainer accessibilityElementAtIndex:1];
683  XCTAssertTrue([scrollableContainer accessibilityElementCount] ==
684  2); // one for scrollable, one for scrollable child.
685  id child = [scrollableContainer accessibilityElementAtIndex:1];
686  /// Replacing node 1 should not accidentally clean up its child's container.
687  XCTAssertNotNil([child accessibilityContainer]);
688 }
689 
690 - (void)testScrollableSemanticsDeallocated {
691  flutter::MockDelegate mock_delegate;
692  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
693  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
694  /*platform=*/thread_task_runner,
695  /*raster=*/thread_task_runner,
696  /*ui=*/thread_task_runner,
697  /*io=*/thread_task_runner);
698 
699  FlutterPlatformViewsController* flutterPlatformViewsController =
700  [[FlutterPlatformViewsController alloc] init];
701  flutterPlatformViewsController.taskRunner =
702  [[FlutterFMLTaskRunner alloc] initWithTaskRunner:thread_task_runner];
703  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
704  /*delegate=*/mock_delegate,
705  /*platform_views_controller=*/flutterPlatformViewsController,
706  /*task_runners=*/runners,
707  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
708  id engine = OCMClassMock([FlutterEngine class]);
709  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
710  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
711  opaque:YES
712  enableWideGamut:NO];
713  StubLoadedView(mockFlutterViewController, flutterView);
714  std::string label = "some label";
715  @autoreleasepool {
716  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
717  /*view_controller=*/mockFlutterViewController,
718  /*platform_view=*/platform_view.get(),
719  /*platform_views_controller=*/flutterPlatformViewsController);
720 
721  flutter::SemanticsNodeUpdates nodes;
722  flutter::SemanticsNode parent;
723  parent.id = 0;
724  parent.rect = SkRect::MakeXYWH(0, 0, 100, 200);
725  parent.label = "label";
726  parent.value = "value";
727  parent.hint = "hint";
728 
729  flutter::SemanticsNode node;
730  node.id = 1;
731  node.flags.hasImplicitScrolling = true;
732  node.actions = flutter::kHorizontalScrollSemanticsActions;
733  node.rect = SkRect::MakeXYWH(0, 0, 100, 200);
734  node.label = "label";
735  node.value = "value";
736  node.hint = "hint";
737  node.scrollExtentMax = 100.0;
738  node.scrollPosition = 0.0;
739  parent.childrenInTraversalOrder.push_back(1);
740  parent.childrenInHitTestOrder.push_back(1);
741  nodes[0] = parent;
742  nodes[1] = node;
743  flutter::CustomAccessibilityActionUpdates actions;
744  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
745  XCTAssertTrue([flutterView.subviews count] == 1);
746  XCTAssertTrue([flutterView.subviews[0] isKindOfClass:[FlutterSemanticsScrollView class]]);
747  XCTAssertTrue([flutterView.subviews[0].accessibilityLabel isEqualToString:@"label"]);
748 
749  // Remove the scrollable from the tree.
750  flutter::SemanticsNodeUpdates new_nodes;
751  flutter::SemanticsNode new_parent;
752  new_parent.id = 0;
753  new_parent.rect = SkRect::MakeXYWH(0, 0, 100, 200);
754  new_parent.label = "label";
755  new_parent.value = "value";
756  new_parent.hint = "hint";
757  new_nodes[0] = new_parent;
758  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
759  }
760  XCTAssertTrue([flutterView.subviews count] == 0);
761 }
762 
763 - (void)testBridgeReplacesSemanticsNode {
764  flutter::MockDelegate mock_delegate;
765  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
766  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
767  /*platform=*/thread_task_runner,
768  /*raster=*/thread_task_runner,
769  /*ui=*/thread_task_runner,
770  /*io=*/thread_task_runner);
771 
772  FlutterPlatformViewsController* flutterPlatformViewsController =
773  [[FlutterPlatformViewsController alloc] init];
774  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
775  /*delegate=*/mock_delegate,
776  /*platform_views_controller=*/flutterPlatformViewsController,
777  /*task_runners=*/runners,
778  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
779  id engine = OCMClassMock([FlutterEngine class]);
780  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
781  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
782  opaque:YES
783  enableWideGamut:NO];
784  StubLoadedView(mockFlutterViewController, flutterView);
785  std::string label = "some label";
786  @autoreleasepool {
787  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
788  /*view_controller=*/mockFlutterViewController,
789  /*platform_view=*/platform_view.get(),
790  /*platform_views_controller=*/flutterPlatformViewsController);
791 
792  flutter::SemanticsNodeUpdates nodes;
793  flutter::SemanticsNode parent;
794  parent.id = 0;
795  parent.rect = SkRect::MakeXYWH(0, 0, 100, 200);
796  parent.label = "label";
797  parent.value = "value";
798  parent.hint = "hint";
799 
800  flutter::SemanticsNode node;
801  node.id = 1;
802  node.flags.hasImplicitScrolling = true;
803  node.actions = flutter::kHorizontalScrollSemanticsActions;
804  node.rect = SkRect::MakeXYWH(0, 0, 100, 200);
805  node.label = "label";
806  node.value = "value";
807  node.hint = "hint";
808  node.scrollExtentMax = 100.0;
809  node.scrollPosition = 0.0;
810  parent.childrenInTraversalOrder.push_back(1);
811  parent.childrenInHitTestOrder.push_back(1);
812  nodes[0] = parent;
813  nodes[1] = node;
814  flutter::CustomAccessibilityActionUpdates actions;
815  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
816  XCTAssertTrue([flutterView.subviews count] == 1);
817  XCTAssertTrue([flutterView.subviews[0] isKindOfClass:[FlutterSemanticsScrollView class]]);
818  XCTAssertTrue([flutterView.subviews[0].accessibilityLabel isEqualToString:@"label"]);
819 
820  // Remove implicit scroll from node 1.
821  flutter::SemanticsNodeUpdates new_nodes;
822  flutter::SemanticsNode new_node;
823  new_node.id = 1;
824  new_node.rect = SkRect::MakeXYWH(0, 0, 100, 200);
825  new_node.label = "label";
826  new_node.value = "value";
827  new_node.hint = "hint";
828  new_node.scrollExtentMax = 100.0;
829  new_node.scrollPosition = 0.0;
830  new_nodes[1] = new_node;
831  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
832  }
833  XCTAssertTrue([flutterView.subviews count] == 0);
834 }
835 
836 - (void)testAnnouncesRouteChanges {
837  flutter::MockDelegate mock_delegate;
838  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
839  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
840  /*platform=*/thread_task_runner,
841  /*raster=*/thread_task_runner,
842  /*ui=*/thread_task_runner,
843  /*io=*/thread_task_runner);
844  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
845  /*delegate=*/mock_delegate,
846  /*platform_views_controller=*/nil,
847  /*task_runners=*/runners,
848  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
849  id mockFlutterView = OCMClassMock([FlutterView class]);
850  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
851  StubLoadedView(mockFlutterViewController, mockFlutterView);
852 
853  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
854  [[NSMutableArray alloc] init];
855  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
856  ios_delegate->on_PostAccessibilityNotification_ =
857  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
858  [accessibility_notifications addObject:@{
859  @"notification" : @(notification),
860  @"argument" : argument ? argument : [NSNull null],
861  }];
862  };
863  __block auto bridge =
864  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
865  /*platform_view=*/platform_view.get(),
866  /*platform_views_controller=*/nil,
867  /*ios_delegate=*/std::move(ios_delegate));
868 
869  flutter::CustomAccessibilityActionUpdates actions;
870  flutter::SemanticsNodeUpdates nodes;
871 
872  flutter::SemanticsNode node1;
873  node1.id = 1;
874  node1.label = "node1";
875  node1.flags.scopesRoute = true;
876  node1.childrenInTraversalOrder = {2, 3};
877  node1.childrenInHitTestOrder = {2, 3};
878  nodes[node1.id] = node1;
879  flutter::SemanticsNode node2;
880  node2.id = 2;
881  node2.label = "node2";
882  nodes[node2.id] = node2;
883  flutter::SemanticsNode node3;
884  node3.id = 3;
885  node3.flags.namesRoute = true;
886  node3.label = "node3";
887  nodes[node3.id] = node3;
888  flutter::SemanticsNode root_node;
889  root_node.id = kRootNodeId;
890  root_node.flags.scopesRoute = true;
891  root_node.childrenInTraversalOrder = {1};
892  root_node.childrenInHitTestOrder = {1};
893  nodes[root_node.id] = root_node;
894  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
895 
896  XCTAssertEqual([accessibility_notifications count], 1ul);
897  XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], @"node3");
898  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
899  UIAccessibilityScreenChangedNotification);
900 }
901 
902 - (void)testRadioButtonIsNotSwitchButton {
903  flutter::MockDelegate mock_delegate;
904  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
905  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
906  /*platform=*/thread_task_runner,
907  /*raster=*/thread_task_runner,
908  /*ui=*/thread_task_runner,
909  /*io=*/thread_task_runner);
910  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
911  /*delegate=*/mock_delegate,
912  /*platform_views_controller=*/nil,
913  /*task_runners=*/runners,
914  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
915  id engine = OCMClassMock([FlutterEngine class]);
916  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
917  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
918  opaque:YES
919  enableWideGamut:NO];
920  StubLoadedView(mockFlutterViewController, flutterView);
921  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
922  __block auto bridge =
923  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
924  /*platform_view=*/platform_view.get(),
925  /*platform_views_controller=*/nil,
926  /*ios_delegate=*/std::move(ios_delegate));
927 
928  flutter::CustomAccessibilityActionUpdates actions;
929  flutter::SemanticsNodeUpdates nodes;
930 
931  flutter::SemanticsNode root_node;
932  root_node.id = kRootNodeId;
933  root_node.flags.isInMutuallyExclusiveGroup = true;
934  root_node.flags.isEnabled = flutter::SemanticsTristate::kTrue;
935  root_node.flags.isChecked = flutter::SemanticsCheckState::kFalse;
936  nodes[root_node.id] = root_node;
937  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
938 
939  SemanticsObjectContainer* rootContainer = flutterView.accessibilityElements[0];
940  FlutterSemanticsObject* rootNode = [rootContainer accessibilityElementAtIndex:0];
941 
942  XCTAssertTrue((rootNode.accessibilityTraits & UIAccessibilityTraitButton) > 0);
943  XCTAssertNil(rootNode.accessibilityValue);
944 }
945 
946 - (void)testSemanticObjectWithNoAccessibilityFlagNotMarkedAsResponsiveToUserInteraction {
947  flutter::MockDelegate mock_delegate;
948  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
949  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
950  /*platform=*/thread_task_runner,
951  /*raster=*/thread_task_runner,
952  /*ui=*/thread_task_runner,
953  /*io=*/thread_task_runner);
954  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
955  /*delegate=*/mock_delegate,
956  /*platform_views_controller=*/nil,
957  /*task_runners=*/runners,
958  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
959  id engine = OCMClassMock([FlutterEngine class]);
960  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
961  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
962  opaque:YES
963  enableWideGamut:NO];
964  StubLoadedView(mockFlutterViewController, flutterView);
965  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
966  __block auto bridge =
967  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
968  /*platform_view=*/platform_view.get(),
969  /*platform_views_controller=*/nil,
970  /*ios_delegate=*/std::move(ios_delegate));
971 
972  flutter::CustomAccessibilityActionUpdates actions;
973  flutter::SemanticsNodeUpdates nodes;
974 
975  flutter::SemanticsNode root_node;
976  root_node.id = kRootNodeId;
977 
978  nodes[root_node.id] = root_node;
979  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
980 
981  SemanticsObjectContainer* rootContainer = flutterView.accessibilityElements[0];
982  FlutterSemanticsObject* rootNode = [rootContainer accessibilityElementAtIndex:0];
983 
984  XCTAssertFalse(rootNode.accessibilityRespondsToUserInteraction);
985 }
986 
987 - (void)testSemanticObjectWithAccessibilityFlagsMarkedAsResponsiveToUserInteraction {
988  flutter::MockDelegate mock_delegate;
989  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
990  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
991  /*platform=*/thread_task_runner,
992  /*raster=*/thread_task_runner,
993  /*ui=*/thread_task_runner,
994  /*io=*/thread_task_runner);
995  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
996  /*delegate=*/mock_delegate,
997  /*platform_views_controller=*/nil,
998  /*task_runners=*/runners,
999  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1000  id engine = OCMClassMock([FlutterEngine class]);
1001  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1002  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
1003  opaque:YES
1004  enableWideGamut:NO];
1005  StubLoadedView(mockFlutterViewController, flutterView);
1006  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1007  __block auto bridge =
1008  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1009  /*platform_view=*/platform_view.get(),
1010  /*platform_views_controller=*/nil,
1011  /*ios_delegate=*/std::move(ios_delegate));
1012 
1013  flutter::CustomAccessibilityActionUpdates actions;
1014  flutter::SemanticsNodeUpdates nodes;
1015 
1016  flutter::SemanticsNode root_node;
1017  root_node.id = kRootNodeId;
1018  root_node.actions = static_cast<int32_t>(flutter::SemanticsAction::kTap);
1019 
1020  nodes[root_node.id] = root_node;
1021  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1022 
1023  SemanticsObjectContainer* rootContainer = flutterView.accessibilityElements[0];
1024  FlutterSemanticsObject* rootNode = [rootContainer accessibilityElementAtIndex:0];
1025 
1026  XCTAssertTrue(rootNode.accessibilityRespondsToUserInteraction);
1027 }
1028 
1029 // Regression test for:
1030 // https://github.com/flutter/flutter/issues/158477
1031 - (void)testLabeledParentAndChildNotInteractive {
1032  flutter::MockDelegate mock_delegate;
1033  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1034  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1035  /*platform=*/thread_task_runner,
1036  /*raster=*/thread_task_runner,
1037  /*ui=*/thread_task_runner,
1038  /*io=*/thread_task_runner);
1039 
1040  FlutterPlatformViewsController* flutterPlatformViewsController =
1041  [[FlutterPlatformViewsController alloc] init];
1042  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1043  /*delegate=*/mock_delegate,
1044  /*platform_views_controller=*/flutterPlatformViewsController,
1045  /*task_runners=*/runners,
1046  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1047  id engine = OCMClassMock([FlutterEngine class]);
1048  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1049  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
1050  opaque:YES
1051  enableWideGamut:NO];
1052  StubLoadedView(mockFlutterViewController, flutterView);
1053 
1054  @autoreleasepool {
1055  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
1056  /*view_controller=*/mockFlutterViewController,
1057  /*platform_view=*/platform_view.get(),
1058  /*platform_views_controller=*/flutterPlatformViewsController);
1059 
1060  flutter::SemanticsNodeUpdates nodes;
1061 
1062  flutter::SemanticsNode parent;
1063  parent.id = 0;
1064  parent.rect = SkRect::MakeXYWH(0, 0, 100, 200);
1065  parent.label = "parent_label";
1066 
1067  flutter::SemanticsNode node;
1068  node.id = 1;
1069  node.rect = SkRect::MakeXYWH(0, 0, 100, 200);
1070  node.label = "child_label";
1071 
1072  parent.childrenInTraversalOrder.push_back(1);
1073  parent.childrenInHitTestOrder.push_back(1);
1074  nodes[0] = parent;
1075  nodes[1] = node;
1076  flutter::CustomAccessibilityActionUpdates actions;
1077  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1078 
1079  SemanticsObjectContainer* parentContainer = flutterView.accessibilityElements[0];
1080  FlutterSemanticsObject* parentNode = [parentContainer accessibilityElementAtIndex:0];
1081  FlutterSemanticsObject* childNode = [parentContainer accessibilityElementAtIndex:1];
1082 
1083  XCTAssertTrue([parentNode.accessibilityLabel isEqualToString:@"parent_label"]);
1084  XCTAssertTrue([childNode.accessibilityLabel isEqualToString:@"child_label"]);
1085  XCTAssertFalse(parentNode.accessibilityRespondsToUserInteraction);
1086  XCTAssertFalse(childNode.accessibilityRespondsToUserInteraction);
1087  }
1088 }
1089 
1090 - (void)testLayoutChangeWithNonAccessibilityElement {
1091  flutter::MockDelegate mock_delegate;
1092  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1093  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1094  /*platform=*/thread_task_runner,
1095  /*raster=*/thread_task_runner,
1096  /*ui=*/thread_task_runner,
1097  /*io=*/thread_task_runner);
1098  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1099  /*delegate=*/mock_delegate,
1100  /*platform_views_controller=*/nil,
1101  /*task_runners=*/runners,
1102  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1103  id mockFlutterView = OCMClassMock([FlutterView class]);
1104  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1105  StubLoadedView(mockFlutterViewController, mockFlutterView);
1106 
1107  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1108  [[NSMutableArray alloc] init];
1109  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1110  ios_delegate->on_PostAccessibilityNotification_ =
1111  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1112  [accessibility_notifications addObject:@{
1113  @"notification" : @(notification),
1114  @"argument" : argument ? argument : [NSNull null],
1115  }];
1116  };
1117  __block auto bridge =
1118  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1119  /*platform_view=*/platform_view.get(),
1120  /*platform_views_controller=*/nil,
1121  /*ios_delegate=*/std::move(ios_delegate));
1122 
1123  flutter::CustomAccessibilityActionUpdates actions;
1124  flutter::SemanticsNodeUpdates nodes;
1125 
1126  flutter::SemanticsNode node1;
1127  node1.id = 1;
1128  node1.label = "node1";
1129  node1.childrenInTraversalOrder = {2, 3};
1130  node1.childrenInHitTestOrder = {2, 3};
1131  nodes[node1.id] = node1;
1132  flutter::SemanticsNode node2;
1133  node2.id = 2;
1134  node2.label = "node2";
1135  nodes[node2.id] = node2;
1136  flutter::SemanticsNode node3;
1137  node3.id = 3;
1138  node3.label = "node3";
1139  nodes[node3.id] = node3;
1140  flutter::SemanticsNode root_node;
1141  root_node.id = kRootNodeId;
1142  root_node.label = "root";
1143  root_node.childrenInTraversalOrder = {1};
1144  root_node.childrenInHitTestOrder = {1};
1145  nodes[root_node.id] = root_node;
1146  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1147 
1148  // Simulates the focusing on the node 1.
1149  bridge->AccessibilityObjectDidBecomeFocused(1);
1150 
1151  // In this update, we make node 1 unfocusable and trigger the
1152  // layout change. The accessibility bridge should send layoutchange
1153  // notification with the first focusable node under node 1
1154  flutter::CustomAccessibilityActionUpdates new_actions;
1155  flutter::SemanticsNodeUpdates new_nodes;
1156 
1157  flutter::SemanticsNode new_node1;
1158  new_node1.id = 1;
1159  new_node1.childrenInTraversalOrder = {2};
1160  new_node1.childrenInHitTestOrder = {2};
1161  new_nodes[new_node1.id] = new_node1;
1162  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/new_actions);
1163 
1164  XCTAssertEqual([accessibility_notifications count], 1ul);
1165  SemanticsObject* focusObject = accessibility_notifications[0][@"argument"];
1166  // Since node 1 is no longer focusable (no label), it will focus node 2 instead.
1167  XCTAssertEqual([focusObject uid], 2);
1168  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1169  UIAccessibilityLayoutChangedNotification);
1170 }
1171 
1172 - (void)testLayoutChangeDoesCallNativeAccessibility {
1173  flutter::MockDelegate mock_delegate;
1174  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1175  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1176  /*platform=*/thread_task_runner,
1177  /*raster=*/thread_task_runner,
1178  /*ui=*/thread_task_runner,
1179  /*io=*/thread_task_runner);
1180  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1181  /*delegate=*/mock_delegate,
1182  /*platform_views_controller=*/nil,
1183  /*task_runners=*/runners,
1184  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1185  id mockFlutterView = OCMClassMock([FlutterView class]);
1186  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1187  StubLoadedView(mockFlutterViewController, mockFlutterView);
1188 
1189  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1190  [[NSMutableArray alloc] init];
1191  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1192  ios_delegate->on_PostAccessibilityNotification_ =
1193  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1194  [accessibility_notifications addObject:@{
1195  @"notification" : @(notification),
1196  @"argument" : argument ? argument : [NSNull null],
1197  }];
1198  };
1199  __block auto bridge =
1200  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1201  /*platform_view=*/platform_view.get(),
1202  /*platform_views_controller=*/nil,
1203  /*ios_delegate=*/std::move(ios_delegate));
1204 
1205  flutter::CustomAccessibilityActionUpdates actions;
1206  flutter::SemanticsNodeUpdates nodes;
1207 
1208  flutter::SemanticsNode node1;
1209  node1.id = 1;
1210  node1.label = "node1";
1211  nodes[node1.id] = node1;
1212  flutter::SemanticsNode root_node;
1213  root_node.id = kRootNodeId;
1214  root_node.label = "root";
1215  root_node.flags.hasImplicitScrolling = true;
1216  root_node.childrenInTraversalOrder = {1};
1217  root_node.childrenInHitTestOrder = {1};
1218  nodes[root_node.id] = root_node;
1219  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1220 
1221  // Simulates the focusing on the node 0.
1222  bridge->AccessibilityObjectDidBecomeFocused(0);
1223 
1224  // Remove node 1 to trigger a layout change notification
1225  flutter::CustomAccessibilityActionUpdates new_actions;
1226  flutter::SemanticsNodeUpdates new_nodes;
1227 
1228  flutter::SemanticsNode new_root_node;
1229  new_root_node.id = kRootNodeId;
1230  new_root_node.label = "root";
1231  new_root_node.flags.hasImplicitScrolling = true;
1232  new_nodes[new_root_node.id] = new_root_node;
1233  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/new_actions);
1234 
1235  XCTAssertEqual([accessibility_notifications count], 1ul);
1236  id focusObject = accessibility_notifications[0][@"argument"];
1237 
1238  // Make sure the focused item is not specificed when it stays the same.
1239  // See: https://github.com/flutter/flutter/issues/104176
1240  XCTAssertEqualObjects(focusObject, [NSNull null]);
1241  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1242  UIAccessibilityLayoutChangedNotification);
1243 }
1244 
1245 - (void)testLayoutChangeDoesCallNativeAccessibilityWhenFocusChanged {
1246  flutter::MockDelegate mock_delegate;
1247  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1248  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1249  /*platform=*/thread_task_runner,
1250  /*raster=*/thread_task_runner,
1251  /*ui=*/thread_task_runner,
1252  /*io=*/thread_task_runner);
1253  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1254  /*delegate=*/mock_delegate,
1255  /*platform_views_controller=*/nil,
1256  /*task_runners=*/runners,
1257  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1258  id mockFlutterView = OCMClassMock([FlutterView class]);
1259  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1260  StubLoadedView(mockFlutterViewController, mockFlutterView);
1261 
1262  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1263  [[NSMutableArray alloc] init];
1264  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1265  ios_delegate->on_PostAccessibilityNotification_ =
1266  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1267  [accessibility_notifications addObject:@{
1268  @"notification" : @(notification),
1269  @"argument" : argument ? argument : [NSNull null],
1270  }];
1271  };
1272  __block auto bridge =
1273  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1274  /*platform_view=*/platform_view.get(),
1275  /*platform_views_controller=*/nil,
1276  /*ios_delegate=*/std::move(ios_delegate));
1277 
1278  flutter::CustomAccessibilityActionUpdates actions;
1279  flutter::SemanticsNodeUpdates nodes;
1280 
1281  flutter::SemanticsNode node1;
1282  node1.id = 1;
1283  node1.label = "node1";
1284  nodes[node1.id] = node1;
1285  flutter::SemanticsNode root_node;
1286  root_node.id = kRootNodeId;
1287  root_node.label = "root";
1288  root_node.flags.hasImplicitScrolling = true;
1289  root_node.childrenInTraversalOrder = {1};
1290  root_node.childrenInHitTestOrder = {1};
1291  nodes[root_node.id] = root_node;
1292  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1293 
1294  // Simulates the focusing on the node 1.
1295  bridge->AccessibilityObjectDidBecomeFocused(1);
1296 
1297  // Remove node 1 to trigger a layout change notification, and focus should be one root
1298  flutter::CustomAccessibilityActionUpdates new_actions;
1299  flutter::SemanticsNodeUpdates new_nodes;
1300 
1301  flutter::SemanticsNode new_root_node;
1302  new_root_node.id = kRootNodeId;
1303  new_root_node.label = "root";
1304  new_root_node.flags.hasImplicitScrolling = true;
1305  new_nodes[new_root_node.id] = new_root_node;
1306  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/new_actions);
1307 
1308  XCTAssertEqual([accessibility_notifications count], 1ul);
1309  SemanticsObject* focusObject2 = accessibility_notifications[0][@"argument"];
1310 
1311  // Bridge should ask accessibility to focus on root because node 1 is moved from screen.
1312  XCTAssertTrue([focusObject2 isKindOfClass:[FlutterSemanticsScrollView class]]);
1313  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1314  UIAccessibilityLayoutChangedNotification);
1315 }
1316 
1317 - (void)testScrollableSemanticsContainerReturnsCorrectChildren {
1318  flutter::MockDelegate mock_delegate;
1319  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1320  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1321  /*platform=*/thread_task_runner,
1322  /*raster=*/thread_task_runner,
1323  /*ui=*/thread_task_runner,
1324  /*io=*/thread_task_runner);
1325  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1326  /*delegate=*/mock_delegate,
1327  /*platform_views_controller=*/nil,
1328  /*task_runners=*/runners,
1329  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1330  id mockFlutterView = OCMClassMock([FlutterView class]);
1331  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1332  StubLoadedView(mockFlutterViewController, mockFlutterView);
1333 
1334  OCMExpect([mockFlutterView
1335  setAccessibilityElements:[OCMArg checkWithBlock:^BOOL(NSArray* value) {
1336  if ([value count] != 1) {
1337  return NO;
1338  }
1339  SemanticsObjectContainer* container = value[0];
1340  SemanticsObject* object = container.semanticsObject;
1341  FlutterScrollableSemanticsObject* scrollable =
1342  (FlutterScrollableSemanticsObject*)object.children[0];
1343  id nativeScrollable = scrollable.nativeAccessibility;
1344  SemanticsObjectContainer* scrollableContainer = [nativeScrollable accessibilityContainer];
1345  return [scrollableContainer indexOfAccessibilityElement:nativeScrollable] == 1;
1346  }]]);
1347  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1348  __block auto bridge =
1349  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1350  /*platform_view=*/platform_view.get(),
1351  /*platform_views_controller=*/nil,
1352  /*ios_delegate=*/std::move(ios_delegate));
1353 
1354  flutter::CustomAccessibilityActionUpdates actions;
1355  flutter::SemanticsNodeUpdates nodes;
1356 
1357  flutter::SemanticsNode node1;
1358  node1.id = 1;
1359  node1.label = "node1";
1360  node1.flags.hasImplicitScrolling = true;
1361  nodes[node1.id] = node1;
1362  flutter::SemanticsNode root_node;
1363  root_node.id = kRootNodeId;
1364  root_node.label = "root";
1365  root_node.childrenInTraversalOrder = {1};
1366  root_node.childrenInHitTestOrder = {1};
1367  nodes[root_node.id] = root_node;
1368  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1369  OCMVerifyAll(mockFlutterView);
1370 }
1371 
1372 - (void)testAnnouncesRouteChangesAndLayoutChangeInOneUpdate {
1373  flutter::MockDelegate mock_delegate;
1374  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1375  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1376  /*platform=*/thread_task_runner,
1377  /*raster=*/thread_task_runner,
1378  /*ui=*/thread_task_runner,
1379  /*io=*/thread_task_runner);
1380  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1381  /*delegate=*/mock_delegate,
1382  /*platform_views_controller=*/nil,
1383  /*task_runners=*/runners,
1384  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1385  id mockFlutterView = OCMClassMock([FlutterView class]);
1386  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1387  StubLoadedView(mockFlutterViewController, mockFlutterView);
1388 
1389  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1390  [[NSMutableArray alloc] init];
1391  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1392  ios_delegate->on_PostAccessibilityNotification_ =
1393  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1394  [accessibility_notifications addObject:@{
1395  @"notification" : @(notification),
1396  @"argument" : argument ? argument : [NSNull null],
1397  }];
1398  };
1399  __block auto bridge =
1400  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1401  /*platform_view=*/platform_view.get(),
1402  /*platform_views_controller=*/nil,
1403  /*ios_delegate=*/std::move(ios_delegate));
1404 
1405  flutter::CustomAccessibilityActionUpdates actions;
1406  flutter::SemanticsNodeUpdates nodes;
1407 
1408  flutter::SemanticsNode node1;
1409  node1.id = 1;
1410  node1.label = "node1";
1411  node1.flags.scopesRoute = true;
1412  node1.flags.namesRoute = true;
1413  nodes[node1.id] = node1;
1414  flutter::SemanticsNode node3;
1415  node3.id = 3;
1416  node3.label = "node3";
1417  nodes[node3.id] = node3;
1418  flutter::SemanticsNode root_node;
1419  root_node.id = kRootNodeId;
1420  root_node.label = "root";
1421  root_node.childrenInTraversalOrder = {1, 3};
1422  root_node.childrenInHitTestOrder = {1, 3};
1423  nodes[root_node.id] = root_node;
1424  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1425 
1426  XCTAssertEqual([accessibility_notifications count], 1ul);
1427  XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], @"node1");
1428  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1429  UIAccessibilityScreenChangedNotification);
1430 
1431  // Simulates the focusing on the node 0.
1432  bridge->AccessibilityObjectDidBecomeFocused(0);
1433 
1434  flutter::SemanticsNodeUpdates new_nodes;
1435 
1436  flutter::SemanticsNode new_node1;
1437  new_node1.id = 1;
1438  new_node1.label = "new_node1";
1439  new_node1.flags.scopesRoute = true;
1440  new_node1.flags.namesRoute = true;
1441  new_node1.childrenInTraversalOrder = {2};
1442  new_node1.childrenInHitTestOrder = {2};
1443  new_nodes[new_node1.id] = new_node1;
1444  flutter::SemanticsNode new_node2;
1445  new_node2.id = 2;
1446  new_node2.label = "new_node2";
1447  new_node2.flags.scopesRoute = true;
1448  new_node2.flags.namesRoute = true;
1449  new_nodes[new_node2.id] = new_node2;
1450  flutter::SemanticsNode new_root_node;
1451  new_root_node.id = kRootNodeId;
1452  new_root_node.label = "root";
1453  new_root_node.childrenInTraversalOrder = {1};
1454  new_root_node.childrenInHitTestOrder = {1};
1455  new_nodes[new_root_node.id] = new_root_node;
1456  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
1457  XCTAssertEqual([accessibility_notifications count], 3ul);
1458  XCTAssertEqualObjects(accessibility_notifications[1][@"argument"], @"new_node2");
1459  XCTAssertEqual([accessibility_notifications[1][@"notification"] unsignedIntValue],
1460  UIAccessibilityScreenChangedNotification);
1461  SemanticsObject* focusObject = accessibility_notifications[2][@"argument"];
1462  XCTAssertEqual([focusObject uid], 0);
1463  XCTAssertEqual([accessibility_notifications[2][@"notification"] unsignedIntValue],
1464  UIAccessibilityLayoutChangedNotification);
1465 }
1466 
1467 - (void)testAnnouncesRouteChangesWhenAddAdditionalRoute {
1468  flutter::MockDelegate mock_delegate;
1469  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1470  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1471  /*platform=*/thread_task_runner,
1472  /*raster=*/thread_task_runner,
1473  /*ui=*/thread_task_runner,
1474  /*io=*/thread_task_runner);
1475  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1476  /*delegate=*/mock_delegate,
1477  /*platform_views_controller=*/nil,
1478  /*task_runners=*/runners,
1479  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1480  id mockFlutterView = OCMClassMock([FlutterView class]);
1481  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1482  StubLoadedView(mockFlutterViewController, mockFlutterView);
1483 
1484  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1485  [[NSMutableArray alloc] init];
1486  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1487  ios_delegate->on_PostAccessibilityNotification_ =
1488  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1489  [accessibility_notifications addObject:@{
1490  @"notification" : @(notification),
1491  @"argument" : argument ? argument : [NSNull null],
1492  }];
1493  };
1494  __block auto bridge =
1495  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1496  /*platform_view=*/platform_view.get(),
1497  /*platform_views_controller=*/nil,
1498  /*ios_delegate=*/std::move(ios_delegate));
1499 
1500  flutter::CustomAccessibilityActionUpdates actions;
1501  flutter::SemanticsNodeUpdates nodes;
1502 
1503  flutter::SemanticsNode node1;
1504  node1.id = 1;
1505  node1.label = "node1";
1506  node1.flags.scopesRoute = true;
1507  node1.flags.namesRoute = true;
1508  nodes[node1.id] = node1;
1509  flutter::SemanticsNode root_node;
1510  root_node.id = kRootNodeId;
1511  root_node.flags.scopesRoute = true;
1512  root_node.childrenInTraversalOrder = {1};
1513  root_node.childrenInHitTestOrder = {1};
1514  nodes[root_node.id] = root_node;
1515  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1516 
1517  XCTAssertEqual([accessibility_notifications count], 1ul);
1518  XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], @"node1");
1519  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1520  UIAccessibilityScreenChangedNotification);
1521 
1522  flutter::SemanticsNodeUpdates new_nodes;
1523 
1524  flutter::SemanticsNode new_node1;
1525  new_node1.id = 1;
1526  new_node1.label = "new_node1";
1527  new_node1.flags.scopesRoute = true;
1528  new_node1.flags.namesRoute = true;
1529  new_node1.childrenInTraversalOrder = {2};
1530  new_node1.childrenInHitTestOrder = {2};
1531  new_nodes[new_node1.id] = new_node1;
1532  flutter::SemanticsNode new_node2;
1533  new_node2.id = 2;
1534  new_node2.label = "new_node2";
1535  new_node2.flags.scopesRoute = true;
1536  new_node2.flags.namesRoute = true;
1537  new_nodes[new_node2.id] = new_node2;
1538  flutter::SemanticsNode new_root_node;
1539  new_root_node.id = kRootNodeId;
1540  new_root_node.flags.scopesRoute = true;
1541  new_root_node.childrenInTraversalOrder = {1};
1542  new_root_node.childrenInHitTestOrder = {1};
1543  new_nodes[new_root_node.id] = new_root_node;
1544  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
1545  XCTAssertEqual([accessibility_notifications count], 2ul);
1546  XCTAssertEqualObjects(accessibility_notifications[1][@"argument"], @"new_node2");
1547  XCTAssertEqual([accessibility_notifications[1][@"notification"] unsignedIntValue],
1548  UIAccessibilityScreenChangedNotification);
1549 }
1550 
1551 - (void)testAnnouncesRouteChangesRemoveRouteInMiddle {
1552  flutter::MockDelegate mock_delegate;
1553  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1554  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1555  /*platform=*/thread_task_runner,
1556  /*raster=*/thread_task_runner,
1557  /*ui=*/thread_task_runner,
1558  /*io=*/thread_task_runner);
1559  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1560  /*delegate=*/mock_delegate,
1561  /*platform_views_controller=*/nil,
1562  /*task_runners=*/runners,
1563  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1564  id mockFlutterView = OCMClassMock([FlutterView class]);
1565  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1566  StubLoadedView(mockFlutterViewController, mockFlutterView);
1567 
1568  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1569  [[NSMutableArray alloc] init];
1570  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1571  ios_delegate->on_PostAccessibilityNotification_ =
1572  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1573  [accessibility_notifications addObject:@{
1574  @"notification" : @(notification),
1575  @"argument" : argument ? argument : [NSNull null],
1576  }];
1577  };
1578  __block auto bridge =
1579  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1580  /*platform_view=*/platform_view.get(),
1581  /*platform_views_controller=*/nil,
1582  /*ios_delegate=*/std::move(ios_delegate));
1583 
1584  flutter::CustomAccessibilityActionUpdates actions;
1585  flutter::SemanticsNodeUpdates nodes;
1586 
1587  flutter::SemanticsNode node1;
1588  node1.id = 1;
1589  node1.label = "node1";
1590  node1.flags.scopesRoute = true;
1591  node1.flags.namesRoute = true;
1592  node1.childrenInTraversalOrder = {2};
1593  node1.childrenInHitTestOrder = {2};
1594  nodes[node1.id] = node1;
1595  flutter::SemanticsNode node2;
1596  node2.id = 2;
1597  node2.label = "node2";
1598  node2.flags.scopesRoute = true;
1599  node2.flags.namesRoute = true;
1600  nodes[node2.id] = node2;
1601  flutter::SemanticsNode root_node;
1602  root_node.id = kRootNodeId;
1603  root_node.flags.scopesRoute = true;
1604  root_node.childrenInTraversalOrder = {1};
1605  root_node.childrenInHitTestOrder = {1};
1606  nodes[root_node.id] = root_node;
1607  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1608 
1609  XCTAssertEqual([accessibility_notifications count], 1ul);
1610  XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], @"node2");
1611  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1612  UIAccessibilityScreenChangedNotification);
1613 
1614  flutter::SemanticsNodeUpdates new_nodes;
1615 
1616  flutter::SemanticsNode new_node1;
1617  new_node1.id = 1;
1618  new_node1.label = "new_node1";
1619  new_node1.childrenInTraversalOrder = {2};
1620  new_node1.childrenInHitTestOrder = {2};
1621  new_nodes[new_node1.id] = new_node1;
1622  flutter::SemanticsNode new_node2;
1623  new_node2.id = 2;
1624  new_node2.label = "new_node2";
1625  new_node2.flags.scopesRoute = true;
1626  new_node2.flags.namesRoute = true;
1627  new_nodes[new_node2.id] = new_node2;
1628  flutter::SemanticsNode new_root_node;
1629  new_root_node.id = kRootNodeId;
1630  new_root_node.flags.scopesRoute = true;
1631  new_root_node.childrenInTraversalOrder = {1};
1632  new_root_node.childrenInHitTestOrder = {1};
1633  new_nodes[new_root_node.id] = new_root_node;
1634  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
1635  XCTAssertEqual([accessibility_notifications count], 2ul);
1636  XCTAssertEqualObjects(accessibility_notifications[1][@"argument"], @"new_node2");
1637  XCTAssertEqual([accessibility_notifications[1][@"notification"] unsignedIntValue],
1638  UIAccessibilityScreenChangedNotification);
1639 }
1640 
1641 - (void)testHandleEvent {
1642  flutter::MockDelegate mock_delegate;
1643  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1644  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1645  /*platform=*/thread_task_runner,
1646  /*raster=*/thread_task_runner,
1647  /*ui=*/thread_task_runner,
1648  /*io=*/thread_task_runner);
1649  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1650  /*delegate=*/mock_delegate,
1651  /*platform_views_controller=*/nil,
1652  /*task_runners=*/runners,
1653  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1654  id mockFlutterView = OCMClassMock([FlutterView class]);
1655  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1656  StubLoadedView(mockFlutterViewController, mockFlutterView);
1657 
1658  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1659  [[NSMutableArray alloc] init];
1660  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1661  ios_delegate->on_PostAccessibilityNotification_ =
1662  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1663  [accessibility_notifications addObject:@{
1664  @"notification" : @(notification),
1665  @"argument" : argument ? argument : [NSNull null],
1666  }];
1667  };
1668  __block auto bridge =
1669  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1670  /*platform_view=*/platform_view.get(),
1671  /*platform_views_controller=*/nil,
1672  /*ios_delegate=*/std::move(ios_delegate));
1673 
1674  NSDictionary<NSString*, id>* annotatedEvent = @{@"type" : @"focus", @"nodeId" : @123};
1675 
1676  bridge->HandleEvent(annotatedEvent);
1677 
1678  XCTAssertEqual([accessibility_notifications count], 1ul);
1679  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1680  UIAccessibilityLayoutChangedNotification);
1681 }
1682 
1683 - (void)testAccessibilityObjectDidBecomeFocused {
1684  flutter::MockDelegate mock_delegate;
1685  auto thread = std::make_unique<fml::Thread>("AccessibilityBridgeTest");
1686  auto thread_task_runner = thread->GetTaskRunner();
1687  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1688  /*platform=*/thread_task_runner,
1689  /*raster=*/thread_task_runner,
1690  /*ui=*/thread_task_runner,
1691  /*io=*/thread_task_runner);
1692  id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1693  id engine = OCMClassMock([FlutterEngine class]);
1694  id flutterViewController = OCMClassMock([FlutterViewController class]);
1695 
1696  OCMStub([flutterViewController engine]).andReturn(engine);
1697  OCMStub([engine binaryMessenger]).andReturn(messenger);
1698  FlutterBinaryMessengerConnection connection = 123;
1699  OCMStub([messenger setMessageHandlerOnChannel:@"flutter/accessibility"
1700  binaryMessageHandler:[OCMArg any]])
1701  .andReturn(connection);
1702 
1703  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1704  /*delegate=*/mock_delegate,
1705  /*platform_views_controller=*/nil,
1706  /*task_runners=*/runners,
1707  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1708  fml::AutoResetWaitableEvent latch;
1709  thread_task_runner->PostTask([&] {
1710  platform_view->SetOwnerViewController(flutterViewController);
1711  auto bridge =
1712  std::make_unique<flutter::AccessibilityBridge>(/*view=*/nil,
1713  /*platform_view=*/platform_view.get(),
1714  /*platform_views_controller=*/nil);
1715  XCTAssertTrue(bridge.get());
1716  OCMVerify([messenger setMessageHandlerOnChannel:@"flutter/accessibility"
1717  binaryMessageHandler:[OCMArg isNotNil]]);
1718 
1719  bridge->AccessibilityObjectDidBecomeFocused(123);
1720 
1721  NSDictionary<NSString*, id>* annotatedEvent = @{@"type" : @"didGainFocus", @"nodeId" : @123};
1722  OCMVerify([messenger sendOnChannel:@"flutter/accessibility"
1723  message:[OCMArg checkWithBlock:^BOOL(NSData* message) {
1725  decode:message] isEqual:annotatedEvent];
1726  }]]);
1727  latch.Signal();
1728  });
1729  latch.Wait();
1730 
1731  [engine stopMocking];
1732 }
1733 
1734 - (void)testAnnouncesRouteChangesWhenNoNamesRoute {
1735  flutter::MockDelegate mock_delegate;
1736  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1737  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1738  /*platform=*/thread_task_runner,
1739  /*raster=*/thread_task_runner,
1740  /*ui=*/thread_task_runner,
1741  /*io=*/thread_task_runner);
1742  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1743  /*delegate=*/mock_delegate,
1744  /*platform_views_controller=*/nil,
1745  /*task_runners=*/runners,
1746  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1747  id mockFlutterView = OCMClassMock([FlutterView class]);
1748  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1749  StubLoadedView(mockFlutterViewController, mockFlutterView);
1750 
1751  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1752  [[NSMutableArray alloc] init];
1753  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1754  ios_delegate->on_PostAccessibilityNotification_ =
1755  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1756  [accessibility_notifications addObject:@{
1757  @"notification" : @(notification),
1758  @"argument" : argument ? argument : [NSNull null],
1759  }];
1760  };
1761  __block auto bridge =
1762  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1763  /*platform_view=*/platform_view.get(),
1764  /*platform_views_controller=*/nil,
1765  /*ios_delegate=*/std::move(ios_delegate));
1766 
1767  flutter::CustomAccessibilityActionUpdates actions;
1768  flutter::SemanticsNodeUpdates nodes;
1769 
1770  flutter::SemanticsNode node1;
1771  node1.id = 1;
1772  node1.label = "node1";
1773  node1.flags.scopesRoute = true;
1774  node1.flags.namesRoute = true;
1775  node1.childrenInTraversalOrder = {2, 3};
1776  node1.childrenInHitTestOrder = {2, 3};
1777  nodes[node1.id] = node1;
1778  flutter::SemanticsNode node2;
1779  node2.id = 2;
1780  node2.label = "node2";
1781  nodes[node2.id] = node2;
1782  flutter::SemanticsNode node3;
1783  node3.id = 3;
1784  node3.label = "node3";
1785  nodes[node3.id] = node3;
1786  flutter::SemanticsNode root_node;
1787  root_node.id = kRootNodeId;
1788  root_node.childrenInTraversalOrder = {1};
1789  root_node.childrenInHitTestOrder = {1};
1790  nodes[root_node.id] = root_node;
1791  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
1792 
1793  // Notification should focus first focusable node, which is node1.
1794  XCTAssertEqual([accessibility_notifications count], 1ul);
1795  id focusObject = accessibility_notifications[0][@"argument"];
1796  XCTAssertTrue([focusObject isKindOfClass:[NSString class]]);
1797  XCTAssertEqualObjects(focusObject, @"node1");
1798  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1799  UIAccessibilityScreenChangedNotification);
1800 }
1801 
1802 - (void)testAnnouncesLayoutChangeWithNilIfLastFocusIsRemoved {
1803  flutter::MockDelegate mock_delegate;
1804  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1805  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1806  /*platform=*/thread_task_runner,
1807  /*raster=*/thread_task_runner,
1808  /*ui=*/thread_task_runner,
1809  /*io=*/thread_task_runner);
1810  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1811  /*delegate=*/mock_delegate,
1812  /*platform_views_controller=*/nil,
1813  /*task_runners=*/runners,
1814  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1815  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1816  id mockFlutterView = OCMClassMock([FlutterView class]);
1817  StubLoadedView(mockFlutterViewController, mockFlutterView);
1818 
1819  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1820  [[NSMutableArray alloc] init];
1821  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1822  ios_delegate->on_PostAccessibilityNotification_ =
1823  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1824  [accessibility_notifications addObject:@{
1825  @"notification" : @(notification),
1826  @"argument" : argument ? argument : [NSNull null],
1827  }];
1828  };
1829  __block auto bridge =
1830  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1831  /*platform_view=*/platform_view.get(),
1832  /*platform_views_controller=*/nil,
1833  /*ios_delegate=*/std::move(ios_delegate));
1834 
1835  flutter::CustomAccessibilityActionUpdates actions;
1836  flutter::SemanticsNodeUpdates first_update;
1837 
1838  flutter::SemanticsNode route_node;
1839  route_node.id = 1;
1840  route_node.label = "route";
1841  first_update[route_node.id] = route_node;
1842  flutter::SemanticsNode root_node;
1843  root_node.id = kRootNodeId;
1844  root_node.label = "root";
1845  root_node.childrenInTraversalOrder = {1};
1846  root_node.childrenInHitTestOrder = {1};
1847  first_update[root_node.id] = root_node;
1848  bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);
1849 
1850  XCTAssertEqual([accessibility_notifications count], 0ul);
1851  // Simulates the focusing on the node 1.
1852  bridge->AccessibilityObjectDidBecomeFocused(1);
1853 
1854  flutter::SemanticsNodeUpdates second_update;
1855  // Simulates the removal of the node 1
1856  flutter::SemanticsNode new_root_node;
1857  new_root_node.id = kRootNodeId;
1858  new_root_node.label = "root";
1859  second_update[root_node.id] = new_root_node;
1860  bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
1861  SemanticsObject* focusObject = accessibility_notifications[0][@"argument"];
1862  // The node 1 was removed, so the bridge will set the focus object to root.
1863  XCTAssertEqual([focusObject uid], 0);
1864  XCTAssertEqualObjects([focusObject accessibilityLabel], @"root");
1865  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1866  UIAccessibilityLayoutChangedNotification);
1867 }
1868 
1869 - (void)testAnnouncesLayoutChangeWithTheSameItemFocused {
1870  flutter::MockDelegate mock_delegate;
1871  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1872  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1873  /*platform=*/thread_task_runner,
1874  /*raster=*/thread_task_runner,
1875  /*ui=*/thread_task_runner,
1876  /*io=*/thread_task_runner);
1877  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1878  /*delegate=*/mock_delegate,
1879  /*platform_views_controller=*/nil,
1880  /*task_runners=*/runners,
1881  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1882  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1883  id mockFlutterView = OCMClassMock([FlutterView class]);
1884  StubLoadedView(mockFlutterViewController, mockFlutterView);
1885 
1886  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1887  [[NSMutableArray alloc] init];
1888  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1889  ios_delegate->on_PostAccessibilityNotification_ =
1890  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1891  [accessibility_notifications addObject:@{
1892  @"notification" : @(notification),
1893  @"argument" : argument ? argument : [NSNull null],
1894  }];
1895  };
1896  __block auto bridge =
1897  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1898  /*platform_view=*/platform_view.get(),
1899  /*platform_views_controller=*/nil,
1900  /*ios_delegate=*/std::move(ios_delegate));
1901 
1902  flutter::CustomAccessibilityActionUpdates actions;
1903  flutter::SemanticsNodeUpdates first_update;
1904 
1905  flutter::SemanticsNode node_one;
1906  node_one.id = 1;
1907  node_one.label = "route1";
1908  first_update[node_one.id] = node_one;
1909  flutter::SemanticsNode node_two;
1910  node_two.id = 2;
1911  node_two.label = "route2";
1912  first_update[node_two.id] = node_two;
1913  flutter::SemanticsNode root_node;
1914  root_node.id = kRootNodeId;
1915  root_node.label = "root";
1916  root_node.childrenInTraversalOrder = {1, 2};
1917  root_node.childrenInHitTestOrder = {1, 2};
1918  first_update[root_node.id] = root_node;
1919  bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);
1920 
1921  XCTAssertEqual([accessibility_notifications count], 0ul);
1922  // Simulates the focusing on the node 1.
1923  bridge->AccessibilityObjectDidBecomeFocused(1);
1924 
1925  flutter::SemanticsNodeUpdates second_update;
1926  // Simulates the removal of the node 2.
1927  flutter::SemanticsNode new_root_node;
1928  new_root_node.id = kRootNodeId;
1929  new_root_node.label = "root";
1930  new_root_node.childrenInTraversalOrder = {1};
1931  new_root_node.childrenInHitTestOrder = {1};
1932  second_update[root_node.id] = new_root_node;
1933  bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
1934  id focusObject = accessibility_notifications[0][@"argument"];
1935  // Since we have focused on the node 1 right before the layout changed, the bridge should not ask
1936  // to refocus again on the same node.
1937  XCTAssertEqualObjects(focusObject, [NSNull null]);
1938  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
1939  UIAccessibilityLayoutChangedNotification);
1940 }
1941 
1942 - (void)testAnnouncesLayoutChangeWhenFocusMovedOutside {
1943  flutter::MockDelegate mock_delegate;
1944  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
1945  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
1946  /*platform=*/thread_task_runner,
1947  /*raster=*/thread_task_runner,
1948  /*ui=*/thread_task_runner,
1949  /*io=*/thread_task_runner);
1950  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
1951  /*delegate=*/mock_delegate,
1952  /*platform_views_controller=*/nil,
1953  /*task_runners=*/runners,
1954  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
1955  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1956  id mockFlutterView = OCMClassMock([FlutterView class]);
1957  StubLoadedView(mockFlutterViewController, mockFlutterView);
1958 
1959  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
1960  [[NSMutableArray alloc] init];
1961  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
1962  ios_delegate->on_PostAccessibilityNotification_ =
1963  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
1964  [accessibility_notifications addObject:@{
1965  @"notification" : @(notification),
1966  @"argument" : argument ? argument : [NSNull null],
1967  }];
1968  };
1969  __block auto bridge =
1970  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
1971  /*platform_view=*/platform_view.get(),
1972  /*platform_views_controller=*/nil,
1973  /*ios_delegate=*/std::move(ios_delegate));
1974 
1975  flutter::CustomAccessibilityActionUpdates actions;
1976  flutter::SemanticsNodeUpdates first_update;
1977 
1978  flutter::SemanticsNode node_one;
1979  node_one.id = 1;
1980  node_one.label = "route1";
1981  first_update[node_one.id] = node_one;
1982  flutter::SemanticsNode node_two;
1983  node_two.id = 2;
1984  node_two.label = "route2";
1985  first_update[node_two.id] = node_two;
1986  flutter::SemanticsNode root_node;
1987  root_node.id = kRootNodeId;
1988  root_node.label = "root";
1989  root_node.childrenInTraversalOrder = {1, 2};
1990  root_node.childrenInHitTestOrder = {1, 2};
1991  first_update[root_node.id] = root_node;
1992  bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);
1993 
1994  XCTAssertEqual([accessibility_notifications count], 0ul);
1995  // Simulates the focusing on the node 1.
1996  bridge->AccessibilityObjectDidBecomeFocused(1);
1997  // Simulates that the focus move outside of flutter.
1998  bridge->AccessibilityObjectDidLoseFocus(1);
1999 
2000  flutter::SemanticsNodeUpdates second_update;
2001  // Simulates the removal of the node 2.
2002  flutter::SemanticsNode new_root_node;
2003  new_root_node.id = kRootNodeId;
2004  new_root_node.label = "root";
2005  new_root_node.childrenInTraversalOrder = {1};
2006  new_root_node.childrenInHitTestOrder = {1};
2007  second_update[root_node.id] = new_root_node;
2008  bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
2009  NSNull* focusObject = accessibility_notifications[0][@"argument"];
2010  // Since the focus is moved outside of the app right before the layout
2011  // changed, the bridge should not try to refocus anything .
2012  XCTAssertEqual(focusObject, [NSNull null]);
2013  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
2014  UIAccessibilityLayoutChangedNotification);
2015 }
2016 
2017 - (void)testAnnouncesScrollChangeWithLastFocused {
2018  flutter::MockDelegate mock_delegate;
2019  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2020  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2021  /*platform=*/thread_task_runner,
2022  /*raster=*/thread_task_runner,
2023  /*ui=*/thread_task_runner,
2024  /*io=*/thread_task_runner);
2025  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2026  /*delegate=*/mock_delegate,
2027  /*platform_views_controller=*/nil,
2028  /*task_runners=*/runners,
2029  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2030  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2031  id mockFlutterView = OCMClassMock([FlutterView class]);
2032  StubLoadedView(mockFlutterViewController, mockFlutterView);
2033 
2034  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
2035  [[NSMutableArray alloc] init];
2036  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
2037  ios_delegate->on_PostAccessibilityNotification_ =
2038  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
2039  [accessibility_notifications addObject:@{
2040  @"notification" : @(notification),
2041  @"argument" : argument ? argument : [NSNull null],
2042  }];
2043  };
2044  __block auto bridge =
2045  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
2046  /*platform_view=*/platform_view.get(),
2047  /*platform_views_controller=*/nil,
2048  /*ios_delegate=*/std::move(ios_delegate));
2049 
2050  flutter::CustomAccessibilityActionUpdates actions;
2051  flutter::SemanticsNodeUpdates first_update;
2052 
2053  flutter::SemanticsNode node_one;
2054  node_one.id = 1;
2055  node_one.label = "route1";
2056  node_one.scrollPosition = 0.0;
2057  first_update[node_one.id] = node_one;
2058  flutter::SemanticsNode root_node;
2059  root_node.id = kRootNodeId;
2060  root_node.label = "root";
2061  root_node.childrenInTraversalOrder = {1};
2062  root_node.childrenInHitTestOrder = {1};
2063  first_update[root_node.id] = root_node;
2064  bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);
2065 
2066  // The first update will trigger a scroll announcement, but we are not interested in it.
2067  [accessibility_notifications removeAllObjects];
2068 
2069  // Simulates the focusing on the node 1.
2070  bridge->AccessibilityObjectDidBecomeFocused(1);
2071 
2072  flutter::SemanticsNodeUpdates second_update;
2073  // Simulates the scrolling on the node 1.
2074  flutter::SemanticsNode new_node_one;
2075  new_node_one.id = 1;
2076  new_node_one.label = "route1";
2077  new_node_one.scrollPosition = 1.0;
2078  second_update[new_node_one.id] = new_node_one;
2079  bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
2080  SemanticsObject* focusObject = accessibility_notifications[0][@"argument"];
2081  // Since we have focused on the node 1 right before the scrolling, the bridge should refocus the
2082  // node 1.
2083  XCTAssertEqual([focusObject uid], 1);
2084  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
2085  UIAccessibilityPageScrolledNotification);
2086 }
2087 
2088 - (void)testAnnouncesScrollChangeDoesCallNativeAccessibility {
2089  flutter::MockDelegate mock_delegate;
2090  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2091  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2092  /*platform=*/thread_task_runner,
2093  /*raster=*/thread_task_runner,
2094  /*ui=*/thread_task_runner,
2095  /*io=*/thread_task_runner);
2096  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2097  /*delegate=*/mock_delegate,
2098  /*platform_views_controller=*/nil,
2099  /*task_runners=*/runners,
2100  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2101  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2102  id mockFlutterView = OCMClassMock([FlutterView class]);
2103  StubLoadedView(mockFlutterViewController, mockFlutterView);
2104 
2105  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
2106  [[NSMutableArray alloc] init];
2107  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
2108  ios_delegate->on_PostAccessibilityNotification_ =
2109  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
2110  [accessibility_notifications addObject:@{
2111  @"notification" : @(notification),
2112  @"argument" : argument ? argument : [NSNull null],
2113  }];
2114  };
2115  __block auto bridge =
2116  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
2117  /*platform_view=*/platform_view.get(),
2118  /*platform_views_controller=*/nil,
2119  /*ios_delegate=*/std::move(ios_delegate));
2120 
2121  flutter::CustomAccessibilityActionUpdates actions;
2122  flutter::SemanticsNodeUpdates first_update;
2123 
2124  flutter::SemanticsNode node_one;
2125  node_one.id = 1;
2126  node_one.label = "route1";
2127  node_one.flags.hasImplicitScrolling = true;
2128  node_one.scrollPosition = 0.0;
2129  first_update[node_one.id] = node_one;
2130  flutter::SemanticsNode root_node;
2131  root_node.id = kRootNodeId;
2132  root_node.label = "root";
2133  root_node.childrenInTraversalOrder = {1};
2134  root_node.childrenInHitTestOrder = {1};
2135  first_update[root_node.id] = root_node;
2136  bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);
2137 
2138  // The first update will trigger a scroll announcement, but we are not interested in it.
2139  [accessibility_notifications removeAllObjects];
2140 
2141  // Simulates the focusing on the node 1.
2142  bridge->AccessibilityObjectDidBecomeFocused(1);
2143 
2144  flutter::SemanticsNodeUpdates second_update;
2145  // Simulates the scrolling on the node 1.
2146  flutter::SemanticsNode new_node_one;
2147  new_node_one.id = 1;
2148  new_node_one.label = "route1";
2149  new_node_one.flags.hasImplicitScrolling = true;
2150  new_node_one.scrollPosition = 1.0;
2151  second_update[new_node_one.id] = new_node_one;
2152  bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
2153  SemanticsObject* focusObject = accessibility_notifications[0][@"argument"];
2154  // Make sure refocus event is sent with the nativeAccessibility of node_one
2155  // which is a FlutterSemanticsScrollView.
2156  XCTAssertTrue([focusObject isKindOfClass:[FlutterSemanticsScrollView class]]);
2157  XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
2158  UIAccessibilityPageScrolledNotification);
2159 }
2160 
2161 - (void)testAnnouncesIgnoresRouteChangesWhenModal {
2162  flutter::MockDelegate mock_delegate;
2163  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2164  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2165  /*platform=*/thread_task_runner,
2166  /*raster=*/thread_task_runner,
2167  /*ui=*/thread_task_runner,
2168  /*io=*/thread_task_runner);
2169  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2170  /*delegate=*/mock_delegate,
2171  /*platform_views_controller=*/nil,
2172  /*task_runners=*/runners,
2173  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2174  id mockFlutterView = OCMClassMock([FlutterView class]);
2175  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2176  StubLoadedView(mockFlutterViewController, mockFlutterView);
2177  std::string label = "some label";
2178 
2179  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
2180  [[NSMutableArray alloc] init];
2181  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
2182  ios_delegate->on_PostAccessibilityNotification_ =
2183  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
2184  [accessibility_notifications addObject:@{
2185  @"notification" : @(notification),
2186  @"argument" : argument ? argument : [NSNull null],
2187  }];
2188  };
2189  ios_delegate->result_IsFlutterViewControllerPresentingModalViewController_ = true;
2190  __block auto bridge =
2191  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
2192  /*platform_view=*/platform_view.get(),
2193  /*platform_views_controller=*/nil,
2194  /*ios_delegate=*/std::move(ios_delegate));
2195 
2196  flutter::CustomAccessibilityActionUpdates actions;
2197  flutter::SemanticsNodeUpdates nodes;
2198 
2199  flutter::SemanticsNode route_node;
2200  route_node.id = 1;
2201  route_node.flags.scopesRoute = true;
2202  route_node.flags.namesRoute = true;
2203  route_node.label = "route";
2204  nodes[route_node.id] = route_node;
2205  flutter::SemanticsNode root_node;
2206  root_node.id = kRootNodeId;
2207  root_node.label = label;
2208  root_node.childrenInTraversalOrder = {1};
2209  root_node.childrenInHitTestOrder = {1};
2210  nodes[root_node.id] = root_node;
2211  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
2212 
2213  XCTAssertEqual([accessibility_notifications count], 0ul);
2214 }
2215 
2216 - (void)testAnnouncesIgnoresLayoutChangeWhenModal {
2217  flutter::MockDelegate mock_delegate;
2218  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2219  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2220  /*platform=*/thread_task_runner,
2221  /*raster=*/thread_task_runner,
2222  /*ui=*/thread_task_runner,
2223  /*io=*/thread_task_runner);
2224  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2225  /*delegate=*/mock_delegate,
2226  /*platform_views_controller=*/nil,
2227  /*task_runners=*/runners,
2228  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2229  id mockFlutterView = OCMClassMock([FlutterView class]);
2230  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2231  StubLoadedView(mockFlutterViewController, mockFlutterView);
2232 
2233  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
2234  [[NSMutableArray alloc] init];
2235  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
2236  ios_delegate->on_PostAccessibilityNotification_ =
2237  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
2238  [accessibility_notifications addObject:@{
2239  @"notification" : @(notification),
2240  @"argument" : argument ? argument : [NSNull null],
2241  }];
2242  };
2243  ios_delegate->result_IsFlutterViewControllerPresentingModalViewController_ = true;
2244  __block auto bridge =
2245  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
2246  /*platform_view=*/platform_view.get(),
2247  /*platform_views_controller=*/nil,
2248  /*ios_delegate=*/std::move(ios_delegate));
2249 
2250  flutter::CustomAccessibilityActionUpdates actions;
2251  flutter::SemanticsNodeUpdates nodes;
2252 
2253  flutter::SemanticsNode child_node;
2254  child_node.id = 1;
2255  child_node.label = "child_node";
2256  nodes[child_node.id] = child_node;
2257  flutter::SemanticsNode root_node;
2258  root_node.id = kRootNodeId;
2259  root_node.label = "root";
2260  root_node.childrenInTraversalOrder = {1};
2261  root_node.childrenInHitTestOrder = {1};
2262  nodes[root_node.id] = root_node;
2263  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
2264 
2265  // Removes child_node to simulate a layout change.
2266  flutter::SemanticsNodeUpdates new_nodes;
2267  flutter::SemanticsNode new_root_node;
2268  new_root_node.id = kRootNodeId;
2269  new_root_node.label = "root";
2270  new_nodes[new_root_node.id] = new_root_node;
2271  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
2272 
2273  XCTAssertEqual([accessibility_notifications count], 0ul);
2274 }
2275 
2276 - (void)testAnnouncesIgnoresScrollChangeWhenModal {
2277  flutter::MockDelegate mock_delegate;
2278  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2279  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2280  /*platform=*/thread_task_runner,
2281  /*raster=*/thread_task_runner,
2282  /*ui=*/thread_task_runner,
2283  /*io=*/thread_task_runner);
2284  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2285  /*delegate=*/mock_delegate,
2286  /*platform_views_controller=*/nil,
2287  /*task_runners=*/runners,
2288  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2289  id mockFlutterView = OCMClassMock([FlutterView class]);
2290  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2291  StubLoadedView(mockFlutterViewController, mockFlutterView);
2292 
2293  NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
2294  [[NSMutableArray alloc] init];
2295  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
2296  ios_delegate->on_PostAccessibilityNotification_ =
2297  [accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
2298  [accessibility_notifications addObject:@{
2299  @"notification" : @(notification),
2300  @"argument" : argument ? argument : [NSNull null],
2301  }];
2302  };
2303  ios_delegate->result_IsFlutterViewControllerPresentingModalViewController_ = true;
2304  __block auto bridge =
2305  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
2306  /*platform_view=*/platform_view.get(),
2307  /*platform_views_controller=*/nil,
2308  /*ios_delegate=*/std::move(ios_delegate));
2309 
2310  flutter::CustomAccessibilityActionUpdates actions;
2311  flutter::SemanticsNodeUpdates nodes;
2312 
2313  flutter::SemanticsNode root_node;
2314  root_node.id = kRootNodeId;
2315  root_node.label = "root";
2316  root_node.scrollPosition = 1;
2317  nodes[root_node.id] = root_node;
2318  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
2319 
2320  // Removes child_node to simulate a layout change.
2321  flutter::SemanticsNodeUpdates new_nodes;
2322  flutter::SemanticsNode new_root_node;
2323  new_root_node.id = kRootNodeId;
2324  new_root_node.label = "root";
2325  new_root_node.scrollPosition = 2;
2326  new_nodes[new_root_node.id] = new_root_node;
2327  bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
2328 
2329  XCTAssertEqual([accessibility_notifications count], 0ul);
2330 }
2331 
2332 - (void)testAccessibilityMessageAfterDeletion {
2333  flutter::MockDelegate mock_delegate;
2334  auto thread = std::make_unique<fml::Thread>("AccessibilityBridgeTest");
2335  auto thread_task_runner = thread->GetTaskRunner();
2336  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2337  /*platform=*/thread_task_runner,
2338  /*raster=*/thread_task_runner,
2339  /*ui=*/thread_task_runner,
2340  /*io=*/thread_task_runner);
2341  id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2342  id engine = OCMClassMock([FlutterEngine class]);
2343  id flutterViewController = OCMClassMock([FlutterViewController class]);
2344 
2345  OCMStub([flutterViewController engine]).andReturn(engine);
2346  OCMStub([engine binaryMessenger]).andReturn(messenger);
2347  FlutterBinaryMessengerConnection connection = 123;
2348  OCMStub([messenger setMessageHandlerOnChannel:@"flutter/accessibility"
2349  binaryMessageHandler:[OCMArg any]])
2350  .andReturn(connection);
2351 
2352  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2353  /*delegate=*/mock_delegate,
2354  /*platform_views_controller=*/nil,
2355  /*task_runners=*/runners,
2356  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2357  fml::AutoResetWaitableEvent latch;
2358  thread_task_runner->PostTask([&] {
2359  platform_view->SetOwnerViewController(flutterViewController);
2360  auto bridge =
2361  std::make_unique<flutter::AccessibilityBridge>(/*view=*/nil,
2362  /*platform_view=*/platform_view.get(),
2363  /*platform_views_controller=*/nil);
2364  XCTAssertTrue(bridge.get());
2365  OCMVerify([messenger setMessageHandlerOnChannel:@"flutter/accessibility"
2366  binaryMessageHandler:[OCMArg isNotNil]]);
2367  bridge.reset();
2368  latch.Signal();
2369  });
2370  latch.Wait();
2371  OCMVerify([messenger cleanUpConnection:connection]);
2372  [engine stopMocking];
2373 }
2374 
2375 - (void)testFlutterSemanticsScrollViewManagedObjectLifecycleCorrectly {
2376  flutter::MockDelegate mock_delegate;
2377  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2378  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2379  /*platform=*/thread_task_runner,
2380  /*raster=*/thread_task_runner,
2381  /*ui=*/thread_task_runner,
2382  /*io=*/thread_task_runner);
2383  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2384  /*delegate=*/mock_delegate,
2385  /*platform_views_controller=*/nil,
2386  /*task_runners=*/runners,
2387  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2388  id mockFlutterView = OCMClassMock([FlutterView class]);
2389  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2390  StubLoadedView(mockFlutterViewController, mockFlutterView);
2391 
2392  auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
2393  __block auto bridge =
2394  std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
2395  /*platform_view=*/platform_view.get(),
2396  /*platform_views_controller=*/nil,
2397  /*ios_delegate=*/std::move(ios_delegate));
2398 
2399  FlutterSemanticsScrollView* flutterSemanticsScrollView;
2400  @autoreleasepool {
2401  FlutterScrollableSemanticsObject* semanticsObject =
2402  [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge->GetWeakPtr() uid:1234];
2403 
2404  flutterSemanticsScrollView = semanticsObject.nativeAccessibility;
2405  }
2406  XCTAssertTrue(flutterSemanticsScrollView);
2407  // If the _semanticsObject is not a weak pointer this (or any other method on
2408  // flutterSemanticsScrollView) will cause an EXC_BAD_ACCESS.
2409  XCTAssertFalse([flutterSemanticsScrollView isAccessibilityElement]);
2410 }
2411 
2412 - (void)testPlatformViewDestructorDoesNotCallSemanticsAPIs {
2413  class TestDelegate : public flutter::MockDelegate {
2414  public:
2415  void OnPlatformViewSetSemanticsEnabled(bool enabled) override { set_semantics_enabled_calls++; }
2416  int set_semantics_enabled_calls = 0;
2417  };
2418 
2419  TestDelegate test_delegate;
2420  auto thread = std::make_unique<fml::Thread>("AccessibilityBridgeTest");
2421  auto thread_task_runner = thread->GetTaskRunner();
2422  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2423  /*platform=*/thread_task_runner,
2424  /*raster=*/thread_task_runner,
2425  /*ui=*/thread_task_runner,
2426  /*io=*/thread_task_runner);
2427 
2428  fml::AutoResetWaitableEvent latch;
2429  thread_task_runner->PostTask([&] {
2430  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2431  /*delegate=*/test_delegate,
2432  /*platform_views_controller=*/nil,
2433  /*task_runners=*/runners,
2434  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2435 
2436  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2437  FlutterPlatformViewsController* flutterPlatformViewsController =
2438  [[FlutterPlatformViewsController alloc] init];
2439  flutterPlatformViewsController.taskRunner =
2440  [[FlutterFMLTaskRunner alloc] initWithTaskRunner:thread_task_runner];
2441 
2442  OCMStub([mockFlutterViewController platformViewsController])
2443  .andReturn(flutterPlatformViewsController);
2444  platform_view->SetOwnerViewController(mockFlutterViewController);
2445 
2446  platform_view->SetSemanticsEnabled(true);
2447  XCTAssertNotEqual(test_delegate.set_semantics_enabled_calls, 0);
2448 
2449  // Deleting PlatformViewIOS should not call OnPlatformViewSetSemanticsEnabled
2450  test_delegate.set_semantics_enabled_calls = 0;
2451  platform_view.reset();
2452  XCTAssertEqual(test_delegate.set_semantics_enabled_calls, 0);
2453 
2454  latch.Signal();
2455  });
2456  latch.Wait();
2457 }
2458 
2459 - (void)testResetsAccessibilityElementsOnHotRestart {
2460  flutter::MockDelegate mock_delegate;
2461  auto thread = std::make_unique<fml::Thread>("AccessibilityBridgeTest");
2462  auto thread_task_runner = thread->GetTaskRunner();
2463  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2464  /*platform=*/thread_task_runner,
2465  /*raster=*/thread_task_runner,
2466  /*ui=*/thread_task_runner,
2467  /*io=*/thread_task_runner);
2468  UIView* flutterView = [[UIView alloc] init];
2469  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2470  OCMStub([mockFlutterViewController viewIfLoaded]).andReturn(flutterView);
2471 
2472  fml::AutoResetWaitableEvent latch;
2473  thread_task_runner->PostTask([&] {
2474  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2475  /*delegate=*/mock_delegate,
2476  /*platform_views_controller=*/nil,
2477  /*task_runners=*/runners,
2478  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2479 
2480  platform_view->SetOwnerViewController(mockFlutterViewController);
2481  platform_view->SetSemanticsEnabled(true);
2482  platform_view->SetSemanticsTreeEnabled(true);
2483 
2484  UpdateRootSemantics(platform_view->GetAccessibilityBridge(), "hot restart");
2485  XCTAssertNotNil(flutterView.accessibilityElements);
2486 
2487  platform_view->OnPreEngineRestart();
2488  XCTAssertNil(flutterView.accessibilityElements);
2489 
2490  latch.Signal();
2491  });
2492  latch.Wait();
2493 }
2494 
2495 - (void)testWeakViewController {
2496  flutter::MockDelegate mock_delegate;
2497  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2498  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2499  /*platform=*/thread_task_runner,
2500  /*raster=*/thread_task_runner,
2501  /*ui=*/thread_task_runner,
2502  /*io=*/thread_task_runner);
2503  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2504  /*delegate=*/mock_delegate,
2505  /*platform_views_controller=*/nil,
2506  /*task_runners=*/runners,
2507  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2508 
2509  std::unique_ptr<flutter::AccessibilityBridge> bridge;
2510  @autoreleasepool {
2511  id mockFlutterView = OCMClassMock([FlutterView class]);
2512  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2513  StubLoadedView(mockFlutterViewController, mockFlutterView);
2514 
2515  bridge = std::make_unique<flutter::AccessibilityBridge>(
2516  /*view_controller=*/mockFlutterViewController,
2517  /*platform_view=*/platform_view.get(),
2518  /*platform_views_controller=*/nil);
2519  XCTAssertTrue(bridge.get());
2520  XCTAssertNotNil(bridge->view());
2521  }
2522  XCTAssertNil(bridge->view());
2523 }
2524 
2525 - (void)testSemanticsObjectAndContainerAccessAfterBridgeDestruction {
2526  flutter::MockDelegate mock_delegate;
2527  auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
2528  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2529  /*platform=*/thread_task_runner,
2530  /*raster=*/thread_task_runner,
2531  /*ui=*/thread_task_runner,
2532  /*io=*/thread_task_runner);
2533  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2534  /*delegate=*/mock_delegate,
2535  /*platform_views_controller=*/nil,
2536  /*task_runners=*/runners,
2537  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2538  id mockEngine = OCMClassMock([FlutterEngine class]);
2539  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2540  FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:mockEngine
2541  opaque:YES
2542  enableWideGamut:NO];
2543  OCMStub([mockFlutterViewController view]).andReturn(flutterView);
2544  // `~AccessibilityBridge` calls `clearState`, which reads `viewIfLoaded` on the view
2545  // controller; stub it so `bridge.reset()` below doesn't hit an unstubbed selector.
2546  OCMStub([mockFlutterViewController viewIfLoaded]).andReturn(flutterView);
2547 
2548  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
2549  /*view_controller=*/mockFlutterViewController,
2550  /*platform_view=*/platform_view.get(),
2551  /*platform_views_controller=*/nil);
2552 
2553  flutter::SemanticsNodeUpdates nodes;
2554  flutter::SemanticsNode root_node;
2555  root_node.id = kRootNodeId;
2556  root_node.label = "root";
2557  root_node.childrenInTraversalOrder = {1};
2558  root_node.childrenInHitTestOrder = {1};
2559 
2560  flutter::SemanticsNode child_node;
2561  child_node.id = 1;
2562  child_node.label = "child";
2563  nodes[root_node.id] = root_node;
2564  nodes[child_node.id] = child_node;
2565 
2566  flutter::CustomAccessibilityActionUpdates actions;
2567  bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
2568 
2569  NSArray* accessibilityElements = flutterView.accessibilityElements;
2570  XCTAssertEqual([accessibilityElements count], 1u);
2571  SemanticsObjectContainer* rootContainer = accessibilityElements[0];
2572  XCTAssertNotNil(rootContainer);
2573  SemanticsObject* rootObject = rootContainer.semanticsObject;
2574  XCTAssertNotNil(rootObject);
2575 
2576  // Destroy the AccessibilityBridge instance. VoiceOver runtime may still hold references
2577  // to SemanticsObject and SemanticsObjectContainer.
2578  bridge.reset();
2579 
2580  XCTAssertNil([rootContainer accessibilityContainer]);
2581 
2582  // Verify calling actions, geometry, and hit-testing methods post-destruction does not crash
2583  // or access freed memory.
2584  XCTAssertFalse([rootObject accessibilityActivate]);
2585  XCTAssertTrue(CGPointEqualToPoint([rootObject accessibilityActivationPoint], CGPointZero));
2586  XCTAssertTrue(CGRectEqualToRect([rootObject accessibilityFrame], CGRectZero));
2587  XCTAssertNil([rootObject _accessibilityHitTest:CGPointZero withEvent:nil]);
2588 }
2589 
2590 - (void)testAccessibilityChannelCallbackAfterBridgeDestruction {
2591  flutter::MockDelegate mock_delegate;
2592  // PlatformViewIOS/AccessibilityBridge need to be called on the platform thread.
2593  // Since the platform thread and UI thread are merged; we use the test's main thread.
2594  fml::MessageLoop::EnsureInitializedForCurrentThread();
2595  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
2596  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2597  /*platform=*/thread_task_runner,
2598  /*raster=*/thread_task_runner,
2599  /*ui=*/thread_task_runner,
2600  /*io=*/thread_task_runner);
2601  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2602  /*delegate=*/mock_delegate,
2603  /*platform_views_controller=*/nil,
2604  /*task_runners=*/runners,
2605  /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
2606 
2607  id mockEngine = OCMClassMock([FlutterEngine class]);
2608  id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
2609  id mockBinaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2610 
2611  OCMStub([mockFlutterViewController engine]).andReturn(mockEngine);
2612  OCMStub([mockEngine binaryMessenger]).andReturn(mockBinaryMessenger);
2613 
2614  // AccessibilityBridge destructor calls clearState, which reads viewIfLoaded on the view
2615  // controller. Stub it so bridge.reset() below doesn't hit an unstubbed selector.
2616  OCMStub([mockFlutterViewController viewIfLoaded]).andReturn(nil);
2617 
2618  // Prevent SetOwnerViewController from taking the attachView path, which touches the real
2619  // view/CALayer/IOSSurface mechanics and isn't relevant here, since we only care about the channel
2620  // handler.
2621  OCMStub([mockFlutterViewController isViewLoaded]).andReturn(NO);
2622 
2623  __block FlutterBinaryMessageHandler capturedHandler = nil;
2624  OCMStub([mockBinaryMessenger setMessageHandlerOnChannel:@"flutter/accessibility"
2625  binaryMessageHandler:[OCMArg checkWithBlock:^BOOL(id obj) {
2626  if (obj) {
2627  capturedHandler = [obj copy];
2628  }
2629  return YES;
2630  }]]);
2631 
2632  platform_view->SetOwnerViewController(mockFlutterViewController);
2633 
2634  auto bridge = std::make_unique<flutter::AccessibilityBridge>(
2635  /*view_controller=*/mockFlutterViewController,
2636  /*platform_view=*/platform_view.get(),
2637  /*platform_views_controller=*/nil);
2638 
2639  XCTAssertNotNil(capturedHandler);
2640 
2641  // Destroy the AccessibilityBridge then invoke the message handler block to ensure no-op.
2642  bridge.reset();
2643  NSDictionary* event = @{@"type" : @"announce", @"data" : @{@"message" : @"test"}};
2644  NSData* messageData = [[FlutterStandardMessageCodec sharedInstance] encode:event];
2645  if (capturedHandler) {
2646  capturedHandler(messageData, ^(NSData* reply){
2647  });
2648  }
2649 }
2650 
2651 @end
void(^ FlutterBinaryMessageHandler)(NSData *_Nullable message, FlutterBinaryReply reply)
int64_t FlutterBinaryMessengerConnection
void(^ FlutterResult)(id _Nullable result)
flutter::Settings settings_
std::unique_ptr< flutter::PlatformViewIOS > platform_view
FlutterViewController * viewController
constexpr NS_ASSUME_NONNULL_BEGIN int32_t kRootNodeId
static __weak MockPlatformView * gMockPlatformView
void UpdateSemantics(flutter::SemanticsNodeUpdates nodes, const flutter::CustomAccessibilityActionUpdates &actions)
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
void reset()
Discards all platform views instances and auxiliary resources.
void registerViewFactory:withId:gestureRecognizersBlockingPolicy:(NSObject< FlutterPlatformViewFactory > *factory,[withId] NSString *factoryId,[gestureRecognizersBlockingPolicy] FlutterPlatformViewGestureRecognizersBlockingPolicy gestureRecognizerBlockingPolicy)
set the factory used to construct embedded UI Views.
FlutterFMLTaskRunner * taskRunner
The task runner used to post rendering tasks to the platform thread.
UIView *_Nullable flutterView
The flutter view.
void onMethodCall:result:(FlutterMethodCall *call,[result] FlutterResult result)
Handler for platform view message channels.
SemanticsObject * semanticsObject
fml::RefPtr< fml::TaskRunner > CreateNewThread(const std::string &name)
instancetype sharedInstance()
int64_t texture_id