Skip to content

Commit 47289c2

Browse files
committed
use mock channel for liveactivities
1 parent 6e42a4b commit 47289c2

File tree

2 files changed

+81
-49
lines changed

2 files changed

+81
-49
lines changed

test/liveactivities_test.dart

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import 'package:flutter/foundation.dart';
2-
import 'package:flutter/services.dart';
32
import 'package:flutter_test/flutter_test.dart';
43
import 'package:onesignal_flutter/src/liveactivities.dart';
54

5+
import 'mock_channel.dart';
6+
67
const activityId = 'test-activity-id';
78
const token = 'test-token';
89
const activityType = 'TestActivityType';
@@ -12,61 +13,45 @@ void main() {
1213

1314
group('OneSignalLiveActivities', () {
1415
late OneSignalLiveActivities liveActivities;
15-
late List<MethodCall> methodCalls;
16+
late OneSignalMockChannelController channelController;
1617

1718
setUp(() {
18-
methodCalls = [];
19+
channelController = OneSignalMockChannelController();
20+
channelController.resetState();
1921
liveActivities = OneSignalLiveActivities();
2022
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
21-
22-
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
23-
.setMockMethodCallHandler(
24-
const MethodChannel('OneSignal#liveactivities'),
25-
(call) async {
26-
methodCalls.add(call);
27-
return null;
28-
},
29-
);
3023
});
3124

3225
tearDown(() {
3326
debugDefaultTargetPlatformOverride = null;
34-
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
35-
.setMockMethodCallHandler(
36-
const MethodChannel('OneSignal#liveactivities'),
37-
null,
38-
);
3927
});
4028

4129
group('enterLiveActivity', () {
4230
test('invokes OneSignal#enterLiveActivity with activityId and token',
4331
() async {
4432
await liveActivities.enterLiveActivity(activityId, token);
4533

46-
expect(methodCalls.length, 1);
47-
expect(methodCalls[0].method, 'OneSignal#enterLiveActivity');
48-
expect(methodCalls[0].arguments,
49-
{'activityId': activityId, 'token': token});
34+
expect(channelController.state.liveActivityEntered, true);
35+
expect(channelController.state.liveActivityId, activityId);
36+
expect(channelController.state.liveActivityToken, token);
5037
});
5138
});
5239

5340
group('exitLiveActivity', () {
5441
test('invokes OneSignal#exitLiveActivity with activityId', () async {
5542
await liveActivities.exitLiveActivity(activityId);
5643

57-
expect(methodCalls.length, 1);
58-
expect(methodCalls[0].method, 'OneSignal#exitLiveActivity');
59-
expect(methodCalls[0].arguments, {'activityId': activityId});
44+
expect(channelController.state.liveActivityExited, true);
45+
expect(channelController.state.liveActivityId, activityId);
6046
});
6147
});
6248

6349
group('setupDefault', () {
6450
test('invokes OneSignal#setupDefault without options', () async {
6551
await liveActivities.setupDefault();
6652

67-
expect(methodCalls.length, 1);
68-
expect(methodCalls[0].method, 'OneSignal#setupDefault');
69-
expect(methodCalls[0].arguments['options'], isNull);
53+
expect(channelController.state.liveActivitySetupCalled, true);
54+
expect(channelController.state.liveActivitySetupOptions, isNull);
7055
});
7156

7257
test('invokes OneSignal#setupDefault with custom options', () async {
@@ -77,9 +62,8 @@ void main() {
7762

7863
await liveActivities.setupDefault(options: options);
7964

80-
expect(methodCalls.length, 1);
81-
expect(methodCalls[0].method, 'OneSignal#setupDefault');
82-
expect(methodCalls[0].arguments['options'], {
65+
expect(channelController.state.liveActivitySetupCalled, true);
66+
expect(channelController.state.liveActivitySetupOptions, {
8367
'enablePushToStart': false,
8468
'enablePushToUpdate': false,
8569
});
@@ -90,7 +74,7 @@ void main() {
9074

9175
await liveActivities.setupDefault(options: options);
9276

93-
expect(methodCalls[0].arguments['options'], {
77+
expect(channelController.state.liveActivitySetupOptions, {
9478
'enablePushToStart': true,
9579
'enablePushToUpdate': true,
9680
});
@@ -104,13 +88,10 @@ void main() {
10488

10589
await liveActivities.startDefault(activityId, attributes, content);
10690

107-
expect(methodCalls.length, 1);
108-
expect(methodCalls[0].method, 'OneSignal#startDefault');
109-
expect(methodCalls[0].arguments, {
110-
'activityId': activityId,
111-
'attributes': attributes,
112-
'content': content,
113-
});
91+
expect(channelController.state.liveActivityStarted, true);
92+
expect(channelController.state.liveActivityId, activityId);
93+
expect(channelController.state.liveActivityAttributes, attributes);
94+
expect(channelController.state.liveActivityContent, content);
11495
});
11596

11697
test('handles complex nested attributes and content', () async {
@@ -126,10 +107,10 @@ void main() {
126107
await liveActivities.startDefault(
127108
activityId, complexAttributes, complexContent);
128109

129-
expect(
130-
methodCalls[0].arguments['attributes']['nested']['key'], 'value');
131-
expect(
132-
methodCalls[0].arguments['content']['data']['status'], 'running');
110+
expect(channelController.state.liveActivityAttributes['nested']['key'],
111+
'value');
112+
expect(channelController.state.liveActivityContent['data']['status'],
113+
'running');
133114
});
134115
});
135116

@@ -138,10 +119,9 @@ void main() {
138119
() async {
139120
await liveActivities.setPushToStartToken(activityType, token);
140121

141-
expect(methodCalls.length, 1);
142-
expect(methodCalls[0].method, 'OneSignal#setPushToStartToken');
143-
expect(methodCalls[0].arguments,
144-
{'activityType': activityType, 'token': token});
122+
expect(channelController.state.liveActivityPushToStartSet, true);
123+
expect(channelController.state.liveActivityType, activityType);
124+
expect(channelController.state.liveActivityPushToken, token);
145125
});
146126
});
147127

@@ -150,9 +130,8 @@ void main() {
150130
() async {
151131
await liveActivities.removePushToStartToken(activityType);
152132

153-
expect(methodCalls.length, 1);
154-
expect(methodCalls[0].method, 'OneSignal#removePushToStartToken');
155-
expect(methodCalls[0].arguments, {'activityType': activityType});
133+
expect(channelController.state.liveActivityPushToStartRemoved, true);
134+
expect(channelController.state.liveActivityType, activityType);
156135
});
157136
});
158137
});

test/mock_channel.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,44 @@ class OneSignalMockChannelController {
9090
break;
9191
case "OneSignal#isShared":
9292
return state.locationShared ?? false;
93+
case "OneSignal#enterLiveActivity":
94+
state.liveActivityEntered = true;
95+
state.liveActivityId =
96+
(call.arguments as Map<dynamic, dynamic>)['activityId'] as String?;
97+
state.liveActivityToken =
98+
(call.arguments as Map<dynamic, dynamic>)['token'] as String?;
99+
break;
100+
case "OneSignal#exitLiveActivity":
101+
state.liveActivityExited = true;
102+
state.liveActivityId =
103+
(call.arguments as Map<dynamic, dynamic>)['activityId'] as String?;
104+
break;
105+
case "OneSignal#setupDefault":
106+
state.liveActivitySetupCalled = true;
107+
state.liveActivitySetupOptions = (call.arguments
108+
as Map<dynamic, dynamic>)['options'] as Map<dynamic, dynamic>?;
109+
break;
110+
case "OneSignal#startDefault":
111+
state.liveActivityStarted = true;
112+
state.liveActivityId =
113+
(call.arguments as Map<dynamic, dynamic>)['activityId'] as String?;
114+
state.liveActivityAttributes =
115+
(call.arguments as Map<dynamic, dynamic>)['attributes'];
116+
state.liveActivityContent =
117+
(call.arguments as Map<dynamic, dynamic>)['content'];
118+
break;
119+
case "OneSignal#setPushToStartToken":
120+
state.liveActivityPushToStartSet = true;
121+
state.liveActivityType = (call.arguments
122+
as Map<dynamic, dynamic>)['activityType'] as String?;
123+
state.liveActivityPushToken =
124+
(call.arguments as Map<dynamic, dynamic>)['token'] as String?;
125+
break;
126+
case "OneSignal#removePushToStartToken":
127+
state.liveActivityPushToStartRemoved = true;
128+
state.liveActivityType = (call.arguments
129+
as Map<dynamic, dynamic>)['activityType'] as String?;
130+
break;
93131
}
94132
}
95133
}
@@ -119,6 +157,21 @@ class OneSignalState {
119157
String? externalId;
120158
String? language;
121159

160+
// live activities
161+
bool? liveActivityEntered;
162+
bool? liveActivityExited;
163+
bool? liveActivityStarted;
164+
bool? liveActivitySetupCalled;
165+
bool? liveActivityPushToStartSet;
166+
bool? liveActivityPushToStartRemoved;
167+
String? liveActivityId;
168+
String? liveActivityToken;
169+
String? liveActivityType;
170+
String? liveActivityPushToken;
171+
dynamic liveActivityAttributes;
172+
dynamic liveActivityContent;
173+
Map<dynamic, dynamic>? liveActivitySetupOptions;
174+
122175
// tags
123176
Map<dynamic, dynamic>? tags;
124177
List<dynamic>? deleteTags;

0 commit comments

Comments
 (0)