Skip to content

Commit e112918

Browse files
authored
Move unit test for openURL (#344)
1 parent 7d637a5 commit e112918

File tree

4 files changed

+77
-38
lines changed

4 files changed

+77
-38
lines changed

Example/Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
9CD04D951F8EE7E30033AB4A /* variables_with_newsfeed_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */; };
103103
A84527FD215C3035001FA20D /* LPRequestTest.m in Sources */ = {isa = PBXBuildFile; fileRef = A84527FC215C3035001FA20D /* LPRequestTest.m */; };
104104
A86C8CF42405E271003F5E4E /* LPAlertMessageSnapshotTest.m in Sources */ = {isa = PBXBuildFile; fileRef = A86C8CF32405E271003F5E4E /* LPAlertMessageSnapshotTest.m */; };
105+
A86C8CF624072814003F5E4E /* LPOpenUrlMessageTemplateTest.m in Sources */ = {isa = PBXBuildFile; fileRef = A86C8CF524072814003F5E4E /* LPOpenUrlMessageTemplateTest.m */; };
105106
A8CB8A36227276A400E1001D /* LeanplumRequestTest.m in Sources */ = {isa = PBXBuildFile; fileRef = A8CB8A35227276A400E1001D /* LeanplumRequestTest.m */; };
106107
A8EEA1AB20E44B0B00FF9A4D /* start_with_variant_debug_info_response.json in Resources */ = {isa = PBXBuildFile; fileRef = A8EEA1AA20E44B0A00FF9A4D /* start_with_variant_debug_info_response.json */; };
107108
A8EEA1AC20E44B0B00FF9A4D /* start_with_variant_debug_info_response.json in Resources */ = {isa = PBXBuildFile; fileRef = A8EEA1AA20E44B0A00FF9A4D /* start_with_variant_debug_info_response.json */; };
@@ -210,6 +211,7 @@
210211
A54ACCFCB36AE8C874708A7F /* libPods-Leanplum-SDK_Example-Leanplum-SDK_Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Leanplum-SDK_Example-Leanplum-SDK_Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
211212
A84527FC215C3035001FA20D /* LPRequestTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPRequestTest.m; sourceTree = "<group>"; };
212213
A86C8CF32405E271003F5E4E /* LPAlertMessageSnapshotTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPAlertMessageSnapshotTest.m; sourceTree = "<group>"; };
214+
A86C8CF524072814003F5E4E /* LPOpenUrlMessageTemplateTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPOpenUrlMessageTemplateTest.m; sourceTree = "<group>"; };
213215
A8CB8A35227276A400E1001D /* LeanplumRequestTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LeanplumRequestTest.m; sourceTree = "<group>"; };
214216
A8EEA1AA20E44B0A00FF9A4D /* start_with_variant_debug_info_response.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = start_with_variant_debug_info_response.json; sourceTree = "<group>"; };
215217
A8F5D58623F3483E00313C98 /* LPBaseInterstitialMessageTemplateTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPBaseInterstitialMessageTemplateTest.m; sourceTree = "<group>"; };
@@ -277,6 +279,7 @@
277279
07E5C8801F052B7800A4B092 /* MessagesTest.m */,
278280
A8F5D58623F3483E00313C98 /* LPBaseInterstitialMessageTemplateTest.m */,
279281
A8F5D58923F34AD400313C98 /* LPBasePushMessageTemplateTest.m */,
282+
A86C8CF524072814003F5E4E /* LPOpenUrlMessageTemplateTest.m */,
280283
07E5C8811F052B7800A4B092 /* LPMessageTemplatesClassTest.m */,
281284
07E5C8821F052B7800A4B092 /* NewsfeedTest.m */,
282285
07E5C8891F052B7800A4B092 /* LPUtilsTest.m */,
@@ -794,6 +797,7 @@
794797
A84527FD215C3035001FA20D /* LPRequestTest.m in Sources */,
795798
A8F5D58823F3488600313C98 /* LPBaseInterstitialMessageTemplateTest.m in Sources */,
796799
07E5C8AC1F052B7800A4B092 /* LPAppIconManagerTest.m in Sources */,
800+
A86C8CF624072814003F5E4E /* LPOpenUrlMessageTemplateTest.m in Sources */,
797801
07E5C8BA1F052B7800A4B092 /* NewsfeedTest.m in Sources */,
798802
07E5C8AB1F052B7800A4B092 /* LPActionManagerTest.m in Sources */,
799803
653192A121517D4F00D7DDDC /* LPFeatureFlagManagerTest.m in Sources */,

Example/Tests/Classes/LPBaseInterstitialMessageTemplateTest.m

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
@interface LPBaseInterstitialMessageTemplate (Test)
3333
- (UIImage *)imageFromColor:(UIColor *)color;
3434
- (UIImage *)dismissImage:(UIColor *)color withSize:(int)size;
35-
- (NSString *)urlEncodedStringFromString:(NSString *)urlString;
3635

3736
- (void)setupPopupLayout:(BOOL)isFullscreen isPushAskToAsk:(BOOL)isPushAskToAsk;
3837
- (void)updatePopupLayout;
@@ -126,12 +125,4 @@ - (void)test_push_popup_setup
126125
XCTAssertNotNil(cancelButton);
127126
}
128127

129-
- (void)test_urlEncodedStringFromString {
130-
LPBaseInterstitialMessageTemplate *template = [[LPBaseInterstitialMessageTemplate alloc] init];
131-
XCTAssertEqualObjects([template urlEncodedStringFromString:@"http://www.leanplum.com"], @"http://www.leanplum.com");
132-
XCTAssertEqualObjects([template urlEncodedStringFromString:@"http://www.leanplum.com?q=simple_english1&test=2"], @"http://www.leanplum.com?q=simple_english1&test=2");
133-
XCTAssertEqualObjects([template urlEncodedStringFromString:@"https://ramsey.tfaforms.net/356302?id={}"], @"https://ramsey.tfaforms.net/356302?id=%7B%7D");
134-
XCTAssertEqualObjects([template urlEncodedStringFromString:@"lomotif://music/月亮"], @"lomotif://music/%E6%9C%88%E4%BA%AE");
135-
}
136-
137128
@end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// LPOpenUrlMessageTemplateTest.m
3+
// Leanplum-SDK
4+
//
5+
// Created by Mayank Sanganeria on 2/1/20.
6+
// Copyright © 2020 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 <OHHTTPStubs/HTTPStubs.h>
28+
#import <OHHTTPStubs/HTTPStubsPathHelpers.h>
29+
#import "LeanplumHelper.h"
30+
#import "LPOpenUrlMessageTemplate.h"
31+
32+
@interface LPOpenUrlMessageTemplate (Test)
33+
34+
- (NSString *)urlEncodedStringFromString:(NSString *)urlString;
35+
36+
@end
37+
38+
39+
@interface LPOpenUrlMessageTemplateTest : XCTestCase
40+
41+
@end
42+
43+
@implementation LPOpenUrlMessageTemplateTest
44+
45+
+ (void)setUp
46+
{
47+
[super setUp];
48+
// Called only once to setup method swizzling.
49+
[LeanplumHelper setup_method_swizzling];
50+
}
51+
52+
- (void)setUp {
53+
[super setUp];
54+
}
55+
56+
- (void)tearDown {
57+
[super tearDown];
58+
// Clean up after every test.
59+
[LeanplumHelper clean_up];
60+
[HTTPStubs removeAllStubs];
61+
}
62+
63+
- (void)test_urlEncodedStringFromString {
64+
LPOpenUrlMessageTemplate *template = [[LPOpenUrlMessageTemplate alloc] init];
65+
XCTAssertEqualObjects([template urlEncodedStringFromString:@"http://www.leanplum.com"], @"http://www.leanplum.com");
66+
XCTAssertEqualObjects([template urlEncodedStringFromString:@"http://www.leanplum.com?q=simple_english1&test=2"], @"http://www.leanplum.com?q=simple_english1&test=2");
67+
XCTAssertEqualObjects([template urlEncodedStringFromString:@"https://ramsey.tfaforms.net/356302?id={}"], @"https://ramsey.tfaforms.net/356302?id=%7B%7D");
68+
XCTAssertEqualObjects([template urlEncodedStringFromString:@"lomotif://music/月亮"], @"lomotif://music/%E6%9C%88%E4%BA%AE");
69+
}
70+
71+
@end

Leanplum-SDK/Classes/MessageTemplates/LPBaseInterstitialMessageTemplate.m

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "LPBaseInterstitialMessageTemplate.h"
10+
#import "LPJSON.h"
1011

1112
@implementation LPBaseInterstitialMessageTemplate
1213

@@ -675,7 +676,7 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
675676
if (event) {
676677
double value = [queryComponents[@"value"] doubleValue];
677678
NSString *info = queryComponents[@"info"];
678-
NSDictionary *parameters = [self JSONFromString:queryComponents[@"parameters"]];
679+
NSDictionary *parameters = [LPJSON JSONFromString:queryComponents[@"parameters"]];
679680

680681
if (queryComponents[@"isMessageEvent"]) {
681682
[context trackMessageEvent:event
@@ -716,32 +717,4 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
716717
decisionHandler(WKNavigationActionPolicyAllow);
717718
}
718719

719-
/**
720-
* Copied from LPJSON. TODO: Remove when we open source.
721-
*/
722-
- (id)JSONFromString:(NSString *)string
723-
{
724-
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
725-
NSError *error;
726-
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
727-
if (error) {
728-
return nil;
729-
}
730-
return json;
731-
}
732-
733-
/**
734-
* Helper method
735-
*/
736-
737-
- (NSString *)urlEncodedStringFromString:(NSString *)urlString {
738-
NSString *unreserved = @":-._~/?&=";
739-
NSMutableCharacterSet *allowed = [NSMutableCharacterSet
740-
alphanumericCharacterSet];
741-
[allowed addCharactersInString:unreserved];
742-
return [urlString
743-
stringByAddingPercentEncodingWithAllowedCharacters:
744-
allowed];
745-
}
746-
747720
@end

0 commit comments

Comments
 (0)