diff options
author | Julian Rex <julian.rex@mapbox.com> | 2019-10-04 11:15:20 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-04 11:15:20 -0400 |
commit | cab756374f4eb42ab632c6db6d049a90884700ad (patch) | |
tree | ff525a2cf8252a8936802c2a6162f1906978106a /platform/ios/test | |
parent | bef635765a1bbec14d7732856c38c037ea8add6a (diff) | |
download | qtlocation-mapboxgl-cab756374f4eb42ab632c6db6d049a90884700ad.tar.gz |
[ios] Fix layout of Scale bar components (#15703)
* [ios] Add failing tests for scale bar updates.
* [ios] Remove borderLayer
* [ios] Ensure bar sections have non-fractional width
* [ios] Add RTL override
* [ios] Update constraints/layout
* [ios] Invalidate scalebars size, after the parent's layout has happened (to ensure the view dimensions are correct when re-laying out the scale bar.
* [ios] Refactor layout
* [ios] Update changelog / remove commented code.
* [ios] Remove whitespace
* [ios] Stop integration tests asserting (when scale bar is hidden). Also reduces the # of calculations (since updateConstraints and layoutSubviews can be called outside of our control).
* [ios] Moves declaration of properties for testing.
* [ios] Removes redundant property attribute
* [ios] Updated change log
Diffstat (limited to 'platform/ios/test')
-rw-r--r-- | platform/ios/test/MGLMapViewLayoutTests.m | 85 | ||||
-rw-r--r-- | platform/ios/test/MGLMapViewScaleBarTests.m | 15 |
2 files changed, 95 insertions, 5 deletions
diff --git a/platform/ios/test/MGLMapViewLayoutTests.m b/platform/ios/test/MGLMapViewLayoutTests.m index 5d9c0339b5..2a9579818a 100644 --- a/platform/ios/test/MGLMapViewLayoutTests.m +++ b/platform/ios/test/MGLMapViewLayoutTests.m @@ -3,6 +3,7 @@ #import "MGLMapViewDelegate.h" #import "MGLAccountManager.h" +#import "MGLScaleBar.h" @interface MGLOrnamentTestData : NSObject @@ -24,6 +25,14 @@ @end +@interface MGLScaleBar (Tests) +@property (nonatomic, readonly) NSArray<UIView *> *labelViews; +@property (nonatomic, readonly) NSArray<UIView *> *bars; +@property (nonatomic, readonly) UIView *containerView; +@property (nonatomic, readonly) CGSize size; +@property (nonatomic) NSNumber *testingRightToLeftOverride; +@end + @interface MGLMapViewLayoutTests : XCTestCase<MGLMapViewDelegate> @@ -140,7 +149,7 @@ expectedOrigin:CGPointMake(margin, margin)], [MGLOrnamentTestData createWithPosition:MGLOrnamentPositionTopRight offset:CGPointMake(margin, margin) - expectedOrigin:CGPointMake(CGRectGetMaxX(self.mapView.bounds) - margin - CGRectGetWidth(view.frame), 4)], + expectedOrigin:CGPointMake(CGRectGetMaxX(self.mapView.bounds) - margin - CGRectGetWidth(view.frame), margin)], [MGLOrnamentTestData createWithPosition:MGLOrnamentPositionBottomLeft offset:CGPointMake(margin, margin) expectedOrigin:CGPointMake(margin, CGRectGetMaxY(self.mapView.bounds) - margin - bottomSafeAreaInset - CGRectGetHeight(view.frame))], @@ -176,6 +185,8 @@ CGFloat margin = 4.0; UIView *scaleBar = self.mapView.scaleBar; + XCTAssertFalse(CGSizeEqualToSize(scaleBar.bounds.size, CGSizeZero)); + NSArray *testDataList = [self makeTestDataListWithView:scaleBar margin:margin]; for (MGLOrnamentTestData *testData in testDataList) { @@ -191,6 +202,78 @@ } } +// This test checks the frames of the scalebar's subviews, based on the positions +// as above, but also with forced Right-to-Left reading, and modifying zoom levels. +- (void)testScalebarSubviewPlacement { + double accuracy = 0.01; + CGFloat margin = 20.0; + + MGLScaleBar *scaleBar = (MGLScaleBar*)self.mapView.scaleBar; + XCTAssertFalse(CGSizeEqualToSize(scaleBar.bounds.size, CGSizeZero)); + + for (NSInteger rtl = 0; rtl <= 1; rtl++) { + scaleBar.testingRightToLeftOverride = @((BOOL)rtl); + + NSString *positions[] = { + @"MGLOrnamentPositionTopLeft", + @"MGLOrnamentPositionTopRight", + @"MGLOrnamentPositionBottomLeft", + @"MGLOrnamentPositionBottomRight" + }; + + for (CGFloat zoomLevel = 0; zoomLevel < 20; zoomLevel++) + { + self.mapView.zoomLevel = zoomLevel; + [self.superView setNeedsLayout]; + [self.superView layoutIfNeeded]; + + // Following method assumes scaleBar has an up-to-date frame, based + // on the current zoom level. Modifying the position and margins + // should not affect the overall size of the scalebar. + + NSArray *testDataList = [self makeTestDataListWithView:scaleBar margin:margin]; + + CGSize initialSize = scaleBar.intrinsicContentSize; + XCTAssert(CGSizeEqualToSize(initialSize, scaleBar.bounds.size)); + + for (MGLOrnamentTestData *testData in testDataList) { + self.mapView.scaleBarPosition = testData.position; + self.mapView.scaleBarMargins = testData.offset; + + [self.superView setNeedsLayout]; + [self.superView layoutIfNeeded]; + + XCTAssert(CGSizeEqualToSize(initialSize, scaleBar.bounds.size)); + + NSString *activityName = [NSString stringWithFormat: + @"Scalebar subview tests: RTL=%@, Zoom=%ld, POS=%@, Visible=%@", + (rtl == 0 ? @"NO" : @"YES"), + (long)zoomLevel, + positions[testData.position], + scaleBar.alpha > 0.0 ? @"YES" : @"NO"]; + + [XCTContext runActivityNamed:activityName + block:^(id<XCTActivity> activity) { + + // Check the subviews + XCTAssertEqualWithAccuracy(CGRectGetMinX(scaleBar.frame), testData.expectedOrigin.x, accuracy); + XCTAssertEqualWithAccuracy(CGRectGetMinY(scaleBar.frame), testData.expectedOrigin.y, accuracy); + + XCTAssertTrue(CGRectContainsRect(scaleBar.bounds, scaleBar.containerView.frame)); + for (UIView *bar in scaleBar.bars) { + XCTAssertTrue(CGRectContainsRect(scaleBar.containerView.bounds, bar.frame)); + } + for (UIView *label in scaleBar.labelViews) { + if (!label.isHidden) { + XCTAssertTrue(CGRectContainsRect(scaleBar.bounds, label.frame)); + } + } + }]; + } + } + } +} + - (void)testAttributionButtonPlacement { double accuracy = 0.01; CGFloat margin = 4.0; diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index 29acc75d7f..b4f81ef62b 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -36,11 +36,14 @@ XCTAssertFalse(scaleBar.hidden); // Scale bar should not be visible at default zoom (~z0), but it should be ready. - XCTAssertFalse(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); + // Size is not a measure of readiness here though. + XCTAssertTrue(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); XCTAssertEqual(scaleBar.alpha, 0); self.mapView.zoomLevel = 15; + [self.mapView layoutIfNeeded]; XCTAssertGreaterThan(scaleBar.alpha, 0); + XCTAssertFalse(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); } - (void)testDirectlySettingScaleBarViewHiddenProperty { @@ -54,10 +57,14 @@ // ... but triggering any camera event will update it. self.mapView.zoomLevel = 1; - XCTAssertFalse(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); + [self.mapView layoutIfNeeded]; + + XCTAssertTrue(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); XCTAssertEqual(scaleBar.alpha, 0); self.mapView.zoomLevel = 15; + [self.mapView layoutIfNeeded]; + XCTAssertGreaterThan(scaleBar.alpha, 0); -} -@end + XCTAssertFalse(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); +}@end |