From d30a412e28d11d7ad75d91706e03e78735833324 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Mon, 18 Mar 2024 11:11:59 -0700 Subject: [PATCH] Support accessibility back gesture to pop route from view controller (flutter/engine#51241) Adapted suggestion from https://github.com/flutter/flutter/issues/74246#issuecomment-971326926 to pop the route if the view controller route if `accessibilityPerformEscape` is called at that level in the responder chain. Ideally this could know if the route was successfully popped, and only then return `YES` (halting propagation through the responder chain). I confirmed by editing https://docs.flutter.dev/cookbook/navigation/navigation-basics#interactive-example as a demo that that the two-finger scrub (move two fingers back and forth three times quickly, making a "z") navigates back to the last route, and that the [`SementicObject`](https://github.com/flutter/engine/blob/e889b287f1518711fd728af075462bed403b4fe3/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm#L763) scrub, which already worked, continued to work. I'm not super familiar with this code, hopefully there aren't further gotchas I'm not thinking of. Fixes https://github.com/flutter/flutter/issues/74246 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../framework/Source/FlutterViewController.mm | 9 +++++++++ .../Source/FlutterViewControllerTest.mm | 16 ++++++++++++++++ .../IosUnitTests.xcodeproj/project.pbxproj | 4 ---- .../Tests/FlutterEngineConfig.xcconfig | 2 +- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 64d53602ad..1134b735c6 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -2104,6 +2104,15 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) return flags; } +- (BOOL)accessibilityPerformEscape { + FlutterMethodChannel* navigationChannel = [_engine.get() navigationChannel]; + if (navigationChannel) { + [self popRoute]; + return YES; + } + return NO; +} + + (BOOL)accessibilityIsOnOffSwitchLabelsEnabled { if (@available(iOS 13, *)) { return UIAccessibilityIsOnOffSwitchLabelsEnabled(); diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index a68a55287f..c0c66796dc 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -1365,6 +1365,22 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; XCTAssert((flags & (int32_t)flutter::AccessibilityFeatureFlag::kOnOffSwitchLabels) != 0); } +- (void)testAccessibilityPerformEscapePopsRoute { + FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]); + [mockEngine createShell:@"" libraryURI:@"" initialRoute:nil]; + id mockNavigationChannel = OCMClassMock([FlutterMethodChannel class]); + OCMStub([mockEngine navigationChannel]).andReturn(mockNavigationChannel); + + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine + nibName:nil + bundle:nil]; + XCTAssertTrue([viewController accessibilityPerformEscape]); + + OCMVerify([mockNavigationChannel invokeMethod:@"popRoute" arguments:nil]); + + [mockNavigationChannel stopMocking]; +} + - (void)testPerformOrientationUpdateForcesOrientationChange { [self orientationTestWithOrientationUpdate:UIInterfaceOrientationMaskPortrait currentOrientation:UIInterfaceOrientationLandscapeLeft diff --git a/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj b/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj index e67b60cdfc..0ff04e310a 100644 --- a/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj +++ b/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj @@ -50,7 +50,6 @@ 0AC232F724BA71D300A85907 /* FlutterEngineTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterEngineTest.mm; sourceTree = ""; }; 0AC2330324BA71D300A85907 /* accessibility_bridge_test.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = accessibility_bridge_test.mm; sourceTree = ""; }; 0AC2330B24BA71D300A85907 /* FlutterTextInputPluginTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterTextInputPluginTest.mm; sourceTree = ""; }; - 0AC2330F24BA71D300A85907 /* FlutterBinaryMessengerRelayTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterBinaryMessengerRelayTest.mm; sourceTree = ""; }; 0AC2331024BA71D300A85907 /* connection_collection_test.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = connection_collection_test.mm; sourceTree = ""; }; 0AC2331224BA71D300A85907 /* FlutterEnginePlatformViewTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterEnginePlatformViewTest.mm; sourceTree = ""; }; 0AC2331924BA71D300A85907 /* FlutterPluginAppLifeCycleDelegateTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterPluginAppLifeCycleDelegateTest.mm; sourceTree = ""; }; @@ -72,7 +71,6 @@ 3DD7D38C27D2B81000DA365C /* FlutterUndoManagerPluginTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterUndoManagerPluginTest.mm; sourceTree = ""; }; 689EC1E2281B30D3008FEB58 /* FlutterSpellCheckPluginTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterSpellCheckPluginTest.mm; sourceTree = ""; }; 68B6091227F62F990036AC78 /* VsyncWaiterIosTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = VsyncWaiterIosTest.mm; sourceTree = ""; }; - 73F12C22288F92FF00AFC3A6 /* FlutterViewControllerTest_mrc.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterViewControllerTest_mrc.mm; sourceTree = ""; }; D2D361A52B234EAC0018964E /* FlutterMetalLayerTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterMetalLayerTest.mm; sourceTree = ""; }; F7521D7226BB671E005F15C5 /* libios_test_flutter.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libios_test_flutter.dylib; path = "../../../../out/$(FLUTTER_ENGINE)/libios_test_flutter.dylib"; sourceTree = ""; }; F7521D7526BB673E005F15C5 /* libocmock_shared.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libocmock_shared.dylib; path = "../../../../out/$(FLUTTER_ENGINE)/libocmock_shared.dylib"; sourceTree = ""; }; @@ -109,12 +107,10 @@ 0AC232F724BA71D300A85907 /* FlutterEngineTest.mm */, 0AC2330324BA71D300A85907 /* accessibility_bridge_test.mm */, 0AC2330B24BA71D300A85907 /* FlutterTextInputPluginTest.mm */, - 0AC2330F24BA71D300A85907 /* FlutterBinaryMessengerRelayTest.mm */, 0AC2331024BA71D300A85907 /* connection_collection_test.mm */, 0AC2331224BA71D300A85907 /* FlutterEnginePlatformViewTest.mm */, 0AC2331924BA71D300A85907 /* FlutterPluginAppLifeCycleDelegateTest.mm */, 0AC2332124BA71D300A85907 /* FlutterViewControllerTest.mm */, - 73F12C22288F92FF00AFC3A6 /* FlutterViewControllerTest_mrc.mm */, D2D361A52B234EAC0018964E /* FlutterMetalLayerTest.mm */, ); name = Source; diff --git a/engine/src/flutter/testing/ios/IosUnitTests/Tests/FlutterEngineConfig.xcconfig b/engine/src/flutter/testing/ios/IosUnitTests/Tests/FlutterEngineConfig.xcconfig index f54d553ef4..8f5ca977be 100644 --- a/engine/src/flutter/testing/ios/IosUnitTests/Tests/FlutterEngineConfig.xcconfig +++ b/engine/src/flutter/testing/ios/IosUnitTests/Tests/FlutterEngineConfig.xcconfig @@ -1,3 +1,3 @@ FLUTTER_ENGINE[arch=x86_64]=ios_debug_sim_unopt FLUTTER_ENGINE[arch=arm64]=ios_debug_sim_unopt_arm64 -FLUTTER_ENGINE=ios_debug_sim_unopt +FLUTTER_ENGINE=ios_debug_sim_unopt_arm64