find.dart 4.33 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
/// Command to find an element.
class Find extends Command {
16
  @override
17
  final String kind = 'find';
18

19 20 21 22
  Find(this.searchSpec);

  final SearchSpecification searchSpec;

23
  @override
24
  Map<String, String> serialize() => searchSpec.serialize();
25

26 27
  static Find deserialize(Map<String, String> json) {
    return new Find(SearchSpecification.deserialize(json));
28 29 30 31
  }
}

/// Describes how to the driver should search for elements.
32
abstract class SearchSpecification {
33 34
  String get searchSpecType;

35
  static SearchSpecification deserialize(Map<String, String> json) {
36 37
    String searchSpecType = json['searchSpecType'];
    switch(searchSpecType) {
38 39 40
      case 'ByValueKey': return ByValueKey.deserialize(json);
      case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json);
      case 'ByText': return ByText.deserialize(json);
41 42 43 44
    }
    throw new DriverError('Unsupported search specification type $searchSpecType');
  }

45
  Map<String, String> serialize() => {
46 47 48 49 50 51
    'searchSpecType': searchSpecType,
  };
}

/// Tells [Find] to search by tooltip text.
class ByTooltipMessage extends SearchSpecification {
52
  @override
53 54 55 56 57 58 59
  final String searchSpecType = 'ByTooltipMessage';

  ByTooltipMessage(this.text);

  /// Tooltip message text.
  final String text;

60
  @override
61
  Map<String, String> serialize() => super.serialize()..addAll({
62 63 64
    'text': text,
  });

65
  static ByTooltipMessage deserialize(Map<String, String> json) {
66 67 68 69 70 71
    return new ByTooltipMessage(json['text']);
  }
}

/// Tells [Find] to search for `Text` widget by text.
class ByText extends SearchSpecification {
72
  @override
73 74 75 76 77 78
  final String searchSpecType = 'ByText';

  ByText(this.text);

  final String text;

79
  @override
80
  Map<String, String> serialize() => super.serialize()..addAll({
81 82 83
    'text': text,
  });

84
  static ByText deserialize(Map<String, String> json) {
85 86 87 88 89 90
    return new ByText(json['text']);
  }
}

/// Tells [Find] to search by `ValueKey`.
class ByValueKey extends SearchSpecification {
91
  @override
92 93 94
  final String searchSpecType = 'ByValueKey';

  ByValueKey(dynamic keyValue)
95 96 97 98
    : this.keyValue = keyValue,
      this.keyValueString = '$keyValue',
      this.keyValueType = '${keyValue.runtimeType}' {
    if (!_supportedKeyValueTypes.contains(keyValue.runtimeType))
99
      throw _createInvalidKeyValueTypeError('$keyValue.runtimeType');
100 101 102 103 104 105 106 107 108 109 110 111 112
  }

  /// 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;

113
  @override
114
  Map<String, String> serialize() => super.serialize()..addAll({
115 116
    'keyValueString': keyValueString,
    'keyValueType': keyValueType,
117
  });
118

119
  static ByValueKey deserialize(Map<String, String> json) {
120 121 122 123
    String keyValueString = json['keyValueString'];
    String keyValueType = json['keyValueType'];
    switch(keyValueType) {
      case 'int':
124
        return new ByValueKey(int.parse(keyValueString));
125
      case 'String':
126
        return new ByValueKey(keyValueString);
127
      default:
128
        throw _createInvalidKeyValueTypeError(keyValueType);
129 130 131 132 133 134
    }
  }
}

/// Command to read the text from a given element.
class GetText extends CommandWithTarget {
Ian Hickson's avatar
Ian Hickson committed
135 136 137
  /// [targetRef] identifies an element that contains a piece of text.
  GetText(ObjectRef targetRef) : super(targetRef);

138
  @override
139 140
  final String kind = 'get_text';

141
  static GetText deserialize(Map<String, String> json) {
142 143 144
    return new GetText(new ObjectRef(json['targetRef']));
  }

145
  @override
146
  Map<String, String> serialize() => super.serialize();
147 148 149 150 151 152 153
}

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

  final String text;

Ian Hickson's avatar
Ian Hickson committed
154 155 156 157
  static GetTextResult fromJson(Map<String, dynamic> json) {
    return new GetTextResult(json['text']);
  }

158
  @override
159 160 161 162
  Map<String, dynamic> toJson() => {
    'text': text,
  };
}