|
| 1 | +// |
| 2 | +// LPActionManagerTest.m |
| 3 | +// Leanplum |
| 4 | +// |
| 5 | +// Created by Alexis Oyama on 11/3/16. |
| 6 | +// Copyright (c) 2016 Leanplum. All rights reserved. |
| 7 | +// |
| 8 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 9 | +// or more contributor license agreements. See the NOTICE file |
| 10 | +// distributed with this work for additional information |
| 11 | +// regarding copyright ownership. The ASF licenses this file |
| 12 | +// to you under the Apache License, Version 2.0 (the "License"); |
| 13 | +// you may not use this file except in compliance with the License. |
| 14 | +// You may obtain a copy of the License at |
| 15 | +// |
| 16 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +// |
| 18 | +// Unless required by applicable law or agreed to in writing, |
| 19 | +// software distributed under the License is distributed on an |
| 20 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 21 | +// KIND, either express or implied. See the License for the |
| 22 | +// specific language governing permissions and limitations |
| 23 | +// under the License. |
| 24 | + |
| 25 | + |
| 26 | +#import <XCTest/XCTest.h> |
| 27 | +#import <OCMock/OCMock.h> |
| 28 | +#import <OHHTTPStubs/OHHTTPStubs.h> |
| 29 | +#import <OHHTTPStubs/OHPathHelpers.h> |
| 30 | +#import "LPActionManager.h" |
| 31 | +#import "LeanplumHelper.h" |
| 32 | +#import "LeanplumRequest+Categories.h" |
| 33 | +#import "LPNetworkEngine+Category.h" |
| 34 | +#import "Leanplum+Extensions.h" |
| 35 | +#import "LPUIAlert.h" |
| 36 | + |
| 37 | +@interface LPActionManager (Test) |
| 38 | +- (void)requireMessageContent:(NSString *)messageId |
| 39 | + withCompletionBlock:(LeanplumVariablesChangedBlock)onCompleted; |
| 40 | ++ (NSString *)messageIdFromUserInfo:(NSDictionary *)userInfo; |
| 41 | +- (void)sendUserNotificationSettingsIfChanged:(UIUserNotificationSettings *)notificationSettings; |
| 42 | +- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo |
| 43 | + withAction:(NSString *)action |
| 44 | + fetchCompletionHandler:(LeanplumFetchCompletionBlock)completionHandler; |
| 45 | +- (void)leanplum_application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; |
| 46 | +@end |
| 47 | + |
| 48 | +@interface ActionManagerTest : XCTestCase |
| 49 | + |
| 50 | +@end |
| 51 | + |
| 52 | +@implementation ActionManagerTest |
| 53 | + |
| 54 | ++ (void)setUp |
| 55 | +{ |
| 56 | + [super setUp]; |
| 57 | + // Called only once to setup method swizzling. |
| 58 | + [LeanplumHelper setup_method_swizzling]; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)setUp |
| 62 | +{ |
| 63 | + [super setUp]; |
| 64 | + // Automatically sets up AppId and AccessKey for development mode. |
| 65 | + [LeanplumHelper setup_development_test]; |
| 66 | +} |
| 67 | + |
| 68 | +- (void)tearDown |
| 69 | +{ |
| 70 | + [super tearDown]; |
| 71 | + [LeanplumHelper clean_up]; |
| 72 | +} |
| 73 | + |
| 74 | +- (void)test_matched_trigger |
| 75 | +{ |
| 76 | + LPActionManager *manager = [LPActionManager sharedManager]; |
| 77 | + |
| 78 | + // Message Object |
| 79 | + NSDictionary *config = @{@"whenLimits":@{@"children":@[], |
| 80 | + @"objects":@[], |
| 81 | + @"subjects":[NSNull null] |
| 82 | + }, |
| 83 | + @"whenTriggers":@{@"children":@[@{@"noun":@"Sick", |
| 84 | + @"objects":@[@"symptom", @"cough"], |
| 85 | + @"subject":@"event", |
| 86 | + @"verb":@"triggersWithParameter" |
| 87 | + }], |
| 88 | + @"verb":@"OR" |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + // track parameters |
| 93 | + LPContextualValues *contextualValues = [[LPContextualValues alloc] init]; |
| 94 | + |
| 95 | + // [Leanplum track:@"Sick"] |
| 96 | + contextualValues.parameters = @{}; |
| 97 | + LeanplumMessageMatchResult result = [manager shouldShowMessage:@"" |
| 98 | + withConfig:config |
| 99 | + when:@"event" |
| 100 | + withEventName:@"Sick" |
| 101 | + contextualValues:contextualValues]; |
| 102 | + XCTAssertFalse(result.matchedTrigger); |
| 103 | + |
| 104 | + // [Leanplum track:@"Sick" withParameters:@{@"symptom":@""}] |
| 105 | + contextualValues.parameters = @{@"symptom":@""}; |
| 106 | + result = [manager shouldShowMessage:@"" |
| 107 | + withConfig:config |
| 108 | + when:@"event" |
| 109 | + withEventName:@"Sick" |
| 110 | + contextualValues:contextualValues]; |
| 111 | + XCTAssertFalse(result.matchedTrigger); |
| 112 | + |
| 113 | + // [Leanplum track:@"Sick" withParameters:@{@"test":@"test"}] |
| 114 | + contextualValues.parameters = @{@"test":@"test"}; |
| 115 | + result = [manager shouldShowMessage:@"" |
| 116 | + withConfig:config |
| 117 | + when:@"event" |
| 118 | + withEventName:@"Sick" |
| 119 | + contextualValues:contextualValues]; |
| 120 | + XCTAssertFalse(result.matchedTrigger); |
| 121 | + |
| 122 | + // [Leanplum track:@"Sick" withParameters:@{@"symptom":@"cough"}] |
| 123 | + contextualValues.parameters = @{@"symptom":@"cough"}; |
| 124 | + result = [manager shouldShowMessage:@"" |
| 125 | + withConfig:config |
| 126 | + when:@"event" |
| 127 | + withEventName:@"Sick" |
| 128 | + contextualValues:contextualValues]; |
| 129 | + XCTAssertTrue(result.matchedTrigger); |
| 130 | + |
| 131 | + // [Leanplum track:@"Sick" withParameters:nil |
| 132 | + contextualValues.parameters = nil; |
| 133 | + result = [manager shouldShowMessage:@"" |
| 134 | + withConfig:config |
| 135 | + when:@"event" |
| 136 | + withEventName:@"Sick" |
| 137 | + contextualValues:contextualValues]; |
| 138 | + XCTAssertFalse(result.matchedTrigger); |
| 139 | + |
| 140 | + // [Leanplum track:@"NotSick" withParameters:@{@"symptom":@"cough"}] |
| 141 | + contextualValues.parameters = @{@"symptom":@"cough"}; |
| 142 | + result = [manager shouldShowMessage:@"" |
| 143 | + withConfig:config |
| 144 | + when:@"event" |
| 145 | + withEventName:@"NotSick" |
| 146 | + contextualValues:contextualValues]; |
| 147 | + XCTAssertFalse(result.matchedTrigger); |
| 148 | +} |
| 149 | + |
| 150 | +- (void)test_require_message_content |
| 151 | +{ |
| 152 | + // Vaidate request. |
| 153 | + [LeanplumRequest validate_request:^(NSString *method, NSString *apiMethod, |
| 154 | + NSDictionary *params) { |
| 155 | + XCTAssertEqualObjects(apiMethod, @"getVars"); |
| 156 | + XCTAssertEqual(params[@"includeMessageId"], @"messageId"); |
| 157 | + return YES; |
| 158 | + }]; |
| 159 | + [[LPActionManager sharedManager] requireMessageContent:@"messageId" withCompletionBlock:nil]; |
| 160 | +} |
| 161 | + |
| 162 | +- (void)test_notification_action |
| 163 | +{ |
| 164 | + id classMock = OCMClassMock([LPUIAlert class]); |
| 165 | + |
| 166 | + NSDictionary* userInfo = @{ |
| 167 | + @"_lpm": @"messageId", |
| 168 | + @"_lpx": @"test_action", |
| 169 | + @"aps" : @{@"alert": @"test"}}; |
| 170 | + [[LPActionManager sharedManager] maybePerformNotificationActions:userInfo |
| 171 | + action:nil |
| 172 | + active:YES]; |
| 173 | + |
| 174 | + OCMVerify([classMock showWithTitle:OCMOCK_ANY |
| 175 | + message:OCMOCK_ANY |
| 176 | + cancelButtonTitle:OCMOCK_ANY |
| 177 | + otherButtonTitles:OCMOCK_ANY |
| 178 | + block:OCMOCK_ANY]); |
| 179 | +} |
| 180 | + |
| 181 | +- (void) test_receive_notification |
| 182 | +{ |
| 183 | + NSDictionary* userInfo = @{ |
| 184 | + @"_lpm": @"messageId", |
| 185 | + @"_lpx": @"test_action", |
| 186 | + @"aps" : @{@"alert": @"test"}}; |
| 187 | + |
| 188 | + XCTestExpectation* expectation = [self expectationWithDescription:@"notification"]; |
| 189 | + |
| 190 | + [[LPActionManager sharedManager] didReceiveRemoteNotification:userInfo |
| 191 | + withAction:@"test_action" |
| 192 | + fetchCompletionHandler: |
| 193 | + ^(LeanplumUIBackgroundFetchResult result) { |
| 194 | + [expectation fulfill]; |
| 195 | + }]; |
| 196 | + [self waitForExpectationsWithTimeout:10 handler:nil]; |
| 197 | +} |
| 198 | + |
| 199 | +- (void)test_messageId_from_userinfo |
| 200 | +{ |
| 201 | + NSDictionary *userInfo = nil; |
| 202 | + NSString* messageId = nil; |
| 203 | + |
| 204 | + userInfo = @{@"_lpm": @"messageId"}; |
| 205 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 206 | + XCTAssertEqual(messageId, @"messageId"); |
| 207 | + |
| 208 | + userInfo = @{@"_lpu": @"messageId"}; |
| 209 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 210 | + XCTAssertEqual(messageId, @"messageId"); |
| 211 | + |
| 212 | + userInfo = @{@"_lpn": @"messageId"}; |
| 213 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 214 | + XCTAssertEqual(messageId, @"messageId"); |
| 215 | + |
| 216 | + userInfo = @{@"_lpv": @"messageId"}; |
| 217 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 218 | + XCTAssertEqual(messageId, @"messageId"); |
| 219 | +} |
| 220 | + |
| 221 | +- (void)test_push_token |
| 222 | +{ |
| 223 | + XCTAssertTrue([LeanplumHelper start_production_test]); |
| 224 | + |
| 225 | + // Partial mock Action Manager. |
| 226 | + LPActionManager *manager = [LPActionManager sharedManager]; |
| 227 | + id actionManagerMock = OCMPartialMock(manager); |
| 228 | + OCMStub([actionManagerMock sharedManager]).andReturn(actionManagerMock); |
| 229 | + OCMStub([actionManagerMock respondsToSelector: |
| 230 | + @selector(leanplum_application:didRegisterForRemoteNotificationsWithDeviceToken:)]).andReturn(NO); |
| 231 | + |
| 232 | + // Remove Push Token. |
| 233 | + NSString *pushTokenKey = [Leanplum pushTokenKey]; |
| 234 | + [[NSUserDefaults standardUserDefaults] removeObjectForKey:pushTokenKey]; |
| 235 | + [[NSUserDefaults standardUserDefaults] synchronize]; |
| 236 | + |
| 237 | + // Test push token is sent on clean start. |
| 238 | + UIApplication *app = [UIApplication sharedApplication]; |
| 239 | + XCTestExpectation *expectNewToken = [self expectationWithDescription:@"expectNewToken"]; |
| 240 | + NSData *token = [@"sample" dataUsingEncoding:NSUTF8StringEncoding]; |
| 241 | + NSString *formattedToken = [token description]; |
| 242 | + formattedToken = [[[formattedToken stringByReplacingOccurrencesOfString:@"<" withString:@""] |
| 243 | + stringByReplacingOccurrencesOfString:@">" withString:@""] |
| 244 | + stringByReplacingOccurrencesOfString:@" " withString:@""]; |
| 245 | + [LeanplumRequest validate_request:^BOOL(NSString *method, NSString *apiMethod, |
| 246 | + NSDictionary *params) { |
| 247 | + XCTAssertTrue([apiMethod isEqual:@"setDeviceAttributes"]); |
| 248 | + XCTAssertTrue([params[@"iosPushToken"] isEqual:formattedToken]); |
| 249 | + [expectNewToken fulfill]; |
| 250 | + return YES; |
| 251 | + }]; |
| 252 | + [manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token]; |
| 253 | + [self waitForExpectationsWithTimeout:2 handler:nil]; |
| 254 | + |
| 255 | + // Test push token will not be sent with the same token. |
| 256 | + [LeanplumRequest validate_request:^BOOL(NSString *method, NSString *apiMethod, |
| 257 | + NSDictionary *params) { |
| 258 | + XCTAssertTrue(NO); |
| 259 | + return YES; |
| 260 | + }]; |
| 261 | + [manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token]; |
| 262 | + |
| 263 | + // Test push token is sent if the token changes. |
| 264 | + token = [@"sample2" dataUsingEncoding:NSUTF8StringEncoding]; |
| 265 | + formattedToken = [token description]; |
| 266 | + formattedToken = [[[formattedToken stringByReplacingOccurrencesOfString:@"<" withString:@""] |
| 267 | + stringByReplacingOccurrencesOfString:@">" withString:@""] |
| 268 | + stringByReplacingOccurrencesOfString:@" " withString:@""]; |
| 269 | + XCTestExpectation *expectUpdatedToken = [self expectationWithDescription:@"expectUpdatedToken"]; |
| 270 | + [LeanplumRequest validate_request:^BOOL(NSString *method, NSString *apiMethod, |
| 271 | + NSDictionary *params) { |
| 272 | + XCTAssertTrue([apiMethod isEqual:@"setDeviceAttributes"]); |
| 273 | + XCTAssertTrue([params[@"iosPushToken"] isEqual:formattedToken]); |
| 274 | + [expectUpdatedToken fulfill]; |
| 275 | + return YES; |
| 276 | + }]; |
| 277 | + [manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token]; |
| 278 | + [self waitForExpectationsWithTimeout:2 handler:nil]; |
| 279 | +} |
| 280 | + |
| 281 | +@end |
0 commit comments