Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/set locale #467

Merged
merged 5 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions LeanplumSDK/LeanplumSDK/Classes/Internal/Leanplum.m
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,14 @@ + (void)setTrafficSourceInfoInternal:(NSDictionary *)info
[[LPRequestSender sharedInstance] send:request];
}

+ (void)setLocale:(NSLocale *)locale
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case - would this be called before starting Leanplum or at some point after it started?
If it has started, the locale in the start API call will be the system one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was for calling it after Leanplum is started, in order to simplify a bit complex variations of starting.

If you think it is better to move it to start, and/or to add some stored property for overriding the system's locale, so the start would take that one, I'm ok with it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved in a way that setLocale now can be called both before and after Leanplum is started

LPRequest *request = [LPRequestFactory setUserAttributesWithParams:@{
LP_KEY_LOCALE: [locale localeIdentifier]
}];
[[LPRequestSender sharedInstance] send:request];
}

+ (void)advanceTo:(NSString *)state
{
[self advanceTo:state withInfo:nil];
Expand Down
6 changes: 6 additions & 0 deletions LeanplumSDK/LeanplumSDK/Classes/Leanplum.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ NS_SWIFT_NAME(setUserId(_:attributes:));
+ (void)setTrafficSourceInfo:(NSDictionary *)info
NS_SWIFT_NAME(setTrafficSource(info:));

/**
* Updates a user locale after session start.
*/
+(void)setLocale:(NSLocale *)locale
NS_SWIFT_NAME(setLocale(_:));

/**
* @{
* Advances to a particular state in your application. The string can be
Expand Down
39 changes: 39 additions & 0 deletions LeanplumSDKApp/LeanplumSDKTests/Classes/LeanplumTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,45 @@ - (void) test_set_device_id_after_start
XCTAssertTrue([[LPAPIConfig sharedConfig].deviceId isEqualToString:deviceId]);
}

/**
* Tests setting the user locale after Leanplum start
*/
- (void) test_set_locale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify the test to:

- (void) test_set_locale
{
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ab_CD"];
    
    XCTestExpectation *request_expectation =
    [self expectationWithDescription:@"request_expectation"];
    [LPRequestSender validate_request_args_dictionary:^(NSDictionary *args) {
        XCTAssertTrue([args[LP_KEY_LOCALE] isEqualToString:[locale localeIdentifier]]);
        XCTAssertFalse([args[LP_KEY_LOCALE] isEqualToString:[[NSLocale currentLocale] localeIdentifier]]);
        [request_expectation fulfill];
    }];
    
    [Leanplum setLocale:locale];
    [self waitForExpectations:@[request_expectation] timeout:1.0];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests has been updated

{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ab_CD"];

[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [request.URL.host isEqualToString:API_HOST];
} withStubResponse:^HTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
NSString *response_file = OHPathForFile(@"simple_start_response.json", self.class);
return [HTTPStubsResponse responseWithFileAtPath:response_file statusCode:200
headers:@{@"Content-Type":@"application/json"}];
}];

[LPRequestSender validate_request:^BOOL(NSString *method, NSString *apiMethod,
NSDictionary *params) {
XCTAssertEqualObjects(apiMethod, @"start");
return YES;
}];

XCTAssertTrue([LeanplumHelper start_production_test]);

dispatch_semaphore_t semaphor = dispatch_semaphore_create(0);
[Leanplum onStartResponse:^(BOOL success) {
XCTAssertTrue(success);
[Leanplum setLocale:locale];
dispatch_semaphore_signal(semaphor);
}];
long timedOut = dispatch_semaphore_wait(semaphor, [LeanplumHelper default_dispatch_time]);
XCTAssertTrue(timedOut == 0);
XCTAssertTrue([Leanplum hasStarted]);

[LPRequestSender validate_request_args_dictionary:^(NSDictionary *args) {
XCTAssertTrue([args[LP_KEY_LOCALE] isEqualToString:[locale localeIdentifier]]);
XCTAssertFalse([args[LP_KEY_LOCALE] isEqualToString:[[NSLocale currentLocale] localeIdentifier]]);
}];
}

/**
* Tests track with events of same type , priority and countdown.
*/
Expand Down