-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_controller.dart
205 lines (135 loc) · 6.46 KB
/
test_controller.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2022 Andrious Solutions Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:example/src/controller.dart';
import 'package:example/src/view.dart';
import 'package:flutter_test/flutter_test.dart';
const location = '========================== test_controller.dart';
void testsController(WidgetTester tester) {
//
/// Explicitly provide what's intentionally should be accessible
/// but is made accessible for 'internal testing' of this framework.
// Find its StatefulWidget first then the 'type' of State object.
AppStateX appState = tester.firstState<AppStateX>(find.byType(MyApp));
expect(appState.widget, isA<MyApp>());
/// Returns the unique identifier assigned to the Controller object.
/// Unnecessary. Merely demonstrating an alternative to 'adding' a
/// Controller object to a StatMVC object.
/// In fact, it's already there and will merely return its assigned id.
String conId = appState.add(Controller());
expect(conId, isA<String>(), reason: location);
Controller con = appState.controllerById(conId) as Controller;
expect(con, isA<Controller>(), reason: location);
/// Do the reverse, test 'adding' a State object to a Controller.
conId = con.addState(appState);
/// null testing
conId = appState.add(null);
expect(conId, isEmpty);
appState = con.rootState!;
/// This State object 'contains' this Controller.
con = appState.controllerByType<Controller>()!;
String keyId = con.identifier;
con = appState.controllerById(keyId) as Controller;
expect(con, isA<Controller>(), reason: location);
final List<String> keyIds = appState.addList([Controller()]);
expect(keyIds, isNotEmpty, reason: location);
con = appState.controllerById(keyIds[0]) as Controller;
expect(con, isA<Controller>(), reason: location);
/// Event the 'first' State object has a reference to itself
appState = appState.rootState!;
expect(appState.widget, isA<MyApp>(), reason: location);
/// Returns the most recent BuildContext/Element created in the App
final BuildContext context = con.lastContext!;
expect(context.widget, isA<Page1>(), reason: location);
/// Call for testing coverage
con.dependOnInheritedWidget(context);
/// Rebuild InheritedWidget
appState.inheritedNeedsBuild('Test');
/// Test AppController class
_testAppController(tester);
/// This State object 'contains' this Controller.
AnotherController another = appState.controllerByType<AnotherController>()!;
keyId = another.identifier;
another = appState.controllerById(keyId) as AnotherController;
expect(another, isA<AnotherController>(), reason: location);
/// This State object 'contains' this Controller.
YetAnotherController andAnother =
appState.controllerByType<YetAnotherController>()!;
keyId = andAnother.identifier;
andAnother = appState.controllerById(keyId) as YetAnotherController;
expect(andAnother, isA<YetAnotherController>(), reason: location);
/// Another way to retrieve its Controller from a list of Controllers
/// Retrieve it by 'type'
/// This controller exists but not with this State object
/// but with the AppMVC (the App's State object)
another = appState.controllerByType<AnotherController>()!;
expect(another, isA<AnotherController>(), reason: location);
/// This Controller will be found in this State object's listing.
con = appState.controllerByType<Controller>()!;
expect(con, isA<Controller>());
conId = con.identifier;
/// Another way to retrieve its Controller from a list of Controllers
/// Retrieve it by its key id Note the casting.
con = appState.controllerById(conId) as Controller;
expect(con, isA<Controller>(), reason: location);
/// Return a List of Controllers specified by key id.
final listCons = con.listControllers([conId]);
expect(listCons, isA<List<StateXController?>>(), reason: location);
expect(listCons[0], isA<Controller>(), reason: location);
/// Only when the [StateMVC] object is first created.
con.initState();
/// The framework calls this method when removed from the widget tree.
/// Called when there's a hot reload.
con.deactivate();
/// The framework calls this method the State object is terminated.
con.dispose();
/// Called if the State object was a dependency to an InheritedWidget
/// and that InheritedWidget has been rebuilt. Hence this is rebuilt.
con.didChangeDependencies();
/// Called during development whenever there's a hot reload.
con.reassemble();
/// When the framework tells the app to pop the current route.
con.didPopRoute();
/// When the application's dimensions change.
con.didChangeMetrics();
/// Called when the platform's text scale factor changes.
con.didChangeTextScaleFactor();
/// Brightness changed.
con.didChangePlatformBrightness();
/// Called when the system is running low on memory.
con.didHaveMemoryPressure();
/// Called when the system changes the set of active accessibility features.
con.didChangeAccessibilityFeatures();
/// Allows you to call 'setState' from the 'current' the State object.
con.setState(() {});
//@Deprecated('Keep with Flutter syntax')
// /// Allows you to call 'setState' from the 'current' the State object.
// con.refresh();
//
// /// Allows you to call 'setState' from the 'current' the State object.
// con.rebuild();
//
// /// Allows you to call 'setState' from the 'current' the State object.
// con.notifyListeners();
/// Return a 'copy' of the Set of State objects.
final Set<StateX>? states = con.states;
expect(states, isA<Set<StateX>>(), reason: location);
}
bool _testAppController(WidgetTester tester) {
/// Explicitly provide what's intentionally should be accessible
/// but is made accessible for 'internal testing' of this framework.
// Find its StatefulWidget first then the 'type' of State object.
final StateX appState = tester.firstState<AppStateX>(find.byType(MyApp));
/// The first Controller added to the App's first State object
final controller = appState.rootCon;
expect(controller, isA<AppController>());
final rootCon = controller as AppController;
final errorDetails = FlutterErrorDetails(
exception: Exception('Pretend Error'),
context: ErrorDescription('Created merely for testing purposes.'),
library: 'widget_test',
);
expect(rootCon.onAsyncError(errorDetails), isA<bool>());
// Take in any Exception so not to 'fail' the running test
return tester.takeException() == null;
}