From 47aed96ca12b32076f1c52ca533a875570a1bb83 Mon Sep 17 00:00:00 2001 From: Denis Bowen <42016383+DBowen33@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:11:08 -0500 Subject: [PATCH] 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 --- .../lib/use_cases/text_button.dart | 54 +++++++++---------- .../test/text_button_test.dart | 22 ++++---- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/dev/a11y_assessments/lib/use_cases/text_button.dart b/dev/a11y_assessments/lib/use_cases/text_button.dart index e39552af719..852a68f4dcc 100644 --- a/dev/a11y_assessments/lib/use_cases/text_button.dart +++ b/dev/a11y_assessments/lib/use_cases/text_button.dart @@ -25,9 +25,8 @@ class MainWidget extends StatefulWidget { } class MainWidgetState extends State { - int _count = 0; - String pageTitle = getUseCaseName(TextButtonUseCase()); + final GlobalKey _formKey = GlobalKey(); @override Widget build(BuildContext context) { @@ -36,35 +35,34 @@ class MainWidgetState extends State { 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: [ - MergeSemantics( - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - 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: [ - 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'), ), ), ], diff --git a/dev/a11y_assessments/test/text_button_test.dart b/dev/a11y_assessments/test/text_button_test.dart index e821a51ff33..4851a6adc04 100644 --- a/dev/a11y_assessments/test/text_button_test.dart +++ b/dev/a11y_assessments/test/text_button_test.dart @@ -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 {