2020#import < objc/runtime.h>
2121
2222#import " FIRAuthAppDelegateProxy.h"
23+ #import < GoogleUtilities/GULAppDelegateSwizzler.h>
24+
2325#import < OCMock/OCMock.h>
2426
2527NS_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.
0 commit comments