Skip to content

Commit 0563606

Browse files
authored
[VPAT] Declare input purpose on a11y_assessments text fields (flutter#13243)
## Problem The a11y_assessments text field demos do not declare their input purpose, so on web the email fields render `autocomplete="off"` and the password fields render `autocomplete="on"`. The email demo uses `maxLines: null`, which makes the field a `<textarea>`, and the engine always writes `autocomplete="off"` on those. Neither demo sets `autofillHints`. ## Fix The email fields are now single-line with `keyboardType: TextInputType.emailAddress` and `autofillHints: [AutofillHints.email]`, so they render `<input autocomplete="email">`. The `@gmail.com` suffix and its `Semantics` wrapper are removed, since autofill would produce `[email protected]@gmail.com`. `hintMaxLines: 3` keeps the hint wrapping at large text scales, and the 3x-scale test now checks for ellipsis at 320px instead of a fixed height. The password fields get `autofillHints: [AutofillHints.password]`, which renders `autocomplete="current-password"` once focused. Unfocused and disabled password fields still render `autocomplete="off"`, because autofill hints are not in the semantics tree. That needs an engine change. ## Demo Run the test app on web and inspect the text inputs on #/text-field and #/text-field-password, before and after focusing them.
1 parent 8d1c565 commit 0563606

4 files changed

Lines changed: 67 additions & 33 deletions

File tree

‎packages/material_ui/test_apps/a11y_assessments/lib/use_cases/text_field.dart‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@ class _MainWidget extends StatelessWidget {
3333
appBar: AppBar(title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo'))),
3434
body: ListView(
3535
children: <Widget>[
36-
Semantics(
37-
label: 'Input field with suffix @gmail.com',
38-
child: const TextField(
39-
key: Key('enabled text field'),
40-
maxLines: null,
41-
decoration: InputDecoration(
42-
labelText: 'Email',
43-
suffixText: '@gmail.com',
44-
hintText: 'Enter your email',
45-
),
36+
const TextField(
37+
key: Key('enabled text field'),
38+
keyboardType: TextInputType.emailAddress,
39+
autofillHints: <String>[AutofillHints.email],
40+
decoration: InputDecoration(
41+
labelText: 'Email',
42+
hintText: 'Enter your email',
43+
// The field is single-line, so let the hint wrap at large
44+
// text scales, one line per word.
45+
hintMaxLines: 3,
4646
),
4747
),
48-
Semantics(
49-
label: 'Input field with suffix @gmail.com',
50-
child: TextField(
51-
key: const Key('disabled text field'),
52-
maxLines: null,
53-
decoration: const InputDecoration(
54-
labelText: 'Email',
55-
suffixText: '@gmail.com',
56-
hintText: 'Enter your email',
57-
),
58-
enabled: false,
59-
controller: TextEditingController(text: 'xyz'),
48+
TextField(
49+
key: const Key('disabled text field'),
50+
keyboardType: TextInputType.emailAddress,
51+
autofillHints: const <String>[AutofillHints.email],
52+
decoration: const InputDecoration(
53+
labelText: 'Email',
54+
hintText: 'Enter your email',
55+
// The field is single-line, so let the hint wrap at large
56+
// text scales, one line per word.
57+
hintMaxLines: 3,
6058
),
59+
enabled: false,
60+
controller: TextEditingController(text: 'xyz'),
6161
),
6262
],
6363
),

‎packages/material_ui/test_apps/a11y_assessments/lib/use_cases/text_field_password.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ class _MainWidget extends StatelessWidget {
3535
children: const <Widget>[
3636
TextField(
3737
key: Key('enabled password'),
38+
autofillHints: <String>[AutofillHints.password],
3839
decoration: InputDecoration(labelText: 'Password'),
3940
obscureText: true,
4041
),
4142
TextField(
4243
key: Key('disabled password'),
44+
autofillHints: <String>[AutofillHints.password],
4345
decoration: InputDecoration(labelText: 'Password'),
4446
enabled: false,
4547
obscureText: true,

‎packages/material_ui/test_apps/a11y_assessments/test/text_field_password_test.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,15 @@ void main() {
5656
await tester.pumpAndSettle();
5757
expect(findHeadingLevelOnes, findsOne);
5858
});
59+
60+
testWidgets('password fields expose their input purpose', (WidgetTester tester) async {
61+
await pumpsUseCase(tester, TextFieldPasswordUseCase());
62+
63+
for (final key in <Key>[const Key('enabled password'), const Key('disabled password')]) {
64+
final EditableText editableText = tester.widget<EditableText>(
65+
find.descendant(of: find.byKey(key), matching: find.byType(EditableText)),
66+
);
67+
expect(editableText.autofillHints, contains(AutofillHints.password));
68+
}
69+
});
5970
}

‎packages/material_ui/test_apps/a11y_assessments/test/text_field_test.dart‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:ui' show SemanticsInputType;
6+
57
import 'package:a11y_assessments/use_cases/text_field.dart';
8+
import 'package:flutter/rendering.dart' show RenderParagraph;
69
import 'package:flutter_test/flutter_test.dart';
710
import 'package:material_ui/material_ui.dart';
811

@@ -32,6 +35,11 @@ void main() {
3235
});
3336

3437
testWidgets('font size increase does not ellipsize hint text', (WidgetTester tester) async {
38+
// A narrow view forces the hint to wrap at 3x text scale.
39+
tester.view.physicalSize = const Size(320, 640);
40+
tester.view.devicePixelRatio = 1.0;
41+
addTearDown(tester.view.reset);
42+
3543
await pumpsUseCase(tester, TextFieldUseCase());
3644
await tester.pumpWidget(
3745
MaterialApp(
@@ -51,24 +59,37 @@ void main() {
5159
final Finder finder = find.byKey(const Key('enabled text field'));
5260
await tester.tap(finder);
5361
await tester.pumpAndSettle();
54-
final Size size = tester.getSize(finder);
55-
// Should have a multi-line height.
56-
expect(size.height, 280);
62+
final RenderParagraph hint = tester.renderObject<RenderParagraph>(
63+
find.descendant(of: finder, matching: find.text('Enter your email')),
64+
);
65+
expect(hint.didExceedMaxLines, isFalse);
5766
}
5867
});
5968

60-
testWidgets('text field wrapper exists', (WidgetTester tester) async {
61-
await pumpsUseCase(tester, TextFieldUseCase());
62-
const textFieldLabel = 'Input field with suffix @gmail.com';
63-
64-
final Finder semanticsWidgets = find.bySemanticsLabel(RegExp(textFieldLabel));
65-
expect(semanticsWidgets, findsExactly(2));
66-
});
67-
6869
testWidgets('text field demo page has one h1 tag', (WidgetTester tester) async {
6970
await pumpsUseCase(tester, TextFieldUseCase());
7071
final Finder findHeadingLevelOnes = find.bySemanticsLabel('TextField Demo');
7172
await tester.pumpAndSettle();
7273
expect(findHeadingLevelOnes, findsOne);
7374
});
75+
76+
testWidgets('email text fields expose their input purpose', (WidgetTester tester) async {
77+
await pumpsUseCase(tester, TextFieldUseCase());
78+
await tester.pumpAndSettle();
79+
80+
for (final key in <Key>[const Key('enabled text field'), const Key('disabled text field')]) {
81+
final Finder finder = find.byKey(key);
82+
// On web, a single-line field with the email input type renders an
83+
// <input autocomplete="email">. A multiline field renders a <textarea>
84+
// with autocomplete="off".
85+
expect(
86+
tester.getSemantics(finder),
87+
containsSemantics(inputType: SemanticsInputType.email, isMultiline: false),
88+
);
89+
final EditableText editableText = tester.widget<EditableText>(
90+
find.descendant(of: finder, matching: find.byType(EditableText)),
91+
);
92+
expect(editableText.autofillHints, contains(AutofillHints.email));
93+
}
94+
});
7495
}

0 commit comments

Comments
 (0)