forked from MarcinusX/NumberPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.dart
165 lines (152 loc) · 4.58 KB
/
test_utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:numberpicker/numberpicker.dart';
Decoration decoration = new BoxDecoration(
border: new Border(
top: new BorderSide(
style: BorderStyle.solid,
color: Colors.black26,
),
bottom: new BorderSide(
style: BorderStyle.solid,
color: Colors.black26,
),
),
);
Future<NumberPicker> testNumberPicker({
WidgetTester tester,
int minValue,
int maxValue,
int initialValue,
int scrollBy,
int step = 1,
int expectedValue,
bool animateToItself = false,
Axis axis = Axis.vertical,
bool infiniteLoop = false,
Decoration decoration,
bool highlightSelectedValue = true,
}) async {
int value = initialValue;
NumberPicker picker;
await tester.pumpWidget(
StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
picker = axis == Axis.vertical
? picker = NumberPicker.integer(
initialValue: value,
minValue: minValue,
maxValue: maxValue,
step: step,
infiniteLoop: infiniteLoop,
decoration: decoration,
highlightSelectedValue: highlightSelectedValue,
onChanged: (newValue) => setState(() => value = newValue),
)
: NumberPicker.horizontal(
initialValue: value,
minValue: minValue,
maxValue: maxValue,
step: step,
decoration: decoration,
highlightSelectedValue: highlightSelectedValue,
onChanged: (newValue) => setState(() => value = newValue),
);
return MaterialApp(
home: Scaffold(
body: picker,
),
);
}),
);
expect(value, equals(initialValue));
await scrollNumberPicker(Offset(0.0, 0.0), tester, scrollBy, axis);
await tester.pumpAndSettle();
expect(value, equals(expectedValue));
if (animateToItself) {
expect(picker.selectedIntValue, equals(expectedValue));
await picker.animateInt(picker.selectedIntValue);
await tester.pumpAndSettle();
expect(picker.selectedIntValue, equals(expectedValue));
}
return picker;
}
Future<NumberPicker> testMultipleValuesInPicker({
WidgetTester tester,
int minValue,
int maxValue,
int initialValue,
int scrollBy,
int step = 1,
bool animateToItself = false,
Axis axis = Axis.vertical,
bool zeroPad = false,
List<String> expectedDisplayValues,
bool infiniteLoop = false,
}) async {
int value = initialValue;
NumberPicker picker;
await tester.pumpWidget(
StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
picker = axis == Axis.vertical
? picker = NumberPicker.integer(
initialValue: value,
minValue: minValue,
maxValue: maxValue,
step: step,
infiniteLoop: infiniteLoop,
onChanged: (newValue) => setState(() => value = newValue),
zeroPad: zeroPad,
)
: NumberPicker.horizontal(
initialValue: value,
minValue: minValue,
maxValue: maxValue,
step: step,
zeroPad: zeroPad,
onChanged: (newValue) => setState(() => value = newValue),
);
return MaterialApp(
home: Scaffold(
body: picker,
),
);
}),
);
expect(value, equals(initialValue));
await scrollNumberPicker(Offset(0.0, 0.0), tester, scrollBy, axis);
await tester.pumpAndSettle();
for (String displayValue in expectedDisplayValues) {
expect(find.text(displayValue), findsOneWidget);
}
return picker;
}
scrollNumberPicker(
Offset pickerPosition,
WidgetTester tester,
int scrollBy,
Axis axis,
) async {
double pickerCenterX, pickerCenterY, offsetX, offsetY;
double pickerCenterMainAxis = 1.5 * NumberPicker.kDefaultItemExtent;
double pickerCenterCrossAxis = NumberPicker.kDefaultListViewCrossAxisSize / 2;
if (axis == Axis.vertical) {
pickerCenterX = pickerCenterCrossAxis;
pickerCenterY = pickerCenterMainAxis;
offsetX = 0.0;
offsetY = -scrollBy * NumberPicker.kDefaultItemExtent;
} else {
pickerCenterX = pickerCenterMainAxis;
pickerCenterY = pickerCenterCrossAxis;
offsetX = -scrollBy * NumberPicker.kDefaultItemExtent;
offsetY = 0.0;
}
Offset pickerCenter = Offset(
pickerPosition.dx + pickerCenterX,
pickerPosition.dy + pickerCenterY,
);
final TestGesture testGesture = await tester.startGesture(pickerCenter);
await testGesture.moveBy(Offset(
offsetX,
offsetY,
));
}