-
Notifications
You must be signed in to change notification settings - Fork 28.4k
/
Copy pathwindow.dart
1991 lines (1779 loc) · 69.5 KB
/
window.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
/// @docImport 'package:flutter/material.dart';
///
/// @docImport 'binding.dart';
/// @docImport 'widget_tester.dart';
library;
import 'dart:ui' hide window;
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
/// Test version of [AccessibilityFeatures] in which specific features may
/// be set to arbitrary values.
///
/// By default, all features are disabled. For an instance where all the
/// features are enabled, consider the [FakeAccessibilityFeatures.allOn]
/// constant.
@immutable
class FakeAccessibilityFeatures implements AccessibilityFeatures {
/// Creates a test instance of [AccessibilityFeatures].
///
/// By default, all features are disabled.
const FakeAccessibilityFeatures({
this.accessibleNavigation = false,
this.invertColors = false,
this.disableAnimations = false,
this.boldText = false,
this.reduceMotion = false,
this.highContrast = false,
this.onOffSwitchLabels = false,
});
/// An instance of [AccessibilityFeatures] where all the features are enabled.
static const FakeAccessibilityFeatures allOn = FakeAccessibilityFeatures(
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
reduceMotion: true,
highContrast: true,
onOffSwitchLabels: true,
);
@override
final bool accessibleNavigation;
@override
final bool invertColors;
@override
final bool disableAnimations;
@override
final bool boldText;
@override
final bool reduceMotion;
@override
final bool highContrast;
@override
final bool onOffSwitchLabels;
@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) {
return false;
}
return other is FakeAccessibilityFeatures &&
other.accessibleNavigation == accessibleNavigation &&
other.invertColors == invertColors &&
other.disableAnimations == disableAnimations &&
other.boldText == boldText &&
other.reduceMotion == reduceMotion &&
other.highContrast == highContrast &&
other.onOffSwitchLabels == onOffSwitchLabels;
}
@override
int get hashCode {
return Object.hash(
accessibleNavigation,
invertColors,
disableAnimations,
boldText,
reduceMotion,
highContrast,
onOffSwitchLabels,
);
}
/// This gives us some grace time when the dart:ui side adds something to
/// [AccessibilityFeatures], and makes things easier when we do rolls to
/// give us time to catch up.
///
/// If you would like to add to this class, changes must first be made in the
/// engine, followed by the framework.
@override
dynamic noSuchMethod(Invocation invocation) {
return null;
}
}
/// Used to fake insets and padding for [TestFlutterView]s.
///
/// See also:
///
/// * [TestFlutterView.padding], [TestFlutterView.systemGestureInsets],
/// [TestFlutterView.viewInsets], and [TestFlutterView.viewPadding] for test
/// properties that make use of [FakeViewPadding].
@immutable
class FakeViewPadding implements ViewPadding {
/// Instantiates a new [FakeViewPadding] object for faking insets and padding
/// during tests.
const FakeViewPadding({this.left = 0.0, this.top = 0.0, this.right = 0.0, this.bottom = 0.0});
FakeViewPadding._wrap(ViewPadding base)
: left = base.left,
top = base.top,
right = base.right,
bottom = base.bottom;
/// A view padding that has zeros for each edge.
static const FakeViewPadding zero = FakeViewPadding();
@override
final double left;
@override
final double top;
@override
final double right;
@override
final double bottom;
}
/// [PlatformDispatcher] that wraps another [PlatformDispatcher] and
/// allows faking of some properties for testing purposes.
///
/// See also:
///
/// * [TestFlutterView], which wraps a [FlutterView] for testing and
/// mocking purposes.
class TestPlatformDispatcher implements PlatformDispatcher {
/// Constructs a [TestPlatformDispatcher] that defers all behavior to the given
/// [PlatformDispatcher] unless explicitly overridden for test purposes.
TestPlatformDispatcher({required PlatformDispatcher platformDispatcher})
: _platformDispatcher = platformDispatcher {
_updateViewsAndDisplays();
_platformDispatcher.onMetricsChanged = _handleMetricsChanged;
_platformDispatcher.onViewFocusChange = _handleViewFocusChanged;
}
/// The [PlatformDispatcher] that is wrapped by this [TestPlatformDispatcher].
final PlatformDispatcher _platformDispatcher;
@override
TestFlutterView? get implicitView {
return _platformDispatcher.implicitView != null
? _testViews[_platformDispatcher.implicitView!.viewId]!
: null;
}
final Map<int, TestFlutterView> _testViews = <int, TestFlutterView>{};
final Map<int, TestDisplay> _testDisplays = <int, TestDisplay>{};
@override
VoidCallback? get onMetricsChanged => _platformDispatcher.onMetricsChanged;
VoidCallback? _onMetricsChanged;
@override
set onMetricsChanged(VoidCallback? callback) {
_onMetricsChanged = callback;
}
void _handleMetricsChanged() {
_updateViewsAndDisplays();
_onMetricsChanged?.call();
}
@override
ViewFocusChangeCallback? get onViewFocusChange => _platformDispatcher.onViewFocusChange;
ViewFocusChangeCallback? _onViewFocusChange;
@override
set onViewFocusChange(ViewFocusChangeCallback? callback) {
_onViewFocusChange = callback;
}
void _handleViewFocusChanged(ViewFocusEvent event) {
_updateViewsAndDisplays();
_currentlyFocusedViewId = switch (event.state) {
ViewFocusState.focused => event.viewId,
ViewFocusState.unfocused => null,
};
_onViewFocusChange?.call(event);
}
/// Returns the list of [ViewFocusEvent]s that have been received by
/// [requestViewFocusChange] since the last call to
/// [resetFocusedViewTestValues].
///
/// Clearing or modifying the returned list will do nothing (it's a copy).
/// Call [resetFocusedViewTestValues] to clear.
List<ViewFocusEvent> get testFocusEvents => _testFocusEvents.toList();
final List<ViewFocusEvent> _testFocusEvents = <ViewFocusEvent>[];
/// Returns the last view ID to be focused by [onViewFocusChange].
/// Returns null if no views are focused.
///
/// Can be reset to null with [resetFocusedViewTestValues].
int? get currentlyFocusedViewIdTestValue => _currentlyFocusedViewId;
int? _currentlyFocusedViewId;
/// Clears [testFocusEvents] and sets [currentlyFocusedViewIdTestValue] to
/// null.
void resetFocusedViewTestValues() {
if (_currentlyFocusedViewId != null) {
// If there is a focused view, then tell everyone who still cares that
// it's unfocusing.
_platformDispatcher.onViewFocusChange?.call(
ViewFocusEvent(
viewId: _currentlyFocusedViewId!,
state: ViewFocusState.unfocused,
direction: ViewFocusDirection.undefined,
),
);
_currentlyFocusedViewId = null;
}
_testFocusEvents.clear();
}
@override
void requestViewFocusChange({
required int viewId,
required ViewFocusState state,
required ViewFocusDirection direction,
}) {
_testFocusEvents.add(ViewFocusEvent(viewId: viewId, state: state, direction: direction));
_platformDispatcher.requestViewFocusChange(viewId: viewId, state: state, direction: direction);
}
@override
Locale get locale => _localeTestValue ?? _platformDispatcher.locale;
Locale? _localeTestValue;
/// Hides the real locale and reports the given [localeTestValue] instead.
// ignore: avoid_setters_without_getters
set localeTestValue(Locale localeTestValue) {
_localeTestValue = localeTestValue;
onLocaleChanged?.call();
}
/// Deletes any existing test locale and returns to using the real locale.
void clearLocaleTestValue() {
_localeTestValue = null;
onLocaleChanged?.call();
}
@override
List<Locale> get locales => _localesTestValue ?? _platformDispatcher.locales;
List<Locale>? _localesTestValue;
/// Hides the real locales and reports the given [localesTestValue] instead.
// ignore: avoid_setters_without_getters
set localesTestValue(List<Locale> localesTestValue) {
_localesTestValue = localesTestValue;
onLocaleChanged?.call();
}
/// Deletes any existing test locales and returns to using the real locales.
void clearLocalesTestValue() {
_localesTestValue = null;
onLocaleChanged?.call();
}
@override
VoidCallback? get onLocaleChanged => _platformDispatcher.onLocaleChanged;
@override
set onLocaleChanged(VoidCallback? callback) {
_platformDispatcher.onLocaleChanged = callback;
}
@override
String get initialLifecycleState => _initialLifecycleStateTestValue;
String _initialLifecycleStateTestValue = '';
/// Sets a faked initialLifecycleState for testing.
// ignore: avoid_setters_without_getters
set initialLifecycleStateTestValue(String state) {
_initialLifecycleStateTestValue = state;
}
/// Resets [initialLifecycleState] to the default value for the platform.
void resetInitialLifecycleState() {
_initialLifecycleStateTestValue = '';
}
@override
double get textScaleFactor => _textScaleFactorTestValue ?? _platformDispatcher.textScaleFactor;
double? _textScaleFactorTestValue;
/// Hides the real text scale factor and reports the given
/// [textScaleFactorTestValue] instead.
// ignore: avoid_setters_without_getters
set textScaleFactorTestValue(double textScaleFactorTestValue) {
_textScaleFactorTestValue = textScaleFactorTestValue;
onTextScaleFactorChanged?.call();
}
/// Deletes any existing test text scale factor and returns to using the real
/// text scale factor.
void clearTextScaleFactorTestValue() {
_textScaleFactorTestValue = null;
onTextScaleFactorChanged?.call();
}
@override
double scaleFontSize(double unscaledFontSize) => textScaleFactor * unscaledFontSize;
@override
Brightness get platformBrightness =>
_platformBrightnessTestValue ?? _platformDispatcher.platformBrightness;
Brightness? _platformBrightnessTestValue;
@override
VoidCallback? get onPlatformBrightnessChanged => _platformDispatcher.onPlatformBrightnessChanged;
@override
set onPlatformBrightnessChanged(VoidCallback? callback) {
_platformDispatcher.onPlatformBrightnessChanged = callback;
}
/// Hides the real platform brightness and reports the given
/// [platformBrightnessTestValue] instead.
// ignore: avoid_setters_without_getters
set platformBrightnessTestValue(Brightness platformBrightnessTestValue) {
_platformBrightnessTestValue = platformBrightnessTestValue;
onPlatformBrightnessChanged?.call();
}
/// Deletes any existing test platform brightness and returns to using the
/// real platform brightness.
void clearPlatformBrightnessTestValue() {
_platformBrightnessTestValue = null;
onPlatformBrightnessChanged?.call();
}
@override
bool get alwaysUse24HourFormat =>
_alwaysUse24HourFormatTestValue ?? _platformDispatcher.alwaysUse24HourFormat;
bool? _alwaysUse24HourFormatTestValue;
/// Hides the real clock format and reports the given
/// [alwaysUse24HourFormatTestValue] instead.
// ignore: avoid_setters_without_getters
set alwaysUse24HourFormatTestValue(bool alwaysUse24HourFormatTestValue) {
_alwaysUse24HourFormatTestValue = alwaysUse24HourFormatTestValue;
}
/// Deletes any existing test clock format and returns to using the real clock
/// format.
void clearAlwaysUse24HourTestValue() {
_alwaysUse24HourFormatTestValue = null;
}
@override
VoidCallback? get onTextScaleFactorChanged => _platformDispatcher.onTextScaleFactorChanged;
@override
set onTextScaleFactorChanged(VoidCallback? callback) {
_platformDispatcher.onTextScaleFactorChanged = callback;
}
@override
bool get nativeSpellCheckServiceDefined =>
_nativeSpellCheckServiceDefinedTestValue ??
_platformDispatcher.nativeSpellCheckServiceDefined;
bool? _nativeSpellCheckServiceDefinedTestValue;
// ignore: avoid_setters_without_getters
set nativeSpellCheckServiceDefinedTestValue(bool nativeSpellCheckServiceDefinedTestValue) {
_nativeSpellCheckServiceDefinedTestValue = nativeSpellCheckServiceDefinedTestValue;
}
/// Deletes existing value that determines whether or not a native spell check
/// service is defined and returns to the real value.
void clearNativeSpellCheckServiceDefined() {
_nativeSpellCheckServiceDefinedTestValue = null;
}
@override
bool get supportsShowingSystemContextMenu =>
_supportsShowingSystemContextMenu ?? _platformDispatcher.supportsShowingSystemContextMenu;
bool? _supportsShowingSystemContextMenu;
set supportsShowingSystemContextMenu(bool value) {
_supportsShowingSystemContextMenu = value;
}
/// Resets [supportsShowingSystemContextMenu] to the default value.
void resetSupportsShowingSystemContextMenu() {
_supportsShowingSystemContextMenu = null;
}
@override
bool get brieflyShowPassword =>
_brieflyShowPasswordTestValue ?? _platformDispatcher.brieflyShowPassword;
bool? _brieflyShowPasswordTestValue;
/// Hides the real [brieflyShowPassword] and reports the given
/// `brieflyShowPasswordTestValue` instead.
// ignore: avoid_setters_without_getters
set brieflyShowPasswordTestValue(bool brieflyShowPasswordTestValue) {
_brieflyShowPasswordTestValue = brieflyShowPasswordTestValue;
}
/// Resets [brieflyShowPassword] to the default value for the platform.
void resetBrieflyShowPassword() {
_brieflyShowPasswordTestValue = null;
}
@override
FrameCallback? get onBeginFrame => _platformDispatcher.onBeginFrame;
@override
set onBeginFrame(FrameCallback? callback) {
_platformDispatcher.onBeginFrame = callback;
}
@override
VoidCallback? get onDrawFrame => _platformDispatcher.onDrawFrame;
@override
set onDrawFrame(VoidCallback? callback) {
_platformDispatcher.onDrawFrame = callback;
}
@override
TimingsCallback? get onReportTimings => _platformDispatcher.onReportTimings;
@override
set onReportTimings(TimingsCallback? callback) {
_platformDispatcher.onReportTimings = callback;
}
@override
PointerDataPacketCallback? get onPointerDataPacket => _platformDispatcher.onPointerDataPacket;
@override
set onPointerDataPacket(PointerDataPacketCallback? callback) {
_platformDispatcher.onPointerDataPacket = callback;
}
@override
String get defaultRouteName => _defaultRouteNameTestValue ?? _platformDispatcher.defaultRouteName;
String? _defaultRouteNameTestValue;
/// Hides the real default route name and reports the given
/// [defaultRouteNameTestValue] instead.
// ignore: avoid_setters_without_getters
set defaultRouteNameTestValue(String defaultRouteNameTestValue) {
_defaultRouteNameTestValue = defaultRouteNameTestValue;
}
/// Deletes any existing test default route name and returns to using the real
/// default route name.
void clearDefaultRouteNameTestValue() {
_defaultRouteNameTestValue = null;
}
@override
void scheduleFrame() {
_platformDispatcher.scheduleFrame();
}
@override
bool get semanticsEnabled => _semanticsEnabledTestValue ?? _platformDispatcher.semanticsEnabled;
bool? _semanticsEnabledTestValue;
/// Hides the real semantics enabled and reports the given
/// [semanticsEnabledTestValue] instead.
// ignore: avoid_setters_without_getters
set semanticsEnabledTestValue(bool semanticsEnabledTestValue) {
_semanticsEnabledTestValue = semanticsEnabledTestValue;
onSemanticsEnabledChanged?.call();
}
/// Deletes any existing test semantics enabled and returns to using the real
/// semantics enabled.
void clearSemanticsEnabledTestValue() {
_semanticsEnabledTestValue = null;
onSemanticsEnabledChanged?.call();
}
@override
VoidCallback? get onSemanticsEnabledChanged => _platformDispatcher.onSemanticsEnabledChanged;
@override
set onSemanticsEnabledChanged(VoidCallback? callback) {
_platformDispatcher.onSemanticsEnabledChanged = callback;
}
@override
SemanticsActionEventCallback? get onSemanticsActionEvent =>
_platformDispatcher.onSemanticsActionEvent;
@override
set onSemanticsActionEvent(SemanticsActionEventCallback? callback) {
_platformDispatcher.onSemanticsActionEvent = callback;
}
@override
AccessibilityFeatures get accessibilityFeatures =>
_accessibilityFeaturesTestValue ?? _platformDispatcher.accessibilityFeatures;
AccessibilityFeatures? _accessibilityFeaturesTestValue;
/// Hides the real accessibility features and reports the given
/// [accessibilityFeaturesTestValue] instead.
///
/// Consider using [FakeAccessibilityFeatures] to provide specific
/// values for the various accessibility features under test.
// ignore: avoid_setters_without_getters
set accessibilityFeaturesTestValue(AccessibilityFeatures accessibilityFeaturesTestValue) {
_accessibilityFeaturesTestValue = accessibilityFeaturesTestValue;
onAccessibilityFeaturesChanged?.call();
}
/// Deletes any existing test accessibility features and returns to using the
/// real accessibility features.
void clearAccessibilityFeaturesTestValue() {
_accessibilityFeaturesTestValue = null;
onAccessibilityFeaturesChanged?.call();
}
@override
VoidCallback? get onAccessibilityFeaturesChanged =>
_platformDispatcher.onAccessibilityFeaturesChanged;
@override
set onAccessibilityFeaturesChanged(VoidCallback? callback) {
_platformDispatcher.onAccessibilityFeaturesChanged = callback;
}
@override
void setIsolateDebugName(String name) {
_platformDispatcher.setIsolateDebugName(name);
}
@override
void sendPlatformMessage(String name, ByteData? data, PlatformMessageResponseCallback? callback) {
_platformDispatcher.sendPlatformMessage(name, data, callback);
}
/// Delete any test value properties that have been set on this [TestPlatformDispatcher]
/// and return to reporting the real [PlatformDispatcher] values for all
/// [PlatformDispatcher] properties.
///
/// If desired, clearing of properties can be done on an individual basis,
/// e.g., [clearLocaleTestValue].
void clearAllTestValues() {
clearAccessibilityFeaturesTestValue();
clearAlwaysUse24HourTestValue();
clearDefaultRouteNameTestValue();
clearPlatformBrightnessTestValue();
clearLocaleTestValue();
clearLocalesTestValue();
clearSemanticsEnabledTestValue();
clearTextScaleFactorTestValue();
clearNativeSpellCheckServiceDefined();
resetBrieflyShowPassword();
resetSupportsShowingSystemContextMenu();
resetInitialLifecycleState();
resetSystemFontFamily();
}
@override
VoidCallback? get onFrameDataChanged => _platformDispatcher.onFrameDataChanged;
@override
set onFrameDataChanged(VoidCallback? value) {
_platformDispatcher.onFrameDataChanged = value;
}
@override
KeyDataCallback? get onKeyData => _platformDispatcher.onKeyData;
@override
set onKeyData(KeyDataCallback? onKeyData) {
_platformDispatcher.onKeyData = onKeyData;
}
@override
VoidCallback? get onPlatformConfigurationChanged =>
_platformDispatcher.onPlatformConfigurationChanged;
@override
set onPlatformConfigurationChanged(VoidCallback? onPlatformConfigurationChanged) {
_platformDispatcher.onPlatformConfigurationChanged = onPlatformConfigurationChanged;
}
@override
Locale? computePlatformResolvedLocale(List<Locale> supportedLocales) =>
_platformDispatcher.computePlatformResolvedLocale(supportedLocales);
@override
ByteData? getPersistentIsolateData() => _platformDispatcher.getPersistentIsolateData();
@override
Iterable<TestFlutterView> get views => _testViews.values;
@override
FlutterView? view({required int id}) => _testViews[id];
@override
Iterable<TestDisplay> get displays => _testDisplays.values;
void _updateViewsAndDisplays() {
final List<Object> extraDisplayKeys = <Object>[..._testDisplays.keys];
for (final Display display in _platformDispatcher.displays) {
extraDisplayKeys.remove(display.id);
if (!_testDisplays.containsKey(display.id)) {
_testDisplays[display.id] = TestDisplay(this, display);
}
}
extraDisplayKeys.forEach(_testDisplays.remove);
final List<Object> extraViewKeys = <Object>[..._testViews.keys];
for (final FlutterView view in _platformDispatcher.views) {
// TODO(pdblasi-google): Remove this try-catch once the Display API is stable and supported on all platforms
late final TestDisplay display;
try {
final Display realDisplay = view.display;
if (_testDisplays.containsKey(realDisplay.id)) {
display = _testDisplays[view.display.id]!;
} else {
display = _UnsupportedDisplay(
this,
view,
'PlatformDispatcher did not contain a Display with id ${realDisplay.id}, '
'which was expected by FlutterView ($view)',
);
}
} catch (error) {
display = _UnsupportedDisplay(this, view, error);
}
extraViewKeys.remove(view.viewId);
if (!_testViews.containsKey(view.viewId)) {
_testViews[view.viewId] = TestFlutterView(
view: view,
platformDispatcher: this,
display: display,
);
}
}
extraViewKeys.forEach(_testViews.remove);
}
@override
ErrorCallback? get onError => _platformDispatcher.onError;
@override
set onError(ErrorCallback? value) {
_platformDispatcher.onError;
}
@override
VoidCallback? get onSystemFontFamilyChanged => _platformDispatcher.onSystemFontFamilyChanged;
@override
set onSystemFontFamilyChanged(VoidCallback? value) {
_platformDispatcher.onSystemFontFamilyChanged = value;
}
@override
FrameData get frameData => _platformDispatcher.frameData;
@override
void registerBackgroundIsolate(RootIsolateToken token) {
_platformDispatcher.registerBackgroundIsolate(token);
}
@override
void requestDartPerformanceMode(DartPerformanceMode mode) {
_platformDispatcher.requestDartPerformanceMode(mode);
}
/// The system font family to use for this test.
///
/// Defaults to the value provided by [PlatformDispatcher.systemFontFamily].
/// This can only be set in a test environment to emulate different platform
/// configurations. A standard [PlatformDispatcher] is not mutable from the
/// framework.
///
/// Setting this value to `null` will force [systemFontFamily] to return
/// `null`. If you want to have the value default to the platform
/// [systemFontFamily], use [resetSystemFontFamily].
///
/// See also:
///
/// * [PlatformDispatcher.systemFontFamily] for the standard implementation
/// * [resetSystemFontFamily] to reset this value specifically
/// * [clearAllTestValues] to reset all test values for this view
@override
String? get systemFontFamily {
return _forceSystemFontFamilyToBeNull
? null
: _systemFontFamily ?? _platformDispatcher.systemFontFamily;
}
String? _systemFontFamily;
bool _forceSystemFontFamilyToBeNull = false;
set systemFontFamily(String? value) {
_systemFontFamily = value;
if (value == null) {
_forceSystemFontFamilyToBeNull = true;
}
onSystemFontFamilyChanged?.call();
}
/// Resets [systemFontFamily] to the default for the platform.
void resetSystemFontFamily() {
_systemFontFamily = null;
_forceSystemFontFamilyToBeNull = false;
onSystemFontFamilyChanged?.call();
}
@override
void updateSemantics(SemanticsUpdate update) {
_platformDispatcher.updateSemantics(update);
}
/// This gives us some grace time when the dart:ui side adds something to
/// [PlatformDispatcher], and makes things easier when we do rolls to give
/// us time to catch up.
@override
dynamic noSuchMethod(Invocation invocation) {
return null;
}
}
/// A [FlutterView] that wraps another [FlutterView] and allows faking of
/// some properties for testing purposes.
///
/// This class should not be instantiated manually, as
/// it requires a backing [FlutterView] that must be produced from
/// a [PlatformDispatcher].
///
/// See also:
///
/// * [WidgetTester.view] which allows for accessing the [TestFlutterView]
/// for single view applications or widget testing.
/// * [WidgetTester.viewOf] which allows for accessing the appropriate
/// [TestFlutterView] in a given situation for multi-view applications.
/// * [TestPlatformDispatcher], which allows for faking of platform specific
/// functionality.
class TestFlutterView implements FlutterView {
/// Constructs a [TestFlutterView] that defers all behavior to the given
/// [FlutterView] unless explicitly overridden for testing.
TestFlutterView({
required FlutterView view,
required TestPlatformDispatcher platformDispatcher,
required TestDisplay display,
}) : _view = view,
_platformDispatcher = platformDispatcher,
_display = display;
/// The [FlutterView] backing this [TestFlutterView].
final FlutterView _view;
@override
TestPlatformDispatcher get platformDispatcher => _platformDispatcher;
final TestPlatformDispatcher _platformDispatcher;
@override
TestDisplay get display => _display;
final TestDisplay _display;
@override
int get viewId => _view.viewId;
/// The device pixel ratio to use for this test.
///
/// Defaults to the value provided by [FlutterView.devicePixelRatio]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FlutterView.devicePixelRatio] for the standard implementation
/// * [TestDisplay.devicePixelRatio] which will stay in sync with this value
/// * [resetDevicePixelRatio] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
double get devicePixelRatio => _display._devicePixelRatio ?? _view.devicePixelRatio;
set devicePixelRatio(double value) {
_display.devicePixelRatio = value;
}
/// Resets [devicePixelRatio] for this test view to the default value for this view.
///
/// This will also reset the [devicePixelRatio] for the [TestDisplay]
/// that is related to this view.
void resetDevicePixelRatio() {
_display.resetDevicePixelRatio();
}
/// The display features to use for this test.
///
/// Defaults to the value provided by [FlutterView.displayFeatures]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FlutterView.displayFeatures] for the standard implementation
/// * [resetDisplayFeatures] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
List<DisplayFeature> get displayFeatures => _displayFeatures ?? _view.displayFeatures;
List<DisplayFeature>? _displayFeatures;
set displayFeatures(List<DisplayFeature> value) {
_displayFeatures = value;
platformDispatcher.onMetricsChanged?.call();
}
/// Resets [displayFeatures] to the default values for this view.
void resetDisplayFeatures() {
_displayFeatures = null;
platformDispatcher.onMetricsChanged?.call();
}
/// The padding to use for this test.
///
/// Defaults to the value provided by [FlutterView.padding]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FakeViewPadding] which is used to set this value for testing
/// * [FlutterView.padding] for the standard implementation.
/// * [resetPadding] to reset this value specifically.
/// * [reset] to reset all test values for this view.
@override
FakeViewPadding get padding => _padding ?? FakeViewPadding._wrap(_view.padding);
FakeViewPadding? _padding;
set padding(FakeViewPadding value) {
_padding = value;
platformDispatcher.onMetricsChanged?.call();
}
/// Resets [padding] to the default value for this view.
void resetPadding() {
_padding = null;
platformDispatcher.onMetricsChanged?.call();
}
/// The physical size to use for this test.
///
/// Defaults to the value provided by [FlutterView.physicalSize]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// Setting this value also sets [physicalConstraints] to tight constraints
/// based on the given size.
///
/// See also:
///
/// * [FlutterView.physicalSize] for the standard implementation
/// * [resetPhysicalSize] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
Size get physicalSize => _physicalSize ?? _view.physicalSize;
Size? _physicalSize;
set physicalSize(Size value) {
_physicalSize = value;
// For backwards compatibility the constraints are set based on the provided size.
physicalConstraints = ViewConstraints.tight(value);
}
/// Resets [physicalSize] (and implicitly also the [physicalConstraints]) to
/// the default value for this view.
void resetPhysicalSize() {
_physicalSize = null;
resetPhysicalConstraints();
}
/// The physical constraints to use for this test.
///
/// Defaults to the value provided by [FlutterView.physicalConstraints]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FlutterView.physicalConstraints] for the standard implementation
/// * [physicalConstraints] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
ViewConstraints get physicalConstraints => _physicalConstraints ?? _view.physicalConstraints;
ViewConstraints? _physicalConstraints;
set physicalConstraints(ViewConstraints value) {
_physicalConstraints = value;
platformDispatcher.onMetricsChanged?.call();
}
/// Resets [physicalConstraints] to the default value for this view.
void resetPhysicalConstraints() {
_physicalConstraints = null;
platformDispatcher.onMetricsChanged?.call();
}
/// The system gesture insets to use for this test.
///
/// Defaults to the value provided by [FlutterView.systemGestureInsets].
/// This can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FakeViewPadding] which is used to set this value for testing
/// * [FlutterView.systemGestureInsets] for the standard implementation
/// * [resetSystemGestureInsets] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
FakeViewPadding get systemGestureInsets =>
_systemGestureInsets ?? FakeViewPadding._wrap(_view.systemGestureInsets);
FakeViewPadding? _systemGestureInsets;
set systemGestureInsets(FakeViewPadding value) {
_systemGestureInsets = value;
platformDispatcher.onMetricsChanged?.call();
}
/// Resets [systemGestureInsets] to the default value for this view.
void resetSystemGestureInsets() {
_systemGestureInsets = null;
platformDispatcher.onMetricsChanged?.call();
}
/// The view insets to use for this test.
///
/// Defaults to the value provided by [FlutterView.viewInsets]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FakeViewPadding] which is used to set this value for testing
/// * [FlutterView.viewInsets] for the standard implementation
/// * [resetViewInsets] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
FakeViewPadding get viewInsets => _viewInsets ?? FakeViewPadding._wrap(_view.viewInsets);
FakeViewPadding? _viewInsets;
set viewInsets(FakeViewPadding value) {
_viewInsets = value;
platformDispatcher.onMetricsChanged?.call();
}
/// Resets [viewInsets] to the default value for this view.
void resetViewInsets() {
_viewInsets = null;
platformDispatcher.onMetricsChanged?.call();
}
/// The view padding to use for this test.
///
/// Defaults to the value provided by [FlutterView.viewPadding]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FakeViewPadding] which is used to set this value for testing
/// * [FlutterView.viewPadding] for the standard implementation
/// * [resetViewPadding] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
FakeViewPadding get viewPadding => _viewPadding ?? FakeViewPadding._wrap(_view.viewPadding);
FakeViewPadding? _viewPadding;
set viewPadding(FakeViewPadding value) {
_viewPadding = value;
platformDispatcher.onMetricsChanged?.call();
}
/// Resets [viewPadding] to the default value for this view.
void resetViewPadding() {
_viewPadding = null;
platformDispatcher.onMetricsChanged?.call();
}
/// The gesture settings to use for this test.
///
/// Defaults to the value provided by [FlutterView.gestureSettings]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FlutterView.gestureSettings] for the standard implementation
/// * [resetGestureSettings] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
GestureSettings get gestureSettings => _gestureSettings ?? _view.gestureSettings;
GestureSettings? _gestureSettings;
set gestureSettings(GestureSettings value) {
_gestureSettings = value;