5 #import <OCMock/OCMock.h>
6 #import <XCTest/XCTest.h>
8 #import "flutter/fml/message_loop.h"
9 #import "flutter/fml/thread.h"
20 class MockDelegate :
public PlatformView::Delegate {
22 void OnPlatformViewCreated(std::unique_ptr<Surface> surface)
override {}
23 void OnPlatformViewDestroyed()
override {}
24 void OnPlatformViewScheduleFrame()
override {}
25 void OnPlatformViewAddView(int64_t view_id,
26 const ViewportMetrics& viewport_metrics,
27 AddViewCallback callback)
override {}
28 void OnPlatformViewRemoveView(int64_t view_id, RemoveViewCallback callback)
override {}
29 void OnPlatformViewSetNextFrameCallback(
const fml::closure& closure)
override {}
30 void OnPlatformViewSetViewportMetrics(int64_t view_id,
const ViewportMetrics& metrics)
override {}
31 const flutter::Settings& OnPlatformViewGetSettings()
const override {
return settings_; }
32 void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message)
override {}
33 void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet)
override {
35 HitTestResponse OnPlatformViewHitTest(int64_t view_id,
const flutter::PointData offset)
override {
36 return {.has_platform_view =
false};
38 void OnPlatformViewSendViewFocusEvent(
const ViewFocusEvent& event)
override {}
39 void OnPlatformViewDispatchSemanticsAction(int64_t view_id,
41 SemanticsAction action,
42 fml::MallocMapping args)
override {}
43 void OnPlatformViewSetSemanticsEnabled(
bool enabled)
override {}
44 void OnPlatformViewSetAccessibilityFeatures(int32_t flags)
override {}
45 void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture)
override {}
46 void OnPlatformViewUnregisterTexture(int64_t
texture_id)
override {}
47 void OnPlatformViewMarkTextureFrameAvailable(int64_t
texture_id)
override {}
49 void LoadDartDeferredLibrary(intptr_t loading_unit_id,
50 std::unique_ptr<const fml::Mapping> snapshot_data,
51 std::unique_ptr<const fml::Mapping> snapshot_instructions)
override {
53 void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
54 const std::string error_message,
55 bool transient)
override {}
56 void UpdateAssetResolverByType(std::unique_ptr<flutter::AssetResolver> updated_asset_resolver,
57 flutter::AssetResolver::AssetResolverType type)
override {}
58 std::shared_ptr<fml::BasicTaskRunner> OnPlatformViewGetShutdownSafeIOTaskRunner()
const override {
73 - (void)testSetSemanticsTreeEnabled {
74 flutter::MockDelegate mock_delegate;
75 auto thread = std::make_unique<fml::Thread>(
"PlatformViewIOSTest");
76 auto thread_task_runner = thread->GetTaskRunner();
77 flutter::TaskRunners runners(
self.name.UTF8String,
87 OCMStub([flutterViewController isViewLoaded]).andReturn(NO);
88 OCMStub([flutterViewController
engine]).andReturn(
engine);
89 OCMStub([
engine binaryMessenger]).andReturn(messenger);
91 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
95 std::make_shared<fml::SyncSwitch>());
96 fml::AutoResetWaitableEvent latch;
97 thread_task_runner->PostTask([&] {
108 [engine stopMocking];
111 - (void)testRebindsAccessibilityBridgeAfterOwnerControllerReattaches {
112 flutter::MockDelegate mock_delegate;
113 fml::MessageLoop::EnsureInitializedForCurrentThread();
114 auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
115 flutter::TaskRunners runners(
self.name.UTF8String,
132 OCMStub([firstViewController isViewLoaded]).andReturn(YES);
133 OCMStub([firstViewController
engine]).andReturn(
engine);
134 OCMStub([firstViewController view]).andReturn(firstView);
135 OCMStub([firstViewController viewIfLoaded]).andReturn(firstView);
136 OCMStub([secondViewController isViewLoaded]).andReturn(YES);
137 OCMStub([secondViewController
engine]).andReturn(
engine);
138 OCMStub([secondViewController view]).andReturn(secondView);
139 OCMStub([secondViewController viewIfLoaded]).andReturn(secondView);
140 OCMStub([
engine binaryMessenger]).andReturn(messenger);
142 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
146 std::make_shared<fml::SyncSwitch>());
150 XCTAssertTrue(bridge !=
nullptr);
151 flutter::SemanticsNode root_node;
153 root_node.label =
"root";
154 flutter::SemanticsNodeUpdates update;
155 update[kRootNodeId] = root_node;
157 flutter::CustomAccessibilityActionUpdates());
158 XCTAssertNotNil(firstView.accessibilityElements);
161 XCTAssertEqual(
platform_view->GetAccessibilityBridge(), bridge);
162 XCTAssertNil(firstView.accessibilityElements);
165 XCTAssertEqual(
platform_view->GetAccessibilityBridge(), bridge);
166 XCTAssertNotNil(secondView.accessibilityElements);
169 [engine stopMocking];
172 - (void)testSwappingOwnerControllersPreservesAccessibilityElementsInstalledByNewOwner {
173 flutter::MockDelegate first_delegate;
174 flutter::MockDelegate second_delegate;
175 fml::MessageLoop::EnsureInitializedForCurrentThread();
176 auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
177 flutter::TaskRunners runners(
self.name.UTF8String,
186 OCMStub([firstEngine binaryMessenger]).andReturn(firstMessenger);
187 OCMStub([secondEngine binaryMessenger]).andReturn(secondMessenger);
197 OCMStub([firstViewController isViewLoaded]).andReturn(YES);
198 OCMStub([firstViewController
engine]).andReturn(firstEngine);
199 OCMStub([firstViewController view]).andReturn(firstView);
200 OCMStub([firstViewController viewIfLoaded]).andReturn(firstView);
201 OCMStub([secondViewController isViewLoaded]).andReturn(YES);
202 OCMStub([secondViewController
engine]).andReturn(secondEngine);
203 OCMStub([secondViewController view]).andReturn(secondView);
204 OCMStub([secondViewController viewIfLoaded]).andReturn(secondView);
206 auto first_platform_view = std::make_unique<flutter::PlatformViewIOS>(
210 std::make_shared<fml::SyncSwitch>());
211 auto second_platform_view = std::make_unique<flutter::PlatformViewIOS>(
215 std::make_shared<fml::SyncSwitch>());
217 first_platform_view->SetOwnerViewController(firstViewController);
218 first_platform_view->SetSemanticsTreeEnabled(
true);
220 flutter::SemanticsNode first_root;
222 flutter::SemanticsNodeUpdates first_update;
223 first_update[kRootNodeId] = first_root;
224 first_platform_view->UpdateSemantics(0, std::move(first_update),
225 flutter::CustomAccessibilityActionUpdates());
227 second_platform_view->SetOwnerViewController(secondViewController);
228 second_platform_view->SetSemanticsTreeEnabled(
true);
230 flutter::SemanticsNode second_root;
232 flutter::SemanticsNodeUpdates second_update;
233 second_update[kRootNodeId] = second_root;
234 second_platform_view->UpdateSemantics(0, std::move(second_update),
235 flutter::CustomAccessibilityActionUpdates());
237 first_platform_view->SetOwnerViewController(secondViewController);
241 second_platform_view->SetOwnerViewController(firstViewController);
243 secondViewRoot = secondView.accessibilityElements.firstObject;
247 first_platform_view->SetSemanticsTreeEnabled(
false);
248 second_platform_view->SetSemanticsTreeEnabled(
false);
249 [firstEngine stopMocking];
250 [secondEngine stopMocking];
253 - (void)testUpdateSemanticsDoesNotLoadOwnerControllerView {
254 flutter::MockDelegate mock_delegate;
255 fml::MessageLoop::EnsureInitializedForCurrentThread();
256 auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
257 flutter::TaskRunners runners(
self.name.UTF8String,
266 OCMStub([flutterViewController isViewLoaded]).andReturn(NO);
267 OCMStub([flutterViewController viewIfLoaded]).andReturn(nil);
268 OCMStub([flutterViewController
engine]).andReturn(
engine);
269 OCMReject([flutterViewController view]);
270 OCMStub([
engine binaryMessenger]).andReturn(messenger);
272 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
276 std::make_shared<fml::SyncSwitch>());
277 platform_view->SetOwnerViewController(flutterViewController);
281 flutter::SemanticsNode root_node;
283 root_node.label =
"root";
284 flutter::SemanticsNodeUpdates update;
285 update[kRootNodeId] = root_node;
287 flutter::CustomAccessibilityActionUpdates());
291 OCMVerifyAll(flutterViewController);
292 [engine stopMocking];
295 - (void)testAttachViewAppliesSemanticsUpdatesReceivedBeforeViewLoaded {
296 flutter::MockDelegate mock_delegate;
297 fml::MessageLoop::EnsureInitializedForCurrentThread();
298 auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
299 flutter::TaskRunners runners(
self.name.UTF8String,
311 __block BOOL isViewLoaded = NO;
312 __block UIView* viewIfLoaded = nil;
313 OCMStub([flutterViewController isViewLoaded]).andDo(^(NSInvocation* invocation) {
314 [invocation setReturnValue:&isViewLoaded];
316 OCMStub([flutterViewController viewIfLoaded]).andDo(^(NSInvocation* invocation) {
317 [invocation setReturnValue:&viewIfLoaded];
319 OCMStub([flutterViewController view]).andReturn(flutterView);
320 OCMStub([flutterViewController
engine]).andReturn(
engine);
321 OCMStub([
engine binaryMessenger]).andReturn(messenger);
323 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
327 std::make_shared<fml::SyncSwitch>());
328 platform_view->SetOwnerViewController(flutterViewController);
332 __block NSInteger semanticsUpdateNotificationCount = 0;
334 [[NSNotificationCenter defaultCenter] addObserverForName:FlutterSemanticsUpdateNotification
335 object:flutterViewController
337 usingBlock:^(NSNotification* notification) {
338 semanticsUpdateNotificationCount += 1;
341 flutter::SemanticsNode root_node;
343 root_node.label =
"root before view loaded";
344 flutter::SemanticsNodeUpdates update;
345 update[kRootNodeId] = root_node;
347 flutter::CustomAccessibilityActionUpdates());
348 XCTAssertNil(flutterView.accessibilityElements);
349 XCTAssertEqual(semanticsUpdateNotificationCount, 0);
352 viewIfLoaded = flutterView;
355 XCTAssertEqual(semanticsUpdateNotificationCount, 1);
356 XCTAssertNotNil(flutterView.accessibilityElements);
357 id rootContainer = flutterView.accessibilityElements.firstObject;
358 id rootElement = [rootContainer accessibilityElementAtIndex:0];
359 XCTAssertEqualObjects([rootElement accessibilityLabel],
@"root before view loaded");
361 [[NSNotificationCenter defaultCenter] removeObserver:observer];
363 [engine stopMocking];
366 - (void)testPreservesSemanticsUpdatesWhileOwnerControllerIsDetached {
367 flutter::MockDelegate mock_delegate;
368 fml::MessageLoop::EnsureInitializedForCurrentThread();
369 auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
370 flutter::TaskRunners runners(
self.name.UTF8String,
387 OCMStub([firstViewController isViewLoaded]).andReturn(YES);
388 OCMStub([firstViewController
engine]).andReturn(
engine);
389 OCMStub([firstViewController view]).andReturn(firstView);
390 OCMStub([firstViewController viewIfLoaded]).andReturn(firstView);
391 OCMStub([secondViewController isViewLoaded]).andReturn(YES);
392 OCMStub([secondViewController
engine]).andReturn(
engine);
393 OCMStub([secondViewController view]).andReturn(secondView);
394 OCMStub([secondViewController viewIfLoaded]).andReturn(secondView);
395 OCMStub([
engine binaryMessenger]).andReturn(messenger);
397 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
401 std::make_shared<fml::SyncSwitch>());
405 XCTAssertTrue(bridge !=
nullptr);
407 flutter::SemanticsNode root_node;
409 root_node.label =
"root";
410 flutter::SemanticsNodeUpdates initial_update;
411 initial_update[kRootNodeId] = root_node;
413 flutter::CustomAccessibilityActionUpdates());
414 XCTAssertNotNil(firstView.accessibilityElements);
418 flutter::SemanticsNode detached_root_node;
420 detached_root_node.label =
"updated while detached";
421 flutter::SemanticsNodeUpdates detached_update;
422 detached_update[kRootNodeId] = detached_root_node;
423 platform_view->UpdateSemantics(0, std::move(detached_update),
424 flutter::CustomAccessibilityActionUpdates());
425 XCTAssertEqual(
platform_view->GetAccessibilityBridge(), bridge);
426 XCTAssertNil(firstView.accessibilityElements);
429 XCTAssertEqual(
platform_view->GetAccessibilityBridge(), bridge);
430 XCTAssertNotNil(secondView.accessibilityElements);
431 id rootContainer = secondView.accessibilityElements.firstObject;
432 id rootElement = [rootContainer accessibilityElementAtIndex:0];
433 XCTAssertEqualObjects([rootElement accessibilityLabel],
@"updated while detached");
436 [engine stopMocking];
439 - (void)testRebindsScrollableSemanticsViewAfterOwnerControllerReattaches {
440 flutter::MockDelegate mock_delegate;
441 fml::MessageLoop::EnsureInitializedForCurrentThread();
442 auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
443 flutter::TaskRunners runners(
self.name.UTF8String,
460 OCMStub([firstViewController isViewLoaded]).andReturn(YES);
461 OCMStub([firstViewController
engine]).andReturn(
engine);
462 OCMStub([firstViewController view]).andReturn(firstView);
463 OCMStub([firstViewController viewIfLoaded]).andReturn(firstView);
464 OCMStub([secondViewController isViewLoaded]).andReturn(YES);
465 OCMStub([secondViewController
engine]).andReturn(
engine);
466 OCMStub([secondViewController view]).andReturn(secondView);
467 OCMStub([secondViewController viewIfLoaded]).andReturn(secondView);
468 OCMStub([
engine binaryMessenger]).andReturn(messenger);
470 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
474 std::make_shared<fml::SyncSwitch>());
478 flutter::SemanticsNode root_node;
480 root_node.childrenInTraversalOrder = {1};
481 root_node.childrenInHitTestOrder = {1};
482 flutter::SemanticsNode scrollable_node;
483 scrollable_node.id = 1;
484 scrollable_node.flags.hasImplicitScrolling =
true;
485 scrollable_node.actions = flutter::kVerticalScrollSemanticsActions;
486 scrollable_node.rect = SkRect::MakeXYWH(0, 0, 100, 120);
487 scrollable_node.scrollExtentMax = 100.0;
488 scrollable_node.scrollPosition = 10.0;
489 flutter::SemanticsNodeUpdates update;
490 update[kRootNodeId] = root_node;
491 update[scrollable_node.id] = scrollable_node;
493 flutter::CustomAccessibilityActionUpdates());
494 XCTAssertEqual(firstView.subviews.count, 1ul);
498 CGRect expected_frame = scrollable_view.frame;
499 CGSize expected_content_size = scrollable_view.contentSize;
500 CGPoint expected_content_offset = scrollable_view.contentOffset;
503 XCTAssertEqual(firstView.subviews.count, 0ul);
504 scrollable_view.frame = CGRectMake(1, 2, 3, 4);
505 scrollable_view.contentSize = CGSizeMake(5, 6);
506 scrollable_view.contentOffset = CGPointMake(7, 8);
509 XCTAssertEqual(secondView.subviews.count, 1ul);
510 XCTAssertEqual(secondView.subviews.firstObject, scrollable_view);
511 XCTAssertTrue(CGRectEqualToRect(scrollable_view.frame, expected_frame));
512 XCTAssertTrue(CGSizeEqualToSize(scrollable_view.contentSize, expected_content_size));
513 XCTAssertTrue(CGPointEqualToPoint(scrollable_view.contentOffset, expected_content_offset));
516 [engine stopMocking];
519 - (void)testLocaleCanBeSetWithoutViewController {
520 flutter::MockDelegate mock_delegate;
521 auto thread = std::make_unique<fml::Thread>(
"PlatformViewIOSTest");
522 auto thread_task_runner = thread->GetTaskRunner();
523 flutter::TaskRunners runners(
self.name.UTF8String,
533 OCMStub([flutterViewController isViewLoaded]).andReturn(NO);
534 OCMStub([flutterViewController
engine]).andReturn(
engine);
535 OCMStub([
engine binaryMessenger]).andReturn(messenger);
537 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
541 std::make_shared<fml::SyncSwitch>());
542 fml::AutoResetWaitableEvent latch;
543 thread_task_runner->PostTask([&] {
544 std::string locale =
"en-US";
546 platform_view->SetOwnerViewController(flutterViewController);
547 OCMVerify([flutterViewController setApplicationLocale:
@"en-US"]);
552 [engine stopMocking];
constexpr NS_ASSUME_NONNULL_BEGIN int32_t kRootNodeId
SemanticsObject * semanticsObject
nullable flutter::AccessibilityBridgeIos * bridge()