2424#import " FIRMessaging.h"
2525#import " FIRMessagingRemoteNotificationsProxy.h"
2626
27+ #import < GoogleUtilities/GULAppDelegateSwizzler.h>
28+
2729#pragma mark - Invalid App Delegate or UNNotificationCenter
2830
2931@interface RandomObject : NSObject
@@ -62,6 +64,8 @@ @implementation IncompleteAppDelegate
6264@interface FakeAppDelegate : NSObject <UIApplicationDelegate>
6365@property (nonatomic ) BOOL remoteNotificationMethodWasCalled;
6466@property (nonatomic ) BOOL remoteNotificationWithFetchHandlerWasCalled;
67+ @property (nonatomic , strong ) NSData *deviceToken;
68+ @property (nonatomic , strong ) NSError *registerForRemoteNotificationsError;
6569@end
6670@implementation FakeAppDelegate
6771#if TARGET_OS_IOS
@@ -75,6 +79,17 @@ - (void)application:(UIApplication *)application
7579 fetchCompletionHandler : (void (^)(UIBackgroundFetchResult))completionHandler {
7680 self.remoteNotificationWithFetchHandlerWasCalled = YES ;
7781}
82+
83+ - (void )application : (UIApplication *)application
84+ didRegisterForRemoteNotificationsWithDeviceToken : (NSData *)deviceToken {
85+ self.deviceToken = deviceToken;
86+ }
87+
88+ - (void )application : (UIApplication *)application
89+ didFailToRegisterForRemoteNotificationsWithError : (NSError *)error {
90+ self.registerForRemoteNotificationsError = error;
91+ }
92+
7893@end
7994
8095#pragma mark - Incompete UNUserNotificationCenterDelegate
@@ -107,6 +122,10 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
107122@end
108123#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
109124
125+ @interface GULAppDelegateSwizzler (FIRMessagingRemoteNotificationsProxyTest)
126+ + (void )resetProxyOriginalDelegateOnceToken ;
127+ @end
128+
110129#pragma mark - Local, Per-Test Properties
111130
112131@interface FIRMessagingRemoteNotificationsProxyTest : XCTestCase
@@ -123,6 +142,9 @@ @implementation FIRMessagingRemoteNotificationsProxyTest
123142
124143- (void )setUp {
125144 [super setUp ];
145+
146+ [GULAppDelegateSwizzler resetProxyOriginalDelegateOnceToken ];
147+
126148 _mockSharedApplication = OCMPartialMock ([UIApplication sharedApplication ]);
127149
128150 _mockMessaging = OCMClassMock ([FIRMessaging class ]);
@@ -134,8 +156,7 @@ - (void)setUp {
134156 OCMStub ([_mockProxyClass sharedProxy ]).andReturn (self.proxy );
135157
136158#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
137- _mockUserNotificationCenter = OCMClassMock ([UNUserNotificationCenter class ]);
138- OCMStub ([_mockUserNotificationCenter currentNotificationCenter ]).andReturn (_mockUserNotificationCenter);
159+ _mockUserNotificationCenter = OCMPartialMock ([UNUserNotificationCenter currentNotificationCenter ]);
139160#endif
140161}
141162
@@ -149,6 +170,9 @@ - (void)tearDown {
149170 [_mockSharedApplication stopMocking ];
150171 _mockSharedApplication = nil ;
151172
173+ [_mockUserNotificationCenter stopMocking ];
174+ _mockUserNotificationCenter = nil ;
175+
152176 _proxy = nil ;
153177 [super tearDown ];
154178}
@@ -168,6 +192,7 @@ - (void)testSwizzlingNonAppDelegate {
168192}
169193
170194- (void )testSwizzledIncompleteAppDelegateRemoteNotificationMethod {
195+ #if TARGET_OS_IOS
171196 IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc ] init ];
172197 [OCMStub ([self .mockSharedApplication delegate ]) andReturn: incompleteAppDelegate];
173198 [self .proxy swizzleMethodsIfPossible ];
@@ -179,6 +204,7 @@ - (void)testSwizzledIncompleteAppDelegateRemoteNotificationMethod {
179204 didReceiveRemoteNotification: notification];
180205
181206 [self .mockMessaging verify ];
207+ #endif // TARGET_OS_IOS
182208}
183209
184210- (void )testIncompleteAppDelegateRemoteNotificationWithFetchHandlerMethod {
@@ -187,10 +213,13 @@ - (void)testIncompleteAppDelegateRemoteNotificationWithFetchHandlerMethod {
187213 [self .proxy swizzleMethodsIfPossible ];
188214
189215 SEL remoteNotificationWithFetchHandler =
190- @selector (application:didReceiveRemoteNotification:fetchCompletionHandler: );
216+ @selector (application:didReceiveRemoteNotification:fetchCompletionHandler: );
191217 XCTAssertFalse ([incompleteAppDelegate respondsToSelector: remoteNotificationWithFetchHandler]);
218+
219+ #if TARGET_OS_IOS
192220 SEL remoteNotification = @selector (application:didReceiveRemoteNotification: );
193221 XCTAssertTrue ([incompleteAppDelegate respondsToSelector: remoteNotification]);
222+ #endif // TARGET_OS_IOS
194223}
195224
196225- (void )testSwizzledAppDelegateRemoteNotificationMethods {
@@ -219,14 +248,34 @@ - (void)testSwizzledAppDelegateRemoteNotificationMethods {
219248 // Verify our swizzled method was called
220249 OCMExpect ([self .mockMessaging appDidReceiveMessage: notification]);
221250
222- [appDelegate application: OCMClassMock ([UIApplication class ])
251+ [appDelegate application: self .mockSharedApplication
223252 didReceiveRemoteNotification: notification
224253 fetchCompletionHandler: ^(UIBackgroundFetchResult result) {}];
225254
226255 // Verify our original method was called
227256 XCTAssertTrue (appDelegate.remoteNotificationWithFetchHandlerWasCalled );
228257
229258 [self .mockMessaging verify ];
259+
260+ // Verify application:didRegisterForRemoteNotificationsWithDeviceToken:
261+ NSData *deviceToken = [NSData data ];
262+
263+ OCMExpect ([self .mockMessaging setAPNSToken: deviceToken]);
264+
265+ [appDelegate application: self .mockSharedApplication
266+ didRegisterForRemoteNotificationsWithDeviceToken: deviceToken];
267+
268+ XCTAssertEqual (appDelegate.deviceToken , deviceToken);
269+ [self .mockMessaging verify ];
270+
271+ // Verify application:didFailToRegisterForRemoteNotificationsWithError:
272+ NSError *error = [NSError errorWithDomain: @" tests" code: -1 userInfo: nil ];
273+
274+ [appDelegate application: self .mockSharedApplication
275+ didFailToRegisterForRemoteNotificationsWithError: error];
276+
277+ XCTAssertEqual (appDelegate.registerForRemoteNotificationsError , error);
278+
230279#endif
231280}
232281
0 commit comments