|
| 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/icon_button/icon_button.3.dart' as example; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + testWidgets('It should select and unselect the icon buttons', (WidgetTester tester) async { |
| 11 | + await tester.pumpWidget( |
| 12 | + const example.IconButtonToggleApp(), |
| 13 | + ); |
| 14 | + |
| 15 | + expect(find.widgetWithIcon(IconButton, Icons.settings_outlined), findsExactly(8)); |
| 16 | + final Finder unselectedIconButtons = find.widgetWithIcon(IconButton, Icons.settings_outlined); |
| 17 | + for (int i = 0; i <= 6; i++) { |
| 18 | + final IconButton button = tester.widget<IconButton>(unselectedIconButtons.at(i)); |
| 19 | + expect(button.onPressed, i.isEven ? isA<VoidCallback>() : isNull); |
| 20 | + expect(button.isSelected, isFalse); |
| 21 | + } |
| 22 | + |
| 23 | + // Select the icons buttons. |
| 24 | + for (int i = 0; i <= 3; i++) { |
| 25 | + await tester.tap(unselectedIconButtons.at(2 * i)); |
| 26 | + } |
| 27 | + await tester.pump(); |
| 28 | + |
| 29 | + expect(find.widgetWithIcon(IconButton, Icons.settings), findsExactly(8)); |
| 30 | + final Finder selectedIconButtons = find.widgetWithIcon(IconButton, Icons.settings); |
| 31 | + for (int i = 0; i <= 6; i++) { |
| 32 | + final IconButton button = tester.widget<IconButton>(selectedIconButtons.at(i)); |
| 33 | + expect(button.onPressed, i.isEven ? isA<VoidCallback>() : isNull); |
| 34 | + expect(button.isSelected, isTrue); |
| 35 | + } |
| 36 | + |
| 37 | + // Unselect the icons buttons. |
| 38 | + for (int i = 0; i <= 3; i++) { |
| 39 | + await tester.tap(selectedIconButtons.at(2 * i)); |
| 40 | + } |
| 41 | + await tester.pump(); |
| 42 | + |
| 43 | + expect(find.widgetWithIcon(IconButton, Icons.settings_outlined), findsExactly(8)); |
| 44 | + for (int i = 0; i <= 6; i++) { |
| 45 | + final IconButton button = tester.widget<IconButton>(unselectedIconButtons.at(i)); |
| 46 | + expect(button.onPressed, i.isEven ? isA<VoidCallback>() : isNull); |
| 47 | + expect(button.isSelected, isFalse); |
| 48 | + } |
| 49 | + }); |
| 50 | +} |
0 commit comments