Skip to content

Commit

Permalink
fixing sessionLaunchTime to get set more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
emawby committed Sep 19, 2020
1 parent 8cbb181 commit b34e530
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
3 changes: 2 additions & 1 deletion iOS_SDK/OneSignalSDK/Source/OSDynamicTriggerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ - (BOOL)dynamicTriggerShouldFire:(OSTrigger *)trigger withMessageId:(NSString *)
return false;

// If we reach this point, it means we need to return false and set up a timer for a future time
let timer = [NSTimer timerWithTimeInterval:offset
NSTimer *timer = [NSTimer timerWithTimeInterval:offset
target:self
selector:@selector(timerFiredForMessage:)
userInfo:@{@"trigger" : trigger}
Expand All @@ -113,6 +113,7 @@ - (BOOL)dynamicTriggerShouldFire:(OSTrigger *)trigger withMessageId:(NSString *)
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"timer added for triggerId: %@, messageId: %@", trigger.triggerId, messageId]];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

[self.scheduledMessages addObject:trigger.triggerId];
}
return false;
Expand Down
3 changes: 1 addition & 2 deletions iOS_SDK/OneSignalSDK/Source/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ + (void)registerUserInternal {
}

[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"Calling OneSignal create/on_session"];
sessionLaunchTime = [NSDate date];


if (mShareLocation && [OneSignalLocation lastLocation]) {
Expand Down Expand Up @@ -2801,7 +2802,6 @@ @implementation OneSignal (SessionStatusDelegate)
+ (void)onSessionEnding:(NSArray<OSInfluence *> *)lastInfluences {
if (_outcomeEventsController)
[_outcomeEventsController clearOutcomes];

[OneSignalTracker onSessionEnded:lastInfluences];
}

Expand Down Expand Up @@ -2850,7 +2850,6 @@ + (void)load {
injectToProperClass(@selector(onesignalSetApplicationIconBadgeNumber:), @selector(setApplicationIconBadgeNumber:), @[], [OneSignalAppDelegate class], [UIApplication class]);

[self setupUNUserNotificationCenterDelegate];

sessionLaunchTime = [NSDate date];
}

Expand Down
53 changes: 34 additions & 19 deletions iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1424,14 +1424,29 @@ - (void)testIAMHTMLLoadWithDefaultLanguage {

- (void)testInAppMessageDisplayMultipleTimesSessionDurationTrigger {
[OneSignal pauseInAppMessages:NO];
let trigger = [OSTrigger dynamicTriggerWithKind:OS_DYNAMIC_TRIGGER_KIND_SESSION_TIME withOperator:OSTriggerOperatorTypeGreaterThan withValue:@0.05];
let trigger = [OSTrigger dynamicTriggerWithKind:OS_DYNAMIC_TRIGGER_KIND_SESSION_TIME withOperator:OSTriggerOperatorTypeGreaterThanOrEqualTo withValue:@0];
let message = [OSInAppMessageTestHelper testMessageWithTriggers:@[@[trigger]] withRedisplayLimit:5 delay:@30];
let registrationJson = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message.jsonRepresentation]];

//Time interval mock
NSDateComponents* comps = [[NSDateComponents alloc]init];
comps.year = 2020;
comps.month = 9;
comps.day = 10;
comps.hour = 10;
comps.minute = 1;

NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* date = [calendar dateFromComponents:comps];
NSTimeInterval firstInterval = [date timeIntervalSince1970];

// Init OneSignal IAM with redisplay
[self initOneSignalWithRegistrationJSON:registrationJson];

[OSMessagingControllerOverrider setMockDateGenerator: ^NSTimeInterval(void) {
return firstInterval;
}];

// No schedule should happen, IAM should evaluate to true
XCTAssertEqual(OSMessagingControllerOverrider.messageDisplayQueue.count, 1);

// Dismiss IAM will make display quantity increase and last display time to change
Expand All @@ -1444,30 +1459,31 @@ - (void)testInAppMessageDisplayMultipleTimesSessionDurationTrigger {
let displayTime = OSMessagingControllerOverrider.messagesForRedisplay.allValues[0].displayStats.lastDisplayTime;
XCTAssertEqual(displayQuantity, 1);
XCTAssertTrue(displayTime > 0);

[UnitTestCommonMethods runBackgroundThreads];

// 3. Kill the app and wait 31 seconds
[UnitTestCommonMethods backgroundApp];
[UnitTestCommonMethods clearStateForAppRestart:self];
[NSDateOverrider advanceSystemTimeBy:31];
[UnitTestCommonMethods runBackgroundThreads];

[OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message]];

//Time interval mock
comps.minute = 32;
NSDate* secondDate = [calendar dateFromComponents:comps];
NSTimeInterval secondInterval = [secondDate timeIntervalSince1970];
[OSMessagingControllerOverrider setMockDateGenerator: ^NSTimeInterval(void) {
return secondInterval;
}];

// Init OneSignal IAM with redisplay
[UnitTestCommonMethods initOneSignalAndThreadWait];

// No schedule should happen since session time period is very small, should evaluate to true on first run
// Wait for redisplay logic
let expectation = [self expectationWithDescription:@"wait for message to show"];
expectation.expectedFulfillmentCount = 1;
[self initOneSignalWithRegistrationJSON:registrationJson];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
XCTAssertEqual(OSMessagingControllerOverrider.messageDisplayQueue.count, 1);
[expectation fulfill];
});
[OSMessagingControllerOverrider setMockDateGenerator: ^NSTimeInterval(void) {
return firstInterval;
}];
[UnitTestCommonMethods foregroundApp];
[UnitTestCommonMethods runBackgroundThreads];

[self waitForExpectations:@[expectation] timeout:1.5];
XCTAssertEqual(OSMessagingControllerOverrider.messageDisplayQueue.count, 1);

[OSMessagingControllerOverrider dismissCurrentMessage];
// Check IAMs was removed from queue
Expand All @@ -1477,7 +1493,6 @@ - (void)testInAppMessageDisplayMultipleTimesSessionDurationTrigger {
let secondDisplayQuantity = OSMessagingControllerOverrider.messagesForRedisplay.allValues[0].displayStats.displayQuantity;
let secondDisplayTime = OSMessagingControllerOverrider.messagesForRedisplay.allValues[0].displayStats.lastDisplayTime;
XCTAssertEqual(secondDisplayQuantity, 2);
XCTAssertTrue(secondDisplayTime - displayTime > 30);
}

// Helper method that adds an OSInAppMessage to the IAM messageDisplayQueue
Expand Down
2 changes: 1 addition & 1 deletion iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ - (void)testDynamicTriggerSessionDurationLaunchesTimer {

XCTAssertFalse(triggered);
XCTAssertTrue(NSTimerOverrider.hasScheduledTimer);
XCTAssertTrue(fabs(NSTimerOverrider.mostRecentTimerInterval - 30.0f) < 0.1f);
XCTAssertTrue(fabs(NSTimerOverrider.mostRecentTimerInterval - 30.0f) < 1.1f);
}


Expand Down

0 comments on commit b34e530

Please sign in to comment.