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
5import 'package:flutter/material.dart';
6import 'package:flutter_api_samples/material/dropdown_menu/dropdown_menu.0.dart' as example;
7import 'package:flutter_test/flutter_test.dart';
8
9void main() {
10 testWidgets('DropdownMenu', (WidgetTester tester) async {
11 await tester.pumpWidget(const example.DropdownMenuExample());
12
13 expect(find.text('You selected a Blue Smile'), findsNothing);
14
15 final Finder colorMenu = find.byType(DropdownMenu<example.ColorLabel>);
16 final Finder iconMenu = find.byType(DropdownMenu<example.IconLabel>);
17 expect(colorMenu, findsOneWidget);
18 expect(iconMenu, findsOneWidget);
19
20 Finder findMenuItem(String label) {
21 return find.widgetWithText(MenuItemButton, label).last;
22 }
23
24 await tester.tap(colorMenu);
25 await tester.pumpAndSettle();
26 expect(findMenuItem('Blue'), findsOneWidget);
27 expect(findMenuItem('Pink'), findsOneWidget);
28 expect(findMenuItem('Green'), findsOneWidget);
29 expect(findMenuItem('Orange'), findsOneWidget);
30 expect(findMenuItem('Grey'), findsOneWidget);
31
32 await tester.tap(findMenuItem('Blue'));
33
34 // The DropdownMenu's onSelected callback is delayed
35 // with SchedulerBinding.instance.addPostFrameCallback
36 // to give the focus a chance to return to where it was
37 // before the menu appeared. The pumpAndSettle()
38 // give the callback a chance to run.
39 await tester.pumpAndSettle();
40
41 await tester.tap(iconMenu);
42 await tester.pumpAndSettle();
43 expect(findMenuItem('Smile'), findsOneWidget);
44 expect(findMenuItem('Cloud'), findsOneWidget);
45 expect(findMenuItem('Brush'), findsOneWidget);
46 expect(findMenuItem('Heart'), findsOneWidget);
47
48 await tester.tap(findMenuItem('Smile'));
49 await tester.pumpAndSettle();
50
51 expect(find.text('You selected a Blue Smile'), findsOneWidget);
52 });
53
54 testWidgets('DropdownMenu has focus when tapping on the text field', (WidgetTester tester) async {
55 await tester.pumpWidget(const example.DropdownMenuExample());
56
57 // Make sure the dropdown menus are there.
58 final Finder colorMenu = find.byType(DropdownMenu<example.ColorLabel>);
59 final Finder iconMenu = find.byType(DropdownMenu<example.IconLabel>);
60 expect(colorMenu, findsOneWidget);
61 expect(iconMenu, findsOneWidget);
62
63 // Tap on the color menu and make sure it is focused.
64 await tester.tap(colorMenu);
65 await tester.pumpAndSettle();
66 expect(FocusScope.of(tester.element(colorMenu)).hasFocus, isTrue);
67
68 // Tap on the icon menu and make sure it is focused.
69 await tester.tap(iconMenu);
70 await tester.pumpAndSettle();
71 expect(FocusScope.of(tester.element(iconMenu)).hasFocus, isTrue);
72 });
73
74 testWidgets('DropdownMenu on small screen', (WidgetTester tester) async {
75 await tester.pumpWidget(const example.DropdownMenuExample());
76
77 final Finder colorMenu = find.byType(DropdownMenu<example.ColorLabel>);
78 final Finder iconMenu = find.byType(DropdownMenu<example.IconLabel>);
79 expect(colorMenu, findsOneWidget);
80 expect(iconMenu, findsOneWidget);
81
82 Finder findMenuItem(String label) {
83 return find.widgetWithText(MenuItemButton, label).last;
84 }
85
86 await tester.tap(colorMenu);
87 await tester.pumpAndSettle();
88 final Finder menuBlue = findMenuItem('Blue');
89 await tester.ensureVisible(menuBlue);
90 await tester.tap(menuBlue);
91 await tester.pumpAndSettle();
92
93 await tester.tap(iconMenu);
94 await tester.pumpAndSettle();
95 final Finder menuSmile = findMenuItem('Smile');
96 await tester.ensureVisible(menuSmile);
97 await tester.tap(menuSmile);
98 await tester.pumpAndSettle();
99
100 expect(find.text('You selected a Blue Smile'), findsOneWidget);
101
102 // Resize the screen to small screen and make sure no overflowed error appears.
103 tester.binding.window.physicalSizeTestValue = const Size(200, 160);
104 tester.binding.window.devicePixelRatioTestValue = 1.0;
105 addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
106 await tester.pump();
107
108 expect(tester.takeException(), isNull);
109 });
110}
111

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com