Revert "Clean up the bindings APIs (#83843)" (#86386)

This reverts commit e2490f2906.
This commit is contained in:
Ian Hickson
2021-07-13 15:47:40 -07:00
committed by GitHub
parent e2490f2906
commit c800b9c3e4
248 changed files with 1265 additions and 1614 deletions

View File

@@ -16,13 +16,18 @@ import 'package:test_api/test_api.dart' as test_package;
import 'binding.dart';
import 'deprecated.dart';
/// Ensure the appropriate test binding is initialized.
TestWidgetsFlutterBinding ensureInitialized([@visibleForTesting Map<String, String>? environment]) {
/// Ensure the [WidgetsBinding] is initialized.
WidgetsBinding ensureInitialized([@visibleForTesting Map<String, String>? environment]) {
environment ??= Platform.environment;
if (environment.containsKey('FLUTTER_TEST') && environment['FLUTTER_TEST'] != 'false') {
return AutomatedTestWidgetsFlutterBinding.ensureInitialized();
if (WidgetsBinding.instance == null) {
if (environment.containsKey('FLUTTER_TEST') && environment['FLUTTER_TEST'] != 'false') {
AutomatedTestWidgetsFlutterBinding();
} else {
LiveTestWidgetsFlutterBinding();
}
}
return LiveTestWidgetsFlutterBinding.ensureInitialized();
assert(WidgetsBinding.instance is TestWidgetsFlutterBinding);
return WidgetsBinding.instance!;
}
/// Setup mocking of the global [HttpClient].
@@ -43,7 +48,7 @@ void mockFlutterAssets() {
/// platform messages.
SystemChannels.navigation.setMockMethodCallHandler((MethodCall methodCall) async {});
ServicesBinding.instance.defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData? message) async {
ServicesBinding.instance!.defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData? message) async {
assert(message != null);
String key = utf8.decode(message!.buffer.asUint8List());
File asset = File(path.join(assetFolderPath, key));

View File

@@ -6,9 +6,13 @@ import 'package:flutter/widgets.dart';
import 'binding.dart';
/// Ensure the appropriate test binding is initialized.
TestWidgetsFlutterBinding ensureInitialized([@visibleForTesting Map<String, String>? environment]) {
return AutomatedTestWidgetsFlutterBinding.ensureInitialized();
/// Ensure the [WidgetsBinding] is initialized.
WidgetsBinding ensureInitialized([@visibleForTesting Map<String, String>? environment]) {
if (WidgetsBinding.instance == null) {
AutomatedTestWidgetsFlutterBinding();
}
assert(WidgetsBinding.instance is TestWidgetsFlutterBinding);
return WidgetsBinding.instance!;
}
/// This method is a noop on the web.

View File

@@ -67,7 +67,7 @@ class MatchesGoldenFile extends AsyncMatcher {
final Uri testNameUri = goldenFileComparator.getTestUri(key, version);
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
return binding.runAsync<String?>(() async {
final ui.Image? image = await imageFuture;
if (image == null) {

View File

@@ -47,7 +47,7 @@ class MatchesGoldenFile extends AsyncMatcher {
final Element element = elements.single;
final RenderObject renderObject = _findRepaintBoundary(element);
final Size size = renderObject.paintBounds.size;
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
final Element e = binding.renderViewElement!;
// Unlike `flutter_tester`, we don't have the ability to render an element

View File

@@ -407,7 +407,7 @@ class _RenderPostFrameCallbacker extends RenderProxyBox {
@override
void paint(PaintingContext context, Offset offset) {
if (callback != null) {
SchedulerBinding.instance.addPostFrameCallback((Duration duration) {
SchedulerBinding.instance!.addPostFrameCallback((Duration duration) {
callback!(duration);
markNeedsPaint();
});

View File

@@ -107,11 +107,8 @@ mixin TestDefaultBinaryMessengerBinding on BindingBase, ServicesBinding {
/// 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.)
/// The [ensureInitialized] method creates (if necessary) and returns
/// an instance of the appropriate subclass.
///
/// When using these bindings, certain features are disabled. For
/// example, [timeDilation] is reset to 1.0 on initialization.
@@ -268,14 +265,6 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
/// equivalent as [Future.delayed].
Future<void> delayed(Duration duration);
/// The current [TestWidgetsFlutterBinding], if one has been created.
///
/// Provides access to the features exposed by this binding. The binding must
/// be initialized before using this getter; this is typically done by calling
/// [testWidgets] or [TestWidgetsFlutterBinding.ensureInitialized].
static TestWidgetsFlutterBinding get instance => BindingBase.checkInstance(_instance);
static TestWidgetsFlutterBinding? _instance;
/// Creates and initializes the binding. This function is
/// idempotent; calling it a second time will just return the
/// previously-created instance.
@@ -294,26 +283,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
/// [LiveTestWidgetsFlutterBinding], so this function will always set up an
/// [AutomatedTestWidgetsFlutterBinding] when run in a web browser.
///
/// The parameter `environment` is used to test the test framework
/// itself by checking how it reacts to different environment
/// variable values, and should not be used outside of this context.
///
/// If a [TestWidgetsFlutterBinding] subclass was explicitly initialized
/// before calling [ensureInitialized], then that version of the binding is
/// returned regardless of the logic described above. This allows tests to
/// force a specific test binding to be used.
///
/// This is called automatically by [testWidgets].
static TestWidgetsFlutterBinding ensureInitialized([@visibleForTesting Map<String, String>? environment]) {
if (_instance != null)
return _instance!;
return binding.ensureInitialized(environment);
}
/// The parameter `environment` is exposed to test different environment
/// variable values, and should not be used.
static WidgetsBinding ensureInitialized([@visibleForTesting Map<String, String>? environment]) => binding.ensureInitialized(environment);
@override
void initInstances() {
super.initInstances();
_instance = this;
timeDilation = 1.0; // just in case the developer has artificially changed it for development
if (overrideHttpClient) {
binding.setupHttpOverrides();
@@ -918,12 +894,12 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
// tests.
// ignore: invalid_use_of_visible_for_testing_member
RawKeyboard.instance.clearKeysPressed();
assert(!RendererBinding.instance.mouseTracker.mouseIsConnected,
assert(!RendererBinding.instance!.mouseTracker.mouseIsConnected,
'The MouseTracker thinks that there is still a mouse connected, which indicates that a '
'test has not removed the mouse pointer which it added. Call removePointer on the '
'active mouse gesture to remove the mouse pointer.');
// ignore: invalid_use_of_visible_for_testing_member
RendererBinding.instance.initMouseTracker();
RendererBinding.instance!.initMouseTracker();
}
}
@@ -935,41 +911,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
///
/// This class assumes it is always run in checked mode (since tests are always
/// run in checked mode).
///
/// See [TestWidgetsFlutterBinding] for a list of mixins that must be
/// provided by the binding active while the test framework is
/// running.
class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
@override
void initInstances() {
super.initInstances();
_instance = this;
binding.mockFlutterAssets();
}
/// The current [AutomatedTestWidgetsFlutterBinding], if one has been created.
///
/// The binding must be initialized before using this getter. If you
/// need the binding to be constructed before calling [testWidgets],
/// you can ensure a binding has been constructed by calling the
/// [TestWidgetsFlutterBinding.ensureInitialized] function.
static AutomatedTestWidgetsFlutterBinding get instance => BindingBase.checkInstance(_instance);
static AutomatedTestWidgetsFlutterBinding? _instance;
/// Returns an instance of the binding that implements
/// [AutomatedTestWidgetsFlutterBinding]. If no binding has yet been
/// initialized, the a new instance is created.
///
/// Generally, there is no need to call this method. Use
/// [TestWidgetsFlutterBinding.ensureInitialized] instead, as it
/// will select the correct test binding implementation
/// automatically.
static AutomatedTestWidgetsFlutterBinding ensureInitialized() {
if (AutomatedTestWidgetsFlutterBinding._instance == null)
AutomatedTestWidgetsFlutterBinding();
return AutomatedTestWidgetsFlutterBinding.instance;
}
FakeAsync? _currentFakeAsync; // set in runTest; cleared in postTest
Completer<void>? _pendingAsyncTasks;
@@ -1428,40 +1376,7 @@ enum LiveTestWidgetsFlutterBindingFramePolicy {
/// [pump]. (There would be no point setting it to a value that
/// doesn't trigger a paint, since then you could not see anything
/// anyway.)
///
/// See [TestWidgetsFlutterBinding] for a list of mixins that must be
/// provided by the binding active while the test framework is
/// running.
class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
@override
void initInstances() {
super.initInstances();
_instance = this;
}
/// The current [LiveTestWidgetsFlutterBinding], if one has been created.
///
/// The binding must be initialized before using this getter. If you
/// need the binding to be constructed before calling [testWidgets],
/// you can ensure a binding has been constructed by calling the
/// [TestWidgetsFlutterBinding.ensureInitialized] function.
static LiveTestWidgetsFlutterBinding get instance => BindingBase.checkInstance(_instance);
static LiveTestWidgetsFlutterBinding? _instance;
/// Returns an instance of the binding that implements
/// [LiveTestWidgetsFlutterBinding]. If no binding has yet been
/// initialized, the a new instance is created.
///
/// Generally, there is no need to call this method. Use
/// [TestWidgetsFlutterBinding.ensureInitialized] instead, as it
/// will select the correct test binding implementation
/// automatically.
static LiveTestWidgetsFlutterBinding ensureInitialized() {
if (LiveTestWidgetsFlutterBinding._instance == null)
LiveTestWidgetsFlutterBinding();
return LiveTestWidgetsFlutterBinding.instance;
}
@override
bool get inTest => _inTest;
bool _inTest = false;

View File

@@ -318,7 +318,7 @@ class CommonFinders {
/// If the `skipOffstage` argument is true (the default), then this skips
/// nodes that are [Offstage] or that are from inactive [Route]s.
Finder bySemanticsLabel(Pattern label, { bool skipOffstage = true }) {
if (WidgetsBinding.instance.pipelineOwner.semanticsOwner == null)
if (WidgetsBinding.instance!.pipelineOwner.semanticsOwner == null)
throw StateError('Semantics are not enabled. '
'Make sure to call tester.ensureSemantics() before using '
'this finder, and call dispose on its return value after.');
@@ -375,7 +375,7 @@ abstract class Finder {
@protected
Iterable<Element> get allCandidates {
return collectAllElementsFrom(
WidgetsBinding.instance.renderViewElement!,
WidgetsBinding.instance!.renderViewElement!,
skipOffstage: skipOffstage,
);
}
@@ -518,7 +518,7 @@ class _HitTestableFinder extends ChainedFinder {
final RenderBox box = candidate.renderObject! as RenderBox;
final Offset absoluteOffset = box.localToGlobal(alignment.alongSize(box.size));
final HitTestResult hitResult = HitTestResult();
WidgetsBinding.instance.hitTest(hitResult, absoluteOffset);
WidgetsBinding.instance!.hitTest(hitResult, absoluteOffset);
for (final HitTestEntry entry in hitResult.path) {
if (entry.target == candidate.renderObject) {
yield candidate;

View File

@@ -1677,7 +1677,7 @@ class _MatchesReferenceImage extends AsyncMatcher {
imageFuture = captureImage(elements.single);
}
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
return binding.runAsync<String?>(() async {
final ui.Image image = await imageFuture;
final ByteData? bytes = await image.toByteData();

View File

@@ -130,7 +130,7 @@ void testWidgets(
}) {
assert(variant != null);
assert(variant.values.isNotEmpty, 'There must be at least one value to test in the testing variant.');
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
final WidgetTester tester = WidgetTester._(binding);
for (final dynamic value in variant.values) {
final String variationDescription = variant.describeValue(value);
@@ -404,7 +404,7 @@ Future<void> benchmarkWidgets(
print(kDebugWarning);
return true;
}());
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
assert(binding is! AutomatedTestWidgetsFlutterBinding);
final WidgetTester tester = WidgetTester._(binding);
SemanticsHandle? semanticsHandle;