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/foundation.dart';
6import 'package:flutter/material.dart';
7import 'package:flutter_api_samples/material/segmented_button/segmented_button.0.dart' as example;
8import 'package:flutter_test/flutter_test.dart';
9
10void main() {
11 testWidgets('Segmented button can be used with a single selection', (WidgetTester tester) async {
12 await tester.pumpWidget(const example.SegmentedButtonApp());
13
14 void expectOneCalendarButton(example.Calendar period) {
15 expect(
16 find.byWidgetPredicate(
17 (Widget widget) =>
18 widget is SegmentedButton<example.Calendar> &&
19 setEquals(widget.selected, <example.Calendar>{period}),
20 ),
21 findsOne,
22 );
23 }
24
25 expect(find.text('Single choice'), findsOne);
26 expect(find.text('Day'), findsOne);
27 expect(find.text('Week'), findsOne);
28 expect(find.text('Month'), findsOne);
29 expect(find.text('Year'), findsOne);
30
31 expectOneCalendarButton(example.Calendar.day);
32
33 // Select the day.
34 await tester.tap(find.text('Week'));
35 await tester.pump();
36
37 expectOneCalendarButton(example.Calendar.week);
38
39 // Select the month.
40 await tester.tap(find.text('Month'));
41 await tester.pump();
42
43 expectOneCalendarButton(example.Calendar.month);
44
45 // Select the year.
46 await tester.tap(find.text('Year'));
47 await tester.pump();
48
49 expectOneCalendarButton(example.Calendar.year);
50
51 // Select the day.
52 await tester.tap(find.text('Day'));
53 await tester.pump();
54
55 expectOneCalendarButton(example.Calendar.day);
56
57 // Try to unselect the day.
58 await tester.tap(find.text('Day'));
59 await tester.pump();
60
61 expectOneCalendarButton(example.Calendar.day);
62 });
63
64 testWidgets('Segmented button can be used with a multiple selection', (
65 WidgetTester tester,
66 ) async {
67 await tester.pumpWidget(const example.SegmentedButtonApp());
68
69 void expectSizeButtons(Set<example.Sizes> sizes) {
70 expect(
71 find.byWidgetPredicate(
72 (Widget widget) =>
73 widget is SegmentedButton<example.Sizes> && setEquals(widget.selected, sizes),
74 ),
75 findsOne,
76 );
77 }
78
79 expect(find.text('Multiple choice'), findsOne);
80 expect(find.text('XS'), findsOne);
81 expect(find.text('S'), findsOne);
82 expect(find.text('M'), findsOne);
83 expect(find.text('L'), findsOne);
84 expect(find.text('XL'), findsOne);
85
86 expectSizeButtons(const <example.Sizes>{example.Sizes.large, example.Sizes.extraLarge});
87
88 // Select everything.
89 await tester.tap(find.text('XS'));
90 await tester.pump();
91 await tester.tap(find.text('S'));
92 await tester.pump();
93 await tester.tap(find.text('M'));
94 await tester.pump();
95
96 expectSizeButtons(example.Sizes.values.toSet());
97
98 // Unselect everything but XS.
99 await tester.tap(find.text('S'));
100 await tester.pump();
101 await tester.tap(find.text('M'));
102 await tester.pump();
103 await tester.tap(find.text('L'));
104 await tester.pump();
105 await tester.tap(find.text('XL'));
106 await tester.pump();
107
108 expectSizeButtons(const <example.Sizes>{example.Sizes.extraSmall});
109 });
110}
111