method_calls.dart 3.85 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 14 15 16 17 18
  return _methodCallSuccessHandshake(
      'JSON success($payload)', channel, payload);
}

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

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

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

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

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

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 {
70
    result = await channel.invokeMethod<dynamic>('success', arguments);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  } 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);
92
    throw PlatformException(
93 94 95 96 97
        code: 'error', message: null, details: arguments);
  });
  dynamic errorDetails = nothing;
  dynamic error = nothing;
  try {
98
    error = await channel.invokeMethod<dynamic>('error', arguments);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  } 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);
121
    throw MissingPluginException();
122 123 124 125
  });
  dynamic result = nothing;
  dynamic error = nothing;
  try {
126
    error = await channel.invokeMethod<dynamic>('notImplemented');
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  } on MissingPluginException {
    result = null;
  } catch (e) {
    error = e;
  }
  return resultOfHandshake(
    'Method call not implemented handshake',
    description,
    null,
    received,
    result,
    error,
  );
}