Skip to content

Commit 95ce3c2

Browse files
authored
Allow users to access VariantDebugInfo by setting a flag (E2-1283) (#158)
Add variant debug info functionality and tests
1 parent e36e6b3 commit 95ce3c2

File tree

12 files changed

+509
-15
lines changed

12 files changed

+509
-15
lines changed

Example/Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
9CB691E11F1601BA002D83D8 /* LPEventCallbackManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CB691E01F1601BA002D83D8 /* LPEventCallbackManagerTest.m */; };
9595
9CD04D941F8EE7E30033AB4A /* variables_with_newsfeed_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */; };
9696
9CD04D951F8EE7E30033AB4A /* variables_with_newsfeed_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */; };
97+
A8EEA1AB20E44B0B00FF9A4D /* start_with_variant_debug_info_response.json in Resources */ = {isa = PBXBuildFile; fileRef = A8EEA1AA20E44B0A00FF9A4D /* start_with_variant_debug_info_response.json */; };
98+
A8EEA1AC20E44B0B00FF9A4D /* start_with_variant_debug_info_response.json in Resources */ = {isa = PBXBuildFile; fileRef = A8EEA1AA20E44B0A00FF9A4D /* start_with_variant_debug_info_response.json */; };
9799
B5CCCA2D1D74B64D005ADEE1 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B5CCCA2C1D74B64D005ADEE1 /* main.m */; };
98100
B5CCCA301D74B64D005ADEE1 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B5CCCA2F1D74B64D005ADEE1 /* AppDelegate.m */; };
99101
B5CCCA331D74B64D005ADEE1 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B5CCCA321D74B64D005ADEE1 /* ViewController.m */; };
@@ -196,6 +198,7 @@
196198
9CB691E01F1601BA002D83D8 /* LPEventCallbackManagerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPEventCallbackManagerTest.m; sourceTree = "<group>"; };
197199
9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = variables_with_newsfeed_response.json; sourceTree = "<group>"; };
198200
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; };
201+
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>"; };
199202
B5CCCA291D74B64D005ADEE1 /* Leanplum-tvOS-SDK_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Leanplum-tvOS-SDK_Example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
200203
B5CCCA2C1D74B64D005ADEE1 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
201204
B5CCCA2E1D74B64D005ADEE1 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -324,6 +327,7 @@
324327
07E5C8981F052B7800A4B092 /* registration_response.json */,
325328
07E5C89A1F052B7800A4B092 /* simple_start_response.json */,
326329
07E5C89B1F052B7800A4B092 /* start_variables_response.json */,
330+
A8EEA1AA20E44B0A00FF9A4D /* start_with_variant_debug_info_response.json */,
327331
07E5C89C1F052B7800A4B092 /* state_response.json */,
328332
07E5C89E1F052B7800A4B092 /* track_event_response.json */,
329333
07E5C89F1F052B7800A4B092 /* variables_response.json */,
@@ -610,6 +614,7 @@
610614
07E5C8EA1F052DCC00A4B092 /* MainAppIcon.png in Resources */,
611615
07E5C8E21F052DC400A4B092 /* start_variables_response.json in Resources */,
612616
07E5C8E71F052DC700A4B092 /* malformed_simple_start_response.json in Resources */,
617+
A8EEA1AB20E44B0B00FF9A4D /* start_with_variant_debug_info_response.json in Resources */,
613618
07E5C8F21F052DD000A4B092 /* regionData.json in Resources */,
614619
07E5C8F01F052DD000A4B092 /* DifferentPrioritiesWithMissingValues.json in Resources */,
615620
6003F5A9195388D20070C39A /* Images.xcassets in Resources */,
@@ -638,6 +643,7 @@
638643
07E5C8D51F052B7800A4B092 /* NoPriorities.json in Resources */,
639644
07E5C8D11F052B7800A4B092 /* ChainedMessage.json in Resources */,
640645
07E5C8C31F052B7800A4B092 /* action_response.json in Resources */,
646+
A8EEA1AC20E44B0B00FF9A4D /* start_with_variant_debug_info_response.json in Resources */,
641647
07E5C8D31F052B7800A4B092 /* DifferentPriorities2.json in Resources */,
642648
07E5C8D41F052B7800A4B092 /* DifferentPrioritiesWithMissingValues.json in Resources */,
643649
07E5C8C71F052B7800A4B092 /* malformed_track_event_response.json in Resources */,

Example/Tests/Classes/LeanplumTest.m

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ - (void) test_simple_development_start
7878
headers:@{@"Content-Type":@"application/json"}];
7979
}];
8080

81-
// Validate requst.
81+
// Validate request.
8282
[LeanplumRequest validate_request:^BOOL(NSString *method, NSString *apiMethod,
8383
NSDictionary *params) {
8484
// Check api method first.
@@ -208,6 +208,50 @@ - (void) test_complex_development_start
208208
XCTAssertTrue([Leanplum hasStarted]);
209209
}
210210

211+
/**
212+
* Tests a simple development start with variant debug info.
213+
*/
214+
- (void) testStartWithParamShouldIncludeVariantDebugInfo
215+
{
216+
[Leanplum setVariantDebugInfoEnabled:YES];
217+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
218+
return [request.URL.host isEqualToString:API_HOST];
219+
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
220+
NSString *response_file = OHPathForFile(@"simple_start_response.json", self.class);
221+
return [OHHTTPStubsResponse responseWithFileAtPath:response_file statusCode:200
222+
headers:@{@"Content-Type":@"application/json"}];
223+
}];
224+
225+
// Validate request.
226+
[LeanplumRequest validate_request:^BOOL(NSString *method, NSString *apiMethod,
227+
NSDictionary *params) {
228+
// Check api method first.
229+
XCTAssertEqualObjects(apiMethod, @"start");
230+
231+
// Check if request has all params.
232+
XCTAssertTrue([params[@"city"] isEqualToString:@"(detect)"]);
233+
XCTAssertTrue([params[@"country"] isEqualToString:@"(detect)"]);
234+
XCTAssertTrue([params[@"location"] isEqualToString:@"(detect)"]);
235+
XCTAssertTrue([params[@"region"] isEqualToString:@"(detect)"]);
236+
NSString* deviceModel = params[@"deviceModel"];
237+
XCTAssertTrue([deviceModel isEqualToString:@"iPhone"] ||
238+
[deviceModel isEqualToString:@"iPhone Simulator"]);
239+
XCTAssertTrue([params[@"deviceName"] isEqualToString:[[UIDevice currentDevice] name]]);
240+
XCTAssertEqualObjects(@0, params[@"includeDefaults"]);
241+
XCTAssertNotNil(params[@"locale"]);
242+
XCTAssertNotNil(params[@"timezone"]);
243+
XCTAssertNotNil(params[@"timezoneOffsetSeconds"]);
244+
XCTAssertTrue(params[@"includeVariantDebugInfo"]);
245+
return YES;
246+
}];
247+
248+
XCTAssertTrue([LeanplumHelper start_development_test]);
249+
XCTAssertTrue([[LPConstantsState sharedState] isDevelopmentModeEnabled]);
250+
XCTAssertTrue([Leanplum hasStarted]);
251+
XCTAssertNotNil([Leanplum deviceId]);
252+
}
253+
254+
211255
/**
212256
* Test complex production start.
213257
*/
@@ -1004,7 +1048,7 @@ - (void)test_variables
10041048
@"myArray": @{
10051049
@"[2]": @33
10061050
}
1007-
} messages:nil updateRules:nil eventRules:nil variants:nil regions:nil];
1051+
} messages:nil updateRules:nil eventRules:nil variants:nil regions:nil variantDebugInfo:nil];
10081052

10091053

10101054
XCTestExpectation *request_expectation =
@@ -1122,6 +1166,69 @@ - (void)test_variables
11221166
XCTAssertEqualObjects(@100LL, [long_long_variable defaultValue]);
11231167
}
11241168

1169+
/**
1170+
* Tests variant debug info.
1171+
*/
1172+
- (void)testStartResponseShouldParseVariantDebugInfo
1173+
{
1174+
//Given: start request
1175+
1176+
//When: VariantDebugInfoEnabled is YES
1177+
[Leanplum setVariantDebugInfoEnabled:YES];
1178+
// This stub have to be removed when start command is successfully executed.
1179+
id<OHHTTPStubsDescriptor> startStub = [OHHTTPStubs stubRequestsPassingTest:
1180+
^BOOL(NSURLRequest * _Nonnull request) {
1181+
return [request.URL.host isEqualToString:API_HOST];
1182+
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
1183+
NSString* response_file = OHPathForFile(@"start_with_variant_debug_info_response.json", self.class);
1184+
return [OHHTTPStubsResponse responseWithFileAtPath:response_file statusCode:200
1185+
headers:@{@"Content-Type":@"application/json"}];
1186+
}];
1187+
1188+
[LeanplumHelper setup_development_test];
1189+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
1190+
[Leanplum startWithResponseHandler:^(BOOL success) {
1191+
XCTAssertTrue(success);
1192+
[OHHTTPStubs removeStub:startStub];
1193+
// Then: variantDebugInfo should be parsed
1194+
XCTAssertNotNil([LPVarCache variantDebugInfo]);
1195+
NSDictionary *abTests = [LPVarCache variantDebugInfo][@"abTests"];
1196+
XCTAssertEqual(abTests.count, 2);
1197+
1198+
// Then: variantDebugInfo should be persisted
1199+
[LPVarCache saveDiffs];
1200+
[LPVarCache setVariantDebugInfo:nil];
1201+
XCTAssertNil([LPVarCache variantDebugInfo]);
1202+
[LPVarCache loadDiffs];
1203+
XCTAssertNotNil([LPVarCache variantDebugInfo]);
1204+
dispatch_semaphore_signal(semaphore);
1205+
}];
1206+
dispatch_semaphore_wait(semaphore, [LeanplumHelper default_dispatch_time]);
1207+
}
1208+
1209+
/**
1210+
* Tests variant debug info persistence.
1211+
*/
1212+
- (void)testShouldPersistVariantDebugInfo
1213+
{
1214+
//Given: a variantDebugInfo set in VarCache
1215+
NSDictionary *mockVariantDebugInfo = @{@"abTests":@[]};
1216+
[LPVarCache setVariantDebugInfo:mockVariantDebugInfo];
1217+
XCTAssertEqual([Leanplum variantDebugInfo].allKeys.count, 1);
1218+
1219+
//When: the varcache is persisted
1220+
[LPVarCache saveDiffs];
1221+
XCTAssertEqual([Leanplum variantDebugInfo].allKeys.count, 1);
1222+
1223+
1224+
[LPVarCache setVariantDebugInfo:nil];
1225+
XCTAssertEqual([Leanplum variantDebugInfo].allKeys.count, 0);
1226+
1227+
//Then: the variantDebugInfo can be loaded from disk
1228+
[LPVarCache loadDiffs];
1229+
XCTAssertEqual([Leanplum variantDebugInfo].allKeys.count, 1);
1230+
}
1231+
11251232
/**
11261233
* Tests advance methods
11271234
*/
@@ -1265,7 +1372,7 @@ - (void)test_metadata
12651372
}};
12661373
NSArray *variants = @[@{@"id":@"1"}, @{@"id":@"2"}];
12671374
[LPVarCache applyVariableDiffs:nil messages:messages updateRules:nil
1268-
eventRules:nil variants:variants regions:nil];
1375+
eventRules:nil variants:variants regions:nil variantDebugInfo:nil];
12691376

12701377
XCTAssertEqualObjects(variants, [Leanplum variants]);
12711378
XCTAssertEqualObjects(messages, [Leanplum messageMetadata]);

0 commit comments

Comments
 (0)