Skip to content

Commit 4e73bf6

Browse files
committed
Fixed some statics not being cleared between tests
* OneSignalHelp's lastMessageID is now being cleared correctly. * OneSignalTrackFirebaseAnalytics statics are now being cleaned up between tests. * Fixed double swizzling of AppDelegate methods which in some cases created recursion * Some misc clean up.
1 parent 6f4361d commit 4e73bf6

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,13 @@ - (NSString*)stringify {
283283

284284
@implementation OneSignalHelper
285285

286+
static var lastMessageID = @"";
287+
static NSString *_lastMessageIdFromAction;
288+
286289
+ (void)resetLocals {
287290
[OneSignalHelper lastMessageReceived:nil];
288291
_lastMessageIdFromAction = nil;
292+
lastMessageID = @"";
289293
}
290294

291295
UIBackgroundTaskIdentifier mediaBackgroundTask;
@@ -416,7 +420,6 @@ + (void)handleNotificationReceived:(OSNotificationDisplayType)displayType fromBa
416420
let notification = [[OSNotification alloc] initWithPayload:payload displayType:displayType];
417421

418422
// Prevent duplicate calls to same receive event
419-
static var lastMessageID = @"";
420423
if ([payload.notificationID isEqualToString:lastMessageID])
421424
return;
422425
lastMessageID = payload.notificationID;
@@ -428,8 +431,6 @@ + (void)handleNotificationReceived:(OSNotificationDisplayType)displayType fromBa
428431
handleNotificationReceived(notification);
429432
}
430433

431-
static NSString *_lastMessageIdFromAction;
432-
433434
+ (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(NSString*)actionID displayType:(OSNotificationDisplayType)displayType {
434435
if (![self isOneSignalPayload:lastMessageReceived])
435436
return;

iOS_SDK/OneSignalSDK/Source/OneSignalTrackFirebaseAnalytics.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ @implementation OneSignalTrackFirebaseAnalytics
3636
static NSTimeInterval lastOpenedTime = 0;
3737
static var trackingEnabled = false;
3838

39+
+ (void)resetLocals {
40+
lastOpenedTime = 0;
41+
trackingEnabled = false;
42+
}
43+
3944
+ (BOOL)libraryExists {
4045
return NSClassFromString(@"FIRAnalytics") != nil;
4146
}

iOS_SDK/OneSignalSDK/UnitTests/OutcomeIntegrationTests.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,6 @@ - (void)testSendingOutcomeWithValue_inUnattributedSession {
543543
[RestClientAsserts assertNumberOfMeasureRequests:2];
544544
}
545545

546-
// TODO: This test is flaky, it fails even on it's own sometimes, on step 5
547546
- (void)testSendingOutcomeWithValue_inIndirectSession {
548547
// 1. Open app
549548
[UnitTestCommonMethods initOneSignalAndThreadWait];

iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#import "OneSignalLocation.h"
5050
#import "NSUserDefaultsOverrider.h"
5151
#import "OneSignalNotificationServiceExtensionHandler.h"
52+
#import "OneSignalTrackFirebaseAnalytics.h"
5253

5354
NSString * serverUrlWithPath(NSString *path) {
5455
return [NSString stringWithFormat:@"%@%@%@", SERVER_URL, API_VERSION, path];
@@ -135,6 +136,7 @@ + (void)clearStateForAppRestart:(XCTestCase *)testCase {
135136
[OneSignal setValue:@0 forKeyPath:@"mSubscriptionStatus"];
136137

137138
[OneSignalTracker performSelector:NSSelectorFromString(@"resetLocals")];
139+
[OneSignalTrackFirebaseAnalytics performSelector:NSSelectorFromString(@"resetLocals")];
138140

139141
[NSObjectOverrider reset];
140142

@@ -162,17 +164,16 @@ + (void)beforeAllTest {
162164
if (setupUIApplicationDelegate)
163165
return;
164166

167+
// Force swizzle in all methods for tests.
168+
OneSignalHelperOverrider.mockIOSVersion = 8;
169+
165170
// Normally this just loops internally, overwrote _run to work around this.
166171
UIApplicationMain(0, nil, nil, NSStringFromClass([UnitTestAppDelegate class]));
167172

168173
setupUIApplicationDelegate = true;
169174

170175
// InstallUncaughtExceptionHandler();
171176

172-
// Force swizzle in all methods for tests.
173-
OneSignalHelperOverrider.mockIOSVersion = 8;
174-
[OneSignalAppDelegate sizzlePreiOS10MethodsPhase1];
175-
[OneSignalAppDelegate sizzlePreiOS10MethodsPhase2];
176177
OneSignalHelperOverrider.mockIOSVersion = 10;
177178
}
178179

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ - (void)testFirebaseAnalyticsInfluenceNotificationOpen {
10111011
// Notification is recieved.
10121012
// The Notification Service Extension runs where the notification received id tracked.
10131013
// Note: This is normally a separate process but can't emulate that here.
1014-
UNNotificationResponse *response = [self createNotificationResponseForAnalyticsTests];
1014+
let response = [self createNotificationResponseForAnalyticsTests];
10151015
[OneSignal didReceiveNotificationExtensionRequest:response.notification.request
10161016
withMutableNotificationContent:nil];
10171017

@@ -1027,8 +1027,9 @@ - (void)testFirebaseAnalyticsInfluenceNotificationOpen {
10271027
XCTAssertEqualObjects(OneSignalTrackFirebaseAnalyticsOverrider.loggedEvents[0], received_event);
10281028

10291029
// Trigger a new app session
1030-
[self backgroundApp];
1031-
NSDateOverrider.timeOffset = 41;
1030+
[UnitTestCommonMethods backgroundApp];
1031+
[UnitTestCommonMethods runBackgroundThreads];
1032+
[NSDateOverrider advanceSystemTimeBy:41];
10321033
[UnitTestCommonMethods foregroundApp];
10331034
[UnitTestCommonMethods runBackgroundThreads];
10341035

@@ -1254,17 +1255,18 @@ - (void)testOpeningWithAdditionalData {
12541255

12551256
[UnitTestCommonMethods runBackgroundThreads];
12561257

1257-
id userInfo = @{@"custom": @{
1258+
let userInfo = @{@"custom": @{
12581259
@"i": @"b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
12591260
@"a": @{ @"foo": @"bar" }
12601261
}};
12611262

1262-
id notifResponse = [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:userInfo];
1263-
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
1264-
id notifCenterDelegate = notifCenter.delegate;
1263+
let notifResponse = [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:userInfo];
1264+
let notifCenter = UNUserNotificationCenter.currentNotificationCenter;
1265+
let notifCenterDelegate = notifCenter.delegate;
12651266

12661267
// UNUserNotificationCenterDelegate method iOS 10 calls directly when a notification is opend.
12671268
[notifCenterDelegate userNotificationCenter:notifCenter didReceiveNotificationResponse:notifResponse withCompletionHandler:^() {}];
1269+
[UnitTestCommonMethods runBackgroundThreads];
12681270
XCTAssertEqual(openedWasFire, true);
12691271

12701272
// Part 2 - New paylaod test
@@ -1300,7 +1302,7 @@ - (void)receivedCallbackWithButtonsWithUserInfo:(NSDictionary *)userInfo {
13001302
[UnitTestCommonMethods runBackgroundThreads];
13011303

13021304
let notifResponse = [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:userInfo];
1303-
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
1305+
let notifCenter = UNUserNotificationCenter.currentNotificationCenter;
13041306
let notifCenterDelegate = notifCenter.delegate;
13051307

13061308
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateInactive;
@@ -1337,7 +1339,6 @@ - (void)testReceiveNotificationDoesNotSubmitOpenedRequest {
13371339
XCTAssertFalse([OneSignalClientOverrider hasExecutedRequestOfType:[OSRequestSubmitNotificationOpened class]]);
13381340
}
13391341

1340-
// TODO: FIx flaky test carry over, only fails when running all tests in this file
13411342
- (void)testReceivedCallbackWithButtonsWithNewFormat {
13421343
let newFormat = @{@"aps": @{@"content_available": @1},
13431344
@"os_data": @{

0 commit comments

Comments
 (0)