Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Add ornaments position asserts (#124)
Browse files Browse the repository at this point in the history
* add ornaments position asserts
  • Loading branch information
lloydsheng committed Jan 6, 2020
1 parent 3d20ca9 commit 58aa001
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 10 deletions.
9 changes: 9 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,15 @@ - (void)layoutSubviews
[self updateUserLocationAnnotationView];

[self updateAttributionAlertView];

MGLAssert(CGRectContainsRect(self.bounds, self.attributionButton.mgl_frameForIdentifyTransform),
@"The attribution is not in the visible area of the mapview. Please check your position and offset settings");
MGLAssert(CGRectContainsRect(self.bounds, self.scaleBar.mgl_frameForIdentifyTransform),
@"The scaleBar is not in the visible area of the mapview. Please check your position and offset settings");
MGLAssert(CGRectContainsRect(self.bounds, self.compassView.mgl_frameForIdentifyTransform),
@"The compassView is not in the visible area of the mapview. Please check your position and offset settings");
MGLAssert(CGRectContainsRect(self.bounds, self.logoView.mgl_frameForIdentifyTransform),
@"The logoView is not in the visible area of the mapview. Please check your position and offset settings");
}

/// Updates `contentInset` to reflect the current window geometry.
Expand Down
2 changes: 2 additions & 0 deletions platform/ios/src/UIView+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ NS_ASSUME_NONNULL_BEGIN

- (NSLayoutXAxisAnchor *)mgl_safeTrailingAnchor;

- (CGRect)mgl_frameForIdentifyTransform;

@end

NS_ASSUME_NONNULL_END
12 changes: 12 additions & 0 deletions platform/ios/src/UIView+MGLAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ - (NSLayoutXAxisAnchor *)mgl_safeTrailingAnchor {
}
}

- (CGRect)mgl_frameForIdentifyTransform {
CGPoint center = self.center;
CGSize size = self.bounds.size;

return CGRectMake(
center.x - size.width / 2,
center.y - size.height / 2,
size.width,
size.height
);
}

@end
107 changes: 97 additions & 10 deletions platform/ios/test/MGLMapViewLayoutTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "MGLAccountManager.h"

#import "MGLScaleBar.h"
#import "UIView+MGLAdditions.h"

@interface MGLOrnamentTestData : NSObject

Expand Down Expand Up @@ -175,8 +176,30 @@ - (void)testCompassPlacement {
[self.superView setNeedsLayout];
[self.superView layoutIfNeeded];

XCTAssertEqualWithAccuracy(CGRectGetMinX(compassView.frame), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(compassView.frame), testData.expectedOrigin.y, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinX(compassView.mgl_frameForIdentifyTransform), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(compassView.mgl_frameForIdentifyTransform), testData.expectedOrigin.y, accuracy);
}
}

- (void)testCompassPlacementWithTransform {
double accuracy = 0.01;
CGFloat margin = 4.0;

UIView *compassView = self.mapView.compassView;
NSArray *testDataList = [self makeTestDataListWithView:compassView margin:margin];

for (MGLOrnamentTestData *testData in testDataList) {
self.mapView.compassViewPosition = testData.position;
self.mapView.compassViewMargins = testData.offset;
// A tranform value which would led compassView's frame outside mapview's bounds
self.mapView.compassView.transform = CGAffineTransformMake(0.7, -0.8, 0.6, 0.7, 0, 0);

//invoke layout
[self.superView setNeedsLayout];
[self.superView layoutIfNeeded];

XCTAssertEqualWithAccuracy(CGRectGetMinX(compassView.mgl_frameForIdentifyTransform), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(compassView.mgl_frameForIdentifyTransform), testData.expectedOrigin.y, accuracy);
}
}

Expand All @@ -197,8 +220,29 @@ - (void)testScalebarPlacement {
[self.superView setNeedsLayout];
[self.superView layoutIfNeeded];

XCTAssertEqualWithAccuracy(CGRectGetMinX(scaleBar.frame), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(scaleBar.frame), testData.expectedOrigin.y, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinX(scaleBar.mgl_frameForIdentifyTransform), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(scaleBar.mgl_frameForIdentifyTransform), testData.expectedOrigin.y, accuracy);
}
}

- (void)testScalebarPlacementInvalidPosition {
CGFloat margin = -_superView.bounds.size.width;

UIView *scaleBar = self.mapView.scaleBar;
NSArray *testDataList = [self makeTestDataListWithView:scaleBar margin:margin];

for (MGLOrnamentTestData *testData in testDataList) {
self.mapView.scaleBarPosition = testData.position;
self.mapView.scaleBarMargins = testData.offset;

//invoke layout
[self.superView setNeedsLayout];
XCTAssertThrowsSpecificNamed(
[self.superView layoutIfNeeded],
NSException,
NSInternalInconsistencyException,
@"should throw NSInternalInconsistencyException"
);
}
}

Expand Down Expand Up @@ -256,8 +300,8 @@ - (void)testScalebarSubviewPlacement {
block:^(id<XCTActivity> activity) {

// Check the subviews
XCTAssertEqualWithAccuracy(CGRectGetMinX(scaleBar.frame), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(scaleBar.frame), testData.expectedOrigin.y, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinX(scaleBar.mgl_frameForIdentifyTransform), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(scaleBar.mgl_frameForIdentifyTransform), testData.expectedOrigin.y, accuracy);

XCTAssertTrue(CGRectContainsRect(scaleBar.bounds, scaleBar.containerView.frame));
for (UIView *bar in scaleBar.bars) {
Expand Down Expand Up @@ -289,11 +333,33 @@ - (void)testAttributionButtonPlacement {
[self.superView setNeedsLayout];
[self.superView layoutIfNeeded];

XCTAssertEqualWithAccuracy(CGRectGetMinX(attributionButton.frame), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(attributionButton.frame), testData.expectedOrigin.y, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinX(attributionButton.mgl_frameForIdentifyTransform), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(attributionButton.mgl_frameForIdentifyTransform), testData.expectedOrigin.y, accuracy);
}
}

- (void)testAttributionButtonPlacementInvalidPosition {
CGFloat margin = -_superView.bounds.size.width;

UIView *attributionButton = self.mapView.attributionButton;
NSArray *testDataList = [self makeTestDataListWithView:attributionButton margin:margin];

for (MGLOrnamentTestData *testData in testDataList) {
self.mapView.attributionButtonPosition = testData.position;
self.mapView.attributionButtonMargins = testData.offset;

//invoke layout
[self.superView setNeedsLayout];
XCTAssertThrowsSpecificNamed(
[self.superView layoutIfNeeded],
NSException,
NSInternalInconsistencyException,
@"should throw NSInternalInconsistencyException"
);
}
}


- (void)testLogoPlacement {
double accuracy = 0.01;
CGFloat margin = 4.0;
Expand All @@ -309,8 +375,29 @@ - (void)testLogoPlacement {
[self.superView setNeedsLayout];
[self.superView layoutIfNeeded];

XCTAssertEqualWithAccuracy(CGRectGetMinX(logoView.frame), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(logoView.frame), testData.expectedOrigin.y, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinX(logoView.mgl_frameForIdentifyTransform), testData.expectedOrigin.x, accuracy);
XCTAssertEqualWithAccuracy(CGRectGetMinY(logoView.mgl_frameForIdentifyTransform), testData.expectedOrigin.y, accuracy);
}
}

- (void)testLogoPlacementInvalidPosition {
CGFloat margin = -_superView.bounds.size.width;

UIView *logoView = self.mapView.logoView;
NSArray *testDataList = [self makeTestDataListWithView:logoView margin:margin];

for (MGLOrnamentTestData *testData in testDataList) {
self.mapView.logoViewPosition = testData.position;
self.mapView.logoViewMargins = testData.offset;

//invoke layout
[self.superView setNeedsLayout];
XCTAssertThrowsSpecificNamed(
[self.superView layoutIfNeeded],
NSException,
NSInternalInconsistencyException,
@"should throw NSInternalInconsistencyException"
);
}
}

Expand Down

0 comments on commit 58aa001

Please sign in to comment.