1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'package:flutter/material.dart'; |
6 | import 'package:flutter_api_samples/material/dropdown_menu/dropdown_menu.0.dart' as example; |
7 | import 'package:flutter_test/flutter_test.dart'; |
8 | |
9 | void main() { |
10 | testWidgets('DropdownMenu' , (WidgetTester tester) async { |
11 | await tester.pumpWidget( |
12 | const example.DropdownMenuExample(), |
13 | ); |
14 | |
15 | expect(find.text('You selected a Blue Smile' ), findsNothing); |
16 | |
17 | final Finder colorMenu = find.byType(DropdownMenu<example.ColorLabel>); |
18 | final Finder iconMenu = find.byType(DropdownMenu<example.IconLabel>); |
19 | expect(colorMenu, findsOneWidget); |
20 | expect(iconMenu, findsOneWidget); |
21 | |
22 | Finder findMenuItem(String label) { |
23 | return find.widgetWithText(MenuItemButton, label).last; |
24 | } |
25 | |
26 | await tester.tap(colorMenu); |
27 | await tester.pumpAndSettle(); |
28 | expect(findMenuItem('Blue' ), findsOneWidget); |
29 | expect(findMenuItem('Pink' ), findsOneWidget); |
30 | expect(findMenuItem('Green' ), findsOneWidget); |
31 | expect(findMenuItem('Orange' ), findsOneWidget); |
32 | expect(findMenuItem('Grey' ), findsOneWidget); |
33 | |
34 | await tester.tap(findMenuItem('Blue' )); |
35 | |
36 | // The DropdownMenu's onSelected callback is delayed |
37 | // with SchedulerBinding.instance.addPostFrameCallback |
38 | // to give the focus a chance to return to where it was |
39 | // before the menu appeared. The pumpAndSettle() |
40 | // give the callback a chance to run. |
41 | await tester.pumpAndSettle(); |
42 | |
43 | await tester.tap(iconMenu); |
44 | await tester.pumpAndSettle(); |
45 | expect(findMenuItem('Smile' ), findsOneWidget); |
46 | expect(findMenuItem('Cloud' ), findsOneWidget); |
47 | expect(findMenuItem('Brush' ), findsOneWidget); |
48 | expect(findMenuItem('Heart' ), findsOneWidget); |
49 | |
50 | await tester.tap(findMenuItem('Smile' )); |
51 | await tester.pumpAndSettle(); |
52 | |
53 | expect(find.text('You selected a Blue Smile' ), findsOneWidget); |
54 | }); |
55 | |
56 | testWidgets('DropdownMenu has focus when tapping on the text field' , (WidgetTester tester) async { |
57 | await tester.pumpWidget( |
58 | const example.DropdownMenuExample(), |
59 | ); |
60 | |
61 | // Make sure the dropdown menus are there. |
62 | final Finder colorMenu = find.byType(DropdownMenu<example.ColorLabel>); |
63 | final Finder iconMenu = find.byType(DropdownMenu<example.IconLabel>); |
64 | expect(colorMenu, findsOneWidget); |
65 | expect(iconMenu, findsOneWidget); |
66 | |
67 | // Tap on the color menu and make sure it is focused. |
68 | await tester.tap(colorMenu); |
69 | await tester.pumpAndSettle(); |
70 | expect(FocusScope.of(tester.element(colorMenu)).hasFocus, isTrue); |
71 | |
72 | // Tap on the icon menu and make sure it is focused. |
73 | await tester.tap(iconMenu); |
74 | await tester.pumpAndSettle(); |
75 | expect(FocusScope.of(tester.element(iconMenu)).hasFocus, isTrue); |
76 | }); |
77 | } |
78 | |