This repository was archived by the owner on Apr 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmock_navigator.dart
More file actions
335 lines (294 loc) · 8.13 KB
/
mock_navigator.dart
File metadata and controls
335 lines (294 loc) · 8.13 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import 'package:flutter/material.dart';
import 'package:mocktail/mocktail.dart';
class _MockMaterialPageRoute extends MaterialPageRoute<void> {
_MockMaterialPageRoute({required super.builder});
void hackOverlays() {
for (var i = 0; i < overlayEntries.length; i++) {
// Entry can only be inserted when the state is mounted
final state = _MockOverlayState().._mounted = true;
final entry = OverlayEntry(builder: (_) => const SizedBox());
try {
// We need to call insert since that is the only way to populate the
// `_overlay` field in the entry. But that method calls a setState,
// which will fail since we are not in a widget tree.
//
// By the time the setState is called, the attribute is already set
// so we just ignore the error and the hack will do its job.
state.insert(entry);
} on Object catch (_) {}
// Set mounted back to false to make sure the state doesn't get
// marked as dirty during OverlayEntry.remove().
state._mounted = false;
overlayEntries[i] = entry;
}
}
}
class _MockOverlayState extends OverlayState {
late bool _mounted;
@override
bool get mounted => _mounted;
}
class _FakeRoute<T> extends Fake implements Route<T> {}
/// {@template mock_navigator_provider}
/// The widget that provides an instance of a [MockNavigator].
/// {@endtemplate}
class MockNavigatorProvider extends Navigator {
/// {@macro mock_navigator_provider}
const MockNavigatorProvider({
required this.navigator,
required this.child,
super.key,
});
/// The mock navigator used to mock navigation calls.
final MockNavigator navigator;
/// The [Widget] to render.
final Widget child;
@override
NavigatorState createState() {
// The hack that makes it all work.
// ignore: no_logic_in_create_state
return _MockNavigatorState(navigator).._child = child;
}
@override
RouteFactory? get onGenerateRoute {
return (_) {
final route = _MockMaterialPageRoute(builder: (_) => child);
navigator._routes.add(route);
return route;
};
}
}
/// {@template mock_navigator}
/// A mock navigator which can be used to stub navigation for testing purposes.
/// {@endtemplate}
class MockNavigator extends Mock
with _MockNavigatorDiagnosticsMixin
implements NavigatorState {
/// {@macro mock_navigator}
MockNavigator() {
registerFallbackValue(_FakeRoute<dynamic>());
registerFallbackValue(_FakeRoute<Object>());
registerFallbackValue(_FakeRoute<void>());
registerFallbackValue(_FakeRoute<bool>());
registerFallbackValue(_FakeRoute<String>());
registerFallbackValue(_FakeRoute<num>());
}
final _routes = <_MockMaterialPageRoute>[];
}
/// A mixin necessary when implementing a [MockNavigator].
mixin _MockNavigatorDiagnosticsMixin on Object {
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return super.toString();
}
}
/// Internal class that imitates a [NavigatorState] and maps all the real
/// [NavigatorState] methods to the mock methods for use in testing.
///
/// Any public method of [NavigatorState] used for routing should be overridden
/// and remapped to the internal [_navigator] before any `verify` or `when`
/// calls can function.
class _MockNavigatorState extends NavigatorState {
_MockNavigatorState(this._navigator);
final MockNavigator _navigator;
late Widget _child;
@override
Widget build(BuildContext context) => _child;
@override
void dispose() {
for (final route in _navigator._routes) {
route.hackOverlays();
}
super.dispose();
}
@override
Future<T?> push<T extends Object?>(Route<T> route) {
return _navigator.push<T>(route);
}
@override
Future<T?> pushNamed<T extends Object?>(
String routeName, {
Object? arguments,
}) {
return _navigator.pushNamed<T>(
routeName,
arguments: arguments,
);
}
@override
Future<T?> pushNamedAndRemoveUntil<T extends Object?>(
String newRouteName,
RoutePredicate predicate, {
Object? arguments,
}) {
return _navigator.pushNamedAndRemoveUntil<T>(
newRouteName,
predicate,
arguments: arguments,
);
}
@override
Future<T?> pushReplacement<T extends Object?, TO extends Object?>(
Route<T> newRoute, {
TO? result,
}) {
return _navigator.pushReplacement<T, TO>(
newRoute,
result: result,
);
}
@override
Future<T?> pushReplacementNamed<T extends Object?, TO extends Object?>(
String routeName, {
TO? result,
Object? arguments,
}) {
return _navigator.pushReplacementNamed<T, TO>(
routeName,
result: result,
arguments: arguments,
);
}
@override
void pop<T extends Object?>([T? result]) {
return _navigator.pop<T>(result);
}
@override
Future<T?> popAndPushNamed<T extends Object?, TO extends Object?>(
String routeName, {
TO? result,
Object? arguments,
}) {
return _navigator.popAndPushNamed<T, TO>(
routeName,
result: result,
arguments: arguments,
);
}
@override
void popUntil(RoutePredicate predicate) {
return _navigator.popUntil(predicate);
}
@override
bool canPop() {
return _navigator.canPop();
}
@override
Future<bool> maybePop<T extends Object?>([T? result]) {
return _navigator.maybePop<T>(result);
}
@override
Future<T?> pushAndRemoveUntil<T extends Object?>(
Route<T> newRoute,
RoutePredicate predicate,
) {
return _navigator.pushAndRemoveUntil<T>(
newRoute,
predicate,
);
}
@override
String restorablePopAndPushNamed<T extends Object?, TO extends Object?>(
String routeName, {
TO? result,
Object? arguments,
}) {
return _navigator.restorablePopAndPushNamed<T, TO>(
routeName,
result: result,
arguments: arguments,
);
}
@override
String restorablePush<T extends Object?>(
RestorableRouteBuilder<T> routeBuilder, {
Object? arguments,
}) {
return _navigator.restorablePush<T>(
routeBuilder,
arguments: arguments,
);
}
@override
String restorablePushAndRemoveUntil<T extends Object?>(
RestorableRouteBuilder<T> newRouteBuilder,
RoutePredicate predicate, {
Object? arguments,
}) {
return _navigator.restorablePushAndRemoveUntil<T>(
newRouteBuilder,
predicate,
arguments: arguments,
);
}
@override
String restorablePushNamed<T extends Object?>(
String routeName, {
Object? arguments,
}) {
return _navigator.restorablePushNamed<T>(
routeName,
arguments: arguments,
);
}
@override
String restorablePushNamedAndRemoveUntil<T extends Object?>(
String newRouteName,
RoutePredicate predicate, {
Object? arguments,
}) {
return _navigator.restorablePushNamedAndRemoveUntil<T>(
newRouteName,
predicate,
arguments: arguments,
);
}
@override
String restorablePushReplacement<T extends Object?, TO extends Object?>(
RestorableRouteBuilder<T> routeBuilder, {
TO? result,
Object? arguments,
}) {
return _navigator.restorablePushReplacement<T, TO>(
routeBuilder,
result: result,
arguments: arguments,
);
}
@override
String restorablePushReplacementNamed<T extends Object?, TO extends Object?>(
String routeName, {
TO? result,
Object? arguments,
}) {
return _navigator.restorablePushReplacementNamed<T, TO>(
routeName,
result: result,
arguments: arguments,
);
}
@override
String restorableReplace<T extends Object?>({
required Route<dynamic> oldRoute,
required RestorableRouteBuilder<T> newRouteBuilder,
Object? arguments,
}) {
return _navigator.restorableReplace<T>(
oldRoute: oldRoute,
newRouteBuilder: newRouteBuilder,
arguments: arguments,
);
}
@override
String restorableReplaceRouteBelow<T extends Object?>({
required Route<dynamic> anchorRoute,
required RestorableRouteBuilder<T> newRouteBuilder,
Object? arguments,
}) {
return _navigator.restorableReplaceRouteBelow<T>(
anchorRoute: anchorRoute,
newRouteBuilder: newRouteBuilder,
arguments: arguments,
);
}
}