message.dart 1.79 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

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

  /// Serializes this command to parameter name/value pairs.
36
  @mustCallSuper
37 38 39 40 41 42 43 44
  Map<String, String> serialize() {
    final Map<String, String> result = <String, String>{
      'command': kind,
    };
    if (timeout != null)
      result['timeout'] = '${timeout.inMilliseconds}';
    return result;
  }
45 46
}

47
/// An object sent from a Flutter application back to the Flutter Driver in
48
/// response to a command.
49
abstract class Result {
50 51 52
  /// Serializes this message to a JSON map.
  Map<String, dynamic> toJson();
}