Skip to content

Commit b2eabf2

Browse files
committed
FIRAuthAppDelegateProxy: implementation updated to use GULAppDelegateSwizzler [WIP]
1 parent 961e9b0 commit b2eabf2

File tree

5 files changed

+70
-373
lines changed

5 files changed

+70
-373
lines changed

Example/Auth/Tests/FIRAuthAppDelegateProxyTests.m

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
#import <objc/runtime.h>
2121

2222
#import "FIRAuthAppDelegateProxy.h"
23+
#import <GoogleUtilities/GULAppDelegateSwizzler.h>
24+
2325
#import <OCMock/OCMock.h>
2426

2527
NS_ASSUME_NONNULL_BEGIN
2628

29+
@interface GULAppDelegateSwizzler (FIRAuthAppDelegateProxyTests)
30+
+ (void)proxyAppDelegate:(id<UIApplicationDelegate>)appDelegate;
31+
@end
32+
2733
/** @class FIRAuthEmptyAppDelegate
2834
@brief A @c UIApplicationDelegate implementation that does nothing.
2935
*/
@@ -208,56 +214,6 @@ - (void)testSharedInstance {
208214
XCTAssertEqual(proxy1, proxy2);
209215
}
210216

211-
/** @fn testNilApplication
212-
@brief Tests that initialization fails if the application is nil.
213-
*/
214-
- (void)testNilApplication {
215-
XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:nil]);
216-
}
217-
218-
/** @fn testNilDelegate
219-
@brief Tests that initialization fails if the application's delegate is nil.
220-
*/
221-
- (void)testNilDelegate {
222-
OCMExpect([_mockApplication delegate]).andReturn(nil);
223-
XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication]);
224-
}
225-
226-
/** @fn testNonconformingDelegate
227-
@brief Tests that initialization fails if the application's delegate does not conform to
228-
@c UIApplicationDelegate protocol.
229-
*/
230-
- (void)testNonconformingDelegate {
231-
OCMExpect([_mockApplication delegate]).andReturn(@"abc");
232-
XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication]);
233-
}
234-
235-
/** @fn testDisabledByBundleEntry
236-
@brief Tests that initialization fails if the proxy is disabled by a bundle entry.
237-
*/
238-
- (void)testDisabledByBundleEntry {
239-
// Swizzle NSBundle's objectForInfoDictionaryKey to return @NO for the specific key.
240-
Method method = class_getInstanceMethod([NSBundle class], @selector(objectForInfoDictionaryKey:));
241-
__block IMP originalImplementation;
242-
IMP newImplmentation = imp_implementationWithBlock(^id(id object, NSString *key) {
243-
if ([key isEqualToString:@"FirebaseAppDelegateProxyEnabled"]) {
244-
return @NO;
245-
}
246-
typedef id (*Implementation)(id object, SEL cmd, NSString *key);
247-
return ((Implementation)originalImplementation)(object, @selector(objectForInfoDictionaryKey:),
248-
key);
249-
});
250-
originalImplementation = method_setImplementation(method, newImplmentation);
251-
252-
// Verify that initialization fails.
253-
FIRAuthEmptyAppDelegate *delegate = [[FIRAuthEmptyAppDelegate alloc] init];
254-
OCMStub([_mockApplication delegate]).andReturn(delegate);
255-
XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication]);
256-
257-
// Unswizzle.
258-
imp_removeBlock(method_setImplementation(method, originalImplementation));
259-
}
260-
261217
// Deprecated methods are call intentionally in tests to verify behaviors on older iOS systems.
262218
#pragma clang diagnostic push
263219
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@@ -268,10 +224,14 @@ - (void)testDisabledByBundleEntry {
268224
- (void)testEmptyDelegateOneHandler {
269225
FIRAuthEmptyAppDelegate *delegate = [[FIRAuthEmptyAppDelegate alloc] init];
270226
OCMExpect([_mockApplication delegate]).andReturn(delegate);
227+
228+
[GULAppDelegateSwizzler proxyAppDelegate:delegate];
229+
271230
__weak id weakProxy;
272231
@autoreleasepool {
273-
FIRAuthAppDelegateProxy *proxy =
274-
[[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
232+
FIRAuthAppDelegateProxy *proxy = [[FIRAuthAppDelegateProxy alloc] init];
233+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:proxy];
234+
275235
XCTAssertNotNil(proxy);
276236

277237
// Verify certain methods are swizzled while others are not.
@@ -391,10 +351,13 @@ - (void)testEmptyDelegateOneHandler {
391351
- (void)testLegacyDelegateTwoHandlers {
392352
FIRAuthLegacyAppDelegate *delegate = [[FIRAuthLegacyAppDelegate alloc] init];
393353
OCMExpect([_mockApplication delegate]).andReturn(delegate);
354+
355+
[GULAppDelegateSwizzler proxyAppDelegate:delegate];
356+
394357
__weak id weakProxy;
395358
@autoreleasepool {
396-
FIRAuthAppDelegateProxy *proxy =
397-
[[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
359+
FIRAuthAppDelegateProxy *proxy = [[FIRAuthAppDelegateProxy alloc] init];
360+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:proxy];
398361
XCTAssertNotNil(proxy);
399362

400363
// Verify certain methods are swizzled while others are not.
@@ -539,10 +502,13 @@ - (void)testModernDelegateWithUnaffectedInstance {
539502
FIRAuthModernAppDelegate *delegate = [[FIRAuthModernAppDelegate alloc] init];
540503
OCMExpect([_mockApplication delegate]).andReturn(delegate);
541504
FIRAuthModernAppDelegate *unaffectedDelegate = [[FIRAuthModernAppDelegate alloc] init];
505+
506+
[GULAppDelegateSwizzler proxyAppDelegate:delegate];
507+
542508
__weak id weakProxy;
543509
@autoreleasepool {
544-
FIRAuthAppDelegateProxy *proxy =
545-
[[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
510+
FIRAuthAppDelegateProxy *proxy = [[FIRAuthAppDelegateProxy alloc] init];
511+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:proxy];
546512
XCTAssertNotNil(proxy);
547513

548514
// Verify certain methods are swizzled while others are not.
@@ -703,10 +669,12 @@ - (void)testModernDelegateWithUnaffectedInstance {
703669
- (void)testOtherLegacyDelegateHandleOpenURL {
704670
FIRAuthOtherLegacyAppDelegate *delegate = [[FIRAuthOtherLegacyAppDelegate alloc] init];
705671
OCMExpect([_mockApplication delegate]).andReturn(delegate);
672+
[GULAppDelegateSwizzler proxyAppDelegate:delegate];
673+
706674
__weak id weakProxy;
707675
@autoreleasepool {
708-
FIRAuthAppDelegateProxy *proxy =
709-
[[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
676+
FIRAuthAppDelegateProxy *proxy = [[FIRAuthAppDelegateProxy alloc] init];
677+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:proxy];
710678
XCTAssertNotNil(proxy);
711679

712680
// Verify certain methods are swizzled while others are not.

Firebase/Auth/Source/FIRAuth.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#import <FirebaseCore/FIRLogger.h>
3131
#import <FirebaseCore/FIROptions.h>
3232
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
33+
#import <GoogleUtilities/GULAppDelegateSwizzler.h>
3334

3435
#import "AuthProviders/EmailPassword/FIREmailPasswordAuthCredential.h"
3536
#import "FIRAdditionalUserInfo_Internal.h"
@@ -373,8 +374,12 @@ - (nullable instancetype)initWithAPIKey:(NSString *)APIKey appName:(NSString *)a
373374
}
374375
UIApplication *application = [applicationClass sharedApplication];
375376

376-
// Initialize the shared FIRAuthAppDelegateProxy instance in the main thread if not already.
377-
[FIRAuthAppDelegateProxy sharedInstance];
377+
static dispatch_once_t onceToken;
378+
dispatch_once(&onceToken, ^{
379+
[GULAppDelegateSwizzler proxyOriginalDelegate];
380+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:[FIRAuthAppDelegateProxy sharedInstance]];
381+
});
382+
378383
#endif
379384

380385
// Continue with the rest of initialization in the work thread.

Firebase/Auth/Source/FIRAuthAppDelegateProxy.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,7 @@ NS_ASSUME_NONNULL_BEGIN
5555
/** @class FIRAuthAppDelegateProxy
5656
@brief A manager for swizzling @c UIApplicationDelegate methods.
5757
*/
58-
@interface FIRAuthAppDelegateProxy : NSObject
59-
60-
/** @fn initWithApplication
61-
@brief Initialize the instance with the given @c UIApplication.
62-
@returns An initialized instance, or @c nil if a proxy cannot be established.
63-
@remarks This method should only be called from tests if called outside of this class.
64-
*/
65-
- (nullable instancetype)initWithApplication:(nullable UIApplication *)application
66-
NS_DESIGNATED_INITIALIZER;
67-
68-
/** @fn init
69-
@brief Call @c sharedInstance to get an instance of this class.
70-
*/
71-
- (instancetype)init NS_UNAVAILABLE;
58+
@interface FIRAuthAppDelegateProxy : NSObject <UIApplicationDelegate>
7259

7360
/** @fn addHandler:
7461
@brief Adds a handler for UIApplicationDelegate methods.

0 commit comments

Comments
 (0)