message.dart 2.42 KB
Newer Older
1 2 3 4 5 6
// 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.

import 'error.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
  /// Identifies the type of the command object and of the handler.
  String get kind;
12 13 14

  /// Serializes this command to parameter name/value pairs.
  Map<String, String> serialize();
15 16
}

17
/// An object sent from a Flutter application back to the Flutter Driver in
18
/// response to a command.
Ian Hickson's avatar
Ian Hickson committed
19
abstract class Result { // ignore: one_member_abstracts
20 21 22
  /// Serializes this message to a JSON map.
  Map<String, dynamic> toJson();
}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

/// A serializable reference to an object that lives in the application isolate.
class ObjectRef extends Result {
  ObjectRef(this.objectReferenceKey);

  ObjectRef.notFound() : this(null);

  static ObjectRef fromJson(Map<String, dynamic> json) {
    return json['objectReferenceKey'] != null
      ? new ObjectRef(json['objectReferenceKey'])
      : null;
  }

  /// Identifier used to dereference an object.
  ///
  /// This value is generated by the application-side isolate. Flutter driver
  /// tests should not generate these keys.
  final String objectReferenceKey;

42
  @override
43 44 45 46 47 48 49 50
  Map<String, dynamic> toJson() => {
    'objectReferenceKey': objectReferenceKey,
  };
}

/// A command aimed at an object represented by [targetRef].
///
/// Implementations must provide a concrete [kind]. If additional data is
51
/// required beyond the [targetRef] the implementation may override [serialize]
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/// and add more keys to the returned map.
abstract class CommandWithTarget extends Command {
  CommandWithTarget(ObjectRef ref) : this.targetRef = ref?.objectReferenceKey {
    if (ref == null)
      throw new DriverError('${this.runtimeType} target cannot be null');

    if (ref.objectReferenceKey == null)
      throw new DriverError('${this.runtimeType} target reference cannot be null');
  }

  /// Refers to the object targeted by this command.
  final String targetRef;

  /// This method is meant to be overridden if data in addition to [targetRef]
  /// is serialized to JSON.
  ///
  /// Example:
  ///
70
  ///     Map<String, String> toJson() => super.toJson()..addAll({
71 72
  ///       'foo': this.foo,
  ///     });
73
  @override
74
  Map<String, String> serialize() => <String, String>{
75 76 77
    'targetRef': targetRef,
  };
}