-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Expand file tree
/
Copy pathbinding.dart
More file actions
3103 lines (2803 loc) · 111 KB
/
binding.dart
File metadata and controls
3103 lines (2803 loc) · 111 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
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.
// ignore_for_file: invalid_use_of_internal_member
// ignore_for_file: implementation_imports
/// @docImport 'dart:io';
///
/// @docImport 'controller.dart';
/// @docImport 'test_pointer.dart';
/// @docImport 'widget_tester.dart';
library;
import 'dart:async';
import 'dart:ui' as ui;
import 'package:clock/clock.dart';
import 'package:fake_async/fake_async.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/foundation/_features.dart' show isWindowingEnabled;
import 'package:flutter/src/widgets/_window.dart';
import 'package:flutter/src/widgets/_window_positioner.dart';
import 'package:flutter/widgets.dart';
import 'package:matcher/expect.dart' show fail;
import 'package:stack_trace/stack_trace.dart' as stack_trace;
import 'package:test_api/scaffolding.dart' as test_package show Timeout;
import 'package:vector_math/vector_math_64.dart';
import '_binding_io.dart' if (dart.library.js_interop) '_binding_web.dart' as binding;
import 'goldens.dart';
import 'platform.dart';
import 'restoration.dart';
import 'stack_manipulation.dart';
import 'test_async_utils.dart';
import 'test_default_binary_messenger.dart';
import 'test_exception_reporter.dart';
import 'test_text_input.dart';
import 'window.dart';
/// Phases that can be reached by [WidgetTester.pumpWidget] and
/// [TestWidgetsFlutterBinding.pump].
///
/// See [WidgetsBinding.drawFrame] for a more detailed description of some of
/// these phases.
enum EnginePhase {
/// The build phase in the widgets library. See [BuildOwner.buildScope].
build,
/// The layout phase in the rendering library. See [PipelineOwner.flushLayout].
layout,
/// The compositing bits update phase in the rendering library. See
/// [PipelineOwner.flushCompositingBits].
compositingBits,
/// The paint phase in the rendering library. See [PipelineOwner.flushPaint].
paint,
/// The compositing phase in the rendering library. See
/// [RenderView.compositeFrame]. This is the phase in which data is sent to
/// the GPU. If semantics are not enabled, then this is the last phase.
composite,
/// The semantics building phase in the rendering library. See
/// [PipelineOwner.flushSemantics].
flushSemantics,
/// The final phase in the rendering library, wherein semantics information is
/// sent to the embedder. See [SemanticsOwner.sendSemanticsUpdate].
sendSemanticsUpdate,
}
/// Signature of callbacks used to intercept messages on a given channel.
///
/// See [TestDefaultBinaryMessenger.setMockDecodedMessageHandler] for more details.
typedef _MockMessageHandler = Future<void> Function(Object?);
/// Parts of the system that can generate pointer events that reach the test
/// binding.
///
/// This is used to identify how to handle events in the
/// [LiveTestWidgetsFlutterBinding]. See
/// [TestWidgetsFlutterBinding.dispatchEvent].
enum TestBindingEventSource {
/// The pointer event came from the test framework itself, e.g. from a
/// [TestGesture] created by [WidgetTester.startGesture].
test,
/// The pointer event came from the system, presumably as a result of the user
/// interactive directly with the device while the test was running.
device,
}
const Size _kDefaultTestViewportSize = Size(800.0, 600.0);
/// Overrides the [ServicesBinding]'s binary messenger logic to use
/// [TestDefaultBinaryMessenger].
///
/// Test bindings that are used by tests that mock message handlers for plugins
/// should mix in this binding to enable the use of the
/// [TestDefaultBinaryMessenger] APIs.
mixin TestDefaultBinaryMessengerBinding on BindingBase, ServicesBinding {
@override
void initInstances() {
super.initInstances();
_instance = this;
}
/// The current [TestDefaultBinaryMessengerBinding], if one has been created.
static TestDefaultBinaryMessengerBinding get instance => BindingBase.checkInstance(_instance);
static TestDefaultBinaryMessengerBinding? _instance;
@override
TestDefaultBinaryMessenger get defaultBinaryMessenger =>
super.defaultBinaryMessenger as TestDefaultBinaryMessenger;
@override
TestDefaultBinaryMessenger createBinaryMessenger() {
Future<ByteData?> keyboardHandler(ByteData? message) async {
return const StandardMethodCodec().encodeSuccessEnvelope(<int, int>{});
}
return TestDefaultBinaryMessenger(
super.createBinaryMessenger(),
outboundHandlers: <String, MessageHandler>{'flutter/keyboard': keyboardHandler},
);
}
}
/// Accessibility announcement data passed to [SemanticsService.sendAnnouncement] captured in a test.
///
/// This class is intended to be used by the testing API to store the announcements
/// in a structured form so that tests can verify announcement details. The fields
/// of this class correspond to parameters of the [SemanticsService.sendAnnouncement] method.
///
/// See also:
///
/// * [WidgetTester.takeAnnouncements], which is the test API that uses this class.
class CapturedAccessibilityAnnouncement {
const CapturedAccessibilityAnnouncement._(
this.message,
this.viewId,
this.textDirection,
this.assertiveness,
);
/// The accessibility message announced by the framework.
final String message;
/// The ID of the view that the announcement was sent to.
final int viewId;
/// The direction in which the text of the [message] flows.
final TextDirection textDirection;
/// Determines the assertiveness level of the accessibility announcement.
final Assertiveness assertiveness;
}
class _TestFlutterView implements FlutterView {
_TestFlutterView({
required this.controller,
required TestPlatformDispatcher platformDispatcher,
this.constraints,
this.onRender,
}) : _platformDispatcher = platformDispatcher,
_viewId = _nextViewId++ {
platformDispatcher.addTestView(this);
}
static int _nextViewId = 1;
final BaseWindowController controller;
final TestPlatformDispatcher _platformDispatcher;
final BoxConstraints? constraints;
final int _viewId;
final void Function(Size? size)? onRender;
@override
double get devicePixelRatio => display.devicePixelRatio;
@override
ui.Display get display => platformDispatcher.displays.first;
@override
List<ui.DisplayFeature> get displayFeatures => List<ui.DisplayFeature>.empty();
@override
ui.GestureSettings get gestureSettings => const ui.GestureSettings();
@override
ui.ViewPadding get padding => ui.ViewPadding.zero;
@override
ui.ViewConstraints get physicalConstraints => constraints != null
? ui.ViewConstraints(
minWidth: constraints!.minWidth,
maxWidth: constraints!.maxWidth,
minHeight: constraints!.minHeight,
maxHeight: constraints!.maxHeight,
)
: ui.ViewConstraints.tight(physicalSize);
@override
ui.Size get physicalSize => controller.contentSize * devicePixelRatio;
@override
ui.PlatformDispatcher get platformDispatcher => _platformDispatcher;
@override
ui.ViewPadding get systemGestureInsets => ui.ViewPadding.zero;
@override
int get viewId => _viewId;
@override
ui.ViewPadding get viewInsets => ui.ViewPadding.zero;
@override
ui.ViewPadding get viewPadding => ui.ViewPadding.zero;
@override
ui.DisplayCornerRadii? get displayCornerRadii => null;
@override
void render(ui.Scene scene, {ui.Size? size}) {
onRender?.call(size);
}
@override
void updateSemantics(ui.SemanticsUpdate update) {}
}
mixin _ChildWindowHierarchyMixin {
final List<BaseWindowController> _children = <BaseWindowController>[];
/// Tracks a child window controller.
void addChild(BaseWindowController child) {
_children.add(child);
}
/// Stops tracking a child window controller.
void removeChild(BaseWindowController child) {
_children.remove(child);
}
/// Removes and destroys all child window controllers.
void removeAllChildren() {
for (final BaseWindowController child in _children) {
child.destroy();
}
_children.clear();
}
/// Returns the first activateable window in this window's hierarchy.
BaseWindowController getFirstActivatableChild() {
// If there are no children, this window is the first activateable window.
if (_children.isEmpty) {
return this as BaseWindowController;
}
// Otherwise, traverse the children to find the first activateable window.
//
// Modal dialogs have focus precedence over popups.
// Popups have focus precedence over regular windows.
// Regular windows have the lowest precedence.
// Tooltips cannot be activated at all, so they are skipped.
var activateable = this as BaseWindowController;
var foundPopup = false;
for (final BaseWindowController child in _children) {
switch (child) {
case final RegularWindowController regularChild:
if (foundPopup) {
// Already found a popup, skip anything else.
break;
}
activateable = (regularChild as _TestRegularWindowController).getFirstActivatableChild();
case final DialogWindowController dialogChild:
// Always return the first dialog found.
return (dialogChild as _TestDialogWindowController).getFirstActivatableChild();
case final TooltipWindowController _: // Tooltips cannot be activated.
break;
case final PopupWindowController popupChild:
if (foundPopup) {
// Already found a popup, skip anything else.
break;
}
activateable = (popupChild as _TestPopupWindowController).getFirstActivatableChild();
foundPopup = true;
}
}
return activateable;
}
}
class _TestRegularWindowController extends RegularWindowController with _ChildWindowHierarchyMixin {
_TestRegularWindowController({
required RegularWindowControllerDelegate delegate,
required TestPlatformDispatcher platformDispatcher,
required this.windowingOwner,
Size? preferredSize,
BoxConstraints? preferredConstraints,
String? title,
}) : _delegate = delegate,
_size = preferredSize ?? const Size(800, 600),
_constraints = preferredConstraints ?? BoxConstraints.loose(const Size(1920, 1080)),
_title = title ?? 'Test Window',
super.empty() {
_constrainToBounds();
rootView = _TestFlutterView(
controller: this,
platformDispatcher: platformDispatcher,
constraints: _constraints,
);
// Automatically activate the window when created.
activate();
}
final RegularWindowControllerDelegate _delegate;
final _TestWindowingOwner windowingOwner;
Size _size;
BoxConstraints _constraints;
String _title;
bool _isMaximized = false;
bool _isMinimized = false;
bool _isFullscreen = false;
@override
Size get contentSize => isFullscreen || isMaximized ? rootView.display.size : _size;
@override
String get title => _title;
@override
bool get isActivated => windowingOwner.isWindowControllerActive(this);
@override
bool get isMaximized => _isMaximized;
@override
bool get isMinimized => _isMinimized;
@override
bool get isFullscreen => _isFullscreen;
@override
void setSize(Size size) {
_size = size;
_constrainToBounds();
notifyListeners();
}
@override
void setConstraints(BoxConstraints constraints) {
_constraints = constraints;
_constrainToBounds();
notifyListeners();
}
@override
void setTitle(String title) {
_title = title;
notifyListeners();
}
@override
void activate() {
final BaseWindowController activated = windowingOwner.activateWindowController(this);
activated.notifyListeners();
}
@override
void setMaximized(bool maximized) {
_isMaximized = maximized;
if (_isMaximized) {
_isMinimized = false;
_isFullscreen = false;
}
notifyListeners();
}
@override
void setMinimized(bool minimized) {
_isMinimized = minimized;
if (_isMinimized) {
windowingOwner.deactivateWindowController(this);
}
notifyListeners();
}
@override
void setFullscreen(bool fullscreen, {ui.Display? display}) {
_isFullscreen = fullscreen;
if (_isFullscreen) {
_isMaximized = false;
_isMinimized = false;
}
notifyListeners();
}
void _constrainToBounds() {
final double width = _constraints.constrainWidth(_size.width);
final double height = _constraints.constrainHeight(_size.height);
_size = Size(width, height);
}
@override
void destroy() {
_delegate.onWindowDestroyed();
removeAllChildren();
windowingOwner.deactivateWindowController(this);
}
}
void _addChildToParent(BaseWindowController? parent, BaseWindowController child) {
if (parent != null) {
switch (parent) {
case final DialogWindowController testParent:
(testParent as _TestDialogWindowController).addChild(child);
case final RegularWindowController testParent:
(testParent as _TestRegularWindowController).addChild(child);
case final PopupWindowController testParent:
(testParent as _TestPopupWindowController).addChild(child);
case TooltipWindowController _:
fail('TooltipWindowController cannot be a parent of another window controller.');
}
}
}
void _removeChildFromParent(BaseWindowController? parent, BaseWindowController child) {
if (parent != null) {
switch (parent) {
case final DialogWindowController testParent:
(testParent as _TestDialogWindowController).removeChild(child);
case final RegularWindowController testParent:
(testParent as _TestRegularWindowController).removeChild(child);
case final PopupWindowController testParent:
(testParent as _TestPopupWindowController).removeChild(child);
case TooltipWindowController _:
fail('TooltipWindowController cannot be a parent of another window controller.');
}
}
}
class _TestDialogWindowController extends DialogWindowController with _ChildWindowHierarchyMixin {
_TestDialogWindowController({
required DialogWindowControllerDelegate delegate,
required TestPlatformDispatcher platformDispatcher,
required this.windowingOwner,
BaseWindowController? parent,
Size? preferredSize,
BoxConstraints? preferredConstraints,
String? title,
}) : _delegate = delegate,
_parent = parent,
_size = preferredSize ?? const Size(800, 600),
_constraints = preferredConstraints ?? BoxConstraints.loose(const Size(1920, 1080)),
_title = title ?? 'Test Window',
super.empty() {
_constrainToBounds();
rootView = _TestFlutterView(
controller: this,
platformDispatcher: platformDispatcher,
constraints: _constraints,
);
_addChildToParent(parent, this);
// Automatically activate the window when created.
activate();
}
final DialogWindowControllerDelegate _delegate;
final BaseWindowController? _parent;
final _TestWindowingOwner windowingOwner;
Size _size;
BoxConstraints _constraints;
String _title;
bool _isMinimized = false;
@override
Size get contentSize => _size;
@override
BaseWindowController? get parent => _parent;
@override
String get title => _title;
@override
bool get isActivated => windowingOwner.isWindowControllerActive(this);
@override
bool get isMinimized => _isMinimized;
@override
void setSize(Size size) {
_size = size;
_constrainToBounds();
notifyListeners();
}
@override
void setConstraints(BoxConstraints constraints) {
_constraints = constraints;
_constrainToBounds();
notifyListeners();
}
@override
void setTitle(String title) {
_title = title;
notifyListeners();
}
@override
void activate() {
final BaseWindowController activated = windowingOwner.activateWindowController(this);
activated.notifyListeners();
}
@override
void setMinimized(bool minimized) {
if (_parent != null && minimized) {
fail('Cannot minimize a modal dialog window.');
}
_isMinimized = minimized;
if (_isMinimized) {
windowingOwner.deactivateWindowController(this);
}
notifyListeners();
}
void _constrainToBounds() {
final double width = _constraints.constrainWidth(_size.width);
final double height = _constraints.constrainHeight(_size.height);
_size = Size(width, height);
}
@override
void destroy() {
_delegate.onWindowDestroyed();
removeAllChildren();
windowingOwner.deactivateWindowController(this);
_removeChildFromParent(_parent, this);
}
}
class _TestTooltipWindowController extends TooltipWindowController with _ChildWindowHierarchyMixin {
_TestTooltipWindowController({
required TooltipWindowControllerDelegate delegate,
required TestPlatformDispatcher platformDispatcher,
required this.windowingOwner,
required BoxConstraints preferredConstraints,
required bool isSizedToContent,
required ui.Rect anchorRect,
required WindowPositioner positioner,
required BaseWindowController parent,
}) : _delegate = delegate,
_constraints = preferredConstraints,
_isSizedToContent = isSizedToContent,
_anchorRect = anchorRect,
_positioner = positioner,
_parent = parent,
super.empty() {
rootView = _TestFlutterView(
controller: this,
platformDispatcher: platformDispatcher,
constraints: _constraints,
onRender: (size) {
if (_isSizedToContent && _lastRenderedSize != size) {
_lastRenderedSize = size;
scheduleMicrotask(() {
notifyListeners();
});
}
},
);
_addChildToParent(parent, this);
}
final TooltipWindowControllerDelegate _delegate;
final _TestWindowingOwner windowingOwner;
BoxConstraints _constraints;
ui.Rect _anchorRect;
WindowPositioner _positioner;
final BaseWindowController _parent;
final bool _isSizedToContent;
Size? _lastRenderedSize;
@override
Size get contentSize =>
_isSizedToContent && _lastRenderedSize != null ? _lastRenderedSize! : _constraints.biggest;
@override
BaseWindowController get parent => _parent;
@override
void setConstraints(BoxConstraints constraints) {
_constraints = constraints;
notifyListeners();
}
@override
void updatePosition({Rect? anchorRect, WindowPositioner? positioner}) {
_anchorRect = anchorRect ?? _anchorRect;
_positioner = positioner ?? _positioner;
}
@override
void destroy() {
_delegate.onWindowDestroyed();
removeAllChildren();
windowingOwner.deactivateWindowController(this);
_removeChildFromParent(parent, this);
}
}
class _TestPopupWindowController extends PopupWindowController with _ChildWindowHierarchyMixin {
_TestPopupWindowController({
required PopupWindowControllerDelegate delegate,
required TestPlatformDispatcher platformDispatcher,
required this.windowingOwner,
required BoxConstraints preferredConstraints,
required ui.Rect anchorRect,
required WindowPositioner positioner,
required BaseWindowController parent,
}) : _delegate = delegate,
_constraints = preferredConstraints,
_anchorRect = anchorRect,
_positioner = positioner,
_parent = parent,
super.empty() {
rootView = _TestFlutterView(
controller: this,
platformDispatcher: platformDispatcher,
constraints: _constraints,
);
_addChildToParent(parent, this);
// Automatically activate the window when created.
activate();
}
final PopupWindowControllerDelegate _delegate;
final _TestWindowingOwner windowingOwner;
BoxConstraints _constraints;
// ignore: unused_field
final ui.Rect _anchorRect;
// ignore: unused_field
final WindowPositioner _positioner;
final BaseWindowController _parent;
@override
Size get contentSize => _constraints.biggest;
@override
BaseWindowController get parent => _parent;
@override
bool get isActivated => windowingOwner.isWindowControllerActive(this);
@override
void activate() {
final BaseWindowController activated = windowingOwner.activateWindowController(this);
activated.notifyListeners();
}
@override
void setConstraints(BoxConstraints constraints) {
_constraints = constraints;
notifyListeners();
}
@override
void destroy() {
_delegate.onWindowDestroyed();
removeAllChildren();
windowingOwner.deactivateWindowController(this);
_removeChildFromParent(parent, this);
}
}
/// A [WindowingOwner] used for tests.
///
/// This windowing owner will behave as a perfect windowing system, with no
/// delays or failures.
///
/// See also:
/// * [TestWidgetsFlutterBinding], which uses this class to create window controllers
/// for tests.
/// * [WindowingOwner], the base class.
class _TestWindowingOwner extends WindowingOwner {
_TestWindowingOwner({required TestPlatformDispatcher platformDispatcher})
: _platformDispatcher = platformDispatcher;
final TestPlatformDispatcher _platformDispatcher;
BaseWindowController? _activeWindowController;
/// Activates the given [controller].
///
/// If the controller has children, the first activateable window in its hierarchy
/// will be activated.
///
/// Tooltips cannot be activated, so if a [TooltipWindowController] is passed in,
/// this method will throw an error.
///
/// Returns the activated [BaseWindowController].
BaseWindowController activateWindowController(BaseWindowController controller) {
switch (controller) {
case final RegularWindowController regularController:
final BaseWindowController leaf = (regularController as _TestRegularWindowController)
.getFirstActivatableChild();
_activeWindowController = leaf;
return _activeWindowController!;
case final DialogWindowController dialogController:
final BaseWindowController leaf = (dialogController as _TestDialogWindowController)
.getFirstActivatableChild();
_activeWindowController = leaf;
return _activeWindowController!;
case final TooltipWindowController _:
fail('Tooltips cannot be activated. Activate the parent window instead.');
case final PopupWindowController _:
final BaseWindowController leaf = (controller as _TestPopupWindowController)
.getFirstActivatableChild();
_activeWindowController = leaf;
return _activeWindowController!;
}
}
bool _tryActivateParent(BaseWindowController? parent) {
if (parent == null) {
return false;
}
switch (parent) {
case final RegularWindowController regularParent:
regularParent.activate();
case final DialogWindowController dialogParent:
dialogParent.activate();
case final TooltipWindowController _:
fail('TooltipWindowController cannot be a parent of DialogWindowController.');
case final PopupWindowController popupParent:
popupParent.activate();
}
return true;
}
/// Deactivates the given [controller] if it is currently active.
///
/// If the controller is not currently active, this method does nothing.
///
/// If the controller is a [DialogWindowController] with a parent, the parent
/// will be activated upon deactivation of the dialog.
///
/// If the controller is a [TooltipWindowController], this method will throw
/// an error, as tooltips cannot be deactivated because they cannot be activated.
void deactivateWindowController(BaseWindowController controller) {
if (_activeWindowController != controller) {
return;
}
switch (controller) {
case final RegularWindowController _:
_activeWindowController = null;
case final DialogWindowController dialogController:
if (!_tryActivateParent(dialogController.parent)) {
_activeWindowController = null;
}
case final TooltipWindowController tooltipController:
fail(
'Tooltips cannot be deactivated. Deactivate the parent window instead: '
'${tooltipController.parent}.',
);
case final PopupWindowController popupController:
if (!_tryActivateParent(popupController.parent)) {
_activeWindowController = null;
}
}
}
/// Returns whether the given [controller] is the currently active window.
bool isWindowControllerActive(BaseWindowController controller) {
return _activeWindowController == controller;
}
@internal
@override
RegularWindowController createRegularWindowController({
required RegularWindowControllerDelegate delegate,
Size? preferredSize,
BoxConstraints? preferredConstraints,
String? title,
}) {
return _TestRegularWindowController(
delegate: delegate,
platformDispatcher: _platformDispatcher,
windowingOwner: this,
preferredSize: preferredSize,
preferredConstraints: preferredConstraints,
title: title,
);
}
@internal
@override
DialogWindowController createDialogWindowController({
required DialogWindowControllerDelegate delegate,
Size? preferredSize,
BoxConstraints? preferredConstraints,
BaseWindowController? parent,
String? title,
}) {
return _TestDialogWindowController(
delegate: delegate,
platformDispatcher: _platformDispatcher,
windowingOwner: this,
parent: parent,
preferredSize: preferredSize,
preferredConstraints: preferredConstraints,
title: title,
);
}
@override
TooltipWindowController createTooltipWindowController({
required TooltipWindowControllerDelegate delegate,
required BoxConstraints preferredConstraints,
required bool isSizedToContent,
required Rect anchorRect,
required WindowPositioner positioner,
required BaseWindowController parent,
}) {
return _TestTooltipWindowController(
delegate: delegate,
platformDispatcher: _platformDispatcher,
windowingOwner: this,
preferredConstraints: preferredConstraints,
isSizedToContent: isSizedToContent,
anchorRect: anchorRect,
positioner: positioner,
parent: parent,
);
}
@override
PopupWindowController createPopupWindowController({
required PopupWindowControllerDelegate delegate,
required BoxConstraints preferredConstraints,
required ui.Rect anchorRect,
required WindowPositioner positioner,
required BaseWindowController parent,
}) {
return _TestPopupWindowController(
delegate: delegate,
platformDispatcher: _platformDispatcher,
windowingOwner: this,
preferredConstraints: preferredConstraints,
anchorRect: anchorRect,
positioner: positioner,
parent: parent,
);
}
}
// Examples can assume:
// late TestWidgetsFlutterBinding binding;
// late Size someSize;
/// Base class for bindings used by widgets library tests.
///
/// The [ensureInitialized] method creates (if necessary) and returns an
/// instance of the appropriate subclass. (If one is already created, it returns
/// that one, even if it's not the one that it would normally create. This
/// allows tests to force the use of [LiveTestWidgetsFlutterBinding] even in a
/// normal unit test environment, e.g. to test that binding.)
///
/// When using these bindings, certain features are disabled. For
/// example, [timeDilation] is reset to 1.0 on initialization.
///
/// In non-browser tests, the binding overrides `HttpClient` creation with a
/// fake client that always returns a status code of 400. This is to prevent
/// tests from making network calls, which could introduce flakiness. A test
/// that actually needs to make a network call should provide its own
/// `HttpClient` to the code making the call, so that it can appropriately mock
/// or fake responses.
///
/// ### Coordinate spaces
///
/// [TestWidgetsFlutterBinding] might be run on devices of different screen
/// sizes, while the testing widget is still told the same size to ensure
/// consistent results. Consequently, code that deals with positions (such as
/// pointer events or painting) must distinguish between two coordinate spaces:
///
/// * The _local coordinate space_ is the one used by the testing widget
/// (typically an 800 by 600 window, but can be altered by [setSurfaceSize]).
/// * The _global coordinate space_ is the one used by the device.
///
/// Positions can be transformed between coordinate spaces with [localToGlobal]
/// and [globalToLocal].
abstract class TestWidgetsFlutterBinding extends BindingBase
with
SchedulerBinding,
ServicesBinding,
GestureBinding,
SemanticsBinding,
RendererBinding,
PaintingBinding,
WidgetsBinding,
TestDefaultBinaryMessengerBinding {
/// Constructor for [TestWidgetsFlutterBinding].
///
/// This constructor overrides the [debugPrint] global hook to point to
/// [debugPrintOverride], which can be overridden by subclasses.
TestWidgetsFlutterBinding()
: platformDispatcher = TestPlatformDispatcher(platformDispatcher: PlatformDispatcher.instance) {
platformDispatcher.defaultRouteNameTestValue = '/';
debugPrint = debugPrintOverride;
debugDisableShadows = disableShadows;
}
/// Deprecated. Will be removed in a future version of Flutter.
///
/// This property has been deprecated to prepare for Flutter's upcoming
/// support for multiple views and multiple windows.
///
/// This represents a combination of a [TestPlatformDispatcher] and a singular
/// [TestFlutterView]. Platform-specific test values can be set through
/// [WidgetTester.platformDispatcher] instead. When testing individual widgets
/// or applications using [WidgetTester.pumpWidget], view-specific test values
/// can be set through [WidgetTester.view]. If multiple views are defined, the
/// appropriate view can be found using [WidgetTester.viewOf] if a sub-view
/// is needed.
///
/// See also:
///
/// * [WidgetTester.platformDispatcher] for changing platform-specific values
/// for testing.
/// * [WidgetTester.view] and [WidgetTester.viewOf] for changing view-specific
/// values for testing.
/// * [BindingBase.window] for guidance dealing with this property outside of
/// a testing context.
@Deprecated(
'Use WidgetTester.platformDispatcher or WidgetTester.view instead. '
'Deprecated to prepare for the upcoming multi-window support. '
'This feature was deprecated after v3.9.0-0.1.pre.',
)
@override
late final TestWindow window;
@override
final TestPlatformDispatcher platformDispatcher;
@override
TestRestorationManager get restorationManager {
_restorationManager ??= createRestorationManager();
return _restorationManager!;
}
TestRestorationManager? _restorationManager;
/// Called by the test framework at the beginning of a widget test to
/// prepare the binding for the next test.
///
/// If [registerTestTextInput] returns true when this method is called,
/// the [testTextInput] is configured to simulate the keyboard.
void reset() {
_restorationManager?.dispose();
_restorationManager = null;
platformDispatcher.defaultRouteNameTestValue = '/';
resetGestureBinding();
testTextInput.reset();
if (registerTestTextInput) {
_testTextInput.register();
}
CustomSemanticsAction.resetForTests(); // ignore: invalid_use_of_visible_for_testing_member
_enableFocusManagerLifecycleAwarenessIfSupported();
}
void _enableFocusManagerLifecycleAwarenessIfSupported() {
if (buildOwner == null) {
return;
}
buildOwner!.focusManager
.listenToApplicationLifecycleChangesIfSupported(); // ignore: invalid_use_of_visible_for_testing_member
}
@override
TestRestorationManager createRestorationManager() {
return TestRestorationManager();
}
/// The value to set [debugPrint] to while tests are running.