@@ -39,8 +39,6 @@ + (void)setUp
3939- (void )setUp
4040{
4141 [super setUp ];
42- // Automatically sets up AppId and AccessKey for development mode.
43- [LeanplumHelper setup_development_test ];
4442}
4543
4644- (void )tearDown
@@ -49,16 +47,48 @@ - (void)tearDown
4947 [LeanplumHelper clean_up ];
5048}
5149
52- - (void ) test_push_token
50+ - (LPPushNotificationsManager*) mockManager
5351{
54- XCTAssertTrue ([LeanplumHelper start_production_test ]);
55-
56- // Partial mock Action Manager.
5752 LPPushNotificationsManager *manager = [LPPushNotificationsManager sharedManager ];
5853 id pushNotificationsManagerMock = OCMPartialMock (manager);
5954 OCMStub ([LPPushNotificationsManager sharedManager ]).andReturn (pushNotificationsManagerMock);
6055 OCMStub ([pushNotificationsManagerMock respondsToSelector:
6156 @selector (leanplum_application:didRegisterForRemoteNotificationsWithDeviceToken: )]).andReturn (NO );
57+ return manager;
58+ }
59+
60+ - (NSString *)formatToken : (NSData *)token
61+ {
62+ NSString *formattedToken = [[LPNotificationsManager shared ] hexadecimalStringFromData: token];
63+ formattedToken = [[[formattedToken stringByReplacingOccurrencesOfString: @" <" withString: @" " ]
64+ stringByReplacingOccurrencesOfString: @" >" withString: @" " ]
65+ stringByReplacingOccurrencesOfString: @" " withString: @" " ];
66+ return formattedToken;
67+ }
68+
69+ - (void )removePushTypes
70+ {
71+ NSString *settingsKey = [[LPPushNotificationsManager sharedManager ] leanplum_createUserNotificationSettingsKey ];
72+ [[NSUserDefaults standardUserDefaults ] removeObjectForKey: settingsKey];
73+ [[NSUserDefaults standardUserDefaults ] synchronize ];
74+ }
75+
76+ - (void )mockUserNotificationSettings : (UIUserNotificationType)type withCategoryId : (NSString *)categoryId
77+ {
78+ id mockApplication = OCMClassMock ([UIApplication class ]);
79+ OCMStub ([mockApplication sharedApplication ]).andReturn (mockApplication);
80+ UIMutableUserNotificationCategory *cat = [[UIMutableUserNotificationCategory alloc ] init ];
81+ cat.identifier = categoryId;
82+ UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: type categories: [NSSet setWithObject: cat]];
83+ OCMStub ([mockApplication currentUserNotificationSettings ]).andReturn (settings);
84+ }
85+
86+ - (void )test_push_token
87+ {
88+ XCTAssertTrue ([LeanplumHelper start_production_test ]);
89+
90+ // Partial mock Action Manager.
91+ LPPushNotificationsManager *manager = [self mockManager ];
6292
6393 // Remove Push Token.
6494 [manager removePushToken ];
@@ -67,14 +97,10 @@ - (void)test_push_token
6797 UIApplication *app = [UIApplication sharedApplication ];
6898 XCTestExpectation *expectNewToken = [self expectationWithDescription: @" expectNewToken" ];
6999 NSData *token = [@" sample" dataUsingEncoding: NSUTF8StringEncoding];
70- NSString *formattedToken = [[LPNotificationsManager shared ] hexadecimalStringFromData: token];
71- formattedToken = [[[formattedToken stringByReplacingOccurrencesOfString: @" <" withString: @" " ]
72- stringByReplacingOccurrencesOfString: @" >" withString: @" " ]
73- stringByReplacingOccurrencesOfString: @" " withString: @" " ];
74100 [LeanplumRequest validate_request: ^BOOL (NSString *method, NSString *apiMethod,
75101 NSDictionary *params) {
76102 XCTAssertTrue ([apiMethod isEqual: @" setDeviceAttributes" ]);
77- XCTAssertTrue ([params[@" iosPushToken" ] isEqual: formattedToken ]);
103+ XCTAssertTrue ([params[@" iosPushToken" ] isEqual: [ self formatToken: token] ]);
78104 [expectNewToken fulfill ];
79105 return YES ;
80106 }];
@@ -91,15 +117,11 @@ - (void)test_push_token
91117
92118 // Test push token is sent if the token changes.
93119 token = [@" sample2" dataUsingEncoding: NSUTF8StringEncoding];
94- formattedToken = [[LPNotificationsManager shared ] hexadecimalStringFromData: token];
95- formattedToken = [[[formattedToken stringByReplacingOccurrencesOfString: @" <" withString: @" " ]
96- stringByReplacingOccurrencesOfString: @" >" withString: @" " ]
97- stringByReplacingOccurrencesOfString: @" " withString: @" " ];
98120 XCTestExpectation *expectUpdatedToken = [self expectationWithDescription: @" expectUpdatedToken" ];
99121 [LeanplumRequest validate_request: ^BOOL (NSString *method, NSString *apiMethod,
100122 NSDictionary *params) {
101123 XCTAssertTrue ([apiMethod isEqual: @" setDeviceAttributes" ]);
102- XCTAssertTrue ([params[@" iosPushToken " ] isEqual: formattedToken ]);
124+ XCTAssertTrue ([params[LP_PARAM_DEVICE_PUSH_TOKEN ] isEqual: [ self formatToken: token] ]);
103125 [expectUpdatedToken fulfill ];
104126 return YES ;
105127 }];
@@ -108,6 +130,103 @@ - (void)test_push_token
108130 [self waitForExpectationsWithTimeout: 2 handler: nil ];
109131}
110132
133+ /* *
134+ * Test iOS Push Types are sent when notifications are registered successfully
135+ */
136+ - (void )test_push_types
137+ {
138+ // Partial mock Action Manager.
139+ LPPushNotificationsManager *manager = [self mockManager ];
140+
141+ // Remove Push Token.
142+ [manager removePushToken ];
143+ [self removePushTypes ];
144+
145+ [self mockUserNotificationSettings: UIUserNotificationTypeAlert withCategoryId: @" testCategory" ];
146+
147+ UIApplication *app = [UIApplication sharedApplication ];
148+ XCTestExpectation *expectNewToken = [self expectationWithDescription: @" expectNewToken" ];
149+ NSData *token = [@" sample" dataUsingEncoding: NSUTF8StringEncoding];
150+ [LeanplumRequest validate_request: ^BOOL (NSString *method, NSString *apiMethod,
151+ NSDictionary *params) {
152+ XCTAssertTrue ([apiMethod isEqual: @" setDeviceAttributes" ]);
153+ XCTAssertTrue ([params[LP_PARAM_DEVICE_PUSH_TOKEN] isEqual: [self formatToken: token]]);
154+ XCTAssertTrue ([params[LP_PARAM_DEVICE_USER_NOTIFICATION_TYPES] isEqual: @(UIUserNotificationTypeAlert)]);
155+ XCTAssertTrue ([params[LP_PARAM_DEVICE_USER_NOTIFICATION_CATEGORIES] isEqual: [LPJSON stringFromJSON: @[@" testCategory" ]?: @" " ]]);
156+ [expectNewToken fulfill ];
157+ return YES ;
158+ }];
159+
160+ [manager leanplum_application: app didRegisterForRemoteNotificationsWithDeviceToken: token];
161+
162+ [self mockUserNotificationSettings: UIUserNotificationTypeBadge withCategoryId: @" testCategory2" ];
163+ // Test categories will be sent even if token is the same
164+ [LeanplumRequest validate_request: ^BOOL (NSString *method, NSString *apiMethod,
165+ NSDictionary *params) {
166+ XCTAssertTrue ([apiMethod isEqual: @" setDeviceAttributes" ]);
167+ XCTAssertNil (params[LP_PARAM_DEVICE_PUSH_TOKEN]);
168+ XCTAssertTrue ([params[LP_PARAM_DEVICE_USER_NOTIFICATION_TYPES] isEqual: @(UIUserNotificationTypeBadge)]);
169+ XCTAssertTrue ([params[LP_PARAM_DEVICE_USER_NOTIFICATION_CATEGORIES] isEqual: [LPJSON stringFromJSON: @[@" testCategory2" ]?: @" " ]]);
170+ return YES ;
171+ }];
172+ [manager leanplum_application: app didRegisterForRemoteNotificationsWithDeviceToken: token];
173+
174+ [self waitForExpectationsWithTimeout: 2 handler: nil ];
175+ }
176+
177+ - (void )test_push_types_foreground
178+ {
179+ [self removePushTypes ];
180+
181+ LPPushNotificationsManager *manager = [LPPushNotificationsManager sharedManager ];
182+ id pushNotificationsManagerMock = OCMPartialMock (manager);
183+ OCMStub ([LPPushNotificationsManager sharedManager ]).andReturn (pushNotificationsManagerMock);
184+
185+ // Call start to attach the observer for App Resume/Foreground
186+ // Note that multiple start calls will attach multiple observers
187+ if (!Leanplum.hasStarted ){
188+ XCTAssertTrue ([LeanplumHelper start_production_test ]);
189+ }
190+
191+ // Mock Application Notification Settings
192+ [self mockUserNotificationSettings: UIUserNotificationTypeAlert withCategoryId: @" testCategory" ];
193+
194+ XCTestExpectation *expectPushTypesSet = [self expectationWithDescription: @" expectPushTypesSet" ];
195+ [LeanplumRequest validate_request: ^BOOL (NSString *method, NSString *apiMethod,
196+ NSDictionary *params) {
197+ if ([apiMethod isEqual: @" setDeviceAttributes" ]) {
198+ // Use the mock object to verify
199+ OCMVerify ([[pushNotificationsManagerMock handler ] sendUserNotificationSettingsIfChanged: [OCMArg any ]]);
200+ XCTAssertTrue ([params[LP_PARAM_DEVICE_USER_NOTIFICATION_TYPES] isEqual: @(UIUserNotificationTypeAlert)]);
201+ XCTAssertTrue ([params[LP_PARAM_DEVICE_USER_NOTIFICATION_CATEGORIES] isEqual: [LPJSON stringFromJSON: @[@" testCategory" ]?: @" " ]]);
202+ [expectPushTypesSet fulfill ];
203+ }
204+ return YES ;
205+ }];
206+
207+ // Triggers sendUserNotificationSettingsIfChanged and resumeSession
208+ [[NSNotificationCenter defaultCenter ] postNotificationName: UIApplicationWillEnterForegroundNotification
209+ object: nil ];
210+
211+ // Verify no request is made if the settings are the same
212+ [LeanplumRequest validate_request: ^BOOL (NSString *method, NSString *apiMethod,
213+ NSDictionary *params) {
214+ if ([apiMethod isEqual: @" setDeviceAttributes" ]) {
215+ XCTAssertTrue (NO );
216+ }
217+ return YES ;
218+ }];
219+
220+ // Triggers sendUserNotificationSettingsIfChanged and resumeSession
221+ [[NSNotificationCenter defaultCenter ] postNotificationName: UIApplicationWillEnterForegroundNotification
222+ object: nil ];
223+ // Use the mock object to verify
224+ OCMVerify ([[pushNotificationsManagerMock handler ] sendUserNotificationSettingsIfChanged: [OCMArg any ]]);
225+
226+ [pushNotificationsManagerMock stopMocking ];
227+ [self waitForExpectationsWithTimeout: 2 handler: nil ];
228+ }
229+
111230- (void )test_update_and_remove_push_token
112231{
113232 LPPushNotificationsManager *manager = [LPPushNotificationsManager sharedManager ];
0 commit comments