-
Notifications
You must be signed in to change notification settings - Fork 26.7k
/
view_test.dart
453 lines (394 loc) · 17.5 KB
/
view_test.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui';
import 'package:collection/collection.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'utils/fake_and_mock_utils.dart';
void main() {
group('TestFlutterView', () {
FlutterView trueImplicitView() => PlatformDispatcher.instance.implicitView!;
FlutterView boundImplicitView() => WidgetsBinding.instance.platformDispatcher.implicitView!;
tearDown(() {
final TestFlutterView view = (WidgetsBinding.instance as TestWidgetsFlutterBinding).platformDispatcher.views.single;
view.reset();
});
testWidgets('can handle new methods without breaking', (WidgetTester tester) async {
final dynamic testView = tester.view;
// ignore: avoid_dynamic_calls
expect(testView.someNewProperty, null);
});
testWidgets('can fake devicePixelRatio', (WidgetTester tester) async {
verifyPropertyFaked<double>(
tester: tester,
realValue: trueImplicitView().devicePixelRatio,
fakeValue: 2.5,
propertyRetriever: () => boundImplicitView().devicePixelRatio,
propertyFaker: (_, double fakeValue) {
tester.view.devicePixelRatio = fakeValue;
},
);
});
testWidgets('can reset devicePixelRatio', (WidgetTester tester) async {
verifyPropertyReset<double>(
tester: tester,
fakeValue: 2.5,
propertyRetriever: () => boundImplicitView().devicePixelRatio,
propertyResetter: () {
tester.view.resetDevicePixelRatio();
},
propertyFaker: (double fakeValue) {
tester.view.devicePixelRatio = fakeValue;
},
);
});
testWidgets('updating devicePixelRatio also updates display.devicePixelRatio', (WidgetTester tester) async {
tester.view.devicePixelRatio = tester.view.devicePixelRatio + 1;
expect(tester.view.display.devicePixelRatio, tester.view.devicePixelRatio);
});
testWidgets('can fake displayFeatures', (WidgetTester tester) async {
verifyPropertyFaked<List<DisplayFeature>>(
tester: tester,
realValue: trueImplicitView().displayFeatures,
fakeValue: <DisplayFeature>[const DisplayFeature(bounds: Rect.fromLTWH(0, 0, 500, 30), type: DisplayFeatureType.unknown, state: DisplayFeatureState.unknown)],
propertyRetriever: () => boundImplicitView().displayFeatures,
propertyFaker: (_, List<DisplayFeature> fakeValue) {
tester.view.displayFeatures = fakeValue;
},
);
});
testWidgets('can reset displayFeatures', (WidgetTester tester) async {
verifyPropertyReset<List<DisplayFeature>>(
tester: tester,
fakeValue: <DisplayFeature>[const DisplayFeature(bounds: Rect.fromLTWH(0, 0, 500, 30), type: DisplayFeatureType.unknown, state: DisplayFeatureState.unknown)],
propertyRetriever: () => boundImplicitView().displayFeatures,
propertyResetter: () {
tester.view.resetDisplayFeatures();
},
propertyFaker: (List<DisplayFeature> fakeValue) {
tester.view.displayFeatures = fakeValue;
},
);
});
testWidgets('can fake padding', (WidgetTester tester) async {
verifyPropertyFaked<ViewPadding>(
tester: tester,
realValue: trueImplicitView().padding,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().padding,
propertyFaker: (_, ViewPadding fakeValue) {
tester.view.padding = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can reset padding', (WidgetTester tester) async {
verifyPropertyReset<ViewPadding>(
tester: tester,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().padding,
propertyResetter: () {
tester.view.resetPadding();
},
propertyFaker: (ViewPadding fakeValue) {
tester.view.padding = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can fake physicalSize', (WidgetTester tester) async {
verifyPropertyFaked<Size>(
tester: tester,
realValue: trueImplicitView().physicalSize,
fakeValue: const Size(50, 50),
propertyRetriever: () => boundImplicitView().physicalSize,
propertyFaker: (_, Size fakeValue) {
tester.view.physicalSize = fakeValue;
},
);
});
testWidgets('faking physicalSize fakes physicalConstraints', (WidgetTester tester) async {
const Size fakeSize = Size(50, 50);
verifyPropertyFaked<ViewConstraints>(
tester: tester,
realValue: trueImplicitView().physicalConstraints,
fakeValue: ViewConstraints.tight(fakeSize),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyFaker: (_, __) {
tester.view.physicalSize = fakeSize;
},
);
});
testWidgets('can reset physicalSize', (WidgetTester tester) async {
verifyPropertyReset<Size>(
tester: tester,
fakeValue: const Size(50, 50),
propertyRetriever: () => boundImplicitView().physicalSize,
propertyResetter: () {
tester.view.resetPhysicalSize();
},
propertyFaker: (Size fakeValue) {
tester.view.physicalSize = fakeValue;
},
);
});
testWidgets('resetting physicalSize resets physicalConstraints', (WidgetTester tester) async {
const Size fakeSize = Size(50, 50);
verifyPropertyReset<ViewConstraints>(
tester: tester,
fakeValue: ViewConstraints.tight(fakeSize),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyResetter: () {
tester.view.resetPhysicalSize();
},
propertyFaker: (_) {
tester.view.physicalSize = fakeSize;
},
);
});
testWidgets('can fake physicalConstraints', (WidgetTester tester) async {
verifyPropertyFaked<ViewConstraints>(
tester: tester,
realValue: trueImplicitView().physicalConstraints,
fakeValue: const ViewConstraints(minWidth: 1, maxWidth: 2, minHeight: 3, maxHeight: 4),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyFaker: (_, ViewConstraints fakeValue) {
tester.view.physicalConstraints = fakeValue;
},
);
});
testWidgets('can reset physicalConstraints', (WidgetTester tester) async {
verifyPropertyReset<ViewConstraints>(
tester: tester,
fakeValue: const ViewConstraints(minWidth: 1, maxWidth: 2, minHeight: 3, maxHeight: 4),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyResetter: () {
tester.view.resetPhysicalConstraints();
},
propertyFaker: (ViewConstraints fakeValue) {
tester.view.physicalConstraints = fakeValue;
},
);
});
testWidgets('can fake systemGestureInsets', (WidgetTester tester) async {
verifyPropertyFaked<ViewPadding>(
tester: tester,
realValue: trueImplicitView().systemGestureInsets,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().systemGestureInsets,
propertyFaker: (_, ViewPadding fakeValue) {
tester.view.systemGestureInsets = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can reset systemGestureInsets', (WidgetTester tester) async {
verifyPropertyReset<ViewPadding>(
tester: tester,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().systemGestureInsets,
propertyResetter: () {
tester.view.resetSystemGestureInsets();
},
propertyFaker: (ViewPadding fakeValue) {
tester.view.systemGestureInsets = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can fake viewInsets', (WidgetTester tester) async {
verifyPropertyFaked<ViewPadding>(
tester: tester,
realValue: trueImplicitView().viewInsets,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().viewInsets,
propertyFaker: (_, ViewPadding fakeValue) {
tester.view.viewInsets = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can reset viewInsets', (WidgetTester tester) async {
verifyPropertyReset<ViewPadding>(
tester: tester,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().viewInsets,
propertyResetter: () {
tester.view.resetViewInsets();
},
propertyFaker: (ViewPadding fakeValue) {
tester.view.viewInsets = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can fake viewPadding', (WidgetTester tester) async {
verifyPropertyFaked<ViewPadding>(
tester: tester,
realValue: trueImplicitView().viewPadding,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().viewPadding,
propertyFaker: (_, ViewPadding fakeValue) {
tester.view.viewPadding = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can reset viewPadding', (WidgetTester tester) async {
verifyPropertyReset<ViewPadding>(
tester: tester,
fakeValue: FakeViewPadding.zero,
propertyRetriever: () => boundImplicitView().viewPadding,
propertyResetter: () {
tester.view.resetViewPadding();
},
propertyFaker: (ViewPadding fakeValue) {
tester.view.viewPadding = fakeValue as FakeViewPadding;
},
matcher: matchesViewPadding
);
});
testWidgets('can clear out fake properties all at once', (WidgetTester tester) async {
final FlutterViewSnapshot initial = FlutterViewSnapshot(tester.view);
tester.view.devicePixelRatio = 7;
tester.view.displayFeatures = <DisplayFeature>[const DisplayFeature(bounds: Rect.fromLTWH(0, 0, 20, 300), type: DisplayFeatureType.unknown, state: DisplayFeatureState.unknown)];
tester.view.padding = FakeViewPadding.zero;
tester.view.systemGestureInsets = FakeViewPadding.zero;
tester.view.viewInsets = FakeViewPadding.zero;
tester.view.viewPadding = FakeViewPadding.zero;
tester.view.gestureSettings = const GestureSettings(physicalTouchSlop: 4, physicalDoubleTapSlop: 5);
final FlutterViewSnapshot faked = FlutterViewSnapshot(tester.view);
tester.view.reset();
final FlutterViewSnapshot reset = FlutterViewSnapshot(tester.view);
expect(initial, isNot(matchesSnapshot(faked)));
expect(initial, matchesSnapshot(reset));
});
testWidgets('render is passed through to backing FlutterView', (WidgetTester tester) async {
final Scene expectedScene = SceneBuilder().build();
final _FakeFlutterView backingView = _FakeFlutterView();
final TestFlutterView view = TestFlutterView(
view: backingView,
platformDispatcher: tester.binding.platformDispatcher,
display: _FakeDisplay(),
);
view.render(expectedScene);
expect(backingView.lastRenderedScene, isNotNull);
expect(backingView.lastRenderedScene, expectedScene);
});
testWidgets('updateSemantics is passed through to backing FlutterView', (WidgetTester tester) async {
// ignore: deprecated_member_use
final SemanticsUpdate expectedUpdate = SemanticsUpdateBuilderNew().build();
final _FakeFlutterView backingView = _FakeFlutterView();
final TestFlutterView view = TestFlutterView(
view: backingView,
platformDispatcher: tester.binding.platformDispatcher,
display: _FakeDisplay(),
);
view.updateSemantics(expectedUpdate);
expect(backingView.lastSemanticsUpdate, isNotNull);
expect(backingView.lastSemanticsUpdate, expectedUpdate);
});
});
}
Matcher matchesSnapshot(FlutterViewSnapshot expected) => _FlutterViewSnapshotMatcher(expected);
class _FlutterViewSnapshotMatcher extends Matcher {
_FlutterViewSnapshotMatcher(this.expected);
final FlutterViewSnapshot expected;
@override
Description describe(Description description) {
description.add('snapshot of a FlutterView matches');
return description;
}
@override
Description describeMismatch(dynamic item, Description mismatchDescription, Map<dynamic, dynamic> matchState, bool verbose) {
assert(item is FlutterViewSnapshot, 'Can only match against snapshots of FlutterView.');
final FlutterViewSnapshot actual = item as FlutterViewSnapshot;
if (actual.devicePixelRatio != expected.devicePixelRatio) {
mismatchDescription.add('actual.devicePixelRatio (${actual.devicePixelRatio}) did not match expected.devicePixelRatio (${expected.devicePixelRatio})');
}
if (!actual.displayFeatures.equals(expected.displayFeatures)) {
mismatchDescription.add('actual.displayFeatures did not match expected.devicePixelRatio');
mismatchDescription.addAll('Actual: [', ',', ']', actual.displayFeatures);
mismatchDescription.addAll('Expected: [', ',', ']', expected.displayFeatures);
}
if (actual.gestureSettings != expected.gestureSettings) {
mismatchDescription.add('actual.gestureSettings (${actual.gestureSettings}) did not match expected.gestureSettings (${expected.gestureSettings})');
}
final Matcher paddingMatcher = matchesViewPadding(expected.padding);
if (!paddingMatcher.matches(actual.padding, matchState)) {
mismatchDescription.add('actual.padding (${actual.padding}) did not match expected.padding (${expected.padding})');
paddingMatcher.describeMismatch(actual.padding, mismatchDescription, matchState, verbose);
}
if (actual.physicalSize != expected.physicalSize) {
mismatchDescription.add('actual.physicalSize (${actual.physicalSize}) did not match expected.physicalSize (${expected.physicalSize})');
}
final Matcher systemGestureInsetsMatcher = matchesViewPadding(expected.systemGestureInsets);
if (!systemGestureInsetsMatcher.matches(actual.systemGestureInsets, matchState)) {
mismatchDescription.add('actual.systemGestureInsets (${actual.systemGestureInsets}) did not match expected.systemGestureInsets (${expected.systemGestureInsets})');
systemGestureInsetsMatcher.describeMismatch(actual.systemGestureInsets, mismatchDescription, matchState, verbose);
}
if (actual.viewId != expected.viewId) {
mismatchDescription.add('actual.viewId (${actual.viewId}) did not match expected.viewId (${expected.viewId})');
}
final Matcher viewInsetsMatcher = matchesViewPadding(expected.viewInsets);
if (!viewInsetsMatcher.matches(actual.viewInsets, matchState)) {
mismatchDescription.add('actual.viewInsets (${actual.viewInsets}) did not match expected.viewInsets (${expected.viewInsets})');
viewInsetsMatcher.describeMismatch(actual.viewInsets, mismatchDescription, matchState, verbose);
}
final Matcher viewPaddingMatcher = matchesViewPadding(expected.viewPadding);
if (!viewPaddingMatcher.matches(actual.viewPadding, matchState)) {
mismatchDescription.add('actual.viewPadding (${actual.viewPadding}) did not match expected.devicePixelRatio (${expected.viewPadding})');
viewPaddingMatcher.describeMismatch(actual.viewPadding, mismatchDescription, matchState, verbose);
}
return mismatchDescription;
}
@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
assert(item is FlutterViewSnapshot, 'Can only match against snapshots of FlutterView.');
final FlutterViewSnapshot actual = item as FlutterViewSnapshot;
return actual.devicePixelRatio == expected.devicePixelRatio &&
actual.displayFeatures.equals(expected.displayFeatures) &&
actual.gestureSettings == expected.gestureSettings &&
matchesViewPadding(expected.padding).matches(actual.padding, matchState) &&
actual.physicalSize == expected.physicalSize &&
matchesViewPadding(expected.systemGestureInsets).matches(actual.padding, matchState) &&
actual.viewId == expected.viewId &&
matchesViewPadding(expected.viewInsets).matches(actual.viewInsets, matchState) &&
matchesViewPadding(expected.viewPadding).matches(actual.viewPadding, matchState);
}
}
class FlutterViewSnapshot {
FlutterViewSnapshot(FlutterView view) :
devicePixelRatio = view.devicePixelRatio,
displayFeatures = <DisplayFeature>[...view.displayFeatures],
gestureSettings = view.gestureSettings,
padding = view.padding,
physicalSize = view.physicalSize,
systemGestureInsets = view.systemGestureInsets,
viewId = view.viewId,
viewInsets = view.viewInsets,
viewPadding = view.viewPadding;
final double devicePixelRatio;
final List<DisplayFeature> displayFeatures;
final GestureSettings gestureSettings;
final ViewPadding padding;
final Size physicalSize;
final ViewPadding systemGestureInsets;
final Object viewId;
final ViewPadding viewInsets;
final ViewPadding viewPadding;
}
class _FakeFlutterView extends Fake implements FlutterView {
SemanticsUpdate? lastSemanticsUpdate;
Scene? lastRenderedScene;
@override
void updateSemantics(SemanticsUpdate update) {
lastSemanticsUpdate = update;
}
@override
void render(Scene scene, {Size? size}) {
lastRenderedScene = scene;
}
}
class _FakeDisplay extends Fake implements TestDisplay { }