text button case rework (#154943)

Rework on the text button use case to pass [b/347102786](https://b.corp.google.com/347102786), 

After talking with the tester, it seems like there isn't an issue with the A11y of the text button, rather just how the test case is set up. In order for the text case to pass, there needs to be real time feedback of an action that is done by pressing the text button (think of a dialog popup, or form submission notification). 

So I rewrote the test case to mimic a simple form with a submit button that once submitted, will let the user know it is submitted with a snack bar notification. https://screencast.googleplex.com/cast/NTM0ODc1NDIxMDE2MDY0MHwzYWI4MTZhMS1hMA
This commit is contained in:
Denis Bowen 2024-09-13 14:11:08 -05:00 committed by GitHub
parent 07647cae9f
commit 47aed96ca1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 39 deletions

View File

@ -25,9 +25,8 @@ class MainWidget extends StatefulWidget {
}
class MainWidgetState extends State<MainWidget> {
int _count = 0;
String pageTitle = getUseCaseName(TextButtonUseCase());
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
@ -36,35 +35,34 @@ class MainWidgetState extends State<MainWidget> {
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo')),
),
body: Center(
body: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
MergeSemantics(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('This is a TextButton:'),
TextButton(
onPressed: () {
setState(() { _count++; });
},
child: const Text('Action'),
),
Text('Clicked $_count time(s).'),
],
),
TextFormField(
// The validator receives the text that the user has entered.
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
const MergeSemantics(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('This is a disabled TextButton:'),
TextButton(
onPressed: null,
child: Text('Action Disabled'),
),
],
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
// If the form is valid, display a snackbar. In the real world,
// this might also send a request to a server.
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Form submitted')),
);
}
},
child: const Text('Submit'),
),
),
],

View File

@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:a11y_assessments/use_cases/text_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'test_utils.dart';
@ -10,22 +11,21 @@ import 'test_utils.dart';
void main() {
testWidgets('text button can run', (WidgetTester tester) async {
await pumpsUseCase(tester, TextButtonUseCase());
expect(find.text('Action'), findsOneWidget);
expect(find.text('Action Disabled'), findsOneWidget);
expect(find.text('Submit'), findsOneWidget);
});
testWidgets('text button increments correctly when clicked',
(WidgetTester tester) async {
testWidgets('submit causes snackbar to show', (WidgetTester tester) async {
await pumpsUseCase(tester, TextButtonUseCase());
final Finder textFormField = find.byType(TextFormField);
final Finder submitButton = find.text('Submit');
expect(find.text('Action'), findsOneWidget);
await tester.tap(find.text('Action'));
await tester.pumpAndSettle();
expect(find.text('Clicked 1 time(s).'), findsOneWidget);
// Enter text in field and submit.
await tester.enterText(textFormField, 'test text');
await tester.tap(submitButton);
await tester.pump();
await tester.tap(find.text('Action'));
await tester.pumpAndSettle();
expect(find.text('Clicked 2 time(s).'), findsOneWidget);
// Verify that the snackbar is visible.
expect(find.text('Form submitted'), findsOneWidget);
});
testWidgets('text button demo page has one h1 tag', (WidgetTester tester) async {