Reland (3): Removes single window assumptions from flutter_test (#122422)
Reland (3): Removes single window assumptions from `flutter_test`
This commit is contained in:
@@ -182,17 +182,18 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
///
|
||||
/// This constructor overrides the [debugPrint] global hook to point to
|
||||
/// [debugPrintOverride], which can be overridden by subclasses.
|
||||
TestWidgetsFlutterBinding() : _window = TestWindow(window: ui.window) {
|
||||
TestWidgetsFlutterBinding() : platformDispatcher = TestPlatformDispatcher(
|
||||
platformDispatcher: PlatformDispatcher.instance,
|
||||
) {
|
||||
debugPrint = debugPrintOverride;
|
||||
debugDisableShadows = disableShadows;
|
||||
}
|
||||
|
||||
@override
|
||||
TestWindow get window => _window;
|
||||
final TestWindow _window;
|
||||
late final TestWindow window;
|
||||
|
||||
@override
|
||||
TestPlatformDispatcher get platformDispatcher => _window.platformDispatcher;
|
||||
final TestPlatformDispatcher platformDispatcher;
|
||||
|
||||
@override
|
||||
TestRestorationManager get restorationManager {
|
||||
@@ -346,6 +347,12 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
|
||||
@override
|
||||
void initInstances() {
|
||||
// This is intialized here because it's needed for the `super.initInstances`
|
||||
// call. It can't be handled as a ctor initializer because it's dependent
|
||||
// on `platformDispatcher`. It can't be handled in the ctor itself because
|
||||
// the base class ctor is called first and calls `initInstances`.
|
||||
window = TestWindow.fromPlatformDispatcher(platformDispatcher: platformDispatcher);
|
||||
|
||||
super.initInstances();
|
||||
_instance = this;
|
||||
timeDilation = 1.0; // just in case the developer has artificially changed it for development
|
||||
|
||||
@@ -14,6 +14,7 @@ import 'event_simulation.dart';
|
||||
import 'finders.dart';
|
||||
import 'test_async_utils.dart';
|
||||
import 'test_pointer.dart';
|
||||
import 'window.dart';
|
||||
|
||||
/// The default drag touch slop used to break up a large drag into multiple
|
||||
/// smaller moves.
|
||||
@@ -234,6 +235,37 @@ abstract class WidgetController {
|
||||
/// A reference to the current instance of the binding.
|
||||
final WidgetsBinding binding;
|
||||
|
||||
/// The [TestPlatformDispatcher] that is being used in this test.
|
||||
///
|
||||
/// This will be injected into the framework such that calls to
|
||||
/// [WidgetsBinding.platformDispatcher] will use this. This allows
|
||||
/// users to change platform specific properties for testing.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [TestFlutterView] which allows changing view specific properties
|
||||
/// for testing
|
||||
/// * [view] and [viewOf] which are used to find
|
||||
/// [TestFlutterView]s from the widget tree
|
||||
TestPlatformDispatcher get platformDispatcher => binding.platformDispatcher as TestPlatformDispatcher;
|
||||
|
||||
/// The [TestFlutterView] provided by default when testing with
|
||||
/// [WidgetTester.pumpWidget].
|
||||
///
|
||||
/// If the test requires multiple views, it will need to use [viewOf] instead
|
||||
/// to ensure that the view related to the widget being evaluated is the one
|
||||
/// that gets updated.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [viewOf], which can find a [TestFlutterView] related to a given finder.
|
||||
/// This is how to modify view properties for testing when dealing with
|
||||
/// multiple views.
|
||||
TestFlutterView get view {
|
||||
assert(platformDispatcher.views.length == 1, 'When testing with multiple views, use `viewOf` instead.');
|
||||
return platformDispatcher.views.single;
|
||||
}
|
||||
|
||||
/// Provides access to a [SemanticsController] for testing anything related to
|
||||
/// the [Semantics] tree.
|
||||
///
|
||||
@@ -257,6 +289,26 @@ abstract class WidgetController {
|
||||
// TODO(ianh): verify that the return values are of type T and throw
|
||||
// a good message otherwise, in all the generic methods below
|
||||
|
||||
/// Finds the [TestFlutterView] that is the closest ancestor of the widget
|
||||
/// found by [finder].
|
||||
///
|
||||
/// [TestFlutterView] can be used to modify view specific properties for testing.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [view] which returns the [TestFlutterView] used when only a single
|
||||
/// view is being used.
|
||||
TestFlutterView viewOf(Finder finder) {
|
||||
final View view = firstWidget<View>(
|
||||
find.ancestor(
|
||||
of: finder,
|
||||
matching: find.byType(View),
|
||||
)
|
||||
);
|
||||
|
||||
return view.view as TestFlutterView;
|
||||
}
|
||||
|
||||
/// Checks if `finder` exists in the tree.
|
||||
bool any(Finder finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user