summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Wray <jason@mapbox.com>2019-07-09 17:59:35 -0700
committerJason Wray <jason@mapbox.com>2019-07-09 17:59:35 -0700
commit0f241f1cbe2ae8119f370f2e20c666bf57dbc508 (patch)
treed4d119bfdb1dd4ca830e77aecd42f0d825bbb8c1
parent593e8cb210934b0422f77797a4748b7ef1ef1e2b (diff)
downloadqtlocation-mapboxgl-0f241f1cbe2ae8119f370f2e20c666bf57dbc508.tar.gz
Add tests and refactor visibility setter
-rw-r--r--platform/ios/app/MBXViewController.m2
-rw-r--r--platform/ios/ios.xcodeproj/project.pbxproj8
-rw-r--r--platform/ios/src/MGLCompassButton.mm30
-rw-r--r--platform/ios/src/MGLCompassButton_Private.h3
-rw-r--r--platform/ios/src/MGLMapView.mm2
-rw-r--r--platform/ios/src/Mapbox.h1
-rw-r--r--platform/ios/test/MGLCompassButtonTests.m84
-rw-r--r--platform/ios/test/MGLCompassViewTests.m23
-rw-r--r--platform/ios/test/MGLMapViewDirectionTests.mm33
9 files changed, 145 insertions, 41 deletions
diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m
index 2036d35fbe..2fb95e1b17 100644
--- a/platform/ios/app/MBXViewController.m
+++ b/platform/ios/app/MBXViewController.m
@@ -302,8 +302,6 @@ CLLocationCoordinate2D randomWorldCoordinate() {
}
}
}];
-
- self.mapView.compassView.compassVisibility = MGLOrnamentVisibilityAdaptive;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj
index 2d9f82e27e..670e78a46c 100644
--- a/platform/ios/ios.xcodeproj/project.pbxproj
+++ b/platform/ios/ios.xcodeproj/project.pbxproj
@@ -426,7 +426,7 @@
96E516FB20005A4000A02306 /* MGLUserLocationHeadingBeamLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = 966FCF4A1F3A5C9200F2B6DE /* MGLUserLocationHeadingBeamLayer.h */; };
96E516FC20005A4400A02306 /* MGLUserLocationHeadingIndicator.h in Headers */ = {isa = PBXBuildFile; fileRef = 96F3F73B1F5711F1003E2D2C /* MGLUserLocationHeadingIndicator.h */; };
96E5170420005A6B00A02306 /* SMCalloutView.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8848891CBB037E00AB86E3 /* SMCalloutView.h */; };
- 96E6145622CC135200109F14 /* MGLCompassViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 96E6145522CC135200109F14 /* MGLCompassViewTests.m */; };
+ 96E6145622CC135200109F14 /* MGLCompassButtonTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 96E6145522CC135200109F14 /* MGLCompassButtonTests.m */; };
96E6145922CC169000109F14 /* MGLCompassButton.h in Headers */ = {isa = PBXBuildFile; fileRef = 96E6145722CC169000109F14 /* MGLCompassButton.h */; settings = {ATTRIBUTES = (Public, ); }; };
96E6145A22CC169000109F14 /* MGLCompassButton.h in Headers */ = {isa = PBXBuildFile; fileRef = 96E6145722CC169000109F14 /* MGLCompassButton.h */; settings = {ATTRIBUTES = (Public, ); }; };
96E6145B22CC169000109F14 /* MGLCompassButton.mm in Sources */ = {isa = PBXBuildFile; fileRef = 96E6145822CC169000109F14 /* MGLCompassButton.mm */; };
@@ -1140,7 +1140,7 @@
96E0272C1E57C7E5004B8E66 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Localizable.strings; sourceTree = "<group>"; };
96E0272D1E57C7E6004B8E66 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = "<group>"; };
96E0272E1E57C7E7004B8E66 /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/Localizable.strings"; sourceTree = "<group>"; };
- 96E6145522CC135200109F14 /* MGLCompassViewTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLCompassViewTests.m; sourceTree = "<group>"; };
+ 96E6145522CC135200109F14 /* MGLCompassButtonTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLCompassButtonTests.m; sourceTree = "<group>"; };
96E6145722CC169000109F14 /* MGLCompassButton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLCompassButton.h; sourceTree = "<group>"; };
96E6145822CC169000109F14 /* MGLCompassButton.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLCompassButton.mm; sourceTree = "<group>"; };
96ED34DD22374C0900E9FCA9 /* MGLMapViewDirectionTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLMapViewDirectionTests.mm; sourceTree = "<group>"; };
@@ -2040,7 +2040,7 @@
DA35A2C31CCA9F8300E826B2 /* MGLClockDirectionFormatterTests.m */,
35D9DDE11DA25EEC00DAAD69 /* MGLCodingTests.mm */,
DA35A2C41CCA9F8300E826B2 /* MGLCompassDirectionFormatterTests.m */,
- 96E6145522CC135200109F14 /* MGLCompassViewTests.m */,
+ 96E6145522CC135200109F14 /* MGLCompassButtonTests.m */,
DA35A2A91CCA058D00E826B2 /* MGLCoordinateFormatterTests.m */,
3598544C1E1D38AA00B29F84 /* MGLDistanceFormatterTests.m */,
6407D66F1E0085FD00F6A9C3 /* MGLDocumentationExampleTests.swift */,
@@ -3259,7 +3259,7 @@
40CFA6511D7875BB008103BD /* MGLShapeSourceTests.mm in Sources */,
DA35A2C51CCA9F8300E826B2 /* MGLClockDirectionFormatterTests.m in Sources */,
35B8E08C1D6C8B5100E768D2 /* MGLPredicateTests.mm in Sources */,
- 96E6145622CC135200109F14 /* MGLCompassViewTests.m in Sources */,
+ 96E6145622CC135200109F14 /* MGLCompassButtonTests.m in Sources */,
96036A0620059BBA00510F3D /* MGLNSOrthographyAdditionsTests.m in Sources */,
1F95931D1E6DE2E900D5B294 /* MGLNSDateAdditionsTests.mm in Sources */,
DA695426215B1E76002041A4 /* MGLMapCameraTests.m in Sources */,
diff --git a/platform/ios/src/MGLCompassButton.mm b/platform/ios/src/MGLCompassButton.mm
index 9d0ade4121..94a6820a74 100644
--- a/platform/ios/src/MGLCompassButton.mm
+++ b/platform/ios/src/MGLCompassButton.mm
@@ -52,6 +52,13 @@
[self sizeToFit];
}
+- (void)setCompassVisibility:(MGLOrnamentVisibility)compassVisibility {
+ if (_compassVisibility == compassVisibility) { return; }
+ _compassVisibility = compassVisibility;
+
+ [self updateCompassAnimated:NO];
+}
+
- (UIImage *)compassImage {
UIImage *scaleImage = [UIImage mgl_resourceImageNamed:@"Compass"];
UIGraphicsBeginImageContextWithOptions(scaleImage.size, NO, UIScreen.mainScreen.scale);
@@ -75,7 +82,12 @@
[self.mapView resetNorth];
}
-- (void)updateCompassWithDirection:(CLLocationDirection)direction {
+- (void)updateCompass {
+ [self updateCompassAnimated:YES];
+}
+
+- (void)updateCompassAnimated:(BOOL)animated {
+ CLLocationDirection direction = self.mapView.direction;
CLLocationDirection plateDirection = mbgl::util::wrap(-direction, 0., 360.);
self.transform = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(plateDirection));
@@ -85,26 +97,26 @@
switch (self.compassVisibility) {
case MGLOrnamentVisibilityAdaptive:
if (direction > 0 && self.alpha < 1) {
- [self showCompass];
+ [self showCompass:animated];
} else if (direction == 0 && self.alpha > 0) {
- [self hideCompass];
+ [self hideCompass:animated];
}
break;
case MGLOrnamentVisibilityVisible:
- [self showCompass];
+ [self showCompass:animated];
break;
case MGLOrnamentVisibilityHidden:
- [self hideCompass];
+ [self hideCompass:animated];
break;
}
}
-- (void)showCompass {
- [self animateToAlpha:1];
+- (void)showCompass:(BOOL)animated {
+ animated ? [self animateToAlpha:1] : [self setAlpha:1];
}
-- (void)hideCompass {
- [self animateToAlpha:0];
+- (void)hideCompass:(BOOL)animated {
+ animated ? [self animateToAlpha:0] : [self setAlpha:0];
}
- (void)animateToAlpha:(CGFloat)alpha {
diff --git a/platform/ios/src/MGLCompassButton_Private.h b/platform/ios/src/MGLCompassButton_Private.h
index 50918d8c45..c9741d79e3 100644
--- a/platform/ios/src/MGLCompassButton_Private.h
+++ b/platform/ios/src/MGLCompassButton_Private.h
@@ -1,5 +1,4 @@
#import <UIKit/UIKit.h>
-#import <CoreLocation/CoreLocation.h>
#import <Mapbox/MGLCompassButton.h>
@@ -13,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, weak) MGLMapView *mapView;
-- (void)updateCompassWithDirection:(CLLocationDirection)direction;
+- (void)updateCompass;
@end
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm
index 97617be517..d027f616bd 100644
--- a/platform/ios/src/MGLMapView.mm
+++ b/platform/ios/src/MGLMapView.mm
@@ -6507,7 +6507,7 @@ public:
- (void)updateCompass
{
- [self.compassView updateCompassWithDirection:self.direction];
+ [self.compassView updateCompass];
}
- (void)updateScaleBar
diff --git a/platform/ios/src/Mapbox.h b/platform/ios/src/Mapbox.h
index 6e4e2c99fc..98e673577c 100644
--- a/platform/ios/src/Mapbox.h
+++ b/platform/ios/src/Mapbox.h
@@ -16,6 +16,7 @@ FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[];
#import "MGLClockDirectionFormatter.h"
#import "MGLCluster.h"
#import "MGLCompassButton.h"
+#import "MGLCompassDirectionFormatter.h"
#import "MGLCoordinateFormatter.h"
#import "MGLDistanceFormatter.h"
#import "MGLFeature.h"
diff --git a/platform/ios/test/MGLCompassButtonTests.m b/platform/ios/test/MGLCompassButtonTests.m
new file mode 100644
index 0000000000..f41de0a2e2
--- /dev/null
+++ b/platform/ios/test/MGLCompassButtonTests.m
@@ -0,0 +1,84 @@
+#import <Mapbox/Mapbox.h>
+#import <XCTest/XCTest.h>
+
+#import "../../ios/src/MGLCompassButton_Private.h"
+
+@interface MGLMapView (MGLCompassButtonTests)
+
+- (void)resetNorthAnimated:(BOOL)animated;
+
+@end
+
+@interface MGLCompassButtonTests : XCTestCase
+
+@property (nonatomic) MGLMapView *mapView;
+
+@end
+
+@implementation MGLCompassButtonTests
+
+- (void)setUp {
+ [super setUp];
+
+ [MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];
+ NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
+ self.mapView = [[MGLMapView alloc] initWithFrame:UIScreen.mainScreen.bounds styleURL:styleURL];
+}
+
+- (void)tearDown {
+ self.mapView = nil;
+ [MGLAccountManager setAccessToken:nil];
+
+ [super tearDown];
+}
+
+- (void)testCompassButton {
+ XCTAssertNotNil(self.mapView.compassView);
+ XCTAssertTrue(self.mapView.compassView.userInteractionEnabled);
+ XCTAssertEqual(self.mapView.compassView.gestureRecognizers.count, 1);
+ XCTAssertEqual(self.mapView.compassView.accessibilityTraits, UIAccessibilityTraitButton);
+ XCTAssertNotNil(self.mapView.compassView.accessibilityLabel);
+ XCTAssertNotNil(self.mapView.compassView.accessibilityHint);
+}
+
+- (void)testVisibilityAdaptive {
+ XCTAssertEqual(self.mapView.compassView.compassVisibility, MGLOrnamentVisibilityAdaptive, @"Adaptive should be the default visibility setting.");
+ XCTAssertEqual(self.mapView.compassView.alpha, 0, @"Compass should not initially be visible.");
+
+ self.mapView.zoomLevel = 15.f;
+ [self.mapView setDirection:45.f animated:false];
+ XCTAssertEqualWithAccuracy(self.mapView.direction, 45, 0.001);
+ XCTAssertEqual(self.mapView.compassView.alpha, 1, @"Compass should become visible when direction changes.");
+
+ [self.mapView resetNorthAnimated:NO];
+ XCTAssertEqual(self.mapView.direction, 0);
+ XCTAssertEqual(self.mapView.compassView.alpha, 0, @"Compass should hide itself when direction is north.");
+}
+
+- (void)testVisibilityHidden {
+ self.mapView.compassView.compassVisibility = MGLOrnamentVisibilityHidden;
+ XCTAssertEqual(self.mapView.compassView.compassVisibility, MGLOrnamentVisibilityHidden);
+ XCTAssertEqual(self.mapView.compassView.alpha, 0, @"Compass should not initially be visible.");
+
+ self.mapView.zoomLevel = 15.f;
+ [self.mapView setDirection:45.f animated:false];
+ XCTAssertEqualWithAccuracy(self.mapView.direction, 45, 0.001);
+ XCTAssertEqual(self.mapView.compassView.alpha, 0, @"Compass should stay hidden when direction changes.");
+}
+
+- (void)testVisibilityVisible {
+ self.mapView.compassView.compassVisibility = MGLOrnamentVisibilityVisible;
+ XCTAssertEqual(self.mapView.compassView.compassVisibility, MGLOrnamentVisibilityVisible);
+ XCTAssertEqual(self.mapView.compassView.alpha, 1, @"Compass should initially be visible.");
+
+ self.mapView.zoomLevel = 15.f;
+ [self.mapView setDirection:45.f animated:false];
+ XCTAssertEqualWithAccuracy(self.mapView.direction, 45, 0.001);
+ XCTAssertEqual(self.mapView.compassView.alpha, 1, @"Compass should continue to be visible when direction changes.");
+
+ [self.mapView resetNorthAnimated:NO];
+ XCTAssertEqual(self.mapView.direction, 0);
+ XCTAssertEqual(self.mapView.compassView.alpha, 1, @"Compass should continue to be visible when direction is north.");
+}
+
+@end
diff --git a/platform/ios/test/MGLCompassViewTests.m b/platform/ios/test/MGLCompassViewTests.m
deleted file mode 100644
index cabd6085a7..0000000000
--- a/platform/ios/test/MGLCompassViewTests.m
+++ /dev/null
@@ -1,23 +0,0 @@
-#import <Mapbox/Mapbox.h>
-#import <XCTest/XCTest.h>
-
-@interface MGLCompassViewTests : XCTestCase
-
-@end
-
-@implementation MGLCompassViewTests
-
-- (void)setUp {
- // Put setup code here. This method is called before the invocation of each test method in the class.
-}
-
-- (void)tearDown {
- // Put teardown code here. This method is called after the invocation of each test method in the class.
-}
-
-- (void)testExample {
- // This is an example of a functional test case.
- // Use XCTAssert and related functions to verify your tests produce the correct results.
-}
-
-@end
diff --git a/platform/ios/test/MGLMapViewDirectionTests.mm b/platform/ios/test/MGLMapViewDirectionTests.mm
index 7f7e0903e3..d3990b581b 100644
--- a/platform/ios/test/MGLMapViewDirectionTests.mm
+++ b/platform/ios/test/MGLMapViewDirectionTests.mm
@@ -51,6 +51,39 @@ static MGLMapView *mapView;
XCTAssertEqual(mapView.direction, 0, @"Reset-to-north should set direction to 0°.");
}
+- (void)testRotateEnabled {
+ mapView.zoomLevel = 10;
+
+ UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] initWithTarget:nil action:nil];
+ gesture.state = UIGestureRecognizerStateBegan;
+ gesture.rotation = MGLRadiansFromDegrees(30);
+ CGFloat wrappedRotation = mbgl::util::wrap(-MGLDegreesFromRadians(gesture.rotation), 0., 360.);
+
+ // Disabled
+ {
+ mapView.rotateEnabled = NO;
+ XCTAssertEqual(mapView.allowsRotating, NO);
+
+ [mapView handleRotateGesture:gesture];
+ XCTAssertNotEqual(mapView.direction, wrappedRotation, @"Gestural rotation should not work when rotation is disabled.");
+
+ mapView.direction = 45.f;
+ XCTAssertEqualWithAccuracy(mapView.direction, 45, 0.001, @"Programmatic rotation is allowed when rotateEnabled = NO.");
+ }
+
+ // Enabled
+ {
+ [mapView resetNorthAnimated:NO];
+ mapView.rotateEnabled = YES;
+ XCTAssertEqual(mapView.allowsRotating, YES);
+
+ gesture.state = UIGestureRecognizerStateChanged;
+ gesture.rotation = MGLRadiansFromDegrees(30);
+ [mapView handleRotateGesture:gesture];
+ XCTAssertEqualWithAccuracy(mapView.direction, wrappedRotation, 0.001, @"Gestural rotation should work when rotation is enabled.");
+ }
+}
+
- (void)testRotateGesture {
mapView.zoomLevel = 15;