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/services.dart';
7import 'package:flutter_api_samples/material/menu_anchor/radio_menu_button.0.dart' as example;
8import 'package:flutter_test/flutter_test.dart';
9
10void main() {
11 testWidgets('Can open menu', (WidgetTester tester) async {
12 await tester.pumpWidget(
13 const example.MenuApp(),
14 );
15
16 await tester.tap(find.byType(TextButton));
17 await tester.pump();
18 await tester.pump();
19
20 expect(find.text('Red Background'), findsOneWidget);
21 expect(find.text('Green Background'), findsOneWidget);
22 expect(find.text('Blue Background'), findsOneWidget);
23 expect(find.byType(Radio<Color>), findsNWidgets(3));
24 expect(tester.widget<Container>(find.byType(Container)).color, equals(Colors.red));
25
26 await tester.tap(find.text('Green Background'));
27 await tester.pumpAndSettle();
28
29 expect(tester.widget<Container>(find.byType(Container)).color, equals(Colors.green));
30 });
31
32 testWidgets('Shortcuts work', (WidgetTester tester) async {
33 await tester.pumpWidget(
34 const example.MenuApp(),
35 );
36
37 // Open the menu so we can watch state changes resulting from the shortcuts
38 // firing.
39 await tester.tap(find.byType(TextButton));
40 await tester.pump();
41
42 expect(find.text('Red Background'), findsOneWidget);
43 expect(find.text('Green Background'), findsOneWidget);
44 expect(find.text('Blue Background'), findsOneWidget);
45 expect(find.byType(Radio<Color>), findsNWidgets(3));
46 expect(tester.widget<Container>(find.byType(Container)).color, equals(Colors.red));
47
48 await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
49 await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
50 await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
51 await tester.pump();
52 // Need to pump twice because of the one frame delay in the notification to
53 // update the overlay entry.
54 await tester.pump();
55
56 expect(tester.widget<Radio<Color>>(find.descendant(of: find.byType(RadioMenuButton<Color>).at(0), matching: find.byType(Radio<Color>))).groupValue, equals(Colors.green));
57 expect(tester.widget<Container>(find.byType(Container)).color, equals(Colors.green));
58
59 await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
60 await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
61 await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
62 await tester.pump();
63 await tester.pump();
64
65 expect(tester.widget<Radio<Color>>(find.descendant(of: find.byType(RadioMenuButton<Color>).at(1), matching: find.byType(Radio<Color>))).groupValue, equals(Colors.red));
66 expect(tester.widget<Container>(find.byType(Container)).color, equals(Colors.red));
67
68 await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
69 await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
70 await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
71 await tester.pump();
72 await tester.pump();
73
74 expect(tester.widget<Radio<Color>>(find.descendant(of: find.byType(RadioMenuButton<Color>).at(2), matching: find.byType(Radio<Color>))).groupValue, equals(Colors.blue));
75 expect(tester.widget<Container>(find.byType(Container)).color, equals(Colors.blue));
76 });
77
78 testWidgets('MenuAnchor is wrapped in a SafeArea', (WidgetTester tester) async {
79 const double safeAreaPadding = 100.0;
80 await tester.pumpWidget(
81 const MediaQuery(
82 data: MediaQueryData(
83 padding: EdgeInsets.symmetric(vertical: safeAreaPadding),
84 ),
85 child: example.MenuApp(),
86 ),
87 );
88
89 expect(tester.getTopLeft(find.byType(MenuAnchor)), const Offset(0.0, safeAreaPadding));
90 });
91}
92

Provided by KDAB

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