summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Guerra <fabian.guerra@mapbox.com>2018-09-10 13:35:26 -0700
committerFabian Guerra <fabian.guerra@mapbox.com>2018-09-13 15:42:02 -0700
commitdbe3df83757c4d8a6085e9da7485abd6cfdc72cf (patch)
tree4ecc8691c021c1a59573ee7d5f61b4b0d034af7b
parent15773e7f62c55fa51a806fb6a36a956a36fd99ac (diff)
downloadqtlocation-mapboxgl-dbe3df83757c4d8a6085e9da7485abd6cfdc72cf.tar.gz
[ios, macos] Add MGLOfflineStorage test cases for adding file contents.
-rw-r--r--platform/darwin/test/MGLOfflineStorageTests.mm90
1 files changed, 71 insertions, 19 deletions
diff --git a/platform/darwin/test/MGLOfflineStorageTests.mm b/platform/darwin/test/MGLOfflineStorageTests.mm
index 67f129d2ca..d63909dedb 100644
--- a/platform/darwin/test/MGLOfflineStorageTests.mm
+++ b/platform/darwin/test/MGLOfflineStorageTests.mm
@@ -292,27 +292,79 @@
}];
[self waitForExpectationsWithTimeout:2 handler:nil];
- NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"barcelona" ofType:@"db"]];
- NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentDir = [documentPaths objectAtIndex:0];
- NSString *filePath = [documentDir stringByAppendingPathComponent:@"barcelona.db"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
-
- BOOL exists = [fileManager fileExistsAtPath:filePath];
- if (exists) {
- [fileManager removeItemAtPath:filePath error:nil];
+ // Valid database
+ {
+ NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"barcelona" ofType:@"db"]];
+ NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *documentDir = [documentPaths objectAtIndex:0];
+ NSString *filePath = [documentDir stringByAppendingPathComponent:@"barcelona.db"];
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+
+ BOOL exists = [fileManager fileExistsAtPath:filePath];
+ if (exists) {
+ [fileManager removeItemAtPath:filePath error:nil];
+ }
+
+ NSError *error;
+ [fileManager moveItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error];
+
+ XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"];
+ MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
+ [os addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) {
+ XCTAssertNil(error, @"Adding contents to a file should not return an error.");
+ [fileAdditionCompletionHandlerExpectation fulfill];
+ }];
+ [self waitForExpectationsWithTimeout:2 handler:nil];
+ }
+ // Invalid database type
+ {
+ NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"one-liner" ofType:@"json"]];
+ NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *documentDir = [documentPaths objectAtIndex:0];
+ NSString *filePath = [documentDir stringByAppendingPathComponent:@"barcelona.db"];
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+
+ BOOL exists = [fileManager fileExistsAtPath:filePath];
+ if (exists) {
+ [fileManager removeItemAtPath:filePath error:nil];
+ }
+
+ NSError *error;
+ [fileManager moveItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error];
+
+ XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"];
+ MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
+ [os addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) {
+ XCTAssertNotNil(error, @"Passing an invalid offline database file should return an error.");
+ [fileAdditionCompletionHandlerExpectation fulfill];
+ }];
+ [self waitForExpectationsWithTimeout:2 handler:nil];
+ }
+ // File non existent
+ {
+ NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *documentDir = [documentPaths objectAtIndex:0];
+ NSString *filePath = [documentDir stringByAppendingPathComponent:@"nonexistent.db"];
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+
+ BOOL exists = [fileManager fileExistsAtPath:filePath];
+ if (exists) {
+ [fileManager removeItemAtPath:filePath error:nil];
+ }
+
+ MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
+ XCTAssertThrowsSpecificNamed([os addContentsOfFile:filePath withCompletionHandler:nil], NSException, NSInvalidArgumentException, "MGLOfflineStorage should rise an exception if an invalid database file is passed.");
+
+ }
+ // URL to a non-file
+ {
+ NSURL *resourceURL = [NSURL URLWithString:@"https://www.mapbox.com"];
+
+ MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
+ XCTAssertThrowsSpecificNamed([os addContentsOfURL:resourceURL withCompletionHandler:nil], NSException, NSInvalidArgumentException, "MGLOfflineStorage should rise an exception if an invalid URL file is passed.");
+
}
- NSError *error;
- [fileManager moveItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error];
-
- XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"];
- MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
- [os addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) {
- XCTAssertNil(error, @"Adding contents to a file should not return an error.");
- [fileAdditionCompletionHandlerExpectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:2 handler:nil];
}
@end