-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmock_method_call_manager.dart
102 lines (89 loc) · 3.68 KB
/
mock_method_call_manager.dart
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import 'package:ably_flutter/ably_flutter.dart';
import 'package:ably_flutter/src/platform/platform_internal.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
typedef MethodCallHandler = Future<dynamic> Function(MethodCall);
class MockMethodCallManager {
int handleCounter = 0;
bool isAuthenticated = false;
final channels = <int, ClientOptions?>{};
final publishedMessages = <AblyMessage<dynamic>>[];
MockMethodCallManager() {
final channel =
MethodChannel('io.ably.flutter.plugin', StandardMethodCodec(Codec()));
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, handler);
Platform(methodChannel: channel);
}
void reset() {
channels.clear();
publishedMessages.clear();
handleCounter = 0;
final channel =
MethodChannel('io.ably.flutter.plugin', StandardMethodCodec(Codec()));
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, handler);
Platform(methodChannel: channel);
}
Future<dynamic> handler(MethodCall methodCall) async {
switch (methodCall.method) {
case PlatformMethod.resetAblyClients:
return true;
case PlatformMethod.createRest:
case PlatformMethod.createRealtime:
final handle = ++handleCounter;
final ablyMessage = methodCall.arguments as AblyMessage;
final channelParams = ablyMessage.message as Map;
final channelOptions =
channelParams[TxTransportKeys.options] as ClientOptions;
channels[handle] = channelOptions;
return handle;
case PlatformMethod.publish:
final ablyMessage = methodCall.arguments as AblyMessage;
final clientOptions = channels[ablyMessage.handle!] as ClientOptions;
// `authUrl` is used to indicate the presence of an authCallback,
// because function references (in `authCallback`) get dropped by the
// PlatformChannel.
if (!isAuthenticated && clientOptions.authUrl == 'hasAuthCallback') {
final channel = MethodChannel(
'io.ably.flutter.plugin', StandardMethodCodec(Codec()));
await AblyMethodCallHandler(channel).onAuthCallback(
AblyMessage(
message: TokenParams(timestamp: DateTime.now()),
handle: ablyMessage.handle,
),
);
isAuthenticated = true;
}
publishedMessages.add(ablyMessage);
return null;
case PlatformMethod.publishRealtimeChannelMessage:
final ablyMessage = methodCall.arguments as AblyMessage;
final clientOptions = channels[ablyMessage.handle!] as ClientOptions;
// `authUrl` is used to indicate the presence of an authCallback,
// because function references (in `authCallback`) get dropped by the
// PlatformChannel.
if (!isAuthenticated && clientOptions.authUrl == 'hasAuthCallback') {
final channel = MethodChannel(
'io.ably.flutter.plugin', StandardMethodCodec(Codec()));
await AblyMethodCallHandler(channel).onRealtimeAuthCallback(
AblyMessage(
message: TokenParams(
timestamp: DateTime.now(),
),
handle: ablyMessage.handle,
),
);
isAuthenticated = true;
}
publishedMessages.add(ablyMessage);
return null;
case PlatformMethod.releaseRestChannel:
case PlatformMethod.releaseRealtimeChannel:
return null;
default:
return throw Exception('Unexpected method call: ${methodCall.method}'
' args: ${methodCall.arguments}');
}
}
}