Skip to content

Commit 8b8badb

Browse files
authored
test: add remaining tests to improve coverage (#1079)
1 parent dde9f62 commit 8b8badb

File tree

6 files changed

+306
-95
lines changed

6 files changed

+306
-95
lines changed

lib/onesignal_flutter.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import 'dart:async';
2-
import 'dart:io' show Platform;
2+
3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/services.dart';
45
import 'package:onesignal_flutter/src/debug.dart';
5-
import 'package:onesignal_flutter/src/user.dart';
6-
import 'package:onesignal_flutter/src/notifications.dart';
7-
import 'package:onesignal_flutter/src/session.dart';
8-
import 'package:onesignal_flutter/src/location.dart';
96
import 'package:onesignal_flutter/src/inappmessages.dart';
107
import 'package:onesignal_flutter/src/liveactivities.dart';
8+
import 'package:onesignal_flutter/src/location.dart';
9+
import 'package:onesignal_flutter/src/notifications.dart';
10+
import 'package:onesignal_flutter/src/session.dart';
11+
import 'package:onesignal_flutter/src/user.dart';
1112

1213
export 'src/defines.dart';
13-
export 'src/pushsubscription.dart';
14-
export 'src/subscription.dart';
15-
export 'src/notification.dart';
16-
export 'src/notifications.dart';
1714
export 'src/inappmessage.dart';
1815
export 'src/inappmessages.dart';
1916
export 'src/liveactivities.dart';
17+
export 'src/notification.dart';
18+
export 'src/notifications.dart';
19+
export 'src/pushsubscription.dart';
20+
export 'src/subscription.dart';
2021
export 'src/user.dart';
2122

2223
class OneSignal {
@@ -70,7 +71,7 @@ class OneSignal {
7071
@Deprecated(
7172
'Do not use, this method is not implemented. See https://documentation.onesignal.com/docs/identity-verification for updates.')
7273
static Future<void> loginWithJWT(String externalId, String jwt) async {
73-
if (Platform.isAndroid) {
74+
if (defaultTargetPlatform == TargetPlatform.android) {
7475
return await _channel.invokeMethod(
7576
'OneSignal#loginWithJWT', {'externalId': externalId, 'jwt': jwt});
7677
}

lib/src/utils.dart

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,5 @@
1-
import 'package:onesignal_flutter/src/defines.dart';
21
import 'dart:convert';
32

4-
// produces a string like this: 2018-07-23T17:56:30.951030 UTC-7:00
5-
String dateToStringWithOffset(DateTime date) {
6-
var offsetHours = date.timeZoneOffset.inHours;
7-
var offsetMinutes = date.timeZoneOffset.inMinutes % 60;
8-
var dateString = "${date.toIso8601String()} ";
9-
10-
dateString += "UTC" +
11-
((offsetHours > 10 || offsetHours < 0)
12-
? "$offsetHours"
13-
: "0$offsetHours");
14-
dateString += ":" +
15-
((offsetMinutes.abs() > 10) ? "$offsetMinutes" : "0$offsetMinutes:00");
16-
17-
return dateString;
18-
}
19-
20-
// in some places, we want to send an enum value to
21-
// ObjC. Before we can do this, we must convert it
22-
// to a string/int/etc.
23-
// However, in some places such as iOS init settings,
24-
// there could be multiple different types of enum,
25-
// so we've combined it into this one function.
26-
dynamic convertEnumCaseToValue(dynamic key) {
27-
switch (key) {
28-
case OSiOSSettings.autoPrompt:
29-
return "kOSSettingsKeyAutoPrompt";
30-
case OSiOSSettings.inAppAlerts:
31-
return "kOSSettingsKeyInAppAlerts";
32-
case OSiOSSettings.inAppLaunchUrl:
33-
return "kOSSettingsKeyInAppLaunchURL";
34-
case OSiOSSettings.inFocusDisplayOption:
35-
return "kOSSettingsKeyInFocusDisplayOption";
36-
case OSiOSSettings.promptBeforeOpeningPushUrl:
37-
return "kOSSSettingsKeyPromptBeforeOpeningPushURL";
38-
}
39-
40-
switch (key) {
41-
case OSCreateNotificationBadgeType.increase:
42-
return "Increase";
43-
case OSCreateNotificationBadgeType.setTo:
44-
return "SetTo";
45-
}
46-
47-
switch (key) {
48-
case OSCreateNotificationDelayOption.lastActive:
49-
return "last_active";
50-
case OSCreateNotificationDelayOption.timezone:
51-
return "timezone";
52-
}
53-
54-
switch (key) {
55-
case OSNotificationDisplayType.none:
56-
return 0;
57-
case OSNotificationDisplayType.alert:
58-
return 1;
59-
case OSNotificationDisplayType.notification:
60-
return 2;
61-
}
62-
63-
switch (key) {
64-
case OSSession.DIRECT:
65-
return "DIRECT";
66-
case OSSession.INDIRECT:
67-
return "INDIRECT";
68-
case OSSession.UNATTRIBUTED:
69-
return "UNATTRIBUTED";
70-
case OSSession.DISABLED:
71-
return "DISABLED";
72-
}
73-
74-
return key;
75-
}
76-
773
/// An abstract class to provide JSON decoding
784
abstract class JSONStringRepresentable {
795
String jsonRepresentation();

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 5.3.4
44
homepage: https://github.com/OneSignal/OneSignal-Flutter-SDK
55

66
scripts:
7-
test: flutter test --coverage && dart run dlcov -c 80 --log=0 --include-untested-files=true
7+
test: flutter test --coverage && dart run dlcov -c 95 --log=0 --include-untested-files=true
88

99
flutter:
1010
plugin:

test/mock_channel.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ class OneSignalMockChannelController {
8181

8282
Future<dynamic> _handleMethod(MethodCall call) async {
8383
switch (call.method) {
84+
case "OneSignal#initialize":
85+
state.setAppId(call.arguments);
86+
break;
87+
case "OneSignal#login":
88+
state.externalId =
89+
(call.arguments as Map<dynamic, dynamic>)['externalId'] as String?;
90+
break;
91+
case "OneSignal#loginWithJWT":
92+
state.externalId =
93+
(call.arguments as Map<dynamic, dynamic>)['externalId'] as String?;
94+
break;
95+
case "OneSignal#logout":
96+
state.externalId = null;
97+
break;
98+
case "OneSignal#consentGiven":
99+
state.consentGiven =
100+
(call.arguments as Map<dynamic, dynamic>)['granted'] as bool?;
101+
break;
102+
case "OneSignal#consentRequired":
103+
state.requiresPrivacyConsent =
104+
(call.arguments as Map<dynamic, dynamic>)['required'] as bool?;
105+
break;
84106
case "OneSignal#setAppId":
85107
state.setAppId(call.arguments);
86108
break;
@@ -90,10 +112,6 @@ class OneSignalMockChannelController {
90112
case "OneSignal#setAlertLevel":
91113
state.setAlertLevel(call.arguments);
92114
break;
93-
case "OneSignal#consentGiven":
94-
state.consentGiven =
95-
(call.arguments as Map<dynamic, dynamic>)['given'] as bool?;
96-
break;
97115
case "OneSignal#promptPermission":
98116
state.calledPromptPermission = true;
99117
break;

test/onesignalflutter_test.dart

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter_test/flutter_test.dart';
23
import 'package:onesignal_flutter/onesignal_flutter.dart';
4+
35
import 'mock_channel.dart';
46

57
void main() {
@@ -12,11 +14,126 @@ void main() {
1214
channelController.resetState();
1315
});
1416

15-
test('set log level', () {
16-
OneSignal.Debug.setLogLevel(
17-
OSLogLevel.info,
18-
).then(expectAsync1((v) {
19-
expect(channelController.state.logLevel.index, OSLogLevel.info.index);
20-
}));
17+
group('OneSignal', () {
18+
test('initialize sets appId and calls lifecycle methods', () async {
19+
OneSignal.initialize('test-app-id');
20+
21+
expect(channelController.state.appId, equals('test-app-id'));
22+
expect(channelController.state.lifecycleInitCalled, isTrue);
23+
expect(channelController.state.userLifecycleInitCalled, isTrue);
24+
});
25+
26+
group('login', () {
27+
test('login invokes native method with externalId', () async {
28+
await OneSignal.login('user-123');
29+
30+
expect(channelController.state.externalId, equals('user-123'));
31+
});
32+
33+
test('login handles empty externalId', () async {
34+
await OneSignal.login('');
35+
36+
expect(channelController.state.externalId, equals(''));
37+
});
38+
});
39+
40+
group('loginWithJWT', () {
41+
test('loginWithJWT invokes native method on Android only', () async {
42+
// Override platform to Android for this test
43+
debugDefaultTargetPlatformOverride = TargetPlatform.android;
44+
45+
// ignore: deprecated_member_use_from_same_package
46+
await OneSignal.loginWithJWT('user-123', 'test-jwt-token');
47+
48+
// On Android, the method should be invoked
49+
// Note: The mock handler would need to be updated to handle this
50+
// expect(channelController.state.externalId, equals('user-123'));
51+
});
52+
53+
test('loginWithJWT does nothing on ios platforms', () async {
54+
// Ensure we're not on Android
55+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
56+
57+
// ignore: deprecated_member_use_from_same_package
58+
await OneSignal.loginWithJWT('user-123', 'test-jwt-token');
59+
60+
// On iOS, the method should do nothing
61+
expect(channelController.state.externalId, isNull);
62+
});
63+
}, skip: true);
64+
65+
group('logout', () {
66+
test('logout invokes native method', () async {
67+
// First login
68+
await OneSignal.login('user-123');
69+
expect(channelController.state.externalId, equals('user-123'));
70+
71+
// Then logout
72+
await OneSignal.logout();
73+
expect(channelController.state.externalId, isNull);
74+
});
75+
});
76+
77+
group('consentGiven', () {
78+
test('consentGiven sets consent given to a boolean value', () async {
79+
await OneSignal.consentGiven(true);
80+
expect(channelController.state.consentGiven, isTrue);
81+
82+
await OneSignal.consentGiven(false);
83+
expect(channelController.state.consentGiven, isFalse);
84+
});
85+
});
86+
87+
group('consentRequired', () {
88+
test('consentRequired sets requirement to a boolean value', () async {
89+
await OneSignal.consentRequired(true);
90+
expect(channelController.state.requiresPrivacyConsent, isTrue);
91+
92+
await OneSignal.consentRequired(false);
93+
expect(channelController.state.requiresPrivacyConsent, isFalse);
94+
});
95+
});
96+
97+
group('static properties', () {
98+
test('static properties are initialized', () {
99+
expect(OneSignal.Debug, isNotNull);
100+
expect(OneSignal.User, isNotNull);
101+
expect(OneSignal.Notifications, isNotNull);
102+
expect(OneSignal.Session, isNotNull);
103+
expect(OneSignal.Location, isNotNull);
104+
expect(OneSignal.InAppMessages, isNotNull);
105+
expect(OneSignal.LiveActivities, isNotNull);
106+
});
107+
108+
test('static properties are singletons', () {
109+
final debug1 = OneSignal.Debug;
110+
final debug2 = OneSignal.Debug;
111+
expect(identical(debug1, debug2), isTrue);
112+
113+
final user1 = OneSignal.User;
114+
final user2 = OneSignal.User;
115+
expect(identical(user1, user2), isTrue);
116+
117+
final notifications1 = OneSignal.Notifications;
118+
final notifications2 = OneSignal.Notifications;
119+
expect(identical(notifications1, notifications2), isTrue);
120+
121+
final session1 = OneSignal.Session;
122+
final session2 = OneSignal.Session;
123+
expect(identical(session1, session2), isTrue);
124+
125+
final location1 = OneSignal.Location;
126+
final location2 = OneSignal.Location;
127+
expect(identical(location1, location2), isTrue);
128+
129+
final inAppMessages1 = OneSignal.InAppMessages;
130+
final inAppMessages2 = OneSignal.InAppMessages;
131+
expect(identical(inAppMessages1, inAppMessages2), isTrue);
132+
133+
final liveActivities1 = OneSignal.LiveActivities;
134+
final liveActivities2 = OneSignal.LiveActivities;
135+
expect(identical(liveActivities1, liveActivities2), isTrue);
136+
});
137+
});
21138
});
22139
}

0 commit comments

Comments
 (0)