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:
@@ -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(() {
|
||||
|
||||
@@ -15,6 +15,9 @@ class GetHealth extends Command {
|
||||
|
||||
@override
|
||||
String get kind => 'get_health';
|
||||
|
||||
@override
|
||||
bool get requiresRootWidgetAttached => false;
|
||||
}
|
||||
|
||||
/// A description of application state.
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user