summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Wray <jason@mapbox.com>2019-06-17 16:17:22 -0700
committerJason Wray <jason@mapbox.com>2019-06-17 16:59:10 -0700
commit171e8a3c5781dd9e1076681af5f854dc2f9d6dd6 (patch)
tree19303dede307ee5bd20cc36ca4e44f4367e1d750
parentc3e94a06480339fe911386cd8be43bdf3e54a17d (diff)
downloadqtlocation-mapboxgl-upstream/friedbunny-simplifies-MGLOfflineStorageTests.tar.gz
[ios] Simplify -[MGLOfflineStorageTests testAddFileContent]upstream/friedbunny-simplifies-MGLOfflineStorageTests
-rw-r--r--platform/darwin/test/MGLOfflineStorageTests.mm80
1 files changed, 20 insertions, 60 deletions
diff --git a/platform/darwin/test/MGLOfflineStorageTests.mm b/platform/darwin/test/MGLOfflineStorageTests.mm
index 86dc28eb04..0903245566 100644
--- a/platform/darwin/test/MGLOfflineStorageTests.mm
+++ b/platform/darwin/test/MGLOfflineStorageTests.mm
@@ -285,35 +285,16 @@
}
- (void)testAddFileContent {
-
- NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentDir = [documentPaths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
-
- BOOL directoryExists = [fileManager fileExistsAtPath:documentDir];
- if (!directoryExists) {
- [fileManager createDirectoryAtPath:documentDir withIntermediateDirectories:YES attributes:nil error:nil];
- }
-
+
// Valid database
- {
+ [XCTContext runActivityNamed:@"Valid database" block:^(id<XCTActivity> activity) {
NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"sideload_sat" ofType:@"db"]];
- NSString *filePath = [documentDir stringByAppendingPathComponent:@"sideload_sat.db"];
-
- BOOL databaseExists = [fileManager fileExistsAtPath:filePath];
- if (databaseExists) {
- [fileManager removeItemAtPath:filePath error:nil];
- }
-
+
NSError *error;
- [fileManager moveItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error];
- NSDictionary *atributes = @{ NSFilePosixPermissions: @0777 };
- error = nil;
- [fileManager setAttributes:atributes ofItemAtPath:filePath error:&error];
- XCTAssertNil(error, @"Changing the file's permissions:%@ should not return an error.", filePath);
- error = nil;
- NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePath error:&error];
-
+ NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:resourceURL.path error:&error];
+ XCTAssertNil(error, @"Getting the file's permissions:%@ should not return an error.", resourceURL.path);
+
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
long long databaseFileSize = 73728;
@@ -330,7 +311,7 @@
XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"];
MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
- [os addContentsOfFile:filePath withCompletionHandler:^(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) {
+ [os addContentsOfURL:resourceURL withCompletionHandler:^(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) {
XCTAssertNotNil(fileURL, @"The fileURL should not be nil.");
XCTAssertNotNil(packs, @"Adding the contents of the sideload_sat.db should update one pack.");
XCTAssertNil(error, @"Adding contents to a file should not return an error.");
@@ -342,58 +323,37 @@
[self waitForExpectationsWithTimeout:10 handler:nil];
// Depending on the database it may update or add a pack. For this case specifically the offline database adds one pack.
XCTAssertEqual([MGLOfflineStorage sharedOfflineStorage].packs.count, countOfPacks + 1, @"Adding contents of sideload_sat.db should add one pack.");
+ }];
- }
// Invalid database type
- {
+ [XCTContext runActivityNamed:@"Invalid database type" block:^(id<XCTActivity> activity) {
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:@"on-liner-copy.json"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
-
- BOOL exists = [fileManager fileExistsAtPath:filePath];
- if (exists) {
- [fileManager removeItemAtPath:filePath error:nil];
- }
- NSError *error;
- [fileManager copyItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error];
-
XCTestExpectation *invalidFileCompletionHandlerExpectation = [self expectationWithDescription:@"invalid content database completion handler"];
MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
- [os addContentsOfFile:filePath withCompletionHandler:^(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) {
+ [os addContentsOfFile:resourceURL.path withCompletionHandler:^(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) {
XCTAssertNotNil(error, @"Passing an invalid offline database file should return an error.");
XCTAssertNil(packs, @"Passing an invalid offline database file should not add packs to the offline database.");
[invalidFileCompletionHandlerExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 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];
- }
-
+ }];
+
+ // File non-existent
+ [XCTContext runActivityNamed:@"File non-existent" block:^(id<XCTActivity> activity) {
+ NSURL *resourceURL = [NSURL URLWithString:@"nonexistent.db"];
+
MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
- XCTAssertThrowsSpecificNamed([os addContentsOfFile:filePath withCompletionHandler:nil], NSException, NSInvalidArgumentException, "MGLOfflineStorage should rise an exception if an invalid database file is passed.");
+ XCTAssertThrowsSpecificNamed([os addContentsOfURL:resourceURL withCompletionHandler:nil], NSException, NSInvalidArgumentException, "MGLOfflineStorage should rise an exception if an invalid database file is passed.");
+ }];
- }
// URL to a non-file
- {
+ [XCTContext runActivityNamed:@"URL to a non-file" block:^(id<XCTActivity> activity) {
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.");
-
- }
-
+ }];
}
- (void)testPutResourceForURL {