|
| 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 | +@end |
| 46 | + |
| 47 | +@interface ActionManagerTest : XCTestCase |
| 48 | + |
| 49 | +@end |
| 50 | + |
| 51 | +@implementation ActionManagerTest |
| 52 | + |
| 53 | ++ (void)setUp |
| 54 | +{ |
| 55 | + [super setUp]; |
| 56 | + // Called only once to setup method swizzling. |
| 57 | + [LeanplumHelper setup_method_swizzling]; |
| 58 | +} |
| 59 | + |
| 60 | +- (void)setUp |
| 61 | +{ |
| 62 | + [super setUp]; |
| 63 | + // Automatically sets up AppId and AccessKey for development mode. |
| 64 | + [LeanplumHelper setup_development_test]; |
| 65 | +} |
| 66 | + |
| 67 | +- (void)tearDown |
| 68 | +{ |
| 69 | + [super tearDown]; |
| 70 | + [LeanplumHelper clean_up]; |
| 71 | +} |
| 72 | + |
| 73 | +- (void)test_matched_trigger |
| 74 | +{ |
| 75 | + LPActionManager *manager = [LPActionManager sharedManager]; |
| 76 | + |
| 77 | + // Message Object |
| 78 | + NSDictionary *config = @{@"whenLimits":@{@"children":@[], |
| 79 | + @"objects":@[], |
| 80 | + @"subjects":[NSNull null] |
| 81 | + }, |
| 82 | + @"whenTriggers":@{@"children":@[@{@"noun":@"Sick", |
| 83 | + @"objects":@[@"symptom", @"cough"], |
| 84 | + @"subject":@"event", |
| 85 | + @"verb":@"triggersWithParameter" |
| 86 | + }], |
| 87 | + @"verb":@"OR" |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + // track parameters |
| 92 | + LPContextualValues *contextualValues = [[LPContextualValues alloc] init]; |
| 93 | + |
| 94 | + // [Leanplum track:@"Sick"] |
| 95 | + contextualValues.parameters = @{}; |
| 96 | + LeanplumMessageMatchResult result = [manager shouldShowMessage:@"" |
| 97 | + withConfig:config |
| 98 | + when:@"event" |
| 99 | + withEventName:@"Sick" |
| 100 | + contextualValues:contextualValues]; |
| 101 | + XCTAssertFalse(result.matchedTrigger); |
| 102 | + |
| 103 | + // [Leanplum track:@"Sick" withParameters:@{@"symptom":@""}] |
| 104 | + contextualValues.parameters = @{@"symptom":@""}; |
| 105 | + result = [manager shouldShowMessage:@"" |
| 106 | + withConfig:config |
| 107 | + when:@"event" |
| 108 | + withEventName:@"Sick" |
| 109 | + contextualValues:contextualValues]; |
| 110 | + XCTAssertFalse(result.matchedTrigger); |
| 111 | + |
| 112 | + // [Leanplum track:@"Sick" withParameters:@{@"test":@"test"}] |
| 113 | + contextualValues.parameters = @{@"test":@"test"}; |
| 114 | + result = [manager shouldShowMessage:@"" |
| 115 | + withConfig:config |
| 116 | + when:@"event" |
| 117 | + withEventName:@"Sick" |
| 118 | + contextualValues:contextualValues]; |
| 119 | + XCTAssertFalse(result.matchedTrigger); |
| 120 | + |
| 121 | + // [Leanplum track:@"Sick" withParameters:@{@"symptom":@"cough"}] |
| 122 | + contextualValues.parameters = @{@"symptom":@"cough"}; |
| 123 | + result = [manager shouldShowMessage:@"" |
| 124 | + withConfig:config |
| 125 | + when:@"event" |
| 126 | + withEventName:@"Sick" |
| 127 | + contextualValues:contextualValues]; |
| 128 | + XCTAssertTrue(result.matchedTrigger); |
| 129 | + |
| 130 | + // [Leanplum track:@"Sick" withParameters:nil |
| 131 | + contextualValues.parameters = nil; |
| 132 | + result = [manager shouldShowMessage:@"" |
| 133 | + withConfig:config |
| 134 | + when:@"event" |
| 135 | + withEventName:@"Sick" |
| 136 | + contextualValues:contextualValues]; |
| 137 | + XCTAssertFalse(result.matchedTrigger); |
| 138 | + |
| 139 | + // [Leanplum track:@"NotSick" withParameters:@{@"symptom":@"cough"}] |
| 140 | + contextualValues.parameters = @{@"symptom":@"cough"}; |
| 141 | + result = [manager shouldShowMessage:@"" |
| 142 | + withConfig:config |
| 143 | + when:@"event" |
| 144 | + withEventName:@"NotSick" |
| 145 | + contextualValues:contextualValues]; |
| 146 | + XCTAssertFalse(result.matchedTrigger); |
| 147 | +} |
| 148 | + |
| 149 | +- (void)test_require_message_content |
| 150 | +{ |
| 151 | + // This stub have to be removed when start command is successfully executed. |
| 152 | + [OHHTTPStubs stubRequestsPassingTest: |
| 153 | + ^BOOL(NSURLRequest * _Nonnull request) { |
| 154 | + return [request.URL.host isEqualToString:API_HOST]; |
| 155 | + } withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) { |
| 156 | + NSString *response_file = OHPathForFile(@"simple_start_response.json", self.class); |
| 157 | + return [OHHTTPStubsResponse responseWithFileAtPath:response_file statusCode:200 |
| 158 | + headers:@{@"Content-Type":@"application/json"}]; |
| 159 | + }]; |
| 160 | + |
| 161 | + XCTAssertTrue([LeanplumHelper start_development_test]); |
| 162 | + |
| 163 | + // Vaidate request. |
| 164 | + [LeanplumRequest validate_request:^(NSString *method, NSString *apiMethod, |
| 165 | + NSDictionary *params) { |
| 166 | + XCTAssertEqualObjects(apiMethod, @"getVars"); |
| 167 | + XCTAssertEqual(params[@"includeMessageId"], @"messageId"); |
| 168 | + return YES; |
| 169 | + }]; |
| 170 | + [[LPActionManager sharedManager] requireMessageContent:@"messageId" withCompletionBlock:nil]; |
| 171 | +} |
| 172 | + |
| 173 | +- (void)test_notification_action |
| 174 | +{ |
| 175 | + id classMock = OCMClassMock([LPUIAlert class]); |
| 176 | + |
| 177 | + NSDictionary* userInfo = @{ |
| 178 | + @"_lpm": @"messageId", |
| 179 | + @"_lpx": @"test_action", |
| 180 | + @"aps" : @{@"alert": @"test"}}; |
| 181 | + [[LPActionManager sharedManager] maybePerformNotificationActions:userInfo |
| 182 | + action:nil |
| 183 | + active:YES]; |
| 184 | + |
| 185 | + OCMVerify([classMock showWithTitle:OCMOCK_ANY |
| 186 | + message:OCMOCK_ANY |
| 187 | + cancelButtonTitle:OCMOCK_ANY |
| 188 | + otherButtonTitles:OCMOCK_ANY |
| 189 | + block:OCMOCK_ANY]); |
| 190 | +} |
| 191 | + |
| 192 | +- (void) test_receive_notification |
| 193 | +{ |
| 194 | + // This stub have to be removed when start command is successfully executed. |
| 195 | + [OHHTTPStubs stubRequestsPassingTest: |
| 196 | + ^BOOL(NSURLRequest * _Nonnull request) { |
| 197 | + return [request.URL.host isEqualToString:API_HOST]; |
| 198 | + } withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) { |
| 199 | + NSString *response_file = OHPathForFile(@"simple_start_response.json", self.class); |
| 200 | + return [OHHTTPStubsResponse responseWithFileAtPath:response_file statusCode:200 |
| 201 | + headers:@{@"Content-Type":@"application/json"}]; |
| 202 | + }]; |
| 203 | + |
| 204 | + XCTAssertTrue([LeanplumHelper start_development_test]); |
| 205 | + |
| 206 | + NSDictionary* userInfo = @{ |
| 207 | + @"_lpm": @"messageId", |
| 208 | + @"_lpx": @"test_action", |
| 209 | + @"aps" : @{@"alert": @"test"}}; |
| 210 | + |
| 211 | + XCTestExpectation* expectation = [self expectationWithDescription:@"notification"]; |
| 212 | + |
| 213 | + [[LPActionManager sharedManager] didReceiveRemoteNotification:userInfo |
| 214 | + withAction:@"test_action" |
| 215 | + fetchCompletionHandler: |
| 216 | + ^(LeanplumUIBackgroundFetchResult result) { |
| 217 | + [expectation fulfill]; |
| 218 | + }]; |
| 219 | + [self waitForExpectationsWithTimeout:10 handler:nil]; |
| 220 | +} |
| 221 | + |
| 222 | +- (void)test_messageId_from_userinfo |
| 223 | +{ |
| 224 | + NSDictionary *userInfo = nil; |
| 225 | + NSString* messageId = nil; |
| 226 | + |
| 227 | + userInfo = @{@"_lpm": @"messageId"}; |
| 228 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 229 | + XCTAssertEqual(messageId, @"messageId"); |
| 230 | + |
| 231 | + userInfo = @{@"_lpu": @"messageId"}; |
| 232 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 233 | + XCTAssertEqual(messageId, @"messageId"); |
| 234 | + |
| 235 | + userInfo = @{@"_lpn": @"messageId"}; |
| 236 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 237 | + XCTAssertEqual(messageId, @"messageId"); |
| 238 | + |
| 239 | + userInfo = @{@"_lpv": @"messageId"}; |
| 240 | + messageId = [LPActionManager messageIdFromUserInfo:userInfo]; |
| 241 | + XCTAssertEqual(messageId, @"messageId"); |
| 242 | +} |
| 243 | +@end |
0 commit comments