Skip to content

Commit 267e9a6

Browse files
FIRMessagingRemoteNotificationsProxy updated to use GULAppDelegateSwizzler (#2683)
1 parent aa60779 commit 267e9a6

File tree

8 files changed

+109
-173
lines changed

8 files changed

+109
-173
lines changed

Example/Firebase.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7601,6 +7601,7 @@
76017601
"$(inherited)",
76027602
"COCOAPODS=1",
76037603
"GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1",
7604+
"GUL_APP_DELEGATE_TESTING=1",
76047605
);
76057606
HEADER_SEARCH_PATHS = (
76067607
"$(inherited)",

Example/Messaging/Tests/FIRMessagingRemoteNotificationsProxyTest.m

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
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

Example/Podfile

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@ target 'DynamicLinks_Example_iOS' do
6262
pod 'OCMock'
6363
pod 'GoogleUtilities/MethodSwizzler', :path => '../'
6464
pod 'GoogleUtilities/SwizzlerTestHelpers', :path => '../'
65-
66-
# Set define to turn on the unswizzler for the unit tests
67-
post_install do |installer_representation|
68-
installer_representation.pods_project.targets.each do |target|
69-
target.build_configurations.each do |config|
70-
if config.name != 'Release'
71-
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'GUL_UNSWIZZLING_ENABLED=1']
72-
end
73-
end
74-
end
75-
end
7665
end
7766
end
7867

@@ -272,3 +261,14 @@ target 'InstanceID_Example_tvOS' do
272261
end
273262
end
274263

264+
# Set define to turn on GUL_UNSWIZZLING and GUL_APP_DELEGATE_TESTING for the unit tests
265+
post_install do |installer_representation|
266+
installer_representation.pods_project.targets.each do |target|
267+
target.build_configurations.each do |config|
268+
if config.name != 'Release'
269+
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'GUL_UNSWIZZLING_ENABLED=1', 'GUL_APP_DELEGATE_TESTING=1']
270+
end
271+
end
272+
end
273+
end
274+

Firebase/Messaging/FIRMessaging.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ + (NSString *)FIRMessagingSDKCurrentLocale {
880880
- (void)receiver:(FIRMessagingReceiver *)receiver
881881
receivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
882882
if ([self.delegate respondsToSelector:@selector(messaging:didReceiveMessage:)]) {
883+
[self appDidReceiveMessage:remoteMessage.appData];
883884
#pragma clang diagnostic push
884885
#pragma clang diagnostic ignored "-Wunguarded-availability"
885886
[self.delegate messaging:self didReceiveMessage:remoteMessage];

0 commit comments

Comments
 (0)