Skip to content

Commit

Permalink
supporting lifecycle observer in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emawby committed Aug 4, 2020
1 parent 9f79fa1 commit 97c8d27
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 8 deletions.
8 changes: 8 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@
DE16C14424D3724700670EFA /* OneSignalLifecycleObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */; };
DE16C14524D3724700670EFA /* OneSignalLifecycleObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */; };
DE16C14724D3727200670EFA /* OneSignalLifecycleObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = DE16C14624D3727200670EFA /* OneSignalLifecycleObserver.h */; };
DE16C17024D3989A00670EFA /* OneSignalLifecycleObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */; };
DE5EFECA24D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m in Sources */ = {isa = PBXBuildFile; fileRef = DE5EFEC924D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -687,6 +689,8 @@
CACBAAAB218A662B000ACAA5 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalLifecycleObserver.m; sourceTree = "<group>"; };
DE16C14624D3727200670EFA /* OneSignalLifecycleObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalLifecycleObserver.h; sourceTree = "<group>"; };
DE5EFEC924D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSInAppMessageViewControllerOverrider.m; sourceTree = "<group>"; };
DE5EFECB24D8DC0E0032632D /* OSInAppMessageViewControllerOverrider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageViewControllerOverrider.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -825,6 +829,8 @@
CAAE0DFC2195216900A57402 /* OneSignalOverrider.m */,
9D348538233D2DCF00EB81C9 /* OneSignalLocationOverrider.h */,
9D348539233D2E3600EB81C9 /* OneSignalLocationOverrider.m */,
DE5EFECB24D8DC0E0032632D /* OSInAppMessageViewControllerOverrider.h */,
DE5EFEC924D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m */,
);
path = Shadows;
sourceTree = "<group>";
Expand Down Expand Up @@ -1703,6 +1709,7 @@
CAABF34D205B157B0042F8E5 /* OneSignalExtensionBadgeHandler.m in Sources */,
912412301E73342200E41FD7 /* OneSignalSelectorHelpers.m in Sources */,
91F58D851E7C88230017D24D /* OneSignalNotificationSettingsIOS10.m in Sources */,
DE16C17024D3989A00670EFA /* OneSignalLifecycleObserver.m in Sources */,
CAAE0DFD2195216900A57402 /* OneSignalOverrider.m in Sources */,
912412241E73342200E41FD7 /* OneSignalLocation.m in Sources */,
CA8E190B2194FE0B009DA223 /* OSMessagingControllerOverrider.m in Sources */,
Expand Down Expand Up @@ -1777,6 +1784,7 @@
7AFE856D2368DDB80091D6A5 /* OSFocusCallParams.m in Sources */,
CA8E19082193C76D009DA223 /* OSInAppMessagingHelpers.m in Sources */,
4529DEE11FA82AB300CEAB1D /* NSBundleOverrider.m in Sources */,
DE5EFECA24D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m in Sources */,
7AF986602447C13C00C36EAE /* OSInfluenceDataRepository.m in Sources */,
912412441E73342200E41FD7 /* UNUserNotificationCenter+OneSignal.m in Sources */,
03866CC12378A67B0009C1D8 /* RestClientAsserts.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion iOS_SDK/OneSignalSDK/Source/OneSignalLifecycleObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ + (void)registerLifecycleObserver {
// Replacing swizzled lifecycle selectors with notification center observers for scene based Apps
if (@available(iOS 13.0, *)) {
NSDictionary *sceneManifest = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"];
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"registering for Scene Lifecycle notifications"];
if (sceneManifest) {
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"registering for Scene Lifecycle notifications"];
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(didEnterBackground) name:UISceneDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(didBecomeActive) name:UISceneDidActivateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(willResignActive) name:UISceneWillDeactivateNotification object:nil];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// OSInAppMessageViewControllerOverrider.h
// UnitTests
//
// Created by Elliot Mawby on 8/3/20.
// Copyright © 2020 Hiptic. All rights reserved.
//

@interface OSInAppMessageViewControllerOverrider : NSObject

@end

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Modified MIT License
*
* Copyright 2020 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#import <Foundation/Foundation.h>
#import "OSInAppMessageViewControllerOverrider.h"
#import "OSInAppMessageViewController.h"
#import "OneSignalSelectorHelpers.h"

@implementation OSInAppMessageViewControllerOverrider

+ (void)load {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
injectToProperClass(@selector(overrideAnimateAppearance), @selector(animateAppearance), @[], [OSInAppMessageViewControllerOverrider class], [OSInAppMessageViewController class]);
#pragma clang diagnostic pop
}

- (void)overrideAnimateAppearance {

}

@end
1 change: 1 addition & 0 deletions iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ NSString * serverUrlWithPath(NSString *path);
+ (void)setCurrentNotificationPermissionAsUnanswered;
+ (void)foregroundApp;
+ (void)backgroundApp;
+ (void)useSceneLifecycle:(BOOL)useSceneLifecycle;
+ (void)initOneSignal;
+ (void)initOneSignalAndThreadWait;
+ (void)runBackgroundThreads;
Expand Down
31 changes: 27 additions & 4 deletions iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.m
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,37 @@ + (void)initOneSignalAndThreadWait {

+ (void)foregroundApp {
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateActive;
UIApplication *sharedApp = [UIApplication sharedApplication];
[sharedApp.delegate applicationDidBecomeActive:sharedApp];

if (@available(iOS 13.0, *)) {
NSDictionary *sceneManifest = [[NSBundle bundleForClass:[OneSignal class]] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"];
if (sceneManifest) {
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification object:nil];
return;
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidBecomeActiveNotification object:nil];
}

+ (void)backgroundApp {
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateBackground;
UIApplication *sharedApp = [UIApplication sharedApplication];
[sharedApp.delegate applicationWillResignActive:sharedApp];
if (@available(iOS 13.0, *)) {
NSDictionary *sceneManifest = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"];
if (sceneManifest) {
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillDeactivateNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidEnterBackgroundNotification object:nil];
return;
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];
}

//Call this method before init OneSignal. Make sure not to overwrite the NSBundleDictionary in later calls.
+ (void)useSceneLifecycle:(BOOL)useSceneLifecycle {
NSMutableDictionary *currentBundleDictionary = [[NSMutableDictionary alloc] initWithDictionary:NSBundleOverrider.nsbundleDictionary];
if (useSceneLifecycle)
[currentBundleDictionary setObject:@[@"SceneDelegate"] forKey:@"UIApplicationSceneManifest"];
NSBundleOverrider.nsbundleDictionary = currentBundleDictionary;
}

+ (void)setCurrentNotificationPermission:(BOOL)accepted {
Expand Down
27 changes: 24 additions & 3 deletions iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,17 @@ - (void)registerForPushNotifications {

- (void)backgroundApp {
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateBackground;
UIApplication *sharedApp = [UIApplication sharedApplication];
[sharedApp.delegate applicationWillResignActive:sharedApp];
if (@available(iOS 13.0, *)) {
NSDictionary *sceneManifest = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"];
if (sceneManifest) {
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillDeactivateNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidEnterBackgroundNotification object:nil];
return;
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];

}

- (UNNotificationResponse*)createBasiciOSNotificationResponse {
Expand Down Expand Up @@ -1632,13 +1641,25 @@ - (void)testFirstInitWithNotificationsAlreadyDeclined {
XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 2);
}

- (void)testPermissionChangedInSettingsOutsideOfApp {
- (void)testPermissionChangedInSettingsOutsideOfAppWithAppDelegate {
[self permissionChangedInSettingsOutsideOfApp:NO];
}

- (void)testPermissionChangedInSettingsOutsideOfAppWithSceneDelegate {
[self permissionChangedInSettingsOutsideOfApp:YES];
}

- (void)permissionChangedInSettingsOutsideOfApp: (BOOL)useSceneDelegate {

[UnitTestCommonMethods clearStateForAppRestart:self];

[self backgroundModesDisabledInXcode];
UNUserNotificationCenterOverrider.notifTypesOverride = 0;
UNUserNotificationCenterOverrider.authorizationStatus = [NSNumber numberWithInteger:UNAuthorizationStatusDenied];


[UnitTestCommonMethods useSceneLifecycle: useSceneDelegate];

[UnitTestCommonMethods initOneSignalAndThreadWait];

OSPermissionStateTestObserver* observer = [OSPermissionStateTestObserver new];
Expand Down

0 comments on commit 97c8d27

Please sign in to comment.