aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Messaging/Tests/FIRMessagingRemoteNotificationsProxyTest.m
blob: 3453f1146c582c87d2df8d30d780f4955805141c (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
/*
 * 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.
 */

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#import <UserNotifications/UserNotifications.h>
#endif
#import <XCTest/XCTest.h>

#import <OCMock/OCMock.h>

#import "FIRMessaging.h"
#import "FIRMessagingRemoteNotificationsProxy.h"

#pragma mark - Expose Internal Methods for Testing
// Expose some internal properties and methods here, in order to test
@interface FIRMessagingRemoteNotificationsProxy ()

@property(readonly, nonatomic) BOOL didSwizzleMethods;
@property(readonly, nonatomic) BOOL didSwizzleAppDelegateMethods;

@property(readonly, nonatomic) BOOL hasSwizzledUserNotificationDelegate;
@property(readonly, nonatomic) BOOL isObservingUserNotificationDelegateChanges;

@property(strong, readonly, nonatomic) id userNotificationCenter;
@property(strong, readonly, nonatomic) id currentUserNotificationCenterDelegate;

+ (instancetype)sharedProxy;

- (BOOL)swizzleAppDelegateMethods:(id<UIApplicationDelegate>)appDelegate;
- (void)listenForDelegateChangesInUserNotificationCenter:(id)notificationCenter;
- (void)swizzleUserNotificationCenterDelegate:(id)delegate;
- (void)unswizzleUserNotificationCenterDelegate:(id)delegate;

void FCM_swizzle_appDidReceiveRemoteNotification(id self,
                                                 SEL _cmd,
                                                 UIApplication *app,
                                                 NSDictionary *userInfo);
void FCM_swizzle_appDidReceiveRemoteNotificationWithHandler(
    id self, SEL _cmd, UIApplication *app, NSDictionary *userInfo,
    void (^handler)(UIBackgroundFetchResult));
void FCM_swizzle_willPresentNotificationWithHandler(
    id self, SEL _cmd, id center, id notification, void (^handler)(NSUInteger));
void FCM_swizzle_didReceiveNotificationResponseWithHandler(
    id self, SEL _cmd, id center, id response, void (^handler)());

@end

#pragma mark - Incomplete App Delegate
@interface IncompleteAppDelegate : NSObject <UIApplicationDelegate>
@end
@implementation IncompleteAppDelegate
@end

#pragma mark - Fake AppDelegate
@interface FakeAppDelegate : NSObject <UIApplicationDelegate>
@property(nonatomic) BOOL remoteNotificationMethodWasCalled;
@property(nonatomic) BOOL remoteNotificationWithFetchHandlerWasCalled;
@end
@implementation FakeAppDelegate
- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo {
  self.remoteNotificationMethodWasCalled = YES;
}
- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  self.remoteNotificationWithFetchHandlerWasCalled = YES;
}
@end

#pragma mark - Incompete UNUserNotificationCenterDelegate
@interface IncompleteUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
@end
@implementation IncompleteUserNotificationCenterDelegate
@end

#pragma mark - Fake UNUserNotificationCenterDelegate

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface FakeUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
@property(nonatomic) BOOL willPresentWasCalled;
@property(nonatomic) BOOL didReceiveResponseWasCalled;
@end
@implementation FakeUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))
    completionHandler {
  self.willPresentWasCalled = YES;
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  self.didReceiveResponseWasCalled = YES;
}
@end
#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

#pragma mark - Local, Per-Test Properties

@interface FIRMessagingRemoteNotificationsProxyTest : XCTestCase

@property(nonatomic, strong) FIRMessagingRemoteNotificationsProxy *proxy;
@property(nonatomic, strong) id mockProxy;
@property(nonatomic, strong) id mockProxyClass;
@property(nonatomic, strong) id mockMessagingClass;

@end

@implementation FIRMessagingRemoteNotificationsProxyTest

- (void)setUp {
  [super setUp];
  _proxy = [[FIRMessagingRemoteNotificationsProxy alloc] init];
  _mockProxy = OCMPartialMock(_proxy);
  _mockProxyClass = OCMClassMock([FIRMessagingRemoteNotificationsProxy class]);
  // Update +sharedProxy to always return our partial mock of FIRMessagingRemoteNotificationsProxy
  OCMStub([_mockProxyClass sharedProxy]).andReturn(_mockProxy);
  // Many of our swizzled methods call [FIRMessaging messaging], but we don't need it,
  // so just stub it to return nil
  _mockMessagingClass = OCMClassMock([FIRMessaging class]);
  OCMStub([_mockMessagingClass messaging]).andReturn(nil);
}

- (void)tearDown {
  [_mockMessagingClass stopMocking];
  _mockMessagingClass = nil;

  [_mockProxyClass stopMocking];
  _mockProxyClass = nil;

  [_mockProxy stopMocking];
  _mockProxy = nil;

  _proxy = nil;
  [super tearDown];
}

#pragma mark - Method Swizzling Tests

- (void)testSwizzlingNonAppDelegate {
  id randomObject = @"Random Object that is not an App Delegate";
  [self.proxy swizzleAppDelegateMethods:randomObject];
  XCTAssertFalse(self.proxy.didSwizzleAppDelegateMethods);
}

- (void)testSwizzlingAppDelegate {
  IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  [self.proxy swizzleAppDelegateMethods:incompleteAppDelegate];
  XCTAssertTrue(self.proxy.didSwizzleAppDelegateMethods);
}

- (void)testSwizzledIncompleteAppDelegateRemoteNotificationMethod {
  IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  [self.mockProxy swizzleAppDelegateMethods:incompleteAppDelegate];
  SEL selector = @selector(application:didReceiveRemoteNotification:);
  XCTAssertTrue([incompleteAppDelegate respondsToSelector:selector]);
  [incompleteAppDelegate application:OCMClassMock([UIApplication class])
      didReceiveRemoteNotification:@{}];
  // Verify our swizzled method was called
  OCMVerify(FCM_swizzle_appDidReceiveRemoteNotification);
}

// If the remote notification with fetch handler is NOT implemented, we will force-implement
// the backup -application:didReceiveRemoteNotification: method
- (void)testIncompleteAppDelegateRemoteNotificationWithFetchHandlerMethod {
  IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  [self.mockProxy swizzleAppDelegateMethods:incompleteAppDelegate];
  SEL remoteNotificationWithFetchHandler =
  @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:);
  XCTAssertFalse([incompleteAppDelegate respondsToSelector:remoteNotificationWithFetchHandler]);

  SEL remoteNotification = @selector(application:didReceiveRemoteNotification:);
  XCTAssertTrue([incompleteAppDelegate respondsToSelector:remoteNotification]);
}

- (void)testSwizzledAppDelegateRemoteNotificationMethods {
  FakeAppDelegate *appDelegate = [[FakeAppDelegate alloc] init];
  [self.mockProxy swizzleAppDelegateMethods:appDelegate];
  [appDelegate application:OCMClassMock([UIApplication class]) didReceiveRemoteNotification:@{}];
  // Verify our swizzled method was called
  OCMVerify(FCM_swizzle_appDidReceiveRemoteNotification);
  // Verify our original method was called
  XCTAssertTrue(appDelegate.remoteNotificationMethodWasCalled);

  // Now call the remote notification with handler method
  [appDelegate application:OCMClassMock([UIApplication class])
      didReceiveRemoteNotification:@{}
      fetchCompletionHandler:^(UIBackgroundFetchResult result) {}];
  // Verify our swizzled method was called
  OCMVerify(FCM_swizzle_appDidReceiveRemoteNotificationWithHandler);
  // Verify our original method was called
  XCTAssertTrue(appDelegate.remoteNotificationWithFetchHandlerWasCalled);
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

- (void)testListeningForDelegateChangesOnInvalidUserNotificationCenter {
  id randomObject = @"Random Object that is not a User Notification Center";
  [self.proxy listenForDelegateChangesInUserNotificationCenter:randomObject];
  XCTAssertFalse(self.proxy.isObservingUserNotificationDelegateChanges);
}

- (void)testSwizzlingInvalidUserNotificationCenterDelegate {
  id randomObject = @"Random Object that is not a User Notification Center Delegate";
  [self.proxy swizzleUserNotificationCenterDelegate:randomObject];
  XCTAssertFalse(self.proxy.hasSwizzledUserNotificationDelegate);
}

- (void)testSwizzlingUserNotificationsCenterDelegate {
  FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
  [self.proxy swizzleUserNotificationCenterDelegate:delegate];
  XCTAssertTrue(self.proxy.hasSwizzledUserNotificationDelegate);
}

// Use a fake delegate that doesn't actually implement the needed delegate method.
// Our swizzled method should not be called.

- (void)testIncompleteUserNotificationCenterDelegateMethod {
  // Early exit if running on pre iOS 10
  if (![UNNotification class]) {
    return;
  }
  IncompleteUserNotificationCenterDelegate *delegate =
      [[IncompleteUserNotificationCenterDelegate alloc] init];
  [self.mockProxy swizzleUserNotificationCenterDelegate:delegate];
  // Because the incomplete delete does not implement either of the optional delegate methods, we
  // should swizzle nothing. If we had swizzled them, then respondsToSelector: would return YES
  // even though the delegate does not implement the methods.
  SEL willPresentSelector = @selector(userNotificationCenter:willPresentNotification:withCompletionHandler:);
  XCTAssertFalse([delegate respondsToSelector:willPresentSelector]);
  SEL didReceiveResponseSelector =
      @selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:);
  XCTAssertFalse([delegate respondsToSelector:didReceiveResponseSelector]);
}

// Use an object that does actually implement the optional methods. Both should be called.
- (void)testSwizzledUserNotificationsCenterDelegate {
  // Early exit if running on pre iOS 10
  if (![UNNotification class]) {
    return;
  }
  FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
  [self.mockProxy swizzleUserNotificationCenterDelegate:delegate];
  // Invoking delegate method should also invoke our swizzled method
  // The swizzled method uses the +sharedProxy, which should be
  // returning our mocked proxy.
  // Use non-nil, proper classes, otherwise our SDK bails out.
  [delegate userNotificationCenter:OCMClassMock([UNUserNotificationCenter class])
           willPresentNotification:[self generateMockNotification]
             withCompletionHandler:^(NSUInteger options) {}];
  // Verify our swizzled method was called
  OCMVerify(FCM_swizzle_willPresentNotificationWithHandler);
  // Verify our original method was called
  XCTAssertTrue(delegate.willPresentWasCalled);

  [delegate userNotificationCenter:OCMClassMock([UNUserNotificationCenter class])
    didReceiveNotificationResponse:[self generateMockNotificationResponse]
             withCompletionHandler:^{}];
  // Verify our swizzled method was called
  OCMVerify(FCM_swizzle_appDidReceiveRemoteNotificationWithHandler);
  // Verify our original method was called
  XCTAssertTrue(delegate.didReceiveResponseWasCalled);
}

- (id)generateMockNotification {
  // Stub out: notification.request.content.userInfo
  id mockNotification = OCMClassMock([UNNotification class]);
  id mockRequest = OCMClassMock([UNNotificationRequest class]);
  id mockContent = OCMClassMock([UNNotificationContent class]);
  OCMStub([mockContent userInfo]).andReturn(@{});
  OCMStub([mockRequest content]).andReturn(mockContent);
  OCMStub([mockNotification request]).andReturn(mockRequest);
  return mockNotification;
}

- (id)generateMockNotificationResponse {
  // Stub out: response.[mock notification above]
  id mockNotificationResponse = OCMClassMock([UNNotificationResponse class]);
  id mockNotification = [self generateMockNotification];
  OCMStub([mockNotificationResponse notification]).andReturn(mockNotification);
  return mockNotificationResponse;
}

#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

@end