Skip to content

Commit

Permalink
Add null-aware access to semantics instance (flutter#40146)
Browse files Browse the repository at this point in the history
* Add null-aware access to semantics instance

* Change callsites to use nullable instance

* Revert

* Add test

* whitespace

* Typo

* Fix comment

* Whitespace

* Comment fix
  • Loading branch information
htoor3 authored Mar 14, 2023
1 parent 59e0ced commit 9adff0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/web_ui/lib/src/engine/semantics/text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
/// This method must be called prior to accessing [instance].
static SemanticsTextEditingStrategy ensureInitialized(
HybridTextEditing owner) {
if (_instance != null && instance.owner == owner) {
return instance;
if (_instance != null && _instance?.owner == owner) {
return _instance!;
}
return _instance = SemanticsTextEditingStrategy(owner);
}
Expand Down Expand Up @@ -401,7 +401,7 @@ class TextField extends RoleManager {
allowInterop((DomEvent event) {
semanticsObject.element.setAttribute('role', 'textbox');
activeEditableElement.remove();
SemanticsTextEditingStrategy.instance.deactivate(this);
SemanticsTextEditingStrategy._instance?.deactivate(this);

// Focus on semantics element before removing the editable element, so that
// the user can continue navigating the page with the assistive technology.
Expand Down Expand Up @@ -430,11 +430,11 @@ class TextField extends RoleManager {
activeEditableElement.focus();
});
}
SemanticsTextEditingStrategy.instance.activate(this);
SemanticsTextEditingStrategy._instance?.activate(this);
} else if (flutterViewEmbedder.glassPaneShadow.activeElement ==
activeEditableElement) {
if (!isIosSafari) {
SemanticsTextEditingStrategy.instance.deactivate(this);
SemanticsTextEditingStrategy._instance?.deactivate(this);
// Only apply text, because this node is not focused.
}
activeEditableElement.blur();
Expand All @@ -460,6 +460,6 @@ class TextField extends RoleManager {
if (!isIosSafari) {
editableElement?.remove();
}
SemanticsTextEditingStrategy.instance.deactivate(this);
SemanticsTextEditingStrategy._instance?.deactivate(this);
}
}
28 changes: 28 additions & 0 deletions lib/web_ui/test/engine/semantics/text_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ void testMain() {
EngineSemanticsOwner.debugResetSemantics();
});

group('$SemanticsTextEditingStrategy pre-initialization tests', () {
setUp(() {
semantics()
..debugOverrideTimestampFunction(() => _testTime)
..semanticsEnabled = true;
});

tearDown(() {
semantics().semanticsEnabled = false;
});

test('Calling dispose() pre-initialization will not throw an error', () {
final SemanticsObject textFieldSemantics = createTextFieldSemantics(
value: 'hi',
isFocused: true,
);
final TextField textField =
textFieldSemantics.debugRoleManagerFor(Role.textField)! as TextField;

// ensureInitialized() isn't called prior to calling dispose() here.
// Since we are conditionally calling dispose() on our
// SemanticsTextEditingStrategy._instance, we shouldn't expect an error.
// ref: https://github.com/flutter/engine/pull/40146
expect(() => textField.dispose(), returnsNormally);
});
});

group('$SemanticsTextEditingStrategy', () {
late HybridTextEditing testTextEditing;
late SemanticsTextEditingStrategy strategy;
Expand Down Expand Up @@ -867,6 +894,7 @@ void testMain() {
}, skip: !isSafari);
}


SemanticsObject createTextFieldSemantics({
required String value,
String label = '',
Expand Down

0 comments on commit 9adff0a

Please sign in to comment.