[e2e] make test bindings friendlier to integration tests (#58210)
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'binding.dart';
|
||||
import 'binding.dart';
|
||||
|
||||
/// Ensure the [WidgetsBinding] is initialized.
|
||||
WidgetsBinding ensureInitialized([@visibleForTesting Map<String, String> environment]) {
|
||||
|
||||
@@ -205,6 +205,27 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
@protected
|
||||
bool get disableShadows => false;
|
||||
|
||||
/// Determines whether the Dart [HttpClient] class should be overriden to
|
||||
/// always return a failure response.
|
||||
///
|
||||
/// By default, this value is true, so that unit tests will not become flaky
|
||||
/// due to intermitten network errors. The value may be overriden by a binding
|
||||
/// intended for use in integration tests that do end to end application
|
||||
/// testing, including working with real network responses.
|
||||
@protected
|
||||
bool get overrideHttpClient => true;
|
||||
|
||||
/// Determines whether the binding automatically registers [testTextInput].
|
||||
///
|
||||
/// Unit tests make use of this to mock out text input communication for
|
||||
/// widgets. An integration test would set this to false, to test real IME
|
||||
/// or keyboard input.
|
||||
///
|
||||
/// [TestTextInput.isRegistered] reports whether the text input mock is
|
||||
/// registered or not.
|
||||
@protected
|
||||
bool get registerTestTextInput => true;
|
||||
|
||||
/// Increase the timeout for the current test by the given duration.
|
||||
///
|
||||
/// This only matters if the test has an `initialTimeout` set on
|
||||
@@ -269,8 +290,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
void initInstances() {
|
||||
super.initInstances();
|
||||
timeDilation = 1.0; // just in case the developer has artificially changed it for development
|
||||
binding.setupHttpOverrides();
|
||||
_testTextInput = TestTextInput(onCleared: _resetFocusedEditable)..register();
|
||||
if (overrideHttpClient) {
|
||||
binding.setupHttpOverrides();
|
||||
}
|
||||
_testTextInput = TestTextInput(onCleared: _resetFocusedEditable);
|
||||
if (registerTestTextInput) {
|
||||
_testTextInput.register();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
@@ -24,4 +26,10 @@ void main() {
|
||||
expect(binding.defaultTestTimeout.duration, const Duration(minutes: 5));
|
||||
});
|
||||
});
|
||||
|
||||
test('Initializes httpOverrides and testTextInput', () async {
|
||||
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
|
||||
expect(binding.testTextInput.isRegistered, true);
|
||||
expect(HttpOverrides.current, isNotNull);
|
||||
});
|
||||
}
|
||||
|
||||
26
packages/flutter_test/test/integration_bindings_test.dart
Normal file
26
packages/flutter_test/test/integration_bindings_test.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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:io';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
test('Initializes httpOverrides and testTextInput', () async {
|
||||
expect(HttpOverrides.current, null);
|
||||
final TestWidgetsFlutterBinding binding = CustomBindings();
|
||||
expect(WidgetsBinding.instance, isA<CustomBindings>());
|
||||
expect(binding.testTextInput.isRegistered, false);
|
||||
expect(HttpOverrides.current, null);
|
||||
});
|
||||
}
|
||||
|
||||
class CustomBindings extends AutomatedTestWidgetsFlutterBinding {
|
||||
@override
|
||||
bool get overrideHttpClient => false;
|
||||
|
||||
@override
|
||||
bool get registerTestTextInput => false;
|
||||
}
|
||||
Reference in New Issue
Block a user