diff options
author | Fabian Guerra Soto <fabian.guerra@mapbox.com> | 2018-09-17 15:31:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-17 15:31:43 -0700 |
commit | a938c3ef68af249bb3199bde2818436a0bff0952 (patch) | |
tree | 5301a86ea2e9b0b46d2684334f36a97dfa99a521 /platform/darwin/test/MGLOfflineStorageTests.mm | |
parent | 153e97c8e7cd03cdd0e94cb5e17a2723f417d973 (diff) | |
download | qtlocation-mapboxgl-a938c3ef68af249bb3199bde2818436a0bff0952.tar.gz |
[ios, macos] Offline regions merge. (#12791)
* [ios, macos] Add offline regions merge.
* [ios, macos] Update changelogs.
* [ios, macos] Fix refreshing the offline packs after new content is added.
* [ios, macos] Update MGLOfflineStorage's add contents of file documentation.
* [ios, macos] Add MGLOfflineStorage test cases for adding file contents.
* [ios, macos] Add offline merge test database.
* [ios, macos] Add packs parameter to MGLBatchedOfflinePackAdditionCompletionHandler.
* [core] Fix a query mege duplication.
* [ios, macos] Remove unnecessary pack iteration.
* [ios, macos] Fix packs update KVO notifications.
* [ios, macos] Add localization strings.
* [core] Fix query readability.
* [ios, macos] Fix MGLOfflineStorageTest, cleanup the cache database after the test.
* [ios, macos] Fix offline packs, updating triggers a crash.
Diffstat (limited to 'platform/darwin/test/MGLOfflineStorageTests.mm')
-rw-r--r-- | platform/darwin/test/MGLOfflineStorageTests.mm | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/platform/darwin/test/MGLOfflineStorageTests.mm b/platform/darwin/test/MGLOfflineStorageTests.mm index e9e2467f21..f89d922f11 100644 --- a/platform/darwin/test/MGLOfflineStorageTests.mm +++ b/platform/darwin/test/MGLOfflineStorageTests.mm @@ -12,6 +12,24 @@ @implementation MGLOfflineStorageTests ++ (void)tearDown { + NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:NO + error:nil]; + // Unit tests don't use the main bundle; use com.mapbox.ios.sdk instead. + NSString *bundleIdentifier = [NSBundle bundleForClass:[MGLMapView class]].bundleIdentifier; + cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier]; + cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:@".mapbox"]; + XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:cacheDirectoryURL.path], @"Cache subdirectory should exist."); + + NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:@"cache.db"]; + + [[NSFileManager defaultManager] removeItemAtURL:cacheURL error:nil]; + XCTAssertFalse([[NSFileManager defaultManager] fileExistsAtPath:cacheURL.path], @"Cache subdirectory should not exist."); +} + - (void)setUp { [super setUp]; @@ -264,4 +282,110 @@ [os setDelegate:nil]; } +- (void)testAddFileContent { + + + // 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]; + 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]; + + NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize]; + long long fileSize = [fileSizeNumber longLongValue]; + long long dabaseFileSize = 32391168; + // Merging databases creates an empty file if the file does not exist at the given path. + XCTAssertEqual(fileSize, dabaseFileSize, @"The dabase file size must be:%l actual size:%l", dabaseFileSize, fileSize); + + NSUInteger countOfPacks = [MGLOfflineStorage sharedOfflineStorage].packs.count; + + [self keyValueObservingExpectationForObject:[MGLOfflineStorage sharedOfflineStorage] keyPath:@"packs" handler:^BOOL(id _Nonnull observedObject, NSDictionary * _Nonnull change) { + const auto changeKind = static_cast<NSKeyValueChange>([change[NSKeyValueChangeKindKey] unsignedLongValue]); + NSIndexSet *indices = change[NSKeyValueChangeIndexesKey]; + return changeKind == NSKeyValueChangeInsertion && indices.count == 1; + }]; + + 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) { + XCTAssertNotNil(fileURL, @"The fileURL should not be nil."); + XCTAssertNotNil(packs, @"Adding the contents of the barcelona.db should update one pack."); + XCTAssertNil(error, @"Adding contents to a file should not return an error."); + for (MGLOfflinePack *pack in [MGLOfflineStorage sharedOfflineStorage].packs) { + NSLog(@"PACK:%@", pack); + } + [fileAdditionCompletionHandlerExpectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:2 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 barcelona.db should add one pack."); + } + // 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:@"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) { + 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: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."); + + } + +} + @end |