find.dart 5.67 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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';
import 'message.dart';

const List<Type> _supportedKeyValueTypes = const <Type>[String, int];

10 11 12 13
DriverError _createInvalidKeyValueTypeError(String invalidType) {
  return new DriverError('Unsupported key value type $invalidType. Flutter Driver only supports ${_supportedKeyValueTypes.join(", ")}');
}

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/// A command aimed at an object to be located by [finder].
///
/// Implementations must provide a concrete [kind]. If additional data is
/// required beyond the [finder] the implementation may override [serialize]
/// and add more keys to the returned map.
abstract class CommandWithTarget extends Command {
  CommandWithTarget(this.finder) {
    if (finder == null)
      throw new DriverError('${this.runtimeType} target cannot be null');
  }

  /// Locates the object or objects targeted by this command.
  final SerializableFinder finder;

  /// This method is meant to be overridden if data in addition to [finder]
  /// is serialized to JSON.
  ///
  /// Example:
  ///
  ///     Map<String, String> toJson() => super.toJson()..addAll({
  ///       'foo': this.foo,
  ///     });
36
  @override
37 38 39
  Map<String, String> serialize() => finder.serialize();
}

40 41
/// Waits until [finder] can locate the target.
class WaitFor extends CommandWithTarget {
42
  @override
43
  final String kind = 'waitFor';
44

45 46
  WaitFor(SerializableFinder finder, {this.timeout})
      : super(finder);
47

48 49 50 51 52 53 54
  final Duration timeout;

  static WaitFor deserialize(Map<String, String> json) {
    Duration timeout = json['timeout'] != null
      ? new Duration(milliseconds: int.parse(json['timeout']))
      : null;
    return new WaitFor(SerializableFinder.deserialize(json), timeout: timeout);
55
  }
56

57
  @override
58 59
  Map<String, String> serialize() {
    Map<String, String> json = super.serialize();
60

61 62 63
    if (timeout != null) {
      json['timeout'] = '${timeout.inMilliseconds}';
    }
64

65
    return json;
66
  }
67
}
68

69 70 71 72 73 74
class WaitForResult extends Result {
  WaitForResult();

  static WaitForResult fromJson(Map<String, dynamic> json) {
    return new WaitForResult();
  }
75 76

  @override
77
  Map<String, dynamic> toJson() => <String, dynamic>{};
78 79 80
}

/// Describes how to the driver should search for elements.
81 82
abstract class SerializableFinder {
  String get finderType;
83

84 85 86
  static SerializableFinder deserialize(Map<String, String> json) {
    String finderType = json['finderType'];
    switch(finderType) {
87 88 89
      case 'ByValueKey': return ByValueKey.deserialize(json);
      case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json);
      case 'ByText': return ByText.deserialize(json);
90
    }
91
    throw new DriverError('Unsupported search specification type $finderType');
92 93
  }

94
  Map<String, String> serialize() => {
95
    'finderType': finderType,
96 97 98
  };
}

99 100
/// Finds widgets by tooltip text.
class ByTooltipMessage extends SerializableFinder {
101
  @override
102
  final String finderType = 'ByTooltipMessage';
103 104 105 106 107 108

  ByTooltipMessage(this.text);

  /// Tooltip message text.
  final String text;

109
  @override
110
  Map<String, String> serialize() => super.serialize()..addAll({
111 112 113
    'text': text,
  });

114
  static ByTooltipMessage deserialize(Map<String, String> json) {
115 116 117 118
    return new ByTooltipMessage(json['text']);
  }
}

119 120
/// Finds widgets by [text] inside a `Text` widget.
class ByText extends SerializableFinder {
121
  @override
122
  final String finderType = 'ByText';
123 124 125 126 127

  ByText(this.text);

  final String text;

128
  @override
129
  Map<String, String> serialize() => super.serialize()..addAll({
130 131 132
    'text': text,
  });

133
  static ByText deserialize(Map<String, String> json) {
134 135 136 137
    return new ByText(json['text']);
  }
}

138 139
/// Finds widgets by `ValueKey`.
class ByValueKey extends SerializableFinder {
140
  @override
141
  final String finderType = 'ByValueKey';
142 143

  ByValueKey(dynamic keyValue)
144 145 146 147
    : this.keyValue = keyValue,
      this.keyValueString = '$keyValue',
      this.keyValueType = '${keyValue.runtimeType}' {
    if (!_supportedKeyValueTypes.contains(keyValue.runtimeType))
148
      throw _createInvalidKeyValueTypeError('$keyValue.runtimeType');
149 150 151 152 153 154 155 156 157 158 159 160 161
  }

  /// The true value of the key.
  final dynamic keyValue;

  /// Stringified value of the key (we can only send strings to the VM service)
  final String keyValueString;

  /// The type name of the key.
  ///
  /// May be one of "String", "int". The list of supported types may change.
  final String keyValueType;

162
  @override
163
  Map<String, String> serialize() => super.serialize()..addAll({
164 165
    'keyValueString': keyValueString,
    'keyValueType': keyValueType,
166
  });
167

168
  static ByValueKey deserialize(Map<String, String> json) {
169 170 171 172
    String keyValueString = json['keyValueString'];
    String keyValueType = json['keyValueType'];
    switch(keyValueType) {
      case 'int':
173
        return new ByValueKey(int.parse(keyValueString));
174
      case 'String':
175
        return new ByValueKey(keyValueString);
176
      default:
177
        throw _createInvalidKeyValueTypeError(keyValueType);
178 179 180 181 182 183
    }
  }
}

/// Command to read the text from a given element.
class GetText extends CommandWithTarget {
184 185
  /// [finder] looks for an element that contains a piece of text.
  GetText(SerializableFinder finder) : super(finder);
Ian Hickson's avatar
Ian Hickson committed
186

187
  @override
188 189
  final String kind = 'get_text';

190
  static GetText deserialize(Map<String, String> json) {
191
    return new GetText(SerializableFinder.deserialize(json));
192 193
  }

194
  @override
195
  Map<String, String> serialize() => super.serialize();
196 197 198 199 200 201 202
}

class GetTextResult extends Result {
  GetTextResult(this.text);

  final String text;

Ian Hickson's avatar
Ian Hickson committed
203 204 205 206
  static GetTextResult fromJson(Map<String, dynamic> json) {
    return new GetTextResult(json['text']);
  }

207
  @override
208 209 210 211
  Map<String, dynamic> toJson() => {
    'text': text,
  };
}