blob: f26d12a3688a2e7a6b5ca53e44f960a0bd81944e [file] [log] [blame]
// Copyright 2013 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 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
class MethodChannelMock {
required String channelName,
required this.methods,
}) : methodChannel = MethodChannel(channelName) {
.setMockMethodCallHandler(methodChannel, _handler);
final Duration? delay;
final MethodChannel methodChannel;
final Map<String, dynamic> methods;
final List<MethodCall> log = <MethodCall>[];
Future<dynamic> _handler(MethodCall methodCall) async {
if (!methods.containsKey(methodCall.method)) {
throw MissingPluginException('No implementation found for method '
'${methodCall.method} on channel ${}');
return Future<dynamic>.delayed(delay ??, () {
final dynamic result = methods[methodCall.method];
if (result is Exception) {
throw result;
return Future<dynamic>.value(result);
/// This allows a value of type T or T? to be treated as a value of type T?.
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;