deserialization_factory.dart 3.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// Copyright 2014 The Flutter 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 'diagnostics_tree.dart';
import 'error.dart';
import 'find.dart';
import 'frame_sync.dart';
import 'geometry.dart';
import 'gesture.dart';
import 'health.dart';
import 'layer_tree.dart';
import 'message.dart';
import 'render_tree.dart';
import 'request_data.dart';
import 'semantics.dart';
import 'text.dart';
import 'wait.dart';

/// A factory for deserializing [Finder]s.
mixin DeserializeFinderFactory {
  /// Deserializes the finder from JSON generated by [SerializableFinder.serialize].
  SerializableFinder deserializeFinder(Map<String, String> json) {
    final String? finderType = json['finderType'];
    switch (finderType) {
      case 'ByType': return ByType.deserialize(json);
      case 'ByValueKey': return ByValueKey.deserialize(json);
      case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json);
      case 'BySemanticsLabel': return BySemanticsLabel.deserialize(json);
      case 'ByText': return ByText.deserialize(json);
      case 'PageBack': return const PageBack();
      case 'Descendant': return Descendant.deserialize(json, this);
      case 'Ancestor': return Ancestor.deserialize(json, this);
    }
    throw DriverError('Unsupported search specification type $finderType');
  }
}

/// A factory for deserializing [Command]s.
mixin DeserializeCommandFactory {
  /// Deserializes the finder from JSON generated by [Command.serialize] or [CommandWithTarget.serialize].
  Command deserializeCommand(Map<String, String> params, DeserializeFinderFactory finderFactory) {
    final String? kind = params['command'];
    switch(kind) {
      case 'get_health': return GetHealth.deserialize(params);
      case 'get_layer_tree': return GetLayerTree.deserialize(params);
      case 'get_render_tree': return GetRenderTree.deserialize(params);
      case 'enter_text': return EnterText.deserialize(params);
      case 'get_text': return GetText.deserialize(params, finderFactory);
      case 'request_data': return RequestData.deserialize(params);
      case 'scroll': return Scroll.deserialize(params, finderFactory);
      case 'scrollIntoView': return ScrollIntoView.deserialize(params, finderFactory);
      case 'set_frame_sync': return SetFrameSync.deserialize(params);
      case 'set_semantics': return SetSemantics.deserialize(params);
      case 'set_text_entry_emulation': return SetTextEntryEmulation.deserialize(params);
      case 'tap': return Tap.deserialize(params, finderFactory);
      case 'waitFor': return WaitFor.deserialize(params, finderFactory);
      case 'waitForAbsent': return WaitForAbsent.deserialize(params, finderFactory);
59
      case 'waitForTappable': return WaitForTappable.deserialize(params, finderFactory);
60
      case 'waitForCondition': return WaitForCondition.deserialize(params);
61 62 63
      case 'waitUntilNoTransientCallbacks': return WaitForCondition.deserialize(params);
      case 'waitUntilNoPendingFrame': return WaitForCondition.deserialize(params);
      case 'waitUntilFirstFrameRasterized': return WaitForCondition.deserialize(params);
64 65 66 67 68 69 70 71
      case 'get_semantics_id': return GetSemanticsId.deserialize(params, finderFactory);
      case 'get_offset': return GetOffset.deserialize(params, finderFactory);
      case 'get_diagnostics_tree': return GetDiagnosticsTree.deserialize(params, finderFactory);
    }

    throw DriverError('Unsupported command kind $kind');
  }
}