Enables semantics when voice control is turned on (flutter/engine#24640)

This commit is contained in:
chunhtai
2021-03-03 17:24:02 -08:00
committed by GitHub
parent 2f060559e5
commit 935384301f
7 changed files with 103 additions and 1 deletions

View File

@@ -1036,6 +1036,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewTest.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/IOKit.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm

View File

@@ -186,6 +186,7 @@ source_set("ios_test_flutter_mrc") {
"framework/Source/FlutterEngineTest_mrc.mm",
"framework/Source/FlutterPlatformPluginTest.mm",
"framework/Source/FlutterPlatformViewsTest.mm",
"framework/Source/FlutterViewTest.mm",
"framework/Source/accessibility_bridge_test.mm",
]
deps = [

View File

@@ -761,7 +761,7 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
arguments:@[ @(client), @(start), @(end) ]];
}
#pragma mark - Screenshot Delegate
#pragma mark - FlutterViewEngineDelegate
- (flutter::Rasterizer::Screenshot)takeScreenshot:(flutter::Rasterizer::ScreenshotType)type
asBase64Encoded:(BOOL)base64Encode {
@@ -769,6 +769,12 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
return _shell->Screenshot(type, base64Encode);
}
- (void)flutterViewAccessibilityDidCall {
if (self.viewController.view.accessibilityElements == nil) {
[self ensureSemanticsEnabled];
}
}
- (NSObject<FlutterBinaryMessenger>*)binaryMessenger {
return _binaryMessenger;
}

View File

@@ -12,6 +12,18 @@
FLUTTER_ASSERT_NOT_ARC
@interface FlutterEngineSpy : FlutterEngine
@property(nonatomic) BOOL ensureSemanticsEnabledCalled;
@end
@implementation FlutterEngineSpy
- (void)ensureSemanticsEnabled {
_ensureSemanticsEnabledCalled = YES;
}
@end
@interface FlutterEngineTest_mrc : XCTestCase
@end
@@ -41,4 +53,11 @@ FLUTTER_ASSERT_NOT_ARC
[spawn release];
}
- (void)testEnableSemanticsWhenFlutterViewAccessibilityDidCall {
FlutterEngineSpy* engine = [[FlutterEngineSpy alloc] initWithName:@"foobar"];
engine.ensureSemanticsEnabledCalled = NO;
[engine flutterViewAccessibilityDidCall];
XCTAssertTrue(engine.ensureSemanticsEnabledCalled);
}
@end

View File

@@ -21,6 +21,16 @@
asBase64Encoded:(BOOL)base64Encode;
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController;
/**
* A callback that is called when iOS queries accessibility information of the Flutter view.
*
* This is useful to predict the current iOS accessibility status. For example, there is
* no API to listen whether voice control is turned on or off. The Flutter engine uses
* this callback to enable semantics in order to catch the case that voice control is
* on.
*/
- (void)flutterViewAccessibilityDidCall;
@end
@interface FlutterView : UIView

View File

@@ -130,4 +130,17 @@ static BOOL _forceSoftwareRendering;
CGContextRestoreGState(context);
}
- (BOOL)isAccessibilityElement {
// iOS does not provide an API to query whether the voice control
// is turned on or off. It is likely at least one of the assitive
// technologies is turned on if this method is called. If we do
// not catch it in notification center, we will catch it here.
//
// TODO(chunhtai): Remove this workaround once iOS provides an
// API to query whether voice control is enabled.
// https://github.com/flutter/flutter/issues/76808.
[_delegate flutterViewAccessibilityDidCall];
return NO;
}
@end

View File

@@ -0,0 +1,52 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <XCTest/XCTest.h>
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
@interface FakeDelegate : NSObject <FlutterViewEngineDelegate>
@property(nonatomic) BOOL callbackCalled;
@end
@implementation FakeDelegate {
std::shared_ptr<flutter::FlutterPlatformViewsController> _platformViewsController;
}
- (instancetype)init {
_callbackCalled = NO;
_platformViewsController = std::shared_ptr<flutter::FlutterPlatformViewsController>(nullptr);
return self;
}
- (flutter::Rasterizer::Screenshot)takeScreenshot:(flutter::Rasterizer::ScreenshotType)type
asBase64Encoded:(BOOL)base64Encode {
return {};
}
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController {
return _platformViewsController;
}
- (void)flutterViewAccessibilityDidCall {
_callbackCalled = YES;
}
@end
@interface FlutterViewTest : XCTestCase
@end
@implementation FlutterViewTest
- (void)testFlutterViewEnableSemanticsWhenIsAccessibilityElementIsCalled {
FakeDelegate* delegate = [[FakeDelegate alloc] init];
FlutterView* view = [[FlutterView alloc] initWithDelegate:delegate opaque:NO];
delegate.callbackCalled = NO;
XCTAssertFalse(view.isAccessibilityElement);
XCTAssertTrue(delegate.callbackCalled);
}
@end