message.dart 2.41 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7 8 9
/// An object sent from the Flutter Driver to a Flutter application to instruct
/// the application to perform a task.
abstract class Command {
10 11
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
12
  const Command({ this.timeout });
13

14
  /// Deserializes this command from the value generated by [serialize].
15
  Command.deserialize(Map<String, String> json)
16
    : timeout = _parseTimeout(json);
17 18 19 20 21 22 23

  static Duration _parseTimeout(Map<String, String> json) {
    final String timeout = json['timeout'];
    if (timeout == null)
      return null;
    return Duration(milliseconds: int.parse(timeout));
  }
24 25 26

  /// The maximum amount of time to wait for the command to complete.
  ///
27 28 29
  /// Defaults to no timeout, because it is common for operations to take oddly
  /// long in test environments (e.g. because the test host is overloaded), and
  /// having timeouts essentially means having race conditions.
30 31
  final Duration timeout;

32 33
  /// Identifies the type of the command object and of the handler.
  String get kind;
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48
  /// 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;

49
  /// Serializes this command to parameter name/value pairs.
50
  @mustCallSuper
51 52 53 54 55 56 57 58
  Map<String, String> serialize() {
    final Map<String, String> result = <String, String>{
      'command': kind,
    };
    if (timeout != null)
      result['timeout'] = '${timeout.inMilliseconds}';
    return result;
  }
59 60
}

61
/// An object sent from a Flutter application back to the Flutter Driver in
62
/// response to a command.
63
abstract class Result {
64 65 66
  /// A const constructor to allow subclasses to be const.
  const Result();

67 68 69
  /// Serializes this message to a JSON map.
  Map<String, dynamic> toJson();
}