Skip to content

Commit d002ed3

Browse files
committed
feat: Adding cupertino tests. #1
1 parent bccc31a commit d002ed3

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

lib/cupertino.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import 'package:flutter/gestures.dart';
33
import 'package:flutter/material.dart';
44
import 'package:intl/intl.dart';
55

6+
final cupertinoDateButtonKey = UniqueKey();
7+
final cupertinoTimeButtonKey = UniqueKey();
8+
final cupertinoDateTimeButtonKey = UniqueKey();
9+
610
class WebScrollBehaviour extends MaterialScrollBehavior {
711
// Override behaviour methods and getters like dragDevices
812
@override
@@ -22,7 +26,7 @@ class CupertinoExamplePage extends StatefulWidget {
2226
}
2327

2428
class _CupertinoExamplePageState extends State<CupertinoExamplePage> {
25-
DateTime dateTime = DateTime.now();
29+
DateTime dateTime = DateTime.utc(2023, 7, 26);
2630

2731
// This function displays a CupertinoModalPopup with a reasonable fixed height
2832
// which hosts CupertinoDatePicker.
@@ -78,6 +82,7 @@ class _CupertinoExamplePageState extends State<CupertinoExamplePage> {
7882
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
7983
children: [
8084
ElevatedButton(
85+
key: cupertinoDateButtonKey,
8186
style: ElevatedButton.styleFrom(backgroundColor: Colors.purple.shade300),
8287
onPressed: () => _showDialog(
8388
CupertinoDatePicker(
@@ -97,6 +102,7 @@ class _CupertinoExamplePageState extends State<CupertinoExamplePage> {
97102
style: TextStyle(fontSize: 20),
98103
)),
99104
ElevatedButton(
105+
key: cupertinoTimeButtonKey,
100106
style: ElevatedButton.styleFrom(backgroundColor: Colors.purple.shade300),
101107
child: const Text(
102108
"Time",
@@ -121,6 +127,7 @@ class _CupertinoExamplePageState extends State<CupertinoExamplePage> {
121127
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
122128
children: [
123129
ElevatedButton(
130+
key: cupertinoDateTimeButtonKey,
124131
style: ElevatedButton.styleFrom(backgroundColor: Colors.green.shade400),
125132
onPressed: () => _showDialog(
126133
CupertinoDatePicker(

lib/material.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:flutter/material.dart';
22
import 'package:intl/intl.dart';
33

4-
final dateButtonKey = UniqueKey();
5-
final timeButtonKey = UniqueKey();
6-
final dateTimeButtonKey = UniqueKey();
4+
final materialDateButtonKey = UniqueKey();
5+
final materialTimeButtonKey = UniqueKey();
6+
final materialDateTimeButtonKey = UniqueKey();
77

88
/// Material example page.
99
/// Showcases the usage of `DatePicker` and `TimePicker` to change date and time.
@@ -73,7 +73,7 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
7373
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
7474
children: [
7575
ElevatedButton(
76-
key: dateButtonKey,
76+
key: materialDateButtonKey,
7777
style: ElevatedButton.styleFrom(backgroundColor: Colors.blue.shade300),
7878
child: const Text(
7979
"Date",
@@ -90,7 +90,7 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
9090
);
9191
}),
9292
ElevatedButton(
93-
key: timeButtonKey,
93+
key: materialTimeButtonKey,
9494
style: ElevatedButton.styleFrom(backgroundColor: Colors.blue.shade300),
9595
child: const Text(
9696
"Time",
@@ -115,7 +115,7 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
115115
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
116116
children: [
117117
ElevatedButton(
118-
key: dateTimeButtonKey,
118+
key: materialDateTimeButtonKey,
119119
style: ElevatedButton.styleFrom(backgroundColor: Colors.orange.shade400),
120120
onPressed: pickDateTime,
121121
child: const Text(

test/widget_test.dart

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// gestures. You can also use WidgetTester to find child widgets in the widget
66
// tree, read text, and verify that the values of widget properties are correct.
77

8+
import 'package:app/cupertino.dart';
89
import 'package:app/material.dart';
910
import 'package:flutter/material.dart';
1011
import 'package:flutter_test/flutter_test.dart';
@@ -35,7 +36,7 @@ void main() {
3536
expect(find.byKey(cupertinoPageKey), findsNothing);
3637

3738
// TEST DATE BUTTON ONLY -----------
38-
final dateButton = find.byKey(dateButtonKey);
39+
final dateButton = find.byKey(materialDateButtonKey);
3940

4041
// Tap on `Cupertino` key and page should change
4142
await tester.tap(dateButton);
@@ -49,7 +50,7 @@ void main() {
4950
expect(find.byKey(materialPageKey), findsOneWidget);
5051

5152
// TEST TIME BUTTON ONLY -----------
52-
final timeButton = find.byKey(timeButtonKey);
53+
final timeButton = find.byKey(materialTimeButtonKey);
5354

5455
// Tap on `Cupertino` key and page should change
5556
await tester.tap(timeButton);
@@ -63,7 +64,7 @@ void main() {
6364
expect(find.byKey(materialPageKey), findsOneWidget);
6465

6566
// TEST DATE AND TIME BUTTON -----------
66-
final dateTimeButton = find.byKey(dateTimeButtonKey);
67+
final dateTimeButton = find.byKey(materialDateTimeButtonKey);
6768

6869
// Tap on `Cupertino` key and page should change
6970
await tester.tap(dateTimeButton);
@@ -80,4 +81,70 @@ void main() {
8081
// Should show page again
8182
expect(find.byKey(materialPageKey), findsOneWidget);
8283
});
84+
85+
testWidgets('Test Cupertino page pickers', (WidgetTester tester) async {
86+
// Build our app and trigger a frame.
87+
await tester.pumpWidget(const App());
88+
89+
expect(find.byKey(materialPageKey), findsOneWidget);
90+
expect(find.byKey(cupertinoPageKey), findsNothing);
91+
92+
// Tap on `Cupertino` key and page should change
93+
await tester.tap(find.byKey(cupertinoButtonKey));
94+
await tester.pumpAndSettle();
95+
96+
// TEST DATE BUTTON ONLY -----------
97+
final dateButton = find.byKey(cupertinoDateButtonKey);
98+
99+
// Tap on `Cupertino` key and page should change
100+
await tester.tap(dateButton);
101+
await tester.pumpAndSettle();
102+
103+
// Tap on Sat 29th and drag
104+
await tester.drag(find.text(" Sat 29 "), const Offset(0.0, -32.0), touchSlopY: 0.0, warnIfMissed: false);
105+
await tester.pumpAndSettle();
106+
107+
// Tap outside the picker
108+
await tester.tapAt(const Offset(0, 0));
109+
await tester.pumpAndSettle();
110+
111+
// Should show page again
112+
expect(find.byKey(cupertinoPageKey), findsOneWidget);
113+
114+
// TEST TIME BUTTON ONLY -----------
115+
final timeButton = find.byKey(cupertinoTimeButtonKey);
116+
117+
// Tap on `Cupertino` key and page should change
118+
await tester.tap(timeButton);
119+
await tester.pumpAndSettle();
120+
121+
// Tap on Sat 29th and drag
122+
await tester.drag(find.text("01"), const Offset(0.0, -32.0), touchSlopY: 0.0, warnIfMissed: false);
123+
await tester.pumpAndSettle();
124+
125+
// Tap outside the picker
126+
await tester.tapAt(const Offset(0, 0));
127+
await tester.pumpAndSettle();
128+
129+
// Should show page again
130+
expect(find.byKey(cupertinoPageKey), findsOneWidget);
131+
132+
// TEST DATE AND TIME BUTTON -----------
133+
final dateTimeButton = find.byKey(cupertinoDateTimeButtonKey);
134+
135+
// Tap on `Cupertino` key and page should change
136+
await tester.tap(dateTimeButton);
137+
await tester.pumpAndSettle();
138+
139+
// Tap on Sat 29th and drag
140+
await tester.drag(find.text("01"), const Offset(0.0, -32.0), touchSlopY: 0.0, warnIfMissed: false);
141+
await tester.pumpAndSettle();
142+
143+
// Tap outside the picker
144+
await tester.tapAt(const Offset(0, 0));
145+
await tester.pumpAndSettle();
146+
147+
// Should show page again
148+
expect(find.byKey(cupertinoPageKey), findsOneWidget);
149+
});
83150
}

0 commit comments

Comments
 (0)