Skip to content

Commit 811247e

Browse files
[web][a11y] Initialize slider semantics values when enabling (flutter#195564)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> When a `Slider` is enabled after its semantics node already exists, the web engine attaches a new `SemanticIncrementable` role to a node whose `value`, `increasedValue`, and `decreasedValue` fields are not dirty. `_updateInputValues()` then returns early on the role's first `update()`, so the hidden `<input type="range">` is exposed to assistive technology without `value`, `min`, `max`, `aria-valuetext`, `aria-valuenow`, `aria-valuemin`, or `aria-valuemax`. The browser announces the default range value ("50") until the slider's value changes once. This change starts `_pendingResync` as `true`, so a newly attached role performs one initial input-value sync on its first `update()` — the approach proposed by @cashmula in the issue's "Likely cause" analysis, which also identified the root cause. The flag is cleared after that sync; its existing role in `change`-event handling is unchanged. `SemanticsNodeUpdate` normalizes absent value fields to empty strings, so the initial sync cannot hit null-asserts. Three regression tests are added to `test/engine/semantics/semantics_test.dart`: enabling a previously disabled slider with unchanged values (the reported bug), disabling and re-enabling a slider (role reattachment resync), and role attachment while the engine is in pointer-events gesture mode (input stays disabled but attributes still sync). The first test fails on unmodified code: the input renders as bare `<input type="range" role="slider" tabindex="0">` with `value="50"`. Verified locally with `felt` on Windows against `--gcs-prod` artifacts matching the base revision: - `chrome-dart2js-canvaskit-engine`, all 12 `test/engine/semantics` files: 280/280 tests passed. - `chrome-dart2wasm-canvaskit-engine`, `semantics_test.dart`: all 191 tests passed. - `dart analyze` and `dart format` clean on the changed files. Limitations: tested in Chrome with the CanvasKit renderer only (the engine test-set has no skwasm suite in `felt_config.yaml`); Skia Gold screenshot checks were skipped locally; verified at the engine DOM level, not with a live screen reader. Upstream CI has not run yet. ### AI disclosure The patch implementation and tests were prepared with assistance from Devin AI under human direction. I have independently reviewed the complete diff and understand the changes. Fixes flutter#195215 No changes to the [flutter/tests] repo were needed, so no migration guide applies. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Co-authored-by: NemesisDevX <[email protected]> Co-authored-by: zhongliugo <[email protected]>
1 parent 63be312 commit 811247e

2 files changed

Lines changed: 216 additions & 4 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/incrementable.dart‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ class SemanticIncrementable extends SemanticRole {
106106
/// [GestureMode.browserGestures].
107107
late final GestureModeCallback _gestureModeListener;
108108

109-
/// Whether we forwarded a semantics action to the framework and awaiting an
110-
/// update.
109+
/// Whether we forwarded a semantics action to the framework and are awaiting
110+
/// an update, or the role was newly attached and has not yet synced its
111+
/// input values.
111112
///
112113
/// This field is used to determine whether the HTML DOM of the semantics
113-
/// tree should be updated.
114-
bool _pendingResync = false;
114+
/// tree should be updated. It starts out as true so that a newly attached
115+
/// role syncs its initial range attributes even when the semantics object's
116+
/// value fields are not dirty.
117+
bool _pendingResync = true;
115118

116119
@override
117120
void updateValidationResult() {

‎engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart‎

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,215 @@ void _testIncrementables() {
28832883
);
28842884
});
28852885

2886+
// Regression test for https://github.com/flutter/flutter/issues/195215.
2887+
// A disabled slider reports value/increasedValue/decreasedValue but no
2888+
// increase/decrease actions, so the node does not receive the incrementable
2889+
// role. When the slider is later enabled without changing those values, the
2890+
// node gains the incrementable role while its value fields are clean. The
2891+
// newly created role must still populate the input's value and aria-*
2892+
// attributes on its first update.
2893+
test('populates input values when the incrementable role is added to a clean node', () async {
2894+
semantics()
2895+
..debugOverrideTimestampFunction(() => _testTime)
2896+
..semanticsEnabled = true;
2897+
addTearDown(() {
2898+
semantics().semanticsEnabled = false;
2899+
});
2900+
2901+
final tester = SemanticsTester(owner());
2902+
2903+
// A disabled slider: value fields are present but there are no
2904+
// increase/decrease actions, so the node is not incrementable.
2905+
tester.updateNode(
2906+
id: 0,
2907+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isFalse, isSlider: true),
2908+
value: '50%',
2909+
increasedValue: '60%',
2910+
decreasedValue: '40%',
2911+
transform: Matrix4.identity().toFloat64(),
2912+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
2913+
);
2914+
tester.apply();
2915+
2916+
final SemanticsObject node = owner().debugSemanticsTree![0]!;
2917+
expect(node.semanticRole?.kind, isNot(EngineSemanticsRole.incrementable));
2918+
expect(owner().semanticsHost.querySelector('input'), isNull);
2919+
2920+
// Enable the slider. Only flags and actions change; the value fields are
2921+
// identical to the previous update, so they are not marked dirty.
2922+
tester.updateNode(
2923+
id: 0,
2924+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isTrue, isSlider: true),
2925+
hasIncrease: true,
2926+
hasDecrease: true,
2927+
value: '50%',
2928+
increasedValue: '60%',
2929+
decreasedValue: '40%',
2930+
transform: Matrix4.identity().toFloat64(),
2931+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
2932+
);
2933+
tester.apply();
2934+
2935+
expect(node.semanticRole?.kind, EngineSemanticsRole.incrementable);
2936+
expectSemanticsTree(owner(), '''
2937+
<sem>
2938+
<input role="slider" aria-valuenow="1" aria-valuetext="50%" aria-valuemax="2" aria-valuemin="0">
2939+
</sem>''');
2940+
2941+
final input = owner().semanticsHost.querySelector('input')! as DomHTMLInputElement;
2942+
expect(input.value, '1');
2943+
expect(input.getAttribute('aria-valuenow'), '1');
2944+
expect(input.getAttribute('aria-valuetext'), '50%');
2945+
expect(input.getAttribute('max'), '2');
2946+
expect(input.getAttribute('aria-valuemax'), '2');
2947+
expect(input.getAttribute('min'), '0');
2948+
expect(input.getAttribute('aria-valuemin'), '0');
2949+
});
2950+
2951+
// A slider that is disabled and then re-enabled with an unchanged value
2952+
// loses and regains the incrementable role. The newly attached role must
2953+
// resync the input's range attributes even though the value fields were not
2954+
// modified between the two updates.
2955+
test(
2956+
'resyncs input values when the incrementable role is reattached with clean values',
2957+
() async {
2958+
semantics()
2959+
..debugOverrideTimestampFunction(() => _testTime)
2960+
..semanticsEnabled = true;
2961+
addTearDown(() {
2962+
semantics().semanticsEnabled = false;
2963+
});
2964+
2965+
final tester = SemanticsTester(owner());
2966+
2967+
// An enabled slider.
2968+
tester.updateNode(
2969+
id: 0,
2970+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isTrue, isSlider: true),
2971+
hasIncrease: true,
2972+
hasDecrease: true,
2973+
value: '50%',
2974+
increasedValue: '60%',
2975+
decreasedValue: '40%',
2976+
transform: Matrix4.identity().toFloat64(),
2977+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
2978+
);
2979+
tester.apply();
2980+
2981+
var input = owner().semanticsHost.querySelector('input')! as DomHTMLInputElement;
2982+
expect(input.getAttribute('aria-valuetext'), '50%');
2983+
2984+
// Disable the slider: the actions are dropped and the node loses the
2985+
// incrementable role.
2986+
tester.updateNode(
2987+
id: 0,
2988+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isFalse, isSlider: true),
2989+
value: '50%',
2990+
increasedValue: '60%',
2991+
decreasedValue: '40%',
2992+
transform: Matrix4.identity().toFloat64(),
2993+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
2994+
);
2995+
tester.apply();
2996+
2997+
final SemanticsObject node = owner().debugSemanticsTree![0]!;
2998+
expect(node.semanticRole?.kind, isNot(EngineSemanticsRole.incrementable));
2999+
expect(owner().semanticsHost.querySelector('input'), isNull);
3000+
3001+
// Re-enable the slider with identical values: the node regains the
3002+
// incrementable role while its value fields are clean.
3003+
tester.updateNode(
3004+
id: 0,
3005+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isTrue, isSlider: true),
3006+
hasIncrease: true,
3007+
hasDecrease: true,
3008+
value: '50%',
3009+
increasedValue: '60%',
3010+
decreasedValue: '40%',
3011+
transform: Matrix4.identity().toFloat64(),
3012+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
3013+
);
3014+
tester.apply();
3015+
3016+
expect(node.semanticRole?.kind, EngineSemanticsRole.incrementable);
3017+
expectSemanticsTree(owner(), '''
3018+
<sem>
3019+
<input role="slider" aria-valuenow="1" aria-valuetext="50%" aria-valuemax="2" aria-valuemin="0">
3020+
</sem>''');
3021+
3022+
input = owner().semanticsHost.querySelector('input')! as DomHTMLInputElement;
3023+
expect(input.value, '1');
3024+
expect(input.getAttribute('aria-valuenow'), '1');
3025+
expect(input.getAttribute('aria-valuetext'), '50%');
3026+
expect(input.getAttribute('max'), '2');
3027+
expect(input.getAttribute('aria-valuemax'), '2');
3028+
expect(input.getAttribute('min'), '0');
3029+
expect(input.getAttribute('aria-valuemin'), '0');
3030+
},
3031+
);
3032+
3033+
// Same as above, but the role is attached while the engine is in
3034+
// pointer-events gesture mode. The input stays disabled, but its range
3035+
// attributes must still be synced so assistive technology can announce the
3036+
// value once the mode switches back to browser gestures.
3037+
test(
3038+
'populates input values when the incrementable role is added in pointer-events mode',
3039+
() async {
3040+
semantics()
3041+
..debugOverrideTimestampFunction(() => _testTime)
3042+
..semanticsEnabled = true;
3043+
addTearDown(() {
3044+
semantics().semanticsEnabled = false;
3045+
});
3046+
3047+
final tester = SemanticsTester(owner());
3048+
3049+
tester.updateNode(
3050+
id: 0,
3051+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isFalse, isSlider: true),
3052+
value: '50%',
3053+
increasedValue: '60%',
3054+
decreasedValue: '40%',
3055+
transform: Matrix4.identity().toFloat64(),
3056+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
3057+
);
3058+
tester.apply();
3059+
expect(owner().semanticsHost.querySelector('input'), isNull);
3060+
3061+
semantics().receiveGlobalEvent(createDomEvent('Event', 'pointerdown'));
3062+
expect(semantics().gestureMode, GestureMode.pointerEvents);
3063+
3064+
tester.updateNode(
3065+
id: 0,
3066+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isTrue, isSlider: true),
3067+
hasIncrease: true,
3068+
hasDecrease: true,
3069+
value: '50%',
3070+
increasedValue: '60%',
3071+
decreasedValue: '40%',
3072+
transform: Matrix4.identity().toFloat64(),
3073+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
3074+
);
3075+
tester.apply();
3076+
3077+
final SemanticsObject node = owner().debugSemanticsTree![0]!;
3078+
expect(node.semanticRole?.kind, EngineSemanticsRole.incrementable);
3079+
expectSemanticsTree(owner(), '''
3080+
<sem>
3081+
<input role="slider" aria-valuenow="1" aria-valuetext="50%" aria-valuemax="2" aria-valuemin="0">
3082+
</sem>''');
3083+
3084+
final input = owner().semanticsHost.querySelector('input')! as DomHTMLInputElement;
3085+
expect(input.value, '1');
3086+
expect(input.getAttribute('aria-valuetext'), '50%');
3087+
expect(
3088+
input.disabled,
3089+
isTrue,
3090+
reason: 'Input must remain disabled while gesture mode is pointer-events.',
3091+
);
3092+
},
3093+
);
3094+
28863095
test('sends focus events', () async {
28873096
semantics()
28883097
..debugOverrideTimestampFunction(() => _testTime)

0 commit comments

Comments
 (0)