-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.dart
89 lines (76 loc) · 3.14 KB
/
utils.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
import 'package:ably_flutter/ably_flutter.dart';
import 'package:ably_flutter/src/impl/message.dart';
import 'package:ably_flutter/src/method_call_handler.dart';
import 'package:ably_flutter/src/platform.dart' as platform;
import 'package:flutter/services.dart';
typedef MethodCallHandler = Future<dynamic> Function(MethodCall);
class MockMethodCallManager {
int handleCounter = 0;
bool isAuthenticated = false;
final channels = <int, AblyMessage>{};
final publishedMessages = <AblyMessage>[];
final methodChannel = platform.methodChannel;
MockMethodCallManager() {
methodChannel.setMockMethodCallHandler(handler);
}
void reset() {
channels.clear();
publishedMessages.clear();
handleCounter = 0;
methodChannel.setMockMethodCallHandler(null);
}
Future<dynamic> handler(MethodCall methodCall) async {
switch (methodCall.method) {
case PlatformMethod.registerAbly:
return true;
case PlatformMethod.createRestWithOptions:
case PlatformMethod.createRealtimeWithOptions:
final handle = ++handleCounter;
channels[handle] = methodCall.arguments as AblyMessage;
return handle;
case PlatformMethod.publish:
final message = methodCall.arguments as AblyMessage;
final handle = (message.message as AblyMessage).handle;
final ablyChannel = channels[handle];
final clientOptions = ablyChannel.message 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') {
await AblyMethodCallHandler(methodChannel).onAuthCallback(
AblyMessage(
TokenParams(timestamp: DateTime.now()),
handle: handle,
),
);
isAuthenticated = true;
throw PlatformException(
code: ErrorCodes.authCallbackFailure.toString(),
);
}
publishedMessages.add(message);
return null;
case PlatformMethod.publishRealtimeChannelMessage:
final message = methodCall.arguments as AblyMessage;
final handle = (message.message as AblyMessage).handle;
final ablyChannel = channels[handle];
final clientOptions = ablyChannel.message 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') {
await AblyMethodCallHandler(methodChannel).onRealtimeAuthCallback(
AblyMessage(TokenParams(timestamp: DateTime.now()),
handle: handle));
isAuthenticated = true;
throw PlatformException(
code: ErrorCodes.authCallbackFailure.toString());
}
publishedMessages.add(message);
return null;
default:
return throw Exception('Unexpected method call: ${methodCall.method}'
' args: ${methodCall.arguments}');
}
}
}