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:
David Shuckerow
2019-06-28 10:37:27 -07:00
committed by GitHub
parent 66273157f0
commit df6e4d4872
8 changed files with 204 additions and 14 deletions

View File

@@ -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();

View File

@@ -0,0 +1,16 @@
// Copyright 2019 The Chromium 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 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('$WidgetsBinding initializes with $LiveTestWidgetsFlutterBinding when FLUTTER_TEST = "false"', () {
TestWidgetsFlutterBinding.ensureInitialized(<String, String>{'FLUTTER_TEST': 'false'});
expect(WidgetsBinding.instance, isInstanceOf<LiveTestWidgetsFlutterBinding>());
}, onPlatform: const <String, dynamic>{
'browser': <Skip>[Skip('Browser will not use the live binding')]
});
}

View File

@@ -0,0 +1,14 @@
// Copyright 2019 The Chromium 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 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('$WidgetsBinding initializes with $AutomatedTestWidgetsFlutterBinding when FLUTTER_TEST has a value that is not "true" or "false"', () {
TestWidgetsFlutterBinding.ensureInitialized(<String, String>{'FLUTTER_TEST': 'value that is neither "true" nor "false"'});
expect(WidgetsBinding.instance, isInstanceOf<AutomatedTestWidgetsFlutterBinding>());
});
}

View File

@@ -0,0 +1,14 @@
// Copyright 2019 The Chromium 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 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('$WidgetsBinding initializes with $AutomatedTestWidgetsFlutterBinding when FLUTTER_TEST is defined but null', () {
TestWidgetsFlutterBinding.ensureInitialized(<String, String>{'FLUTTER_TEST': null});
expect(WidgetsBinding.instance, isInstanceOf<AutomatedTestWidgetsFlutterBinding>());
});
}

View File

@@ -0,0 +1,14 @@
// Copyright 2019 The Chromium 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 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('$WidgetsBinding initializes with $AutomatedTestWidgetsFlutterBinding when FLUTTER_TEST = "true"', () {
TestWidgetsFlutterBinding.ensureInitialized(<String, String>{'FLUTTER_TEST': 'true'});
expect(WidgetsBinding.instance, isInstanceOf<AutomatedTestWidgetsFlutterBinding>());
});
}

View File

@@ -0,0 +1,14 @@
// Copyright 2019 The Chromium 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 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('$WidgetsBinding initializes with $LiveTestWidgetsFlutterBinding when the environment does not contain FLUTTER_TEST', () {
TestWidgetsFlutterBinding.ensureInitialized(<String, String>{});
expect(WidgetsBinding.instance, isInstanceOf<LiveTestWidgetsFlutterBinding>());
});
}