method_calls.dart 3.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'package:flutter/services.dart';
7
import 'basic_messaging.dart';
8 9 10 11
import 'test_step.dart';

Future<TestStepResult> methodCallJsonSuccessHandshake(dynamic payload) async {
  const MethodChannel channel =
12
      MethodChannel('json-method', JSONMethodCodec());
13
  return _methodCallSuccessHandshake('JSON success($payload)', channel, payload);
14 15 16 17
}

Future<TestStepResult> methodCallJsonErrorHandshake(dynamic payload) async {
  const MethodChannel channel =
18
      MethodChannel('json-method', JSONMethodCodec());
19 20 21 22 23
  return _methodCallErrorHandshake('JSON error($payload)', channel, payload);
}

Future<TestStepResult> methodCallJsonNotImplementedHandshake() async {
  const MethodChannel channel =
24
      MethodChannel('json-method', JSONMethodCodec());
25 26 27 28 29
  return _methodCallNotImplementedHandshake('JSON notImplemented()', channel);
}

Future<TestStepResult> methodCallStandardSuccessHandshake(
    dynamic payload) async {
30
  const MethodChannel channel = MethodChannel(
31
    'std-method',
32
    StandardMethodCodec(ExtendedStandardMessageCodec()),
33
  );
34
  return _methodCallSuccessHandshake('Standard success($payload)', channel, payload);
35 36 37
}

Future<TestStepResult> methodCallStandardErrorHandshake(dynamic payload) async {
38
  const MethodChannel channel = MethodChannel(
39
    'std-method',
40
    StandardMethodCodec(ExtendedStandardMessageCodec()),
41
  );
42
  return _methodCallErrorHandshake('Standard error($payload)', channel, payload);
43 44 45
}

Future<TestStepResult> methodCallStandardNotImplementedHandshake() async {
46
  const MethodChannel channel = MethodChannel(
47
    'std-method',
48
    StandardMethodCodec(ExtendedStandardMessageCodec()),
49
  );
50
  return _methodCallNotImplementedHandshake('Standard notImplemented()', channel);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

Future<TestStepResult> _methodCallSuccessHandshake(
  String description,
  MethodChannel channel,
  dynamic arguments,
) async {
  final List<dynamic> received = <dynamic>[];
  channel.setMethodCallHandler((MethodCall call) async {
    received.add(call.arguments);
    return call.arguments;
  });
  dynamic result = nothing;
  dynamic error = nothing;
  try {
66
    result = await channel.invokeMethod<dynamic>('success', arguments);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  } catch (e) {
    error = e;
  }
  return resultOfHandshake(
    'Method call success handshake',
    description,
    arguments,
    received,
    result,
    error,
  );
}

Future<TestStepResult> _methodCallErrorHandshake(
  String description,
  MethodChannel channel,
  dynamic arguments,
) async {
  final List<dynamic> received = <dynamic>[];
  channel.setMethodCallHandler((MethodCall call) async {
    received.add(call.arguments);
88
    throw PlatformException(code: 'error', details: arguments);
89 90 91 92
  });
  dynamic errorDetails = nothing;
  dynamic error = nothing;
  try {
93
    error = await channel.invokeMethod<dynamic>('error', arguments);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  } on PlatformException catch (e) {
    errorDetails = e.details;
  } catch (e) {
    error = e;
  }
  return resultOfHandshake(
    'Method call error handshake',
    description,
    arguments,
    received,
    errorDetails,
    error,
  );
}

Future<TestStepResult> _methodCallNotImplementedHandshake(
  String description,
  MethodChannel channel,
) async {
  final List<dynamic> received = <dynamic>[];
  channel.setMethodCallHandler((MethodCall call) async {
    received.add(call.arguments);
116
    throw MissingPluginException();
117 118 119 120
  });
  dynamic result = nothing;
  dynamic error = nothing;
  try {
121
    error = await channel.invokeMethod<dynamic>('notImplemented');
122 123 124 125 126 127 128 129 130 131 132 133 134 135
  } on MissingPluginException {
    result = null;
  } catch (e) {
    error = e;
  }
  return resultOfHandshake(
    'Method call not implemented handshake',
    description,
    null,
    received,
    result,
    error,
  );
}