aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Auth/Tests/FIRAuthAppDelegateProxyTests.m
blob: b01ee26a3f00e26d3a7d36086d3ab37c06b07af8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
 * Copyright 2017 Google
 *
 * 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 <XCTest/XCTest.h>
#import <UIKit/UIKit.h>

#import <objc/runtime.h>

#import "FIRAuthAppDelegateProxy.h"
#import <OCMock/OCMock.h>

NS_ASSUME_NONNULL_BEGIN

/** @class FIRAuthEmptyAppDelegate
    @brief A @c UIApplicationDelegate implementation that does nothing.
 */
@interface FIRAuthEmptyAppDelegate : NSObject <UIApplicationDelegate>
@end
@implementation FIRAuthEmptyAppDelegate
@end

/** @class FIRAuthLegacyAppDelegate
    @brief A @c UIApplicationDelegate implementation that implements
        `application:didReceiveRemoteNotification:` and
        `application:openURL:sourceApplication:annotation:`.
 */
@interface FIRAuthLegacyAppDelegate : NSObject <UIApplicationDelegate>

/** @var notificationReceived
    @brief The last notification received, if any.
 */
@property(nonatomic, copy, nullable) NSDictionary *notificationReceived;

/** @var urlOpened
    @brief The last URL opened, if any.
 */
@property(nonatomic, copy, nullable) NSURL *urlOpened;

@end

@implementation FIRAuthLegacyAppDelegate

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo {
  self.notificationReceived = userInfo;
}

- (BOOL)application:(UIApplication *)application
              openURL:(NSURL *)url
    sourceApplication:(nullable NSString *)sourceApplication
           annotation:(id)annotation {
  self.urlOpened = url;
  return NO;
}

@end

/** @class FIRAuthModernAppDelegate
    @brief A @c UIApplicationDelegate implementation that implements both
        `application:didRegisterForRemoteNotificationsWithDeviceToken:`,
        `application:didReceiveRemoteNotification:fetchCompletionHandler:`, and
        `application:openURL:options:`.
 */
@interface FIRAuthModernAppDelegate : NSObject <UIApplicationDelegate>

/** @var deviceTokenReceived
    @brief The last device token received, if any.
 */
@property(nonatomic, copy, nullable) NSData *deviceTokenReceived;

/** @var tokenErrorReceived
    @brief The last token error received, if any.
 */
@property(nonatomic, copy, nullable) NSError *tokenErrorReceived;

/** @var notificationReceived
    @brief The last notification received, if any.
 */
@property(nonatomic, copy, nullable) NSDictionary *notificationReceived;

/** @var urlOpened
    @brief The last URL opened, if any.
 */
@property(nonatomic, copy, nullable) NSURL *urlOpened;

@end

@implementation FIRAuthModernAppDelegate

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  self.deviceTokenReceived = deviceToken;
}

- (void)application:(UIApplication *)application
    didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error {
  self.tokenErrorReceived = error;
}

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  self.notificationReceived = userInfo;
  completionHandler(UIBackgroundFetchResultNewData);
}

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
  self.urlOpened = url;
  return NO;
}

@end

/** @class FIRAuthOtherLegacyAppDelegate
    @brief A @c UIApplicationDelegate implementation that implements `application:handleOpenURL:`.
 */
@interface FIRAuthOtherLegacyAppDelegate : NSObject <UIApplicationDelegate>

/** @var urlOpened
    @brief The last URL opened, if any.
 */
@property(nonatomic, copy, nullable) NSURL *urlOpened;

@end

@implementation FIRAuthOtherLegacyAppDelegate

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  self.urlOpened = url;
  return NO;
}

@end

/** @class FIRAuthAppDelegateProxyTests
    @brief Unit tests for @c FIRAuthAppDelegateProxy .
 */
@interface FIRAuthAppDelegateProxyTests : XCTestCase
@end
@implementation FIRAuthAppDelegateProxyTests {
  /** @var _mockApplication
      @brief The mock UIApplication used for testing.
   */
  id _mockApplication;

  /** @var _deviceToken
      @brief The fake APNs device token for testing.
   */
  NSData *_deviceToken;

  /** @var _error
      @brief The fake error for testing.
   */
  NSError *_error;

  /** @var _notification
      @brief The fake notification for testing.
   */
  NSDictionary* _notification;

  /** @var _url
      @brief The fake URL for testing.
   */
  NSURL *_url;

  /** @var _isIOS9orLater
      @brief Whether the OS version is iOS 9 or later.
   */
  BOOL _isIOS9orLater;
}

- (void)setUp {
  [super setUp];
  _mockApplication = OCMClassMock([UIApplication class]);
  _deviceToken = [@"asdf" dataUsingEncoding:NSUTF8StringEncoding];
  _error = [NSError errorWithDomain:@"FakeError" code:12345 userInfo:nil];
  _notification = @{ @"zxcv" : @1234 };
  _url = [NSURL URLWithString:@"https://abc.def/ghi"];
  _isIOS9orLater = [[[UIDevice currentDevice] systemVersion] doubleValue] >= 9.0;
}

- (void)tearDown {
  OCMVerifyAll(_mockApplication);
  [super tearDown];
}

/** @fn testSharedInstance
    @brief Tests that the shared instance is the same one.
 */
- (void)testSharedInstance {
  FIRAuthAppDelegateProxy *proxy1 = [FIRAuthAppDelegateProxy sharedInstance];
  FIRAuthAppDelegateProxy *proxy2 = [FIRAuthAppDelegateProxy sharedInstance];
  XCTAssertEqual(proxy1, proxy2);
}

/** @fn testNilApplication
    @brief Tests that initialization fails if the application is nil.
 */
- (void)testNilApplication {
  XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:nil]);
}

/** @fn testNilDelegate
    @brief Tests that initialization fails if the application's delegate is nil.
 */
- (void)testNilDelegate {
  OCMExpect([_mockApplication delegate]).andReturn(nil);
  XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication]);
}

/** @fn testNonconformingDelegate
    @brief Tests that initialization fails if the application's delegate does not conform to
        @c UIApplicationDelegate protocol.
 */
- (void)testNonconformingDelegate {
  OCMExpect([_mockApplication delegate]).andReturn(@"abc");
  XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication]);
}

/** @fn testDisabledByBundleEntry
    @brief Tests that initialization fails if the proxy is disabled by a bundle entry.
 */
- (void)testDisabledByBundleEntry {
  // Swizzle NSBundle's objectForInfoDictionaryKey to return @NO for the specific key.
  Method method = class_getInstanceMethod([NSBundle class], @selector(objectForInfoDictionaryKey:));
  __block IMP originalImplementation;
  IMP newImplmentation = imp_implementationWithBlock(^id(id object, NSString *key) {
    if ([key isEqualToString:@"FirebaseAppDelegateProxyEnabled"]) {
      return @NO;
    }
    typedef id (*Implementation)(id object, SEL cmd, NSString *key);
    return ((Implementation)originalImplementation)(object, @selector(objectForInfoDictionaryKey:),
                                                    key);
  });
  originalImplementation = method_setImplementation(method, newImplmentation);

  // Verify that initialization fails.
  FIRAuthEmptyAppDelegate *delegate = [[FIRAuthEmptyAppDelegate alloc] init];
  OCMStub([_mockApplication delegate]).andReturn(delegate);
  XCTAssertNil([[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication]);

  // Unswizzle.
  imp_removeBlock(method_setImplementation(method, originalImplementation));
}

// Deprecated methods are call intentionally in tests to verify behaviors on older iOS systems.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

/** @fn testEmptyDelegateOneHandler
    @brief Tests that the proxy works against an empty @c UIApplicationDelegate for one handler.
 */
- (void)testEmptyDelegateOneHandler {
  FIRAuthEmptyAppDelegate *delegate = [[FIRAuthEmptyAppDelegate alloc] init];
  OCMExpect([_mockApplication delegate]).andReturn(delegate);
  __weak id weakProxy;
  @autoreleasepool {
    FIRAuthAppDelegateProxy *proxy =
        [[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
    XCTAssertNotNil(proxy);

    // Verify certain methods are swizzled while others are not.
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]);
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didFailToRegisterForRemoteNotificationsWithError:)]);
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]);
    XCTAssertFalse([delegate respondsToSelector:
                    @selector(application:didReceiveRemoteNotification:)]);
    if (_isIOS9orLater) {
      XCTAssertTrue([delegate respondsToSelector:@selector(application:openURL:options:)]);
      XCTAssertFalse([delegate respondsToSelector:
                      @selector(application:openURL:sourceApplication:annotation:)]);
    } else {
      XCTAssertFalse([delegate respondsToSelector:@selector(application:openURL:options:)]);
      XCTAssertTrue([delegate respondsToSelector:
                     @selector(application:openURL:sourceApplication:annotation:)]);
    }
    XCTAssertFalse([delegate respondsToSelector:@selector(application:handleOpenURL:)]);

    // Verify the handler is called after being added.
    __weak id weakHandler;
    @autoreleasepool {
      id mockHandler = OCMProtocolMock(@protocol(FIRAuthAppDelegateHandler));
      [proxy addHandler:mockHandler];

      // Verify `application:didRegisterForRemoteNotificationsWithDeviceToken:` is handled.
      OCMExpect([mockHandler setAPNSToken:_deviceToken]);
      [delegate application:_mockApplication
          didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
      OCMVerifyAll(mockHandler);

      // Verify `application:didFailToRegisterForRemoteNotificationsWithError:` is handled.
      OCMExpect([mockHandler handleAPNSTokenError:_error]);
      [delegate application:_mockApplication
          didFailToRegisterForRemoteNotificationsWithError:_error];
      OCMVerifyAll(mockHandler);

      // Verify `application:didReceiveRemoteNotification:fetchCompletionHandler:` is handled.
      OCMExpect([mockHandler canHandleNotification:_notification]).andReturn(YES);
      __block BOOL fetchCompletionHandlerCalled = NO;
      [delegate application:_mockApplication
          didReceiveRemoteNotification:_notification
                fetchCompletionHandler:^(UIBackgroundFetchResult result) {
        XCTAssertEqual(result, UIBackgroundFetchResultNoData);
        fetchCompletionHandlerCalled = YES;
      }];
      OCMVerifyAll(mockHandler);
      XCTAssertTrue(fetchCompletionHandlerCalled);

      // Verify one of the `application:openURL:...` methods is handled.
      OCMExpect([mockHandler canHandleURL:_url]).andReturn(YES);
      if (_isIOS9orLater) {
        // Verify `application:openURL:options:` is handled.
        XCTAssertTrue([delegate application:_mockApplication openURL:_url options:@{}]);
      } else {
        // Verify `application:openURL:sourceApplication:annotation:` is handled.
        XCTAssertTrue([delegate application:_mockApplication
                                    openURL:_url
                          sourceApplication:@"sourceApplication"
                                 annotation:@"annotaton"]);
      }
      OCMVerifyAll(mockHandler);

      weakHandler = mockHandler;
      XCTAssertNotNil(weakHandler);
    }
    // Verify the handler is not retained by the proxy.
    XCTAssertNil(weakHandler);

    // Verify nothing bad happens after the handler is released.
    [delegate application:_mockApplication
        didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
    [delegate application:_mockApplication
        didFailToRegisterForRemoteNotificationsWithError:_error];
    [delegate application:_mockApplication
        didReceiveRemoteNotification:_notification
              fetchCompletionHandler:^(UIBackgroundFetchResult result) {
      XCTFail(@"Should not call completion handler.");
    }];
    if (_isIOS9orLater) {
      XCTAssertFalse([delegate application:_mockApplication openURL:_url options:@{}]);
    } else {
      XCTAssertFalse([delegate application:_mockApplication
                                   openURL:_url
                         sourceApplication:@"sourceApplication"
                                annotation:@"annotaton"]);
    }
    weakProxy = proxy;
    XCTAssertNotNil(weakProxy);
  }
  // Verify the proxy does not retain itself.
  XCTAssertNil(weakProxy);
  // Verify nothing bad happens after the proxy is released.
  [delegate application:_mockApplication
      didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
  [delegate application:_mockApplication
      didReceiveRemoteNotification:_notification
            fetchCompletionHandler:^(UIBackgroundFetchResult result) {
    XCTFail(@"Should not call completion handler.");
  }];
  if (_isIOS9orLater) {
    XCTAssertFalse([delegate application:_mockApplication openURL:_url options:@{}]);
  } else {
    XCTAssertFalse([delegate application:_mockApplication
                                 openURL:_url
                       sourceApplication:@"sourceApplication"
                              annotation:@"annotaton"]);
  }
}

/** @fn testLegacyDelegateTwoHandlers
    @brief Tests that the proxy works against a legacy @c UIApplicationDelegate for two handlers.
 */
- (void)testLegacyDelegateTwoHandlers {
  FIRAuthLegacyAppDelegate *delegate = [[FIRAuthLegacyAppDelegate alloc] init];
  OCMExpect([_mockApplication delegate]).andReturn(delegate);
  __weak id weakProxy;
  @autoreleasepool {
    FIRAuthAppDelegateProxy *proxy =
        [[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
    XCTAssertNotNil(proxy);

    // Verify certain methods are swizzled while others are not.
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]);
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didFailToRegisterForRemoteNotificationsWithError:)]);
    XCTAssertFalse([delegate respondsToSelector:
                    @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]);
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didReceiveRemoteNotification:)]);
    XCTAssertFalse([delegate respondsToSelector:@selector(application:openURL:options:)]);
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:openURL:sourceApplication:annotation:)]);
    XCTAssertFalse([delegate respondsToSelector:@selector(application:handleOpenURL:)]);

    // Verify the handler is called after being added.
    __weak id weakHandler1;
    @autoreleasepool {
      id mockHandler1 = OCMProtocolMock(@protocol(FIRAuthAppDelegateHandler));
      [proxy addHandler:mockHandler1];
      __weak id weakHandler2;
      @autoreleasepool {
        id mockHandler2 = OCMProtocolMock(@protocol(FIRAuthAppDelegateHandler));
        [proxy addHandler:mockHandler2];

        // Verify `application:didRegisterForRemoteNotificationsWithDeviceToken:` is handled.
        OCMExpect([mockHandler1 setAPNSToken:_deviceToken]);
        OCMExpect([mockHandler2 setAPNSToken:_deviceToken]);
        [delegate application:_mockApplication
            didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
        OCMVerifyAll(mockHandler1);
        OCMVerifyAll(mockHandler2);

        // Verify `application:didFailToRegisterForRemoteNotificationsWithError:` is handled.
        OCMExpect([mockHandler1 handleAPNSTokenError:_error]);
        OCMExpect([mockHandler2 handleAPNSTokenError:_error]);
        [delegate application:_mockApplication
            didFailToRegisterForRemoteNotificationsWithError:_error];
        OCMVerifyAll(mockHandler1);
        OCMVerifyAll(mockHandler2);

        // Verify `application:didReceiveRemoteNotification:fetchCompletionHandler:` is handled.
        OCMExpect([mockHandler1 canHandleNotification:_notification]).andReturn(YES);
        // handler2 shouldn't been invoked because it is already handled by handler1.
        [delegate application:_mockApplication didReceiveRemoteNotification:_notification];
        OCMVerifyAll(mockHandler1);
        OCMVerifyAll(mockHandler2);
        XCTAssertNil(delegate.notificationReceived);

        // Verify `application:openURL:sourceApplication:annotation:` is handled.
        OCMExpect([mockHandler1 canHandleURL:_url]).andReturn(YES);
        XCTAssertTrue([delegate application:_mockApplication
                                    openURL:_url
                          sourceApplication:@"sourceApplication"
                                 annotation:@"annotaton"]);
        OCMVerifyAll(mockHandler1);
        OCMVerifyAll(mockHandler2);
        XCTAssertNil(delegate.urlOpened);

        weakHandler2 = mockHandler2;
        XCTAssertNotNil(weakHandler2);
      }
      // Verify the handler2 is not retained by the proxy.
      XCTAssertNil(weakHandler2);

      // Verify `application:didRegisterForRemoteNotificationsWithDeviceToken:` is handled.
      OCMExpect([mockHandler1 setAPNSToken:_deviceToken]);
      [delegate application:_mockApplication
          didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
      OCMVerifyAll(mockHandler1);

      // Verify `application:didFailToRegisterForRemoteNotificationsWithError:` is handled.
      OCMExpect([mockHandler1 handleAPNSTokenError:_error]);
      [delegate application:_mockApplication
          didFailToRegisterForRemoteNotificationsWithError:_error];
      OCMVerifyAll(mockHandler1);

      // Verify `application:didReceiveRemoteNotification:fetchCompletionHandler:` is NOT handled.
      OCMExpect([mockHandler1 canHandleNotification:_notification]).andReturn(NO);
      [delegate application:_mockApplication didReceiveRemoteNotification:_notification];
      OCMVerifyAll(mockHandler1);
      XCTAssertEqualObjects(delegate.notificationReceived, _notification);
      delegate.notificationReceived = nil;

      // Verify `application:openURL:sourceApplication:annotation:` is NOT handled.
      OCMExpect([mockHandler1 canHandleURL:_url]).andReturn(NO);
      XCTAssertFalse([delegate application:_mockApplication
                                   openURL:_url
                         sourceApplication:@"sourceApplication"
                                annotation:@"annotation"]);
      OCMVerifyAll(mockHandler1);
      XCTAssertEqualObjects(delegate.urlOpened, _url);
      delegate.urlOpened = nil;

      weakHandler1 = mockHandler1;
      XCTAssertNotNil(weakHandler1);
    }
    // Verify the handler1 is not retained by the proxy.
    XCTAssertNil(weakHandler1);

    // Verify the delegate still works after all handlers are released.
    [delegate application:_mockApplication
        didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
    [delegate application:_mockApplication
        didFailToRegisterForRemoteNotificationsWithError:_error];
    [delegate application:_mockApplication didReceiveRemoteNotification:_notification];
    XCTAssertEqualObjects(delegate.notificationReceived, _notification);
    delegate.notificationReceived = nil;
    XCTAssertFalse([delegate application:_mockApplication
                                 openURL:_url
                       sourceApplication:@"sourceApplication"
                              annotation:@"annotation"]);
    XCTAssertEqualObjects(delegate.urlOpened, _url);
    delegate.urlOpened = nil;

    weakProxy = proxy;
    XCTAssertNotNil(weakProxy);
  }
  // Verify the proxy does not retain itself.
  XCTAssertNil(weakProxy);

  // Verify the delegate still works after the proxy is released.
  [delegate application:_mockApplication
      didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
  [delegate application:_mockApplication didReceiveRemoteNotification:_notification];
  XCTAssertEqualObjects(delegate.notificationReceived, _notification);
  delegate.notificationReceived = nil;
  XCTAssertFalse([delegate application:_mockApplication
                               openURL:_url
                     sourceApplication:@"sourceApplication"
                            annotation:@"annotation"]);
  XCTAssertEqualObjects(delegate.urlOpened, _url);
  delegate.urlOpened = nil;
}

/** @fn testModernDelegateWithOtherInstance
    @brief Tests that the proxy works against a modern @c UIApplicationDelegate along with
        another unaffected instance.
 */
- (void)testModernDelegateWithUnaffectedInstance {
  FIRAuthModernAppDelegate *delegate = [[FIRAuthModernAppDelegate alloc] init];
  OCMExpect([_mockApplication delegate]).andReturn(delegate);
  FIRAuthModernAppDelegate *unaffectedDelegate = [[FIRAuthModernAppDelegate alloc] init];
  __weak id weakProxy;
  @autoreleasepool {
    FIRAuthAppDelegateProxy *proxy =
        [[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
    XCTAssertNotNil(proxy);

    // Verify certain methods are swizzled while others are not.
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]);
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didFailToRegisterForRemoteNotificationsWithError:)]);
    XCTAssertTrue([delegate respondsToSelector:
                   @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]);
    XCTAssertFalse([delegate respondsToSelector:
                    @selector(application:didReceiveRemoteNotification:)]);
    XCTAssertTrue([delegate respondsToSelector:@selector(application:openURL:options:)]);
    if (_isIOS9orLater) {
      XCTAssertFalse([delegate respondsToSelector:
                      @selector(application:openURL:sourceApplication:annotation:)]);
    } else {
      XCTAssertTrue([delegate respondsToSelector:
                     @selector(application:openURL:sourceApplication:annotation:)]);
    }
    XCTAssertFalse([delegate respondsToSelector:@selector(application:handleOpenURL:)]);

    // Verify the handler is called after being added.
    __weak id weakHandler;
    @autoreleasepool {
      id mockHandler = OCMProtocolMock(@protocol(FIRAuthAppDelegateHandler));
      [proxy addHandler:mockHandler];

      // Verify `application:didRegisterForRemoteNotificationsWithDeviceToken:` is handled.
      OCMExpect([mockHandler setAPNSToken:_deviceToken]);
      [delegate application:_mockApplication
          didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
      OCMVerifyAll(mockHandler);
      XCTAssertEqualObjects(delegate.deviceTokenReceived, _deviceToken);
      delegate.deviceTokenReceived = nil;

      // Verify `application:didFailToRegisterForRemoteNotificationsWithError:` is handled.
      OCMExpect([mockHandler handleAPNSTokenError:_error]);
      [delegate application:_mockApplication
          didFailToRegisterForRemoteNotificationsWithError:_error];
      OCMVerifyAll(mockHandler);
      XCTAssertEqualObjects(delegate.tokenErrorReceived, _error);
      delegate.tokenErrorReceived = nil;

      // Verify `application:didReceiveRemoteNotification:fetchCompletionHandler:` is handled.
      OCMExpect([mockHandler canHandleNotification:_notification]).andReturn(YES);
      __block BOOL fetchCompletionHandlerCalled = NO;
      [delegate application:_mockApplication
          didReceiveRemoteNotification:_notification
                fetchCompletionHandler:^(UIBackgroundFetchResult result) {
        XCTAssertEqual(result, UIBackgroundFetchResultNoData);
        fetchCompletionHandlerCalled = YES;
      }];
      OCMVerifyAll(mockHandler);
      XCTAssertTrue(fetchCompletionHandlerCalled);
      XCTAssertNil(delegate.notificationReceived);

      // Verify one of the `application:openURL:...` methods is handled.
      OCMExpect([mockHandler canHandleURL:_url]).andReturn(YES);
      if (_isIOS9orLater) {
        // Verify `application:openURL:options:` is handled.
        XCTAssertTrue([delegate application:_mockApplication openURL:_url options:@{}]);
      } else {
        // Verify `application:openURL:sourceApplication:annotation:` is handled.
        XCTAssertTrue([delegate application:_mockApplication
                                    openURL:_url
                          sourceApplication:@"sourceApplication"
                                 annotation:@"annotaton"]);
      }
      OCMVerifyAll(mockHandler);
      XCTAssertNil(delegate.urlOpened);

      // Verify unaffected delegate instance.
      [unaffectedDelegate application:_mockApplication
          didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
      XCTAssertEqualObjects(unaffectedDelegate.deviceTokenReceived, _deviceToken);
      unaffectedDelegate.deviceTokenReceived = nil;
      [unaffectedDelegate application:_mockApplication
          didFailToRegisterForRemoteNotificationsWithError:_error];
      XCTAssertEqualObjects(unaffectedDelegate.tokenErrorReceived, _error);
      unaffectedDelegate.tokenErrorReceived = nil;
      fetchCompletionHandlerCalled = NO;
      [unaffectedDelegate application:_mockApplication
          didReceiveRemoteNotification:_notification
                fetchCompletionHandler:^(UIBackgroundFetchResult result) {
        XCTAssertEqual(result, UIBackgroundFetchResultNewData);
        fetchCompletionHandlerCalled = YES;
      }];
      XCTAssertTrue(fetchCompletionHandlerCalled);
      XCTAssertEqualObjects(unaffectedDelegate.notificationReceived, _notification);
      unaffectedDelegate.notificationReceived = nil;
      XCTAssertFalse([unaffectedDelegate application:_mockApplication openURL:_url options:@{}]);
      XCTAssertEqualObjects(unaffectedDelegate.urlOpened, _url);
      unaffectedDelegate.urlOpened = nil;

      weakHandler = mockHandler;
      XCTAssertNotNil(weakHandler);
    }
    // Verify the handler is not retained by the proxy.
    XCTAssertNil(weakHandler);

    // Verify the delegate still works after the handler is released.
    [delegate application:_mockApplication
        didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
    XCTAssertEqualObjects(delegate.deviceTokenReceived, _deviceToken);
    delegate.deviceTokenReceived = nil;
    [delegate application:_mockApplication
        didFailToRegisterForRemoteNotificationsWithError:_error];
    XCTAssertEqualObjects(delegate.tokenErrorReceived, _error);
    delegate.tokenErrorReceived = nil;
    __block BOOL fetchCompletionHandlerCalled = NO;
    [delegate application:_mockApplication
        didReceiveRemoteNotification:_notification
              fetchCompletionHandler:^(UIBackgroundFetchResult result) {
      XCTAssertEqual(result, UIBackgroundFetchResultNewData);
      fetchCompletionHandlerCalled = YES;
    }];
    XCTAssertEqualObjects(delegate.notificationReceived, _notification);
    delegate.notificationReceived = nil;
    XCTAssertTrue(fetchCompletionHandlerCalled);
    XCTAssertFalse([delegate application:_mockApplication openURL:_url options:@{}]);
    XCTAssertEqualObjects(delegate.urlOpened, _url);
    delegate.urlOpened = nil;

    weakProxy = proxy;
    XCTAssertNotNil(weakProxy);
  }
  // Verify the proxy does not retain itself.
  XCTAssertNil(weakProxy);

  // Verify the delegate still works after the proxy is released.
  [delegate application:_mockApplication
      didRegisterForRemoteNotificationsWithDeviceToken:_deviceToken];
  XCTAssertEqualObjects(delegate.deviceTokenReceived, _deviceToken);
  delegate.deviceTokenReceived = nil;
  [delegate application:_mockApplication
      didFailToRegisterForRemoteNotificationsWithError:_error];
  XCTAssertEqualObjects(delegate.tokenErrorReceived, _error);
  delegate.tokenErrorReceived = nil;
  __block BOOL fetchCompletionHandlerCalled = NO;
  [delegate application:_mockApplication
      didReceiveRemoteNotification:_notification
            fetchCompletionHandler:^(UIBackgroundFetchResult result) {
    XCTAssertEqual(result, UIBackgroundFetchResultNewData);
    fetchCompletionHandlerCalled = YES;
  }];
  XCTAssertEqualObjects(delegate.notificationReceived, _notification);
  delegate.notificationReceived = nil;
  XCTAssertTrue(fetchCompletionHandlerCalled);
  XCTAssertFalse([delegate application:_mockApplication openURL:_url options:@{}]);
  XCTAssertEqualObjects(delegate.urlOpened, _url);
  delegate.urlOpened = nil;
}

/** @fn testOtherLegacyDelegateHandleOpenURL
    @brief Tests that the proxy works against another legacy @c UIApplicationDelegate for
        `application:handleOpenURL:`.
 */
- (void)testOtherLegacyDelegateHandleOpenURL {
  FIRAuthOtherLegacyAppDelegate *delegate = [[FIRAuthOtherLegacyAppDelegate alloc] init];
  OCMExpect([_mockApplication delegate]).andReturn(delegate);
  __weak id weakProxy;
  @autoreleasepool {
    FIRAuthAppDelegateProxy *proxy =
        [[FIRAuthAppDelegateProxy alloc] initWithApplication:_mockApplication];
    XCTAssertNotNil(proxy);

    // Verify certain methods are swizzled while others are not.
    XCTAssertFalse([delegate respondsToSelector:@selector(application:openURL:options:)]);
    XCTAssertFalse([delegate respondsToSelector:
                    @selector(application:openURL:sourceApplication:annotation:)]);
    XCTAssertTrue([delegate respondsToSelector:@selector(application:handleOpenURL:)]);

    // Verify the handler is called after being added.
    __weak id weakHandler;
    @autoreleasepool {
      id mockHandler = OCMProtocolMock(@protocol(FIRAuthAppDelegateHandler));
      [proxy addHandler:mockHandler];

      // Verify `application:handleOpenURL:` is handled.
      OCMExpect([mockHandler canHandleURL:_url]).andReturn(YES);
      XCTAssertTrue([delegate application:_mockApplication handleOpenURL:_url]);
      OCMVerifyAll(mockHandler);

      weakHandler = mockHandler;
      XCTAssertNotNil(weakHandler);
    }
    // Verify the handler is not retained by the proxy.
    XCTAssertNil(weakHandler);

    // Verify nothing bad happens after the handler is released.
    XCTAssertFalse([delegate application:_mockApplication handleOpenURL:_url]);
    XCTAssertEqualObjects(delegate.urlOpened, _url);
    delegate.urlOpened = nil;

    weakProxy = proxy;
    XCTAssertNotNil(weakProxy);
  }
  // Verify the proxy does not retain itself.
  XCTAssertNil(weakProxy);
  // Verify nothing bad happens after the proxy is released.
  XCTAssertFalse([delegate application:_mockApplication handleOpenURL:_url]);
  XCTAssertEqualObjects(delegate.urlOpened, _url);
  delegate.urlOpened = nil;
}

#pragma clang diagnostic pop  // ignored "-Wdeprecated-declarations"

@end

NS_ASSUME_NONNULL_END