From 1044ef57a4a520183e77acbbee61af530f829139 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Fri, 10 Sep 2021 11:32:02 -0700 Subject: [PATCH] Fixes accessibility issue that bridge fails to clean up FlutterScrollableSemanticsObject (flutter/engine#28495) --- .../ci/licenses_golden/licenses_flutter | 2 + .../shell/platform/darwin/ios/BUILD.gn | 2 + .../Source/FlutterSemanticsScrollView.h | 30 ++ .../Source/FlutterSemanticsScrollView.mm | 138 +++++++++ .../ios/framework/Source/SemanticsObject.h | 17 +- .../ios/framework/Source/SemanticsObject.mm | 188 +++--------- .../framework/Source/SemanticsObjectTest.mm | 162 +++++----- .../framework/Source/accessibility_bridge.mm | 21 +- .../Source/accessibility_bridge_test.mm | 282 ++++++++++++++++++ 9 files changed, 587 insertions(+), 255 deletions(-) create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.mm diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 2414d16eaa..215297bdfd 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1104,6 +1104,8 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginA FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPlugin.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPlugin.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPluginTest.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index 100d06916d..fdc3e9fd1a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -76,6 +76,8 @@ source_set("flutter_framework_source") { "framework/Source/FlutterPluginAppLifeCycleDelegate.mm", "framework/Source/FlutterRestorationPlugin.h", "framework/Source/FlutterRestorationPlugin.mm", + "framework/Source/FlutterSemanticsScrollView.h", + "framework/Source/FlutterSemanticsScrollView.mm", "framework/Source/FlutterTextInputDelegate.h", "framework/Source/FlutterTextInputPlugin.h", "framework/Source/FlutterTextInputPlugin.mm", diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h new file mode 100644 index 0000000000..e5de3e0bab --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h @@ -0,0 +1,30 @@ +// 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. + +#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_SEMANTICS_SCROLL_VIEW_H_ +#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_SEMANTICS_SCROLL_VIEW_H_ + +#import + +NS_ASSUME_NONNULL_BEGIN +@class SemanticsObject; + +/** + * A UIScrollView to represent Flutter scrollable in iOS accessibility + * services. + * + * This class is hidden from the user and can't be interacted with. It + * sends all of selector calls from accessibility services to the + * owner SemanticsObject. + */ +@interface FlutterSemanticsScrollView : UIScrollView + +- (instancetype)init NS_UNAVAILABLE; +- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; +- (instancetype)initWithCoder:(NSCoder*)coder NS_UNAVAILABLE; +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject; + +@end +NS_ASSUME_NONNULL_END +#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_SEMANTICS_SCROLL_VIEW_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.mm new file mode 100644 index 0000000000..ad5dd0d19d --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.mm @@ -0,0 +1,138 @@ +// 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 "flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h" + +#import "flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h" + +@interface FlutterSemanticsScrollView () +@property(nonatomic, assign) SemanticsObject* semanticsObject; +@end + +@implementation FlutterSemanticsScrollView + +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject { + self = [super initWithFrame:CGRectZero]; + if (self) { + _semanticsObject = semanticsObject; + } + return self; +} + +- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event { + return nil; +} + +// The following methods are explicitly forwarded to the wrapped SemanticsObject because the +// forwarding logic above doesn't apply to them since they are also implemented in the +// UIScrollView class, the base class. + +- (BOOL)isAccessibilityElement { + if (![_semanticsObject isAccessibilityBridgeAlive]) { + return NO; + } + + if ([_semanticsObject isAccessibilityElement]) { + return YES; + } + if (self.contentSize.width > self.frame.size.width || + self.contentSize.height > self.frame.size.height) { + // In SwitchControl or VoiceControl, the isAccessibilityElement must return YES + // in order to use scroll actions. + return !_semanticsObject.bridge->isVoiceOverRunning(); + } else { + return NO; + } +} + +- (NSString*)accessibilityLabel { + return [_semanticsObject accessibilityLabel]; +} + +- (NSAttributedString*)accessibilityAttributedLabel { + return [_semanticsObject accessibilityAttributedLabel]; +} + +- (NSString*)accessibilityValue { + return [_semanticsObject accessibilityValue]; +} + +- (NSAttributedString*)accessibilityAttributedValue { + return [_semanticsObject accessibilityAttributedValue]; +} + +- (NSString*)accessibilityHint { + return [_semanticsObject accessibilityHint]; +} + +- (NSAttributedString*)accessibilityAttributedHint { + return [_semanticsObject accessibilityAttributedHint]; +} + +- (BOOL)accessibilityActivate { + return [_semanticsObject accessibilityActivate]; +} + +- (void)accessibilityIncrement { + [_semanticsObject accessibilityIncrement]; +} + +- (void)accessibilityDecrement { + [_semanticsObject accessibilityDecrement]; +} + +- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { + return [_semanticsObject accessibilityScroll:direction]; +} + +- (BOOL)accessibilityPerformEscape { + return [_semanticsObject accessibilityPerformEscape]; +} + +- (void)accessibilityElementDidBecomeFocused { + [_semanticsObject accessibilityElementDidBecomeFocused]; +} + +- (void)accessibilityElementDidLoseFocus { + [_semanticsObject accessibilityElementDidLoseFocus]; +} + +- (id)accessibilityContainer { + return [_semanticsObject accessibilityContainer]; +} + +- (NSInteger)accessibilityElementCount { + return [[_semanticsObject children] count]; +} + +- (id)accessibilityElementAtIndex:(NSInteger)index { + SemanticsObject* child = [_semanticsObject children][index]; + + // Swap the original `SemanticsObject` to a `PlatformViewSemanticsContainer` + if (child.node.IsPlatformViewNode()) { + child.platformViewSemanticsContainer.index = index; + return child.platformViewSemanticsContainer; + } + + if ([child hasChildren]) + return [child accessibilityContainer]; + return [child nativeAccessibility]; +} + +- (NSInteger)indexOfAccessibilityElement:(id)element { + if ([element isKindOfClass:[FlutterPlatformViewSemanticsContainer class]]) { + return ((FlutterPlatformViewSemanticsContainer*)element).index; + } + + NSArray* children = [_semanticsObject children]; + for (size_t i = 0; i < [children count]; i++) { + SemanticsObject* child = children[i]; + if ((![child hasChildren] && child == element) || + ([child hasChildren] && [child accessibilityContainer] == element)) + return i; + } + return NSNotFound; +} + +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h index dc9e0073e4..b2202f0e61 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h @@ -66,6 +66,15 @@ constexpr float kScrollExtentMaxForInf = 1000; */ @property(strong, nonatomic) FlutterPlatformViewSemanticsContainer* platformViewSemanticsContainer; +/** + * The UIAccessibility that represents this object. + * + * By default, this return self. Subclasses can override to return different + * objects to represent them. For example, FlutterScrollableSemanticsObject[s] + * maintain UIScrollView[s] to represent their UIAccessibility[s]. + */ +@property(nonatomic, readonly) id nativeAccessibility; + /** * Due to the fact that VoiceOver may hold onto SemanticObjects even after it shuts down, * there can be situations where the AccessibilityBridge is shutdown, but the SemanticObject @@ -171,13 +180,7 @@ constexpr float kScrollExtentMaxForInf = 1000; /// The semantics object for scrollable. This class creates an UIScrollView to interact with the /// iOS. -@interface FlutterScrollableSemanticsObject : UIScrollView - -- (instancetype)init NS_UNAVAILABLE; -- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; -- (instancetype)initWithCoder:(NSCoder*)coder NS_UNAVAILABLE; -- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject NS_DESIGNATED_INITIALIZER; -- (void)accessibilityBridgeDidFinishUpdate; +@interface FlutterScrollableSemanticsObject : SemanticsObject @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm index 50ff757c51..af44bdd245 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm @@ -6,6 +6,7 @@ #include "flutter/fml/platform/darwin/scoped_nsobject.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h" namespace { @@ -145,48 +146,31 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { @end // FlutterSwitchSemanticsObject @interface FlutterScrollableSemanticsObject () -@property(nonatomic, strong) SemanticsObject* semanticsObject; +@property(nonatomic, strong) FlutterSemanticsScrollView* scrollView; @end @implementation FlutterScrollableSemanticsObject { fml::scoped_nsobject _container; } -- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject { - self = [super initWithFrame:CGRectZero]; +- (instancetype)initWithBridge:(fml::WeakPtr)bridge + uid:(int32_t)uid { + self = [super initWithBridge:bridge uid:uid]; if (self) { - _semanticsObject = [semanticsObject retain]; - [semanticsObject.bridge->view() addSubview:self]; - [self setShowsHorizontalScrollIndicator:NO]; - [self setShowsVerticalScrollIndicator:NO]; + _scrollView = [[FlutterSemanticsScrollView alloc] initWithSemanticsObject:self]; + [_scrollView setShowsHorizontalScrollIndicator:NO]; + [_scrollView setShowsVerticalScrollIndicator:NO]; + [self.bridge->view() addSubview:_scrollView]; } return self; } - (void)dealloc { - _container.get().semanticsObject = nil; - [_semanticsObject release]; - [self removeFromSuperview]; + [_scrollView removeFromSuperview]; + [_scrollView release]; [super dealloc]; } -- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event { - return nil; -} - -- (NSMethodSignature*)methodSignatureForSelector:(SEL)sel { - NSMethodSignature* result = [super methodSignatureForSelector:sel]; - if (!result) { - result = [_semanticsObject methodSignatureForSelector:sel]; - } - return result; -} - -- (void)forwardInvocation:(NSInvocation*)anInvocation { - [anInvocation setTarget:_semanticsObject]; - [anInvocation invoke]; -} - - (void)accessibilityBridgeDidFinishUpdate { // In order to make iOS think this UIScrollView is scrollable, the following // requirements must be true. @@ -196,67 +180,22 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { // Once the requirements are met, the iOS uses contentOffset to determine // what scroll actions are available. e.g. If the view scrolls vertically and // contentOffset is 0.0, only the scroll down action is available. - [self setFrame:[_semanticsObject accessibilityFrame]]; - [self setContentSize:[self contentSizeInternal]]; - [self setContentOffset:[self contentOffsetInternal] animated:NO]; + [_scrollView setFrame:[self accessibilityFrame]]; + [_scrollView setContentSize:[self contentSizeInternal]]; + [_scrollView setContentOffset:[self contentOffsetInternal] animated:NO]; } -- (void)setChildren:(NSArray*)children { - [_semanticsObject setChildren:children]; - // The children's parent is pointing to _semanticsObject, need to manually - // set it this object. - for (SemanticsObject* child in _semanticsObject.children) { - child.parent = (SemanticsObject*)self; - } -} - -- (id)accessibilityContainer { - if (![_semanticsObject isAccessibilityBridgeAlive]) { - return nil; - } - - if ([_semanticsObject hasChildren] || [_semanticsObject uid] == kRootNodeId) { - if (_container == nil) { - _container.reset([[SemanticsObjectContainer alloc] - initWithSemanticsObject:(SemanticsObject*)self - bridge:[_semanticsObject bridge]]); - } - return _container.get(); - } - if ([_semanticsObject parent] == nil) { - // This can happen when we have released the accessibility tree but iOS is - // still holding onto our objects. iOS can take some time before it - // realizes that the tree has changed. - return nil; - } - return [[_semanticsObject parent] accessibilityContainer]; -} - -- (BOOL)isAccessibilityElement { - if (![_semanticsObject isAccessibilityBridgeAlive]) { - return NO; - } - - if ([_semanticsObject isAccessibilityElement]) { - return YES; - } - if (self.contentSize.width > self.frame.size.width || - self.contentSize.height > self.frame.size.height) { - // In SwitchControl or VoiceControl, the isAccessibilityElement must return YES - // in order to use scroll actions. - return !_semanticsObject.bridge->isVoiceOverRunning(); - } else { - return NO; - } +- (id)nativeAccessibility { + return _scrollView; } // private methods - (float)scrollExtentMax { - if (![_semanticsObject isAccessibilityBridgeAlive]) { + if (![self isAccessibilityBridgeAlive]) { return 0.0f; } - float scrollExtentMax = _semanticsObject.node.scrollExtentMax; + float scrollExtentMax = self.node.scrollExtentMax; if (isnan(scrollExtentMax)) { scrollExtentMax = 0.0f; } else if (!isfinite(scrollExtentMax)) { @@ -266,10 +205,10 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { } - (float)scrollPosition { - if (![_semanticsObject isAccessibilityBridgeAlive]) { + if (![self isAccessibilityBridgeAlive]) { return 0.0f; } - float scrollPosition = _semanticsObject.node.scrollPosition; + float scrollPosition = self.node.scrollPosition; if (isnan(scrollPosition)) { scrollPosition = 0.0f; } @@ -279,89 +218,32 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { - (CGSize)contentSizeInternal { CGRect result; - const SkRect& rect = _semanticsObject.node.rect; + const SkRect& rect = self.node.rect; - if (_semanticsObject.node.actions & flutter::kVerticalScrollSemanticsActions) { + if (self.node.actions & flutter::kVerticalScrollSemanticsActions) { result = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height() + [self scrollExtentMax]); - } else if (_semanticsObject.node.actions & flutter::kHorizontalScrollSemanticsActions) { + } else if (self.node.actions & flutter::kHorizontalScrollSemanticsActions) { result = CGRectMake(rect.x(), rect.y(), rect.width() + [self scrollExtentMax], rect.height()); } else { result = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()); } - return ConvertRectToGlobal(_semanticsObject, result).size; + return ConvertRectToGlobal(self, result).size; } - (CGPoint)contentOffsetInternal { CGPoint result; - CGPoint origin = self.frame.origin; - const SkRect& rect = _semanticsObject.node.rect; - if (_semanticsObject.node.actions & flutter::kVerticalScrollSemanticsActions) { - result = ConvertPointToGlobal(_semanticsObject, - CGPointMake(rect.x(), rect.y() + [self scrollPosition])); - } else if (_semanticsObject.node.actions & flutter::kHorizontalScrollSemanticsActions) { - result = ConvertPointToGlobal(_semanticsObject, - CGPointMake(rect.x() + [self scrollPosition], rect.y())); + CGPoint origin = _scrollView.frame.origin; + const SkRect& rect = self.node.rect; + if (self.node.actions & flutter::kVerticalScrollSemanticsActions) { + result = ConvertPointToGlobal(self, CGPointMake(rect.x(), rect.y() + [self scrollPosition])); + } else if (self.node.actions & flutter::kHorizontalScrollSemanticsActions) { + result = ConvertPointToGlobal(self, CGPointMake(rect.x() + [self scrollPosition], rect.y())); } else { result = origin; } return CGPointMake(result.x - origin.x, result.y - origin.y); } -// The following methods are explicitly forwarded to the wrapped SemanticsObject because the -// forwarding logic above doesn't apply to them since they are also implemented in the -// UIScrollView class, the base class. - -- (NSString*)accessibilityLabel { - return [_semanticsObject accessibilityLabel]; -} - -- (NSAttributedString*)accessibilityAttributedLabel { - return [_semanticsObject accessibilityAttributedLabel]; -} - -- (NSString*)accessibilityValue { - return [_semanticsObject accessibilityValue]; -} - -- (NSAttributedString*)accessibilityAttributedValue { - return [_semanticsObject accessibilityAttributedValue]; -} - -- (NSString*)accessibilityHint { - return [_semanticsObject accessibilityHint]; -} - -- (NSAttributedString*)accessibilityAttributedHint { - return [_semanticsObject accessibilityAttributedHint]; -} - -- (BOOL)accessibilityActivate { - return [_semanticsObject accessibilityActivate]; -} - -- (void)accessibilityIncrement { - [_semanticsObject accessibilityIncrement]; -} - -- (void)accessibilityDecrement { - [_semanticsObject accessibilityDecrement]; -} - -- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { - return [_semanticsObject accessibilityScroll:direction]; -} - -- (BOOL)accessibilityPerformEscape { - return [_semanticsObject accessibilityPerformEscape]; -} - -- (void)accessibilityElementDidBecomeFocused { - [_semanticsObject accessibilityElementDidBecomeFocused]; -} - -- (void)accessibilityElementDidLoseFocus { - [_semanticsObject accessibilityElementDidLoseFocus]; -} @end // FlutterScrollableSemanticsObject @implementation FlutterCustomAccessibilityAction { @@ -516,6 +398,10 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { return nil; } +- (id)nativeAccessibility { + return self; +} + #pragma mark - Semantic object private method - (void)privateSetParent:(SemanticsObject*)parent { @@ -913,7 +799,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { - (nullable id)accessibilityElementAtIndex:(NSInteger)index { FML_DCHECK(index < 2); if (index == 0) { - return _semanticsObject; + return _semanticsObject.nativeAccessibility; } else { return _platformView; } @@ -990,7 +876,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { if (index < 0 || index >= [self accessibilityElementCount]) return nil; if (index == 0) { - return _semanticsObject; + return _semanticsObject.nativeAccessibility; } SemanticsObject* child = [_semanticsObject children][index - 1]; @@ -1003,7 +889,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { if ([child hasChildren]) return [child accessibilityContainer]; - return child; + return child.nativeAccessibility; } - (NSInteger)indexOfAccessibilityElement:(id)element { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm index 724bd0ce7d..6b8940448c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm @@ -222,19 +222,18 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollPosition = scrollPosition; node.transform = { transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, 1.0}; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; XCTAssertTrue( - CGRectEqualToRect(scrollable.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, + CGRectEqualToRect(scrollView.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, w * effectivelyScale, h * effectivelyScale))); XCTAssertTrue(CGSizeEqualToSize( - scrollable.contentSize, + scrollView.contentSize, CGSizeMake(w * effectivelyScale, (h + scrollExtentMax) * effectivelyScale))); - XCTAssertTrue(CGPointEqualToPoint(scrollable.contentOffset, + XCTAssertTrue(CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(0, scrollPosition * effectivelyScale))); } @@ -262,19 +261,18 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollPosition = scrollPosition; node.transform = { transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, 1.0}; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; XCTAssertTrue( - CGRectEqualToRect(scrollable.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, + CGRectEqualToRect(scrollView.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, w * effectivelyScale, h * effectivelyScale))); XCTAssertTrue(CGSizeEqualToSize( - scrollable.contentSize, + scrollView.contentSize, CGSizeMake(w * effectivelyScale, (h + scrollExtentMax) * effectivelyScale))); - XCTAssertTrue(CGPointEqualToPoint(scrollable.contentOffset, + XCTAssertTrue(CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(0, scrollPosition * effectivelyScale))); } @@ -301,19 +299,18 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollPosition = scrollPosition; node.transform = { transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, 1.0}; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; XCTAssertTrue( - CGRectEqualToRect(scrollable.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, + CGRectEqualToRect(scrollView.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, w * effectivelyScale, h * effectivelyScale))); XCTAssertTrue(CGSizeEqualToSize( - scrollable.contentSize, + scrollView.contentSize, CGSizeMake((w + scrollExtentMax) * effectivelyScale, h * effectivelyScale))); - XCTAssertTrue(CGPointEqualToPoint(scrollable.contentOffset, + XCTAssertTrue(CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(scrollPosition * effectivelyScale, 0))); } @@ -340,20 +337,19 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollPosition = scrollPosition; node.transform = { transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, 1.0}; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; XCTAssertTrue( - CGRectEqualToRect(scrollable.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, + CGRectEqualToRect(scrollView.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, w * effectivelyScale, h * effectivelyScale))); XCTAssertTrue(CGSizeEqualToSize( - scrollable.contentSize, + scrollView.contentSize, CGSizeMake(w * effectivelyScale, (h + kScrollExtentMaxForInf + scrollPosition) * effectivelyScale))); - XCTAssertTrue(CGPointEqualToPoint(scrollable.contentOffset, + XCTAssertTrue(CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(0, scrollPosition * effectivelyScale))); } @@ -380,19 +376,18 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollPosition = scrollPosition; node.transform = { transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, 1.0}; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; XCTAssertTrue( - CGRectEqualToRect(scrollable.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, + CGRectEqualToRect(scrollView.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, w * effectivelyScale, h * effectivelyScale))); // Content size equal to the scrollable size. - XCTAssertTrue(CGSizeEqualToSize(scrollable.contentSize, + XCTAssertTrue(CGSizeEqualToSize(scrollView.contentSize, CGSizeMake(w * effectivelyScale, h * effectivelyScale))); - XCTAssertTrue(CGPointEqualToPoint(scrollable.contentOffset, CGPointMake(0, 0))); + XCTAssertTrue(CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(0, 0))); } - (void)testFlutterScrollableSemanticsObjectIsNotHittestable { @@ -407,13 +402,12 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollExtentMax = 100.0; node.scrollPosition = 0.0; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; - XCTAssertEqual([scrollable hitTest:CGPointMake(10, 10) withEvent:nil], nil); + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; + XCTAssertEqual([scrollView hitTest:CGPointMake(10, 10) withEvent:nil], nil); } - (void)testFlutterScrollableSemanticsObjectIsHiddenWhenVoiceOverIsRunning { @@ -429,15 +423,14 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollExtentMax = 100.0; node.scrollPosition = 0.0; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; - XCTAssertTrue(scrollable_object.isAccessibilityElement); + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; + XCTAssertTrue(scrollView.isAccessibilityElement); mock->isVoiceOverRunningValue = true; - XCTAssertFalse(scrollable_object.isAccessibilityElement); + XCTAssertFalse(scrollView.isAccessibilityElement); } - (void)testFlutterScrollableSemanticsObjectWithLabelValueHintIsNotHiddenWhenVoiceOverIsRunning { @@ -456,16 +449,15 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollExtentMax = 100.0; node.scrollPosition = 0.0; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; - XCTAssertTrue(scrollable_object.isAccessibilityElement); - XCTAssertTrue([scrollable_object.accessibilityLabel isEqualToString:@"label"]); - XCTAssertTrue([scrollable_object.accessibilityValue isEqualToString:@"value"]); - XCTAssertTrue([scrollable_object.accessibilityHint isEqualToString:@"hint"]); + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; + XCTAssertTrue(scrollView.isAccessibilityElement); + XCTAssertTrue([scrollView.accessibilityLabel isEqualToString:@"label"]); + XCTAssertTrue([scrollView.accessibilityValue isEqualToString:@"value"]); + XCTAssertTrue([scrollView.accessibilityHint isEqualToString:@"hint"]); } - (void)testFlutterSemanticsObjectMergeTooltipToLabel { @@ -526,18 +518,17 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { uid:0]; [parentObject setSemanticsNode:&parent]; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:1]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:1]; + [scrollable setSemanticsNode:&node]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; - parentObject.children = @[ scrollable_object ]; + parentObject.children = @[ scrollable ]; [parentObject accessibilityBridgeDidFinishUpdate]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; - XCTAssertTrue(scrollable_object.isAccessibilityElement); + [scrollable accessibilityBridgeDidFinishUpdate]; + XCTAssertTrue(scrollView.isAccessibilityElement); SemanticsObjectContainer* container = - static_cast(scrollable_object.accessibilityContainer); + static_cast(scrollable.accessibilityContainer); XCTAssertEqual(container.semanticsObject, parentObject); } @@ -553,14 +544,14 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { node.scrollExtentMax = 100.0; node.scrollPosition = 0.0; - FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; FlutterScrollableSemanticsObject* scrollable = - [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - SemanticsObject* scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; - XCTAssertFalse(scrollable.showsHorizontalScrollIndicator); - XCTAssertFalse(scrollable.showsVerticalScrollIndicator); + [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; + UIScrollView* scrollView = [scrollable nativeAccessibility]; + + XCTAssertFalse(scrollView.showsHorizontalScrollIndicator); + XCTAssertFalse(scrollView.showsVerticalScrollIndicator); } - (void)testSemanticsObjectBuildsAttributedString { @@ -759,9 +750,7 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { - (void)testFlutterSemanticsObjectReturnsNilContainerWhenBridgeIsNotAlive { FlutterSemanticsObject* parentObject; - FlutterSemanticsObject* delegate; FlutterScrollableSemanticsObject* scrollable; - SemanticsObject* scrollable_object; FlutterSemanticsObject* object2; flutter::SemanticsNode parent; @@ -802,22 +791,21 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { parentObject = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; [parentObject setSemanticsNode:&parent]; - delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:1]; - scrollable = [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; - scrollable_object = static_cast(scrollable); - [scrollable_object setSemanticsNode:&node]; + scrollable = [[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:1]; + [scrollable setSemanticsNode:&node]; + [scrollable accessibilityBridgeDidFinishUpdate]; object2 = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:2]; [object2 setSemanticsNode:&node2]; - parentObject.children = @[ scrollable_object, object2 ]; + parentObject.children = @[ scrollable, object2 ]; [parentObject accessibilityBridgeDidFinishUpdate]; - [scrollable_object accessibilityBridgeDidFinishUpdate]; + [scrollable accessibilityBridgeDidFinishUpdate]; [object2 accessibilityBridgeDidFinishUpdate]; // Returns the correct container if the bridge is alive. SemanticsObjectContainer* container = - static_cast(scrollable_object.accessibilityContainer); + static_cast(scrollable.accessibilityContainer); XCTAssertEqual(container.semanticsObject, parentObject); SemanticsObjectContainer* container2 = static_cast(object2.accessibilityContainer); @@ -825,7 +813,7 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { } // The bridge pointer went out of scope and was deallocated. - XCTAssertNil(scrollable_object.accessibilityContainer); + XCTAssertNil(scrollable.accessibilityContainer); XCTAssertNil(object2.accessibilityContainer); } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm index 073d1d2239..0e7a4ac01d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm @@ -220,14 +220,16 @@ void AccessibilityBridge::UpdateSemantics(flutter::SemanticsNodeUpdates nodes, } if (layoutChanged) { - ios_delegate_->PostAccessibilityNotification(UIAccessibilityLayoutChangedNotification, - FindNextFocusableIfNecessary()); + ios_delegate_->PostAccessibilityNotification( + UIAccessibilityLayoutChangedNotification, + FindNextFocusableIfNecessary().nativeAccessibility); } else if (scrollOccured) { // TODO(chunhtai): figure out what string to use for notification. At this // point, it is guarantee the previous focused object is still in the tree // so that we don't need to worry about focus lost. (e.g. "Screen 0 of 3") - ios_delegate_->PostAccessibilityNotification(UIAccessibilityPageScrolledNotification, - FindNextFocusableIfNecessary()); + ios_delegate_->PostAccessibilityNotification( + UIAccessibilityPageScrolledNotification, + FindNextFocusableIfNecessary().nativeAccessibility); } } } @@ -246,7 +248,7 @@ static void ReplaceSemanticsObject(SemanticsObject* oldObject, SemanticsObject* newObject, NSMutableDictionary* objects) { // `newObject` should represent the same id as `oldObject`. - assert(oldObject.node.id == newObject.node.id); + assert(oldObject.node.id == newObject.uid); NSNumber* nodeId = @(oldObject.node.id); NSUInteger positionInChildlist = [oldObject.parent.children indexOfObject:oldObject]; [objects removeObjectForKey:nodeId]; @@ -264,10 +266,8 @@ static SemanticsObject* CreateObject(const flutter::SemanticsNode& node, node.HasFlag(flutter::SemanticsFlags::kHasCheckedState)) { return [[[FlutterSwitchSemanticsObject alloc] initWithBridge:weak_ptr uid:node.id] autorelease]; } else if (node.HasFlag(flutter::SemanticsFlags::kHasImplicitScrolling)) { - SemanticsObject* delegateObject = - [[[FlutterSemanticsObject alloc] initWithBridge:weak_ptr uid:node.id] autorelease]; - return (SemanticsObject*)[[[FlutterScrollableSemanticsObject alloc] - initWithSemanticsObject:delegateObject] autorelease]; + return [[[FlutterScrollableSemanticsObject alloc] initWithBridge:weak_ptr + uid:node.id] autorelease]; } else { return [[[FlutterSemanticsObject alloc] initWithBridge:weak_ptr uid:node.id] autorelease]; } @@ -294,7 +294,8 @@ SemanticsObject* AccessibilityBridge::GetOrCreateObject(int32_t uid, if (DidFlagChange(object.node, node, flutter::SemanticsFlags::kIsTextField) || DidFlagChange(object.node, node, flutter::SemanticsFlags::kIsReadOnly) || DidFlagChange(object.node, node, flutter::SemanticsFlags::kHasCheckedState) || - DidFlagChange(object.node, node, flutter::SemanticsFlags::kHasToggledState)) { + DidFlagChange(object.node, node, flutter::SemanticsFlags::kHasToggledState) || + DidFlagChange(object.node, node, flutter::SemanticsFlags::kHasImplicitScrolling)) { // The node changed its type. In this case, we cannot reuse the existing // SemanticsObject implementation. Instead, we replace it with a new // instance. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm index 403bf119dd..44035fd238 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm @@ -9,6 +9,7 @@ #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h" #import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h" #import "flutter/shell/platform/darwin/ios/platform_view_ios.h" @@ -304,6 +305,142 @@ fml::RefPtr CreateNewThread(std::string name) { XCTAssertNil(gMockPlatformView); } +- (void)testScrollableSemanticsDeallocated { + flutter::MockDelegate mock_delegate; + auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest"); + flutter::TaskRunners runners(/*label=*/self.name.UTF8String, + /*platform=*/thread_task_runner, + /*raster=*/thread_task_runner, + /*ui=*/thread_task_runner, + /*io=*/thread_task_runner); + + auto flutterPlatformViewsController = std::make_shared(); + auto platform_view = std::make_unique( + /*delegate=*/mock_delegate, + /*rendering_api=*/flutter::IOSRenderingAPI::kSoftware, + /*platform_views_controller=*/flutterPlatformViewsController, + /*task_runners=*/runners); + id engine = OCMClassMock([FlutterEngine class]); + id mockFlutterViewController = OCMClassMock([FlutterViewController class]); + FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine opaque:YES]; + OCMStub([mockFlutterViewController view]).andReturn(flutterView); + std::string label = "some label"; + @autoreleasepool { + auto bridge = std::make_unique( + /*view_controller=*/mockFlutterViewController, + /*platform_view=*/platform_view.get(), + /*platform_views_controller=*/flutterPlatformViewsController); + + flutter::SemanticsNodeUpdates nodes; + flutter::SemanticsNode parent; + parent.id = 0; + parent.rect = SkRect::MakeXYWH(0, 0, 100, 200); + parent.label = "label"; + parent.value = "value"; + parent.hint = "hint"; + + flutter::SemanticsNode node; + node.id = 1; + node.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + node.actions = flutter::kHorizontalScrollSemanticsActions; + node.rect = SkRect::MakeXYWH(0, 0, 100, 200); + node.label = "label"; + node.value = "value"; + node.hint = "hint"; + node.scrollExtentMax = 100.0; + node.scrollPosition = 0.0; + parent.childrenInTraversalOrder.push_back(1); + nodes[0] = parent; + nodes[1] = node; + flutter::CustomAccessibilityActionUpdates actions; + bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions); + XCTAssertTrue([flutterView.subviews count] == 1); + XCTAssertTrue([flutterView.subviews[0] isKindOfClass:[FlutterSemanticsScrollView class]]); + XCTAssertTrue([flutterView.subviews[0].accessibilityLabel isEqualToString:@"label"]); + + // Remove the scrollable from the tree. + flutter::SemanticsNodeUpdates new_nodes; + flutter::SemanticsNode new_parent; + new_parent.id = 0; + new_parent.rect = SkRect::MakeXYWH(0, 0, 100, 200); + new_parent.label = "label"; + new_parent.value = "value"; + new_parent.hint = "hint"; + new_nodes[0] = new_parent; + bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions); + } + XCTAssertTrue([flutterView.subviews count] == 0); +} + +- (void)testBridgeReplacesSemanticsNode { + flutter::MockDelegate mock_delegate; + auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest"); + flutter::TaskRunners runners(/*label=*/self.name.UTF8String, + /*platform=*/thread_task_runner, + /*raster=*/thread_task_runner, + /*ui=*/thread_task_runner, + /*io=*/thread_task_runner); + + auto flutterPlatformViewsController = std::make_shared(); + auto platform_view = std::make_unique( + /*delegate=*/mock_delegate, + /*rendering_api=*/flutter::IOSRenderingAPI::kSoftware, + /*platform_views_controller=*/flutterPlatformViewsController, + /*task_runners=*/runners); + id engine = OCMClassMock([FlutterEngine class]); + id mockFlutterViewController = OCMClassMock([FlutterViewController class]); + FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine opaque:YES]; + OCMStub([mockFlutterViewController view]).andReturn(flutterView); + std::string label = "some label"; + @autoreleasepool { + auto bridge = std::make_unique( + /*view_controller=*/mockFlutterViewController, + /*platform_view=*/platform_view.get(), + /*platform_views_controller=*/flutterPlatformViewsController); + + flutter::SemanticsNodeUpdates nodes; + flutter::SemanticsNode parent; + parent.id = 0; + parent.rect = SkRect::MakeXYWH(0, 0, 100, 200); + parent.label = "label"; + parent.value = "value"; + parent.hint = "hint"; + + flutter::SemanticsNode node; + node.id = 1; + node.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + node.actions = flutter::kHorizontalScrollSemanticsActions; + node.rect = SkRect::MakeXYWH(0, 0, 100, 200); + node.label = "label"; + node.value = "value"; + node.hint = "hint"; + node.scrollExtentMax = 100.0; + node.scrollPosition = 0.0; + parent.childrenInTraversalOrder.push_back(1); + nodes[0] = parent; + nodes[1] = node; + flutter::CustomAccessibilityActionUpdates actions; + bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions); + XCTAssertTrue([flutterView.subviews count] == 1); + XCTAssertTrue([flutterView.subviews[0] isKindOfClass:[FlutterSemanticsScrollView class]]); + XCTAssertTrue([flutterView.subviews[0].accessibilityLabel isEqualToString:@"label"]); + + // Remove implicit scroll from node 1. + flutter::SemanticsNodeUpdates new_nodes; + flutter::SemanticsNode new_node; + new_node.id = 1; + new_node.rect = SkRect::MakeXYWH(0, 0, 100, 200); + new_node.label = "label"; + new_node.value = "value"; + new_node.hint = "hint"; + new_node.scrollExtentMax = 100.0; + new_node.scrollPosition = 0.0; + new_nodes[1] = new_node; + bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions); + } + XCTAssertTrue([flutterView.subviews count] == 0); +} + - (void)testAnnouncesRouteChanges { flutter::MockDelegate mock_delegate; auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest"); @@ -452,6 +589,78 @@ fml::RefPtr CreateNewThread(std::string name) { UIAccessibilityLayoutChangedNotification); } +- (void)testLayoutChangeDoesCallNativeAccessibility { + flutter::MockDelegate mock_delegate; + auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest"); + flutter::TaskRunners runners(/*label=*/self.name.UTF8String, + /*platform=*/thread_task_runner, + /*raster=*/thread_task_runner, + /*ui=*/thread_task_runner, + /*io=*/thread_task_runner); + auto platform_view = std::make_unique( + /*delegate=*/mock_delegate, + /*rendering_api=*/flutter::IOSRenderingAPI::kSoftware, + /*platform_views_controller=*/nil, + /*task_runners=*/runners); + id mockFlutterView = OCMClassMock([FlutterView class]); + id mockFlutterViewController = OCMClassMock([FlutterViewController class]); + OCMStub([mockFlutterViewController view]).andReturn(mockFlutterView); + + NSMutableArray*>* accessibility_notifications = + [[[NSMutableArray alloc] init] autorelease]; + auto ios_delegate = std::make_unique(); + ios_delegate->on_PostAccessibilityNotification_ = + [accessibility_notifications](UIAccessibilityNotifications notification, id argument) { + [accessibility_notifications addObject:@{ + @"notification" : @(notification), + @"argument" : argument ? argument : [NSNull null], + }]; + }; + __block auto bridge = + std::make_unique(/*view_controller=*/mockFlutterViewController, + /*platform_view=*/platform_view.get(), + /*platform_views_controller=*/nil, + /*ios_delegate=*/std::move(ios_delegate)); + + flutter::CustomAccessibilityActionUpdates actions; + flutter::SemanticsNodeUpdates nodes; + + flutter::SemanticsNode node1; + node1.id = 1; + node1.label = "node1"; + nodes[node1.id] = node1; + flutter::SemanticsNode root_node; + root_node.id = kRootNodeId; + root_node.label = "root"; + root_node.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + root_node.childrenInTraversalOrder = {1}; + root_node.childrenInHitTestOrder = {1}; + nodes[root_node.id] = root_node; + bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions); + + // Simulates the focusing on the node 0. + bridge->AccessibilityObjectDidBecomeFocused(0); + + // Remove node 1 to trigger a layout change notification + flutter::CustomAccessibilityActionUpdates new_actions; + flutter::SemanticsNodeUpdates new_nodes; + + flutter::SemanticsNode new_root_node; + new_root_node.id = kRootNodeId; + new_root_node.label = "root"; + new_root_node.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + new_nodes[new_root_node.id] = new_root_node; + bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/new_actions); + + XCTAssertEqual([accessibility_notifications count], 1ul); + SemanticsObject* focusObject = accessibility_notifications[0][@"argument"]; + // Make sure refocus event is sent with the nativeAccessibility of root node + // which is a FlutterSemanticsScrollView. + XCTAssertTrue([focusObject isKindOfClass:[FlutterSemanticsScrollView class]]); + XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue], + UIAccessibilityLayoutChangedNotification); +} + - (void)testAnnouncesRouteChangesAndLayoutChangeInOneUpdate { flutter::MockDelegate mock_delegate; auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest"); @@ -1076,6 +1285,79 @@ fml::RefPtr CreateNewThread(std::string name) { UIAccessibilityPageScrolledNotification); } +- (void)testAnnouncesScrollChangeDoesCallNativeAccessibility { + flutter::MockDelegate mock_delegate; + auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest"); + flutter::TaskRunners runners(/*label=*/self.name.UTF8String, + /*platform=*/thread_task_runner, + /*raster=*/thread_task_runner, + /*ui=*/thread_task_runner, + /*io=*/thread_task_runner); + auto platform_view = std::make_unique( + /*delegate=*/mock_delegate, + /*rendering_api=*/flutter::IOSRenderingAPI::kSoftware, + /*platform_views_controller=*/nil, + /*task_runners=*/runners); + id mockFlutterViewController = OCMClassMock([FlutterViewController class]); + id mockFlutterView = OCMClassMock([FlutterView class]); + OCMStub([mockFlutterViewController view]).andReturn(mockFlutterView); + + NSMutableArray*>* accessibility_notifications = + [[[NSMutableArray alloc] init] autorelease]; + auto ios_delegate = std::make_unique(); + ios_delegate->on_PostAccessibilityNotification_ = + [accessibility_notifications](UIAccessibilityNotifications notification, id argument) { + [accessibility_notifications addObject:@{ + @"notification" : @(notification), + @"argument" : argument ? argument : [NSNull null], + }]; + }; + __block auto bridge = + std::make_unique(/*view_controller=*/mockFlutterViewController, + /*platform_view=*/platform_view.get(), + /*platform_views_controller=*/nil, + /*ios_delegate=*/std::move(ios_delegate)); + + flutter::CustomAccessibilityActionUpdates actions; + flutter::SemanticsNodeUpdates first_update; + + flutter::SemanticsNode node_one; + node_one.id = 1; + node_one.label = "route1"; + node_one.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + node_one.scrollPosition = 0.0; + first_update[node_one.id] = node_one; + flutter::SemanticsNode root_node; + root_node.id = kRootNodeId; + root_node.label = "root"; + root_node.childrenInTraversalOrder = {1}; + root_node.childrenInHitTestOrder = {1}; + first_update[root_node.id] = root_node; + bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions); + + // The first update will trigger a scroll announcement, but we are not interested in it. + [accessibility_notifications removeAllObjects]; + + // Simulates the focusing on the node 1. + bridge->AccessibilityObjectDidBecomeFocused(1); + + flutter::SemanticsNodeUpdates second_update; + // Simulates the scrolling on the node 1. + flutter::SemanticsNode new_node_one; + new_node_one.id = 1; + new_node_one.label = "route1"; + new_node_one.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + new_node_one.scrollPosition = 1.0; + second_update[new_node_one.id] = new_node_one; + bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions); + SemanticsObject* focusObject = accessibility_notifications[0][@"argument"]; + // Make sure refocus event is sent with the nativeAccessibility of node_one + // which is a FlutterSemanticsScrollView. + XCTAssertTrue([focusObject isKindOfClass:[FlutterSemanticsScrollView class]]); + XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue], + UIAccessibilityPageScrolledNotification); +} + - (void)testAnnouncesIgnoresRouteChangesWhenModal { flutter::MockDelegate mock_delegate; auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");