diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-09-10 13:35:26 -0700 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-09-13 15:42:02 -0700 |
commit | dbe3df83757c4d8a6085e9da7485abd6cfdc72cf (patch) | |
tree | 4ecc8691c021c1a59573ee7d5f61b4b0d034af7b | |
parent | 15773e7f62c55fa51a806fb6a36a956a36fd99ac (diff) | |
download | qtlocation-mapboxgl-dbe3df83757c4d8a6085e9da7485abd6cfdc72cf.tar.gz |
[ios, macos] Add MGLOfflineStorage test cases for adding file contents.
-rw-r--r-- | platform/darwin/test/MGLOfflineStorageTests.mm | 90 |
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 |