Make it possible to override the FLUTTER_TEST env variable (#34301)
* Make it possible to override the FLUTTER_TEST env variable without unsetting it. * Switch to using platform instead of Platform. * Document the bindings, and introduce tests that initialize multiple WidgetsBindings with different environments. * Add tests for the flutter platform test. * Add license headers * Fix lints * Remove trailing whitespace * Respond to Jonahs comments * Respond to Ians comments * Mock out the HttpServer in flutter_platform_test * Mock out the HttpServer in flutter_platform_test * Explain why we mock out the HttpServer in flutter_platform_test
This commit is contained in:
@@ -173,11 +173,29 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
/// This function will use [AutomatedTestWidgetsFlutterBinding] if
|
||||
/// the test was run using `flutter test`, and
|
||||
/// [LiveTestWidgetsFlutterBinding] otherwise (e.g. if it was run
|
||||
/// using `flutter run`). (This is determined by looking at the
|
||||
/// environment variables for a variable called `FLUTTER_TEST`.)
|
||||
static WidgetsBinding ensureInitialized() {
|
||||
/// using `flutter run`). This is determined by looking at the
|
||||
/// environment variables for a variable called `FLUTTER_TEST`.
|
||||
///
|
||||
/// If `FLUTTER_TEST` is set with a value of 'true', then this test was
|
||||
/// invoked by `flutter test`. If `FLUTTER_TEST` is not set, or if it is set
|
||||
/// to 'false', then this test was invoked by `flutter run`.
|
||||
///
|
||||
/// Browser environments do not currently support the
|
||||
/// [LiveTestWidgetsFlutterBinding], so this function will always set up an
|
||||
/// [AutomatedTestWidgetsFlutterBinding] when run in a web browser.
|
||||
///
|
||||
/// The parameter `environment` is exposed to test different environment
|
||||
/// variable values, and should not be used.
|
||||
static WidgetsBinding ensureInitialized([@visibleForTesting Map<String, String> environment]) {
|
||||
if (!isBrowser) {
|
||||
// Accessing Platform may throw from a browser, so we guard this.
|
||||
environment ??= Platform.environment;
|
||||
}
|
||||
if (WidgetsBinding.instance == null) {
|
||||
if (isBrowser || Platform.environment.containsKey('FLUTTER_TEST')) {
|
||||
if (isBrowser) {
|
||||
// Browser environments do not support the LiveTestWidgetsFlutterBinding.
|
||||
AutomatedTestWidgetsFlutterBinding();
|
||||
} else if (environment.containsKey('FLUTTER_TEST') && environment['FLUTTER_TEST'] != 'false') {
|
||||
AutomatedTestWidgetsFlutterBinding();
|
||||
} else {
|
||||
LiveTestWidgetsFlutterBinding();
|
||||
|
||||
Reference in New Issue
Block a user