Re-apply "Add assert that the root widget has been attached" (#33084)

This re-applies #32437 with a fix to the broken device lab test.
This commit is contained in:
Todd Volkert
2019-05-21 11:21:57 -07:00
committed by GitHub
parent c6be1b9619
commit ceec48785a
5 changed files with 28 additions and 0 deletions

View File

@@ -736,6 +736,12 @@ mixin WidgetsBinding on BindingBase, SchedulerBinding, GestureBinding, RendererB
).attachToRenderTree(buildOwner, renderViewElement);
}
/// Whether the [renderViewElement] has been initialized.
///
/// This will be false until [runApp] is called (or [WidgetTester.pumpWidget]
/// is called in the context of a [TestWidgetsFlutterBinding]).
bool get isRootWidgetAttached => _renderViewElement != null;
@override
Future<void> performReassemble() {
assert(() {

View File

@@ -15,6 +15,9 @@ class GetHealth extends Command {
@override
String get kind => 'get_health';
@override
bool get requiresRootWidgetAttached => false;
}
/// A description of application state.

View File

@@ -32,6 +32,20 @@ abstract class Command {
/// Identifies the type of the command object and of the handler.
String get kind;
/// Whether this command requires the widget tree to be initialized before
/// the command may be run.
///
/// This defaults to true to force the application under test to call [runApp]
/// before attempting to remotely drive the application. Subclasses may
/// override this to return false if they allow invocation before the
/// application has started.
///
/// See also:
///
/// * [WidgetsBinding.isRootWidgetAttached], which indicates whether the
/// widget tree has been initialized.
bool get requiresRootWidgetAttached => true;
/// Serializes this command to parameter name/value pairs.
@mustCallSuper
Map<String, String> serialize() {

View File

@@ -21,6 +21,9 @@ class RequestData extends Command {
@override
String get kind => 'request_data';
@override
bool get requiresRootWidgetAttached => false;
@override
Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
'message': message,

View File

@@ -183,6 +183,8 @@ class FlutterDriverExtension {
if (commandHandler == null || commandDeserializer == null)
throw 'Extension $_extensionMethod does not support command $commandKind';
final Command command = commandDeserializer(params);
assert(WidgetsBinding.instance.isRootWidgetAttached || !command.requiresRootWidgetAttached,
'No root widget is attached; have you remembered to call runApp()?');
Future<Result> responseFuture = commandHandler(command);
if (command.timeout != null)
responseFuture = responseFuture.timeout(command.timeout);