summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2016-03-26 21:13:42 -0700
committerMinh Nguyễn <mxn@1ec5.org>2016-03-29 11:46:27 -0700
commitb248e43112f24c3570a7df5a9dd4096c353b6f0e (patch)
tree86fecfe38cb81c097452ccf8d77ba4903af7776a
parent4fd875a971db970b2868e2c37a7a3432cc961d64 (diff)
downloadqtlocation-mapboxgl-b248e43112f24c3570a7df5a9dd4096c353b6f0e.tar.gz
[osx] Offline unit tests
Added unit tests for MGLTilePyramidOfflineRegion, MGLOfflinePack, and MGLOfflineStorage. Fixes #4426.
-rw-r--r--platform/osx/test/MGLOfflinePackTests.m25
-rw-r--r--platform/osx/test/MGLOfflineRegionTests.m35
-rw-r--r--platform/osx/test/MGLOfflineStorageTests.m113
-rw-r--r--platform/osx/test/osxtest.gypi3
4 files changed, 176 insertions, 0 deletions
diff --git a/platform/osx/test/MGLOfflinePackTests.m b/platform/osx/test/MGLOfflinePackTests.m
new file mode 100644
index 0000000000..c33a72cc08
--- /dev/null
+++ b/platform/osx/test/MGLOfflinePackTests.m
@@ -0,0 +1,25 @@
+#import <Mapbox/Mapbox.h>
+
+#pragma clang diagnostic ignored "-Wgnu-statement-expression"
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+
+#import <XCTest/XCTest.h>
+
+@interface MGLOfflinePackTests : XCTestCase
+
+@end
+
+@implementation MGLOfflinePackTests
+
+- (void)testInvalidation {
+ MGLOfflinePack *invalidPack = [[MGLOfflinePack alloc] init];
+
+ XCTAssertEqual(invalidPack.state, MGLOfflinePackStateInvalid, @"Offline pack should be invalid when initialized independently of MGLOfflineStorage.");
+
+ XCTAssertThrowsSpecificNamed(invalidPack.region, NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when accessing its region.");
+ XCTAssertThrowsSpecificNamed(invalidPack.context, NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when accessing its context.");
+ XCTAssertThrowsSpecificNamed([invalidPack resume], NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when being resumed.");
+ XCTAssertThrowsSpecificNamed([invalidPack suspend], NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when being suspended.");
+}
+
+@end
diff --git a/platform/osx/test/MGLOfflineRegionTests.m b/platform/osx/test/MGLOfflineRegionTests.m
new file mode 100644
index 0000000000..63befdf14c
--- /dev/null
+++ b/platform/osx/test/MGLOfflineRegionTests.m
@@ -0,0 +1,35 @@
+#import <Mapbox/Mapbox.h>
+
+#pragma clang diagnostic ignored "-Wgnu-statement-expression"
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+
+#import <XCTest/XCTest.h>
+
+@interface MGLOfflineRegionTests : XCTestCase
+
+@end
+
+@implementation MGLOfflineRegionTests
+
+- (void)testStyleURLs {
+ MGLCoordinateBounds bounds = MGLCoordinateBoundsMake(kCLLocationCoordinate2DInvalid, kCLLocationCoordinate2DInvalid);
+ MGLTilePyramidOfflineRegion *region = [[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:nil bounds:bounds fromZoomLevel:0 toZoomLevel:DBL_MAX];
+ XCTAssertEqualObjects(region.styleURL, [MGLStyle streetsStyleURL], @"Streets isn’t the default style.");
+
+ NSURL *localURL = [NSURL URLWithString:@"beautiful.style"];
+ XCTAssertThrowsSpecificNamed([[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:localURL bounds:bounds fromZoomLevel:0 toZoomLevel:DBL_MAX], NSException, @"Invalid style URL", @"No exception raised when initializing region with a local file URL as the style URL.");
+}
+
+- (void)testEquality {
+ MGLCoordinateBounds bounds = MGLCoordinateBoundsMake(kCLLocationCoordinate2DInvalid, kCLLocationCoordinate2DInvalid);
+ MGLTilePyramidOfflineRegion *original = [[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:[MGLStyle lightStyleURL] bounds:bounds fromZoomLevel:5 toZoomLevel:10];
+ MGLTilePyramidOfflineRegion *copy = [original copy];
+ XCTAssertEqualObjects(original, copy, @"Tile pyramid region should be equal to its copy.");
+
+ XCTAssertEqualObjects(original.styleURL, copy.styleURL, @"Style URL has changed.");
+ XCTAssert(MGLCoordinateBoundsEqualToCoordinateBounds(original.bounds, copy.bounds), @"Bounds have changed.");
+ XCTAssertEqual(original.minimumZoomLevel, original.minimumZoomLevel, @"Minimum zoom level has changed.");
+ XCTAssertEqual(original.maximumZoomLevel, original.maximumZoomLevel, @"Maximum zoom level has changed.");
+}
+
+@end
diff --git a/platform/osx/test/MGLOfflineStorageTests.m b/platform/osx/test/MGLOfflineStorageTests.m
new file mode 100644
index 0000000000..ff738f9239
--- /dev/null
+++ b/platform/osx/test/MGLOfflineStorageTests.m
@@ -0,0 +1,113 @@
+#import <Mapbox/Mapbox.h>
+
+#pragma clang diagnostic ignored "-Wgnu-statement-expression"
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+
+#import <XCTest/XCTest.h>
+
+@interface MGLOfflineStorageTests : XCTestCase
+
+@end
+
+@implementation MGLOfflineStorageTests
+
+- (void)testSharedObject {
+ XCTAssertEqual([MGLOfflineStorage sharedOfflineStorage], [MGLOfflineStorage sharedOfflineStorage], @"There should only be one shared offline storage object.");
+}
+
+// This test needs to come first so it can test the initial loading of packs.
+- (void)testAAALoadPacks {
+ XCTestExpectation *kvoExpectation = [self keyValueObservingExpectationForObject:[MGLOfflineStorage sharedOfflineStorage] keyPath:@"packs" handler:^BOOL(id _Nonnull observedObject, NSDictionary * _Nonnull change) {
+ NSKeyValueChange changeKind = [change[NSKeyValueChangeKindKey] unsignedIntegerValue];
+ return changeKind = NSKeyValueChangeSetting;
+ }];
+
+ [self waitForExpectationsWithTimeout:1 handler:nil];
+
+ XCTAssertNotNil([MGLOfflineStorage sharedOfflineStorage].packs, @"Shared offline storage object should have a non-nil collection of packs by this point.");
+}
+
+- (void)testAddPack {
+ NSUInteger countOfPacks = [MGLOfflineStorage sharedOfflineStorage].packs.count;
+
+ NSURL *styleURL = [MGLStyle lightStyleURL];
+ /// Somewhere near Grape Grove, Ohio, United States.
+ MGLCoordinateBounds bounds = {
+ { 39.70358155855172, -83.69506472545841 },
+ { 39.703818870225376, -83.69420641857361 },
+ };
+ double zoomLevel = 20;
+ MGLTilePyramidOfflineRegion *region = [[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:styleURL bounds:bounds fromZoomLevel:zoomLevel toZoomLevel:zoomLevel];
+
+ NSString *nameKey = @"Name";
+ NSString *name = @"🍇 Grape Grove";
+
+ NSData *context = [NSKeyedArchiver archivedDataWithRootObject:@{
+ nameKey: name,
+ }];
+
+ __block MGLOfflinePack *pack;
+ [self keyValueObservingExpectationForObject:[MGLOfflineStorage sharedOfflineStorage] keyPath:@"packs" handler:^BOOL(id _Nonnull observedObject, NSDictionary * _Nonnull change) {
+ NSKeyValueChange changeKind = [change[NSKeyValueChangeKindKey] unsignedIntegerValue];
+ NSIndexSet *indices = change[NSKeyValueChangeIndexesKey];
+ return changeKind == NSKeyValueChangeInsertion && indices.count == 1;
+ }];
+ XCTestExpectation *additionCompletionHandlerExpectation = [self expectationWithDescription:@"add pack completion handler"];
+ [[MGLOfflineStorage sharedOfflineStorage] addPackForRegion:region withContext:context completionHandler:^(MGLOfflinePack * _Nullable completionHandlerPack, NSError * _Nullable error) {
+ XCTAssertNotNil(completionHandlerPack, @"Added pack should exist.");
+ XCTAssertEqual(completionHandlerPack.state, MGLOfflinePackStateUnknown, @"Pack should initially have unknown state.");
+ pack = completionHandlerPack;
+ [additionCompletionHandlerExpectation fulfill];
+ }];
+ [self waitForExpectationsWithTimeout:1 handler:nil];
+
+ XCTAssertEqual([MGLOfflineStorage sharedOfflineStorage].packs.count, countOfPacks + 1, @"Added pack should have been added to the canonical collection of packs owned by the shared offline storage object. This assertion can fail if this test is run before -testAAALoadPacks.");
+
+ XCTAssertEqual(pack, [MGLOfflineStorage sharedOfflineStorage].packs.lastObject, @"Pack should be appended to end of packs array.");
+
+ XCTAssertEqualObjects(pack.region, region, @"Added pack’s region has changed.");
+
+ NSDictionary *userInfo = [NSKeyedUnarchiver unarchiveObjectWithData:pack.context];
+ XCTAssert([userInfo isKindOfClass:[NSDictionary class]], @"Context of offline pack isn’t a dictionary.");
+ XCTAssert([userInfo[nameKey] isKindOfClass:[NSString class]], @"Name of offline pack isn’t a string.");
+ XCTAssertEqualObjects(userInfo[nameKey], name, @"Name of offline pack has changed.");
+
+ XCTAssertEqual(pack.state, MGLOfflinePackStateUnknown, @"Pack should initially have unknown state.");
+
+ [self keyValueObservingExpectationForObject:pack keyPath:@"state" handler:^BOOL(id _Nonnull observedObject, NSDictionary * _Nonnull change) {
+ NSKeyValueChange changeKind = [change[NSKeyValueChangeKindKey] unsignedIntegerValue];
+ MGLOfflinePackState state = [change[NSKeyValueChangeNewKey] integerValue];
+ return changeKind == NSKeyValueChangeSetting && state == MGLOfflinePackStateInactive;
+ }];
+ [self expectationForNotification:MGLOfflinePackProgressChangedNotification object:pack handler:^BOOL(NSNotification * _Nonnull notification) {
+ MGLOfflinePack *notificationPack = notification.object;
+ XCTAssert([notificationPack isKindOfClass:[MGLOfflinePack class]], @"Object of notification should be an MGLOfflinePack.");
+ return notificationPack == pack && pack.state == MGLOfflinePackStateInactive;
+ }];
+ [self waitForExpectationsWithTimeout:1 handler:nil];
+}
+
+- (void)testRemovePack {
+ NSUInteger countOfPacks = [MGLOfflineStorage sharedOfflineStorage].packs.count;
+
+ MGLOfflinePack *pack = [MGLOfflineStorage sharedOfflineStorage].packs.lastObject;
+ XCTAssertNotNil(pack, @"Added pack should still exist.");
+
+ XCTestExpectation *kvoExpectation = [self keyValueObservingExpectationForObject:[MGLOfflineStorage sharedOfflineStorage] keyPath:@"packs" handler:^BOOL(id _Nonnull observedObject, NSDictionary * _Nonnull change) {
+ NSKeyValueChange changeKind = [change[NSKeyValueChangeKindKey] unsignedIntegerValue];
+ NSIndexSet *indices = change[NSKeyValueChangeIndexesKey];
+ return changeKind = NSKeyValueChangeRemoval && indices.count == 1;
+ }];
+ XCTestExpectation *completionHandlerExpectation = [self expectationWithDescription:@"remove pack completion handler"];
+ [[MGLOfflineStorage sharedOfflineStorage] removePack:pack withCompletionHandler:^(NSError * _Nullable error) {
+ XCTAssertEqual(pack.state, MGLOfflinePackStateInvalid, @"Removed pack should be invalid in the completion handler.");
+ [completionHandlerExpectation fulfill];
+ }];
+ [self waitForExpectationsWithTimeout:1 handler:nil];
+
+ XCTAssertEqual(pack.state, MGLOfflinePackStateInvalid, @"Removed pack should have been invalidated synchronously.");
+
+ XCTAssertEqual([MGLOfflineStorage sharedOfflineStorage].packs.count, countOfPacks - 1, @"Removed pack should have been removed from the canonical collection of packs owned by the shared offline storage object. This assertion can fail if this test is run before -testAAALoadPacks or -testAddPack.");
+}
+
+@end
diff --git a/platform/osx/test/osxtest.gypi b/platform/osx/test/osxtest.gypi
index 30bced31c4..6165b6fa88 100644
--- a/platform/osx/test/osxtest.gypi
+++ b/platform/osx/test/osxtest.gypi
@@ -44,6 +44,9 @@
'sources': [
'./MGLGeometryTests.mm',
+ './MGLOfflinePackTests.m',
+ './MGLOfflineRegionTests.m',
+ './MGLOfflineStorageTests.m',
'./MGLStyleTests.mm',
],