|
| 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/checkbox/checkbox.1.dart' |
| 7 | + as example; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + testWidgets('Checkbox can be checked', (WidgetTester tester) async { |
| 12 | + await tester.pumpWidget( |
| 13 | + const example.CheckboxExampleApp(), |
| 14 | + ); |
| 15 | + |
| 16 | + expect(find.byType(Checkbox), findsNWidgets(3)); |
| 17 | + Checkbox checkbox = tester.widget(find.byType(Checkbox).first); |
| 18 | + Checkbox checkboxWithError = tester.widget(find.byType(Checkbox).at(1)); |
| 19 | + Checkbox checkboxDisabled = tester.widget(find.byType(Checkbox).last); |
| 20 | + |
| 21 | + // Verify the initial state of the checkboxes. |
| 22 | + expect(checkbox.value, isTrue); |
| 23 | + expect(checkboxWithError.value, isTrue); |
| 24 | + expect(checkboxDisabled.value, isTrue); |
| 25 | + |
| 26 | + expect(checkboxWithError.isError, isTrue); |
| 27 | + expect(checkboxDisabled.onChanged, null); |
| 28 | + |
| 29 | + expect(checkbox.tristate, isTrue); |
| 30 | + expect(checkboxWithError.tristate, isTrue); |
| 31 | + expect(checkboxDisabled.tristate, isTrue); |
| 32 | + |
| 33 | + // Tap the first Checkbox and verify the state change. |
| 34 | + await tester.tap(find.byType(Checkbox).first); |
| 35 | + await tester.pump(); |
| 36 | + checkbox = tester.widget(find.byType(Checkbox).first); |
| 37 | + checkboxWithError = tester.widget(find.byType(Checkbox).at(1)); |
| 38 | + checkboxDisabled = tester.widget(find.byType(Checkbox).last); |
| 39 | + |
| 40 | + expect(checkbox.value, isNull); |
| 41 | + expect(checkboxWithError.value, isNull); |
| 42 | + expect(checkboxDisabled.value, isNull); |
| 43 | + |
| 44 | + // Tap the second Checkbox and verify the state change. |
| 45 | + await tester.tap(find.byType(Checkbox).at(1)); |
| 46 | + await tester.pump(); |
| 47 | + |
| 48 | + checkbox = tester.widget(find.byType(Checkbox).first); |
| 49 | + checkboxWithError = tester.widget(find.byType(Checkbox).at(1)); |
| 50 | + checkboxDisabled = tester.widget(find.byType(Checkbox).last); |
| 51 | + |
| 52 | + expect(checkbox.value, isFalse); |
| 53 | + expect(checkboxWithError.value, isFalse); |
| 54 | + expect(checkboxDisabled.value, isFalse); |
| 55 | + |
| 56 | + // Tap the third Checkbox and verify that should remain unchanged. |
| 57 | + await tester.tap(find.byType(Checkbox).last); |
| 58 | + await tester.pump(); |
| 59 | + |
| 60 | + checkbox = tester.widget(find.byType(Checkbox).first); |
| 61 | + checkboxWithError = tester.widget(find.byType(Checkbox).at(1)); |
| 62 | + checkboxDisabled = tester.widget(find.byType(Checkbox).last); |
| 63 | + |
| 64 | + expect(checkbox.value, isFalse); |
| 65 | + expect(checkboxWithError.value, isFalse); |
| 66 | + expect(checkboxDisabled.value, isFalse); |
| 67 | + }); |
| 68 | +} |
0 commit comments