From abad37204e4a2cb56ea911ad18cde5dd978ec801 Mon Sep 17 00:00:00 2001 From: derdilla <82763757+NobodyForNothing@users.noreply.github.com> Date: Thu, 23 May 2024 10:52:13 +0200 Subject: [PATCH] Test raw autocomplete api examples (#148234) Part of #130459. --- dev/bots/check_code_samples.dart | 4 - .../autocomplete/raw_autocomplete.0_test.dart | 47 ++++++++++ .../autocomplete/raw_autocomplete.1_test.dart | 76 ++++++++++++++++ .../autocomplete/raw_autocomplete.2_test.dart | 86 +++++++++++++++++++ .../raw_autocomplete.focus_node.0_test.dart | 66 ++++++++++++++ 5 files changed, 275 insertions(+), 4 deletions(-) create mode 100644 examples/api/test/widgets/autocomplete/raw_autocomplete.0_test.dart create mode 100644 examples/api/test/widgets/autocomplete/raw_autocomplete.1_test.dart create mode 100644 examples/api/test/widgets/autocomplete/raw_autocomplete.2_test.dart create mode 100644 examples/api/test/widgets/autocomplete/raw_autocomplete.focus_node.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 1abbbf6e72..deb9d7e009 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -364,10 +364,6 @@ final Set _knownMissingTests = { 'examples/api/test/rendering/sliver_grid/sliver_grid_delegate_with_fixed_cross_axis_count.1_test.dart', 'examples/api/test/rendering/scroll_direction/scroll_direction.0_test.dart', 'examples/api/test/painting/star_border/star_border.0_test.dart', - 'examples/api/test/widgets/autocomplete/raw_autocomplete.focus_node.0_test.dart', - 'examples/api/test/widgets/autocomplete/raw_autocomplete.2_test.dart', - 'examples/api/test/widgets/autocomplete/raw_autocomplete.1_test.dart', - 'examples/api/test/widgets/autocomplete/raw_autocomplete.0_test.dart', 'examples/api/test/widgets/navigator/navigator.restorable_push_and_remove_until.0_test.dart', 'examples/api/test/widgets/navigator/navigator.0_test.dart', 'examples/api/test/widgets/navigator/navigator.restorable_push.0_test.dart', diff --git a/examples/api/test/widgets/autocomplete/raw_autocomplete.0_test.dart b/examples/api/test/widgets/autocomplete/raw_autocomplete.0_test.dart new file mode 100644 index 0000000000..80bae5d870 --- /dev/null +++ b/examples/api/test/widgets/autocomplete/raw_autocomplete.0_test.dart @@ -0,0 +1,47 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/widgets/autocomplete/raw_autocomplete.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Autocomplete example is visible', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + expect(find.text('RawAutocomplete Basic'), findsOneWidget); + expect(find.byType(TextFormField), findsOneWidget); + + expect(find.text('aardvark'), findsNothing); + expect(find.text('bobcat'), findsNothing); + expect(find.text('chameleon'), findsNothing); + }); + + testWidgets('Options are shown correctly and selectable', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + await tester.tap(find.byType(TextFormField)); + await tester.pump(); + + expect(find.byType(ListTile), findsNWidgets(3)); + expect(find.text('aardvark'), findsOneWidget); + expect(find.text('bobcat'), findsOneWidget); + expect(find.text('chameleon'), findsOneWidget); + + await tester.enterText(find.byType(TextFormField), 'b'); + await tester.pump(); + + expect(find.byType(ListTile), findsOneWidget); + expect(find.text('aardvark'), findsNothing); + expect(find.text('bobcat'), findsOneWidget); + expect(find.text('chameleon'), findsNothing); + + await tester.tap(find.text('bobcat')); + await tester.pump(); + + expect(find.byType(ListTile), findsNothing); + expect(find.descendant( + of: find.byType(TextFormField), + matching: find.text('bobcat'), + ), findsOneWidget); + }); +} diff --git a/examples/api/test/widgets/autocomplete/raw_autocomplete.1_test.dart b/examples/api/test/widgets/autocomplete/raw_autocomplete.1_test.dart new file mode 100644 index 0000000000..b625c64e23 --- /dev/null +++ b/examples/api/test/widgets/autocomplete/raw_autocomplete.1_test.dart @@ -0,0 +1,76 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/widgets/autocomplete/raw_autocomplete.1.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Autocomplete example is visible', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + expect(find.text('RawAutocomplete Custom Type'), findsOneWidget); + expect(find.byType(TextFormField), findsOneWidget); + + expect(find.text('Alice'), findsNothing); + expect(find.text('Bob'), findsNothing); + expect(find.text('Charlie'), findsNothing); + }); + + testWidgets('Options are shown correctly and selectable', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + await tester.tap(find.byType(TextFormField)); + await tester.pump(); + + expect(find.byType(ListTile), findsNWidgets(3)); + expect(find.text('Alice'), findsOneWidget); + expect(find.text('Bob'), findsOneWidget); + expect(find.text('Charlie'), findsOneWidget); + + await tester.enterText(find.byType(TextFormField), 'b'); + await tester.pump(); + + expect(find.byType(ListTile), findsOneWidget); + expect(find.text('Alice'), findsNothing); + expect(find.text('Bob'), findsOneWidget); + expect(find.text('Charlie'), findsNothing); + + await tester.tap(find.text('Bob')); + await tester.pump(); + + expect(find.byType(ListTile), findsNothing); + expect(find.descendant( + of: find.byType(TextFormField), + matching: find.text('Bob'), + ), findsOneWidget); + }); + + testWidgets('Finds users by email address', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + + await tester.enterText(find.byType(TextFormField), '@'); + await tester.pump(); + + expect(find.byType(ListTile), findsNWidgets(3)); + expect(find.text('Alice'), findsOneWidget); + expect(find.text('Bob'), findsOneWidget); + expect(find.text('Charlie'), findsOneWidget); + + await tester.enterText(find.byType(TextFormField), '@gmail'); + await tester.pump(); + + expect(find.byType(ListTile), findsOneWidget); + expect(find.text('Alice'), findsNothing); + expect(find.text('Bob'), findsNothing); + expect(find.text('Charlie'), findsOneWidget); + + await tester.tap(find.text('Charlie')); + await tester.pump(); + + expect(find.byType(ListTile), findsNothing); + expect(find.descendant( + of: find.byType(TextFormField), + matching: find.text('Charlie'), + ), findsOneWidget); + }); +} diff --git a/examples/api/test/widgets/autocomplete/raw_autocomplete.2_test.dart b/examples/api/test/widgets/autocomplete/raw_autocomplete.2_test.dart new file mode 100644 index 0000000000..313ed73366 --- /dev/null +++ b/examples/api/test/widgets/autocomplete/raw_autocomplete.2_test.dart @@ -0,0 +1,86 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/widgets/autocomplete/raw_autocomplete.2.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Form is entirely visible and rejects invalid responses', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + expect(find.text('RawAutocomplete Form'), findsOneWidget); + expect(find.byType(TextFormField), findsNWidgets(2)); + expect(find.byIcon(Icons.arrow_downward), findsOneWidget); + expect(find.text('This is a regular DropdownButtonFormField'), findsOneWidget); + expect(find.text('This is a regular TextFormField'), findsOneWidget); + expect(find.text('This is a RawAutocomplete!'), findsOneWidget); + expect(find.text('Submit'), findsOneWidget); + + expect(find.text('One'), findsNothing); + expect(find.text('Two'), findsNothing); + expect(find.text('Free'), findsNothing); + expect(find.text('Four'), findsNothing); + expect(find.text('aardvark'), findsNothing); + expect(find.text('bobcat'), findsNothing); + expect(find.text('chameleon'), findsNothing); + + expect(find.text('Must make a selection.'), findsNothing); + expect(find.text("Can't be empty."), findsNothing); + expect(find.text('Nothing selected.'), findsNothing); + await tester.tap(find.text('Submit')); + await tester.pump(); + expect(find.text('Must make a selection.'), findsOneWidget); + expect(find.text("Can't be empty."), findsOneWidget); + expect(find.text('Nothing selected.'), findsOneWidget); + }); + + testWidgets('Form accepts valid inputs', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + await tester.tap(find.byIcon(Icons.arrow_downward)); + await tester.pump(); + + expect(find.text('One'), findsOneWidget); + expect(find.text('Two'), findsOneWidget); + expect(find.text('Free'), findsOneWidget); + expect(find.text('Four'), findsOneWidget); + await tester.tap(find.text('Free')); + await tester.pump(); + expect(find.text('Two'), findsNothing); + expect(find.text('Free'), findsOneWidget); + + expect(find.text('This is a regular TextFormField'), findsOneWidget); + await tester.enterText( + find.ancestor( + of: find.text('This is a regular TextFormField'), + matching: find.byType(TextFormField), + ), + 'regular user input', + ); + + await tester.tap(find.ancestor( + of: find.text('This is a RawAutocomplete!'), + matching: find.byType(TextFormField), + )); + await tester.pump(); + expect(find.text('aardvark'), findsOneWidget); + expect(find.text('bobcat'), findsOneWidget); + expect(find.text('chameleon'), findsOneWidget); + await tester.tap(find.text('aardvark')); + await tester.pump(); + + expect(find.byType(AlertDialog), findsNothing); + await tester.tap(find.text('Submit')); + await tester.pump(); + expect(find.byType(AlertDialog), findsOneWidget); + expect(find.text('Successfully submitted'), findsOneWidget); + expect(find.text('DropdownButtonFormField: "Free"'), findsOneWidget); + expect(find.text('TextFormField: "regular user input"'), findsOneWidget); + expect(find.text('RawAutocomplete: "aardvark"'), findsOneWidget); + expect(find.text('Ok'), findsOneWidget); + + await tester.tap(find.text('Ok')); + await tester.pump(); + expect(find.byType(AlertDialog), findsNothing); + }); +} diff --git a/examples/api/test/widgets/autocomplete/raw_autocomplete.focus_node.0_test.dart b/examples/api/test/widgets/autocomplete/raw_autocomplete.focus_node.0_test.dart new file mode 100644 index 0000000000..36c097a4b1 --- /dev/null +++ b/examples/api/test/widgets/autocomplete/raw_autocomplete.focus_node.0_test.dart @@ -0,0 +1,66 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/widgets/autocomplete/raw_autocomplete.focus_node.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Autocomplete example is visible', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + expect(find.text('Split RawAutocomplete App'), findsOneWidget); + expect(find.byType(AppBar), findsOneWidget); + expect(find.byType(TextFormField), findsOneWidget); + expect( + find.descendant( + of: find.byType(AppBar), + matching: find.byType(TextFormField) + ), + findsOneWidget, + ); + + expect(find.byType(RawAutocomplete), findsOneWidget); + expect(find.text('aardvark'), findsNothing); + expect(find.text('bobcat'), findsNothing); + expect(find.text('chameleon'), findsNothing); + expect( + find.ancestor( + matching: find.byType(AppBar), + of: find.byType(RawAutocomplete) + ), + findsNothing, + ); + }); + + testWidgets('Options are shown correctly and selectable', (WidgetTester tester) async { + await tester.pumpWidget(const example.AutocompleteExampleApp()); + await tester.tap(find.byType(TextFormField)); + await tester.pump(); + + expect(find.byType(ListTile), findsNWidgets(3)); + expect(find.text('aardvark'), findsOneWidget); + expect(find.text('bobcat'), findsOneWidget); + expect(find.text('chameleon'), findsOneWidget); + + await tester.enterText(find.byType(TextFormField), 'b'); + await tester.pump(); + + expect(find.byType(ListTile), findsOneWidget); + expect(find.text('aardvark'), findsNothing); + expect(find.text('bobcat'), findsOneWidget); + expect(find.text('chameleon'), findsNothing); + + await tester.tap(find.text('bobcat')); + await tester.pump(); + + expect(find.byType(ListTile), findsNothing); + expect( + find.descendant( + of: find.byType(TextFormField), + matching: find.text('bobcat'), + ), + findsOneWidget, + ); + }); +}