| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304 |
- // Copyright 2018 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #import <GoogleUtilities/GULAppDelegateSwizzler.h>
- #import "GoogleUtilities/AppDelegateSwizzler/Internal/GULAppDelegateSwizzler_Private.h"
- #import <OCMock/OCMock.h>
- #import <XCTest/XCTest.h>
- #import <objc/runtime.h>
- #if (defined(__IPHONE_9_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0))
- #define SDK_HAS_USERACTIVITY 1
- #endif
- /** Plist key that allows Firebase developers to disable App Delegate Proxying. Source of truth is
- * the GULAppDelegateSwizzler class.
- */
- static NSString *const kGULFirebaseAppDelegateProxyEnabledPlistKey =
- @"FirebaseAppDelegateProxyEnabled";
- /** Plist key that allows non-Firebase developers to disable App Delegate Proxying. Source of truth
- * is the GULAppDelegateSwizzler class.
- */
- static NSString *const kGULGoogleAppDelegateProxyEnabledPlistKey =
- @"GoogleUtilitiesAppDelegateProxyEnabled";
- #pragma mark - GULTestAppDelegate
- /** This class conforms to the application delegate protocol and is there to be able to test the
- * App Delegate Swizzler's behavior.
- */
- @interface GULTestAppDelegate : NSObject <GULApplicationDelegate> {
- @public // Because we want to access the ivars from outside the class like obj->ivar for testing.
- /** YES if the application:openURL:options: was called on an instance, NO otherwise. */
- BOOL _isOpenURLOptionsMethodCalled;
- /** Contains the backgroundSessionID that was passed to the
- * application:handleEventsForBackgroundURLSession:completionHandler: method.
- */
- NSString *_backgroundSessionID;
- /** YES if init was called. Used for memory layout testing after isa swizzling. */
- BOOL _isInitialized;
- /** An arbitrary number. Used for memory layout testing after isa swizzling. */
- int _arbitraryNumber;
- }
- /** A URL property that is set by the app delegate methods, which is then used to verify if the app
- * delegate methods were properly called.
- */
- @property(nonatomic, strong) NSURL *url;
- @property(nonatomic, strong) NSDictionary<NSString *, id> *openURLOptions;
- @property(nonatomic, strong) NSString *openURLSourceApplication;
- @property(nonatomic, strong) id openURLAnnotation;
- @property(nonatomic, strong) NSUserActivity *userActivity;
- @property(nonatomic, strong) NSData *remoteNotificationsDeviceToken;
- @property(nonatomic, strong) NSError *failToRegisterForRemoteNotificationsError;
- @property(nonatomic, strong) NSDictionary *remoteNotification;
- #if TARGET_OS_IOS || TARGET_OS_TV
- @property(nonatomic, copy) void (^remoteNotificationCompletionHandler)(UIBackgroundFetchResult);
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- /**
- * The application is set each time a GULApplicationDelegate method is called
- */
- @property(nonatomic, weak) GULApplication *application;
- @end
- @implementation GULTestAppDelegate
- // TODO: The static BOOLs below being accurate is dependent on the runtime loading
- // GULTestAppDelegate before GULAppDelegateSwizzlerTest. It works, but it might be a good idea to
- // figure a way to make this more deterministic.
- /** YES if GULTestAppDelegate responds to application:openURL:sourceApplication:annotation:, NO
- * otherwise.
- */
- #if TARGET_OS_IOS || TARGET_OS_TV
- static BOOL gRespondsToOpenURLHandler_iOS8;
- #endif
- /** YES if GULTestAppDelegate responds to application:openURL:options:, NO otherwise. */
- static BOOL gRespondsToOpenURLHandler_iOS9;
- /** YES if GULTestAppDelegate responds to application:continueUserActivity:restorationHandler:, NO
- * otherwise.
- */
- static BOOL gRespondsToContinueUserActivity;
- /** YES if GULTestAppDelegate responds to
- * application:handleEventsForBackgroundURLSession:completionHandler:, NO otherwise.
- */
- static BOOL gRespondsToHandleBackgroundSession;
- + (void)load {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- // Before being proxied, it should be only be able to respond to
- // application:openURL:sourceApplication:annotation:.
- #if TARGET_OS_IOS || TARGET_OS_TV
- gRespondsToOpenURLHandler_iOS8 = [self
- instancesRespondToSelector:@selector(application:openURL:sourceApplication:annotation:)];
- #endif
- gRespondsToOpenURLHandler_iOS9 =
- [self instancesRespondToSelector:@selector(application:openURL:options:)];
- gRespondsToHandleBackgroundSession =
- [self instancesRespondToSelector:@selector(application:
- handleEventsForBackgroundURLSession:completionHandler:)];
- gRespondsToContinueUserActivity = [self
- instancesRespondToSelector:@selector(application:continueUserActivity:restorationHandler:)];
- #pragma clang diagnostic pop
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- _isOpenURLOptionsMethodCalled = NO;
- _isInitialized = YES;
- _arbitraryNumber = 123456789;
- _backgroundSessionID = @"randomSessionID";
- _url = nil;
- }
- return self;
- }
- - (BOOL)application:(GULApplication *)app
- openURL:(NSURL *)url
- options:(NSDictionary<NSString *, id> *)options {
- self.application = app;
- self.url = url;
- self.openURLOptions = options;
- _isOpenURLOptionsMethodCalled = YES;
- return NO;
- }
- #if TARGET_OS_IOS
- - (BOOL)application:(GULApplication *)application
- openURL:(NSURL *)url
- sourceApplication:(NSString *)sourceApplication
- annotation:(id)annotation {
- self.application = application;
- self.url = url;
- self.openURLSourceApplication = sourceApplication;
- self.openURLAnnotation = annotation;
- return NO;
- }
- #endif // TARGET_OS_IOS
- - (BOOL)application:(GULApplication *)application
- continueUserActivity:(NSUserActivity *)userActivity
- restorationHandler:(void (^)(NSArray<id<GULUserActivityRestoring>> *__nullable
- restorableObjects))restorationHandler {
- self.application = application;
- self.userActivity = userActivity;
- return NO;
- }
- - (void)application:(GULApplication *)application
- handleEventsForBackgroundURLSession:(nonnull NSString *)identifier
- completionHandler:(nonnull void (^)(void))completionHandler {
- self.application = application;
- _backgroundSessionID = identifier;
- }
- - (void)application:(GULApplication *)application
- didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- self.application = application;
- self.remoteNotificationsDeviceToken = deviceToken;
- }
- - (void)application:(GULApplication *)application
- didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- self.application = application;
- self.failToRegisterForRemoteNotificationsError = error;
- }
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-implementations"
- - (void)application:(GULApplication *)application
- didReceiveRemoteNotification:(NSDictionary *)userInfo {
- self.application = application;
- self.remoteNotification = userInfo;
- }
- #pragma clang diagnostic pop
- #if TARGET_OS_IOS || TARGET_OS_TV
- - (void)application:(GULApplication *)application
- didReceiveRemoteNotification:(NSDictionary *)userInfo
- fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- self.application = application;
- self.remoteNotification = userInfo;
- self.remoteNotificationCompletionHandler = completionHandler;
- }
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- // These are methods to test whether changing the class still maintains behavior that the app
- // delegate proxy shouldn't have modified.
- - (NSString *)someArbitraryMethod {
- return @"blabla";
- }
- + (int)someNumber {
- return 890;
- }
- @end
- @interface GULEmptyTestAppDelegate : NSObject <GULApplicationDelegate>
- @end
- @implementation GULEmptyTestAppDelegate
- @end
- #pragma mark - Interceptor class
- /** This is a class used to test whether interceptors work with the App Delegate Swizzler. */
- @interface GULTestInterceptorAppDelegate : NSObject <GULApplicationDelegate>
- /** URL sent to application:openURL:options:. */
- @property(nonatomic, copy) NSURL *URLForIOS9;
- /** URL sent to application:openURL:sourceApplication:annotation:. */
- @property(nonatomic, copy) NSURL *URLForIOS8;
- /** The NSUserActivity sent to application:continueUserActivity:restorationHandler:. */
- @property(nonatomic, copy) NSUserActivity *userActivity;
- @end
- @implementation GULTestInterceptorAppDelegate
- - (BOOL)application:(GULApplication *)app
- openURL:(NSURL *)url
- options:(NSDictionary<NSString *, id> *)options {
- _URLForIOS9 = [url copy];
- return YES;
- }
- #if TARGET_OS_IOS
- - (BOOL)application:(GULApplication *)application
- openURL:(nonnull NSURL *)url
- sourceApplication:(nullable NSString *)sourceApplication
- annotation:(nonnull id)annotation {
- _URLForIOS8 = [url copy];
- return YES;
- }
- #endif // TARGET_OS_IOS
- #if SDK_HAS_USERACTIVITY
- - (BOOL)application:(GULApplication *)application
- continueUserActivity:(NSUserActivity *)userActivity
- restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> *__nullable
- restorableObjects))restorationHandler {
- _userActivity = userActivity;
- return YES;
- }
- #endif // SDK_HAS_USERACTIVITY
- @end
- @interface GULAppDelegateSwizzlerTest : XCTestCase
- @property(nonatomic, strong) id mockSharedApplication;
- @end
- @implementation GULAppDelegateSwizzlerTest
- - (void)setUp {
- [super setUp];
- self.mockSharedApplication = OCMPartialMock([GULApplication sharedApplication]);
- }
- - (void)tearDown {
- [GULAppDelegateSwizzler clearInterceptors];
- [GULAppDelegateSwizzler resetProxyOriginalDelegateOnceToken];
- self.mockSharedApplication = nil;
- [super tearDown];
- }
- - (void)testNotAppDelegateIsNotSwizzled {
- NSObject *notAppDelegate = [[NSObject alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(notAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- XCTAssertEqualObjects(NSStringFromClass([notAppDelegate class]), @"NSObject");
- }
- /** Tests proxying an object that responds to application delegate protocol and makes sure that
- * it is isa swizzled and that the object after proxying responds to the expected methods
- * and doesn't have its ivars modified.
- */
- - (void)testProxyAppDelegate {
- GULTestAppDelegate *realAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(realAppDelegate);
- size_t sizeBefore = class_getInstanceSize([GULTestAppDelegate class]);
- Class realAppDelegateClassBefore = [realAppDelegate class];
- // Create the proxy.
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- XCTAssertTrue([realAppDelegate isKindOfClass:[GULTestAppDelegate class]]);
- NSString *newClassName = NSStringFromClass([realAppDelegate class]);
- XCTAssertTrue([newClassName hasPrefix:@"GUL_"]);
- // It is no longer GULTestAppDelegate class instance.
- XCTAssertFalse([realAppDelegate isMemberOfClass:[GULTestAppDelegate class]]);
- size_t sizeAfter = class_getInstanceSize([realAppDelegate class]);
- // Class size must stay the same.
- XCTAssertEqual(sizeBefore, sizeAfter);
- // After being proxied, it should be able to respond to the required method selector.
- #if TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:openURL:sourceApplication:annotation:)]);
- #endif // TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:
- continueUserActivity:restorationHandler:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)]);
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:
- didReceiveRemoteNotification:)]);
- #if TARGET_OS_IOS || TARGET_OS_TV
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:openURL:options:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:
- handleEventsForBackgroundURLSession:completionHandler:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:
- didReceiveRemoteNotification:fetchCompletionHandler:)]);
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- // Make sure that the class has changed.
- XCTAssertNotEqualObjects([realAppDelegate class], realAppDelegateClassBefore);
- // Make sure that the ivars are not changed in memory as the subclass is created. Directly
- // accessing the ivars should not crash.
- XCTAssertEqual(realAppDelegate->_arbitraryNumber, 123456789);
- XCTAssertEqual(realAppDelegate->_isInitialized, 1);
- XCTAssertFalse(realAppDelegate->_isOpenURLOptionsMethodCalled);
- XCTAssertEqualObjects(realAppDelegate->_backgroundSessionID, @"randomSessionID");
- }
- - (void)testProxyEmptyAppDelegate {
- GULEmptyTestAppDelegate *realAppDelegate = [[GULEmptyTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(realAppDelegate);
- size_t sizeBefore = class_getInstanceSize([GULEmptyTestAppDelegate class]);
- Class realAppDelegateClassBefore = [realAppDelegate class];
- // Create the proxy.
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- XCTAssertTrue([realAppDelegate isKindOfClass:[GULEmptyTestAppDelegate class]]);
- NSString *newClassName = NSStringFromClass([realAppDelegate class]);
- XCTAssertTrue([newClassName hasPrefix:@"GUL_"]);
- // It is no longer GULTestAppDelegate class instance.
- XCTAssertFalse([realAppDelegate isMemberOfClass:[GULEmptyTestAppDelegate class]]);
- size_t sizeAfter = class_getInstanceSize([realAppDelegate class]);
- // Class size must stay the same.
- XCTAssertEqual(sizeBefore, sizeAfter);
- // After being proxied, it should be able to respond to the required method selector.
- #if TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:openURL:sourceApplication:annotation:)]);
- #endif // TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:
- continueUserActivity:restorationHandler:)]);
- // Remote notifications methods should be added only by
- // -proxyOriginalDelegateIncludingAPNSMethods
- XCTAssertFalse([realAppDelegate
- respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]);
- XCTAssertFalse([realAppDelegate
- respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)]);
- XCTAssertFalse([realAppDelegate respondsToSelector:@selector(application:
- didReceiveRemoteNotification:)]);
- #if TARGET_OS_IOS || TARGET_OS_TV
- // The implementation should not be added if there is no original implementation
- XCTAssertFalse([realAppDelegate respondsToSelector:@selector(application:openURL:options:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:
- handleEventsForBackgroundURLSession:completionHandler:)]);
- XCTAssertFalse([realAppDelegate
- respondsToSelector:@selector(application:
- didReceiveRemoteNotification:fetchCompletionHandler:)]);
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- // Make sure that the class has changed.
- XCTAssertNotEqualObjects([realAppDelegate class], realAppDelegateClassBefore);
- }
- - (void)testProxyRemoteNotificationsMethodsEmptyAppDelegate {
- GULEmptyTestAppDelegate *realAppDelegate = [[GULEmptyTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(realAppDelegate);
- size_t sizeBefore = class_getInstanceSize([GULEmptyTestAppDelegate class]);
- Class realAppDelegateClassBefore = [realAppDelegate class];
- // Create the proxy.
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- XCTAssertTrue([realAppDelegate isKindOfClass:[GULEmptyTestAppDelegate class]]);
- NSString *newClassName = NSStringFromClass([realAppDelegate class]);
- XCTAssertTrue([newClassName hasPrefix:@"GUL_"]);
- // It is no longer GULTestAppDelegate class instance.
- XCTAssertFalse([realAppDelegate isMemberOfClass:[GULEmptyTestAppDelegate class]]);
- size_t sizeAfter = class_getInstanceSize([realAppDelegate class]);
- // Class size must stay the same.
- XCTAssertEqual(sizeBefore, sizeAfter);
- // After being proxied, it should be able to respond to the required method selector.
- #if TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:openURL:sourceApplication:annotation:)]);
- #endif // TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:
- continueUserActivity:restorationHandler:)]);
- // Remote notifications methods should be added only by
- // -proxyOriginalDelegateIncludingAPNSMethods
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)]);
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:
- didReceiveRemoteNotification:)]);
- #if TARGET_OS_IOS || TARGET_OS_TV
- // The implementation should not be added if there is no original implementation
- XCTAssertFalse([realAppDelegate respondsToSelector:@selector(application:openURL:options:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:
- handleEventsForBackgroundURLSession:completionHandler:)]);
- // The implementation should not be added if there is no original implementation
- XCTAssertFalse([realAppDelegate
- respondsToSelector:@selector(application:
- didReceiveRemoteNotification:fetchCompletionHandler:)]);
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- // Make sure that the class has changed.
- XCTAssertNotEqualObjects([realAppDelegate class], realAppDelegateClassBefore);
- }
- - (void)testProxyRemoteNotificationsMethodsEmptyAppDelegateAfterInitialProxy {
- GULEmptyTestAppDelegate *realAppDelegate = [[GULEmptyTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(realAppDelegate);
- size_t sizeBefore = class_getInstanceSize([GULEmptyTestAppDelegate class]);
- Class realAppDelegateClassBefore = [realAppDelegate class];
- // Create the proxy.
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- XCTAssertTrue([realAppDelegate isKindOfClass:[GULEmptyTestAppDelegate class]]);
- NSString *newClassName = NSStringFromClass([realAppDelegate class]);
- XCTAssertTrue([newClassName hasPrefix:@"GUL_"]);
- // It is no longer GULTestAppDelegate class instance.
- XCTAssertFalse([realAppDelegate isMemberOfClass:[GULEmptyTestAppDelegate class]]);
- size_t sizeAfter = class_getInstanceSize([realAppDelegate class]);
- // Class size must stay the same.
- XCTAssertEqual(sizeBefore, sizeAfter);
- // After being proxied, it should be able to respond to the required method selector.
- #if TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:openURL:sourceApplication:annotation:)]);
- #endif // TARGET_OS_IOS
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:
- continueUserActivity:restorationHandler:)]);
- // Proxy remote notifications methods
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)]);
- XCTAssertTrue([realAppDelegate respondsToSelector:@selector(application:
- didReceiveRemoteNotification:)]);
- #if TARGET_OS_IOS || TARGET_OS_TV
- // The implementation should not be added if there is no original implementation
- XCTAssertFalse([realAppDelegate respondsToSelector:@selector(application:openURL:options:)]);
- XCTAssertTrue([realAppDelegate
- respondsToSelector:@selector(application:
- handleEventsForBackgroundURLSession:completionHandler:)]);
- // The implementation should not be added if there is no original implementation
- XCTAssertFalse([realAppDelegate
- respondsToSelector:@selector(application:
- didReceiveRemoteNotification:fetchCompletionHandler:)]);
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- // Make sure that the class has changed.
- XCTAssertNotEqualObjects([realAppDelegate class], realAppDelegateClassBefore);
- }
- #if SDK_HAS_USERACTIVITY
- - (void)testHandleBackgroundSessionMethod {
- GULTestAppDelegate *realAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(realAppDelegate);
- // Create the proxy.
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- GULApplication *currentApplication = [GULApplication sharedApplication];
- NSString *sessionID = @"123";
- void (^nilHandler)(void) = nil;
- [realAppDelegate application:currentApplication
- handleEventsForBackgroundURLSession:sessionID
- completionHandler:nilHandler];
- // Intentionally access the ivars directly. It should be set to the session ID as the real method
- // is called.
- XCTAssertEqualObjects(realAppDelegate->_backgroundSessionID, sessionID);
- }
- #endif // SDK_HAS_USERACTIVITY
- /** Tests registering and unregistering invalid interceptors. */
- - (void)testInvalidInterceptor {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wnonnull"
- XCTAssertThrows([GULAppDelegateSwizzler registerAppDelegateInterceptor:nil],
- @"Should not register nil interceptor");
- #pragma clang diagnostic pop
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 0);
- // Try to register some random object that does not conform to application delegate.
- NSObject *randomObject = [[NSObject alloc] init];
- XCTAssertThrows([GULAppDelegateSwizzler
- registerAppDelegateInterceptor:(id<GULApplicationDelegate>)randomObject],
- @"Should not register interceptor that does not conform to %@Delegate",
- kGULApplicationClassName);
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 0);
- GULTestInterceptorAppDelegate *interceptorAppDelegate =
- [[GULTestInterceptorAppDelegate alloc] init];
- GULAppDelegateInterceptorID interceptorID =
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptorAppDelegate];
- XCTAssertNotNil(interceptorID);
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 1);
- // Register the same object. Should not change the number of objects.
- XCTAssertNotNil([GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptorAppDelegate]);
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 1);
- XCTAssertThrows([GULAppDelegateSwizzler unregisterAppDelegateInterceptorWithID:@""],
- @"Should not unregister empty interceptor ID");
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 1);
- // Try to unregister an empty string. Should not remove anything.
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wnonnull"
- XCTAssertThrows([GULAppDelegateSwizzler unregisterAppDelegateInterceptorWithID:nil],
- @"Should not unregister nil interceptorID");
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 1);
- // Try to unregister a random string. Should not remove anything.
- [GULAppDelegateSwizzler unregisterAppDelegateInterceptorWithID:@"random ID"];
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 1);
- // Unregister the right one.
- [GULAppDelegateSwizzler unregisterAppDelegateInterceptorWithID:interceptorID];
- XCTAssertEqual([GULAppDelegateSwizzler interceptors].count, 0);
- }
- /** Tests that the description of appDelegate object doesn't change even after proxying it. */
- - (void)testDescription {
- GULTestAppDelegate *realAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(realAppDelegate);
- Class classBefore = [realAppDelegate class];
- NSString *descriptionBefore = [realAppDelegate description];
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- Class classAfter = [realAppDelegate class];
- NSString *descriptionAfter = [realAppDelegate description];
- NSString *descriptionString =
- [NSString stringWithFormat:@"<GULTestAppDelegate: %p>", realAppDelegate];
- // The description must be the same even though the class has changed.
- XCTAssertEqualObjects(descriptionBefore, descriptionAfter);
- XCTAssertNotEqualObjects(classAfter, classBefore);
- XCTAssertEqualObjects(descriptionAfter, descriptionString);
- }
- /** Tests that methods that are not overriden by the App Delegate Proxy still work as expected. */
- - (void)testNotOverriddenMethods {
- GULTestAppDelegate *realAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(realAppDelegate);
- // Create the proxy.
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- // Make sure that original class instance method still works.
- XCTAssertEqualObjects([realAppDelegate someArbitraryMethod], @"blabla");
- // Make sure that the new subclass inherits the original class method.
- XCTAssertEqual([[realAppDelegate class] someNumber], 890);
- // Make sure that the original class still works.
- XCTAssertEqual([GULTestAppDelegate someNumber], 890);
- }
- /** Tests that if the app delegate changes after it has been proxied, the App Delegate Swizzler
- * handles it correctly.
- */
- - (void)testAppDelegateInstance {
- // The test logic involves using KVC on the UIApplication.delegate propery. This does not really
- // work well with OCMPartialMock([GULApplication sharedApplication]) and triggers issue
- // https://github.com/erikdoe/ocmock/issues/346.
- // Let's stop mocking the shared application for this particular test.
- [self.mockSharedApplication stopMocking];
- self.mockSharedApplication = nil;
- GULTestAppDelegate *realAppDelegate = [[GULTestAppDelegate alloc] init];
- [GULApplication sharedApplication].delegate = realAppDelegate;
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- XCTAssertEqualObjects([GULAppDelegateSwizzler originalDelegate], realAppDelegate);
- GULTestInterceptorAppDelegate *anotherAppDelegate = [[GULTestInterceptorAppDelegate alloc] init];
- XCTAssertNotEqualObjects(realAppDelegate, anotherAppDelegate);
- [GULApplication sharedApplication].delegate = anotherAppDelegate;
- // Make sure that the new delegate is swizzled out and set correctly.
- XCTAssertNil([GULAppDelegateSwizzler originalDelegate]);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- // Swizzling of an updated app delegate is not supported so far.
- XCTAssertNil([GULAppDelegateSwizzler originalDelegate]);
- }
- #pragma mark - Tests the behaviour with interceptors
- #if TARGET_OS_IOS || TARGET_OS_TV
- /** Tests that application:openURL:options: is invoked on the interceptor if it exists. */
- - (void)testApplicationOpenURLOptionsIsInvokedOnInterceptors {
- if (@available(iOS 10, *)) {
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
- .andReturn(NO);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
- .andReturn(NO);
- NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
- NSDictionary *testOpenURLOptions = @{UIApplicationOpenURLOptionUniversalLinksOnly : @"test"};
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- options:testOpenURLOptions];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- // Check that original implementation was called with proper parameters
- XCTAssertEqual(testAppDelegate.application, [GULApplication sharedApplication]);
- XCTAssertEqual(testAppDelegate.url, testURL);
- }
- }
- /** Tests that the result of application:openURL:options: from all interceptors is ORed. */
- - (void)testResultOfApplicationOpenURLOptionsIsORed {
- if (@available(iOS 10, *)) {
- NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
- NSDictionary *testOpenURLOptions = @{UIApplicationOpenURLOptionUniversalLinksOnly : @"test"};
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- BOOL shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- options:testOpenURLOptions];
- // Verify that the original app delegate returns NO.
- XCTAssertFalse(shouldOpen);
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
- .andReturn(NO);
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- options:testOpenURLOptions];
- // Verify that if the only interceptor returns NO, the value is still NO.
- XCTAssertFalse(shouldOpen);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
- .andReturn(YES);
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
- .andReturn(NO);
- shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- options:testOpenURLOptions];
- // Verify that if one of the two interceptors returns YES, the value is YES.
- XCTAssertTrue(shouldOpen);
- }
- }
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- #if TARGET_OS_IOS
- /** Tests that application:openURL:sourceApplication:annotation: is invoked on the interceptors if
- * it exists.
- */
- - (void)testApplicationOpenURLSourceApplicationAnnotationIsInvokedOnInterceptors {
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:OCMOCK_ANY
- openURL:OCMOCK_ANY
- sourceApplication:OCMOCK_ANY
- annotation:OCMOCK_ANY])
- .andReturn(NO);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:OCMOCK_ANY
- openURL:OCMOCK_ANY
- sourceApplication:OCMOCK_ANY
- annotation:OCMOCK_ANY])
- .andReturn(NO);
- NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- NSString *sourceApplication = @"testApp";
- NSString *annotation = @"testAnnotation";
- [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- sourceApplication:sourceApplication
- annotation:annotation];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- // Check that original implementation was called with proper parameters
- XCTAssertEqual(testAppDelegate.application, [GULApplication sharedApplication]);
- XCTAssertEqual(testAppDelegate.url, testURL);
- XCTAssertEqual(testAppDelegate.openURLSourceApplication, sourceApplication);
- XCTAssertEqual(testAppDelegate.openURLAnnotation, annotation);
- }
- /** Tests that the result of application:openURL:sourceApplication:annotation: from all interceptors
- * is ORed.
- */
- - (void)testApplicationOpenURLSourceApplicationAnnotationResultIsORed {
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- BOOL shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- sourceApplication:@"test"
- annotation:@"test"];
- // Verify that without interceptors the result is NO.
- XCTAssertFalse(shouldOpen);
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:OCMOCK_ANY
- openURL:OCMOCK_ANY
- sourceApplication:OCMOCK_ANY
- annotation:OCMOCK_ANY])
- .andReturn(NO);
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- sourceApplication:@"test"
- annotation:@"test"];
- // The result is still NO if the only interceptor returns NO.
- XCTAssertFalse(shouldOpen);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:OCMOCK_ANY
- openURL:OCMOCK_ANY
- sourceApplication:OCMOCK_ANY
- annotation:OCMOCK_ANY])
- .andReturn(YES);
- OCMExpect([interceptor application:OCMOCK_ANY
- openURL:OCMOCK_ANY
- sourceApplication:OCMOCK_ANY
- annotation:OCMOCK_ANY])
- .andReturn(NO);
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
- openURL:testURL
- sourceApplication:@"test"
- annotation:@"test"];
- // The result is YES if one of the interceptors returns YES.
- XCTAssertTrue(shouldOpen);
- }
- #endif // TARGET_OS_IOS
- #if TARGET_OS_IOS || TARGET_OS_TV
- /** Tests that application:handleEventsForBackgroundURLSession:completionHandler: is invoked on the
- * interceptors if it exists.
- */
- - (void)testApplicationHandleEventsForBackgroundURLSessionIsInvokedOnInterceptors {
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:OCMOCK_ANY
- handleEventsForBackgroundURLSession:OCMOCK_ANY
- completionHandler:OCMOCK_ANY]);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:OCMOCK_ANY
- handleEventsForBackgroundURLSession:OCMOCK_ANY
- completionHandler:OCMOCK_ANY]);
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- NSString *backgroundSessionID = @"testBackgroundSessionID";
- [testAppDelegate application:[GULApplication sharedApplication]
- handleEventsForBackgroundURLSession:backgroundSessionID
- completionHandler:^{
- }];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- // Check that original implementation was called with proper parameters
- XCTAssertEqual(testAppDelegate.application, [GULApplication sharedApplication]);
- XCTAssertEqual(testAppDelegate->_backgroundSessionID, backgroundSessionID);
- }
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- /** Tests that application:continueUserActivity:restorationHandler: is invoked on the interceptors
- * if it exists.
- */
- - (void)testApplicationContinueUserActivityRestorationHandlerIsInvokedOnInterceptors {
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:OCMOCK_ANY
- continueUserActivity:OCMOCK_ANY
- restorationHandler:OCMOCK_ANY])
- .andReturn(NO);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:OCMOCK_ANY
- continueUserActivity:OCMOCK_ANY
- restorationHandler:OCMOCK_ANY])
- .andReturn(NO);
- NSUserActivity *testUserActivity = [[NSUserActivity alloc] initWithActivityType:@"test"];
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- [testAppDelegate application:[GULApplication sharedApplication]
- continueUserActivity:testUserActivity
- restorationHandler:^(NSArray *restorableObjects){
- }];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- // Check that original implementation was called with proper parameters
- XCTAssertEqual(testAppDelegate.application, [GULApplication sharedApplication]);
- XCTAssertEqual(testAppDelegate.userActivity, testUserActivity);
- }
- /** Tests that the results of application:continueUserActivity:restorationHandler: from the
- * interceptors are ORed.
- */
- - (void)testApplicationContinueUserActivityRestorationHandlerResultsAreORed {
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- NSUserActivity *testUserActivity = [[NSUserActivity alloc] initWithActivityType:@"test"];
- BOOL shouldContinueUserActvitiy = [testAppDelegate application:[GULApplication sharedApplication]
- continueUserActivity:testUserActivity
- restorationHandler:^(NSArray *restorableObjects){
- }];
- // Verify that it is NO when there are no interceptors.
- XCTAssertFalse(shouldContinueUserActvitiy);
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:OCMOCK_ANY
- continueUserActivity:OCMOCK_ANY
- restorationHandler:OCMOCK_ANY])
- .andReturn(NO);
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- shouldContinueUserActvitiy = [testAppDelegate application:[GULApplication sharedApplication]
- continueUserActivity:testUserActivity
- restorationHandler:^(NSArray *restorableObjects){
- }];
- // Verify that it is NO when the only interceptor returns a NO.
- XCTAssertFalse(shouldContinueUserActvitiy);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:OCMOCK_ANY
- continueUserActivity:OCMOCK_ANY
- restorationHandler:OCMOCK_ANY])
- .andReturn(YES);
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- OCMExpect([interceptor application:OCMOCK_ANY
- continueUserActivity:OCMOCK_ANY
- restorationHandler:OCMOCK_ANY])
- .andReturn(NO);
- shouldContinueUserActvitiy = [testAppDelegate application:[GULApplication sharedApplication]
- continueUserActivity:testUserActivity
- restorationHandler:^(NSArray *restorableObjects){
- }];
- // The result is YES if one of the interceptors returns YES.
- XCTAssertTrue(shouldContinueUserActvitiy);
- }
- - (void)testApplicationDidRegisterForRemoteNotificationsIsInvokedOnInterceptors {
- NSData *deviceToken = [NSData data];
- GULApplication *application = [GULApplication sharedApplication];
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:application
- didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:application
- didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]);
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- [testAppDelegate application:application
- didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- XCTAssertEqual(testAppDelegate.application, application);
- XCTAssertEqual(testAppDelegate.remoteNotificationsDeviceToken, deviceToken);
- }
- - (void)testApplicationDidFailToRegisterForRemoteNotificationsIsInvokedOnInterceptors {
- NSError *error = [NSError errorWithDomain:@"test" code:-1 userInfo:nil];
- GULApplication *application = [GULApplication sharedApplication];
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:application
- didFailToRegisterForRemoteNotificationsWithError:error]);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:application
- didFailToRegisterForRemoteNotificationsWithError:error]);
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- [testAppDelegate application:application didFailToRegisterForRemoteNotificationsWithError:error];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- XCTAssertEqual(testAppDelegate.application, application);
- XCTAssertEqual(testAppDelegate.failToRegisterForRemoteNotificationsError, error);
- }
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- - (void)testApplicationDidReceiveRemoteNotificationIsInvokedOnInterceptors {
- NSDictionary *notification = @{};
- GULApplication *application = [GULApplication sharedApplication];
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:application didReceiveRemoteNotification:notification]);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:application didReceiveRemoteNotification:notification]);
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- [testAppDelegate application:application didReceiveRemoteNotification:notification];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- XCTAssertEqual(testAppDelegate.application, application);
- XCTAssertEqual(testAppDelegate.remoteNotification, notification);
- }
- #pragma clang diagnostic pop
- #if TARGET_OS_IOS || TARGET_OS_TV
- - (void)testApplicationDidReceiveRemoteNotificationWithCompletionIsInvokedOnInterceptors {
- NSDictionary *notification = @{};
- GULApplication *application = [GULApplication sharedApplication];
- void (^completion)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result) {
- };
- id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor application:application
- didReceiveRemoteNotification:notification
- fetchCompletionHandler:completion]);
- id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
- OCMExpect([interceptor2 application:application
- didReceiveRemoteNotification:notification
- fetchCompletionHandler:completion]);
- GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
- [GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
- [testAppDelegate application:application
- didReceiveRemoteNotification:notification
- fetchCompletionHandler:completion];
- OCMVerifyAll(interceptor);
- OCMVerifyAll(interceptor2);
- XCTAssertEqual(testAppDelegate.application, application);
- XCTAssertEqual(testAppDelegate.remoteNotification, notification);
- XCTAssertEqual(testAppDelegate.remoteNotificationCompletionHandler, completion);
- }
- - (void)testApplicationDidReceiveRemoteNotificationWithCompletionImplementationIsNotAdded {
- // The delegate without application:didReceiveRemoteNotification:fetchCompletionHandler:
- // implementation
- GULTestInterceptorAppDelegate *legacyDelegate = [[GULTestInterceptorAppDelegate alloc] init];
- OCMStub([self.mockSharedApplication delegate]).andReturn(legacyDelegate);
- XCTAssertFalse([legacyDelegate
- respondsToSelector:@selector(application:
- didReceiveRemoteNotification:fetchCompletionHandler:)]);
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- XCTAssertFalse([legacyDelegate
- respondsToSelector:@selector(application:
- didReceiveRemoteNotification:fetchCompletionHandler:)]);
- }
- #endif // TARGET_OS_IOS || TARGET_OS_TV
- #pragma mark - Tests to test that Plist flag is honored
- /** Tests that app delegate proxy is enabled when there is no Info.plist dictionary. */
- - (void)testAppProxyPlistFlag_NoFlag {
- // No keys anywhere. If there is no key, the default should be enabled.
- NSDictionary *mainDictionary = nil;
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that app delegate proxy is enabled when there is neither the Firebase nor the non-Firebase
- * Info.plist key present.
- */
- - (void)testAppProxyPlistFlag_NoAppDelegateProxyKey {
- // No app delegate disable key. If there is no key, the default should be enabled.
- NSDictionary *mainDictionary = @{@"randomKey" : @"randomValue"};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that app delegate proxy is enabled when the Firebase plist is explicitly set to YES and
- * the Google flag is not present. */
- - (void)testAppProxyPlistFlag_FirebaseEnabled {
- // Set proxy enabled to YES.
- NSDictionary *mainDictionary = @{kGULFirebaseAppDelegateProxyEnabledPlistKey : @(YES)};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that app delegate proxy is enabled when the Google plist is explicitly set to YES and the
- * Firebase flag is not present. */
- - (void)testAppProxyPlistFlag_GoogleEnabled {
- // Set proxy enabled to YES.
- NSDictionary *mainDictionary = @{kGULGoogleAppDelegateProxyEnabledPlistKey : @(YES)};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate proxy is enabled when the Firebase flag has the wrong type of value
- * and the Google flag is not present. */
- - (void)testAppProxyPlist_WrongFirebaseDisableFlagValueType {
- // Set proxy enabled to "NO" - a string.
- NSDictionary *mainDictionary = @{kGULFirebaseAppDelegateProxyEnabledPlistKey : @"NO"};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate proxy is enabled when the Google flag has the wrong type of value
- * and the Firebase flag is not present. */
- - (void)testAppProxyPlist_WrongGoogleDisableFlagValueType {
- // Set proxy enabled to "NO" - a string.
- NSDictionary *mainDictionary = @{kGULGoogleAppDelegateProxyEnabledPlistKey : @"NO"};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate proxy is disabled when the Firebase flag is set to NO and the Google
- * flag is not present. */
- - (void)testAppProxyPlist_FirebaseDisableFlag {
- // Set proxy enabled to NO.
- NSDictionary *mainDictionary = @{kGULFirebaseAppDelegateProxyEnabledPlistKey : @(NO)};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertFalse([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate proxy is disabled when the Google flag is set to NO and the Firebase
- * flag is not present. */
- - (void)testAppProxyPlist_GoogleDisableFlag {
- // Set proxy enabled to NO.
- NSDictionary *mainDictionary = @{kGULGoogleAppDelegateProxyEnabledPlistKey : @(NO)};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertFalse([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate proxy is disabled when the Google flag is set to NO and the Firebase
- * flag is set to YES. */
- - (void)testAppProxyPlist_GoogleDisableFlagFirebaseEnableFlag {
- // Set proxy enabled to NO.
- NSDictionary *mainDictionary = @{
- kGULGoogleAppDelegateProxyEnabledPlistKey : @(NO),
- kGULFirebaseAppDelegateProxyEnabledPlistKey : @(YES)
- };
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertFalse([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate proxy is disabled when the Google flag is set to NO and the Firebase
- * flag is set to YES. */
- - (void)testAppProxyPlist_FirebaseDisableFlagGoogleEnableFlag {
- // Set proxy enabled to NO.
- NSDictionary *mainDictionary = @{
- kGULGoogleAppDelegateProxyEnabledPlistKey : @(YES),
- kGULFirebaseAppDelegateProxyEnabledPlistKey : @(NO)
- };
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertFalse([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate proxy is disabled when the Google flag is set to NO and the Firebase
- * flag is set to NO. */
- - (void)testAppProxyPlist_FirebaseDisableFlagGoogleDisableFlag {
- // Set proxy enabled to NO.
- NSDictionary *mainDictionary = @{
- kGULGoogleAppDelegateProxyEnabledPlistKey : @(NO),
- kGULFirebaseAppDelegateProxyEnabledPlistKey : @(NO)
- };
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock expect] andReturn:mainDictionary] infoDictionary];
- XCTAssertFalse([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- [mainBundleMock stopMocking];
- }
- /** Tests that the App Delegate is not proxied when it is disabled. */
- - (void)testAppDelegateIsNotProxiedWhenDisabled {
- // Set proxy enabled to NO.
- NSDictionary *mainDictionary = @{kGULFirebaseAppDelegateProxyEnabledPlistKey : @(NO)};
- id mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
- [[[mainBundleMock stub] andReturn:mainDictionary] infoDictionary];
- XCTAssertFalse([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- id originalAppDelegate = OCMProtocolMock(@protocol(GULApplicationDelegate));
- Class originalAppDelegateClass = [originalAppDelegate class];
- XCTAssertNotNil(originalAppDelegate);
- OCMStub([self.mockSharedApplication delegate]).andReturn(originalAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- XCTAssertEqualObjects([originalAppDelegate class], originalAppDelegateClass);
- [mainBundleMock stopMocking];
- }
- // TODO(tejasd): There is some weirdness that happens (at least when running this locally on Xcode)
- // where the actual app delegate is nilled out in one of these tests, causing the tests to fail.
- // Disabling this test seems to fix the problem.
- /** Tests that the App Delegate is proxied when it is enabled. */
- - (void)testAppDelegateIsProxiedWhenEnabled {
- // App Delegate Proxying is enabled by default.
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- id originalAppDelegate = [[GULTestAppDelegate alloc] init];
- Class originalAppDelegateClass = [originalAppDelegate class];
- XCTAssertNotNil(originalAppDelegate);
- OCMStub([self.mockSharedApplication delegate]).andReturn(originalAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegate];
- XCTAssertNotEqualObjects([originalAppDelegate class], originalAppDelegateClass);
- }
- - (void)testAppDelegateIsProxiedIncludingAPNSMethodsWhenEnabled {
- // App Delegate Proxying is enabled by default.
- XCTAssertTrue([GULAppDelegateSwizzler isAppDelegateProxyEnabled]);
- id originalAppDelegate = [[GULTestAppDelegate alloc] init];
- Class originalAppDelegateClass = [originalAppDelegate class];
- XCTAssertNotNil(originalAppDelegate);
- OCMStub([self.mockSharedApplication delegate]).andReturn(originalAppDelegate);
- [GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];
- XCTAssertNotEqualObjects([originalAppDelegate class], originalAppDelegateClass);
- }
- @end
|