From fe53af7de031e296cdb9b0a7e88688fd3f54e0d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Thu, 8 Dec 2016 17:43:17 -0800 Subject: [ios, macos] Source-driven attribution (#5999) * [ios, macos] Source-driven attribution Refactored MGLSource initialization. Implemented a new private class, MGLAttributionInfo, that parses an HTML attribution string from TileJSON and stores the resulting structured data. Added methods to MGLTileSet and MGLStyle to aggregate MGLAttributionInfos. On macOS, update the attribution view as soon as the source attribution changes. On iOS, fetch the current attribution information when displaying the action sheet. Removed hard-coded attribution strings. * [macos] Respect inline formatting in attribution HTML Apply a default font and color to attribution HTML as it is imported into an attributed string. Pass the attributed string into MGLAttributionButton as is, without stripping formatting. Avoid overriding the font and color after importing the HTML, in case these attributes are explicitly specified rather than intrinsic to a hyperlink. Constrain the top of the attribution view to all the attribution buttons, in case one of them needs additional headspace. * [ios, macos] Display unlinked attribution strings Unlinked attribution strings are represented on macOS as buttons that have the default cursor and do nothing when clicked. On iOS, they are action sheet buttons that do nothing but dismiss the action sheet. * [macos] Fixed random Auto Layout exception Auto Layout randomly finds itself unable to satisfy constraints when updating attribution, due to some spurious constraints between attribution buttons. Regenerate the entire attribution view every time the source attribution changes. * [ios, macos] Thoroughly dedupe attribution infos Also added a test to verify parity with the GL JS implementation. This implementation avoids sorting. * [ios, macos] Trim whitespace from attribution strings Also added parsing tests. * [ios, macos] Added attribution parsing tests for styles Included an emoji test to ensure that attribution strings are interpreted as UTF-8, to avoid mojibake. Included a test of removing the underline from a leading copyright symbol. * [ios, macos] Derive feedback link from source MGLAttributionInfo now detects feedback links in the attribution HTML code, and it is responsible for tailoring the feedback URL to the current viewport. Removed the hard-coded feedback action from the attribution sheet on iOS in favor of a source-derived feedback title and URL. Moved the feedback action from macosapp to MGLMapView; applications are now expected to hook an Improve This Map menu item to an MGLMapView action. --- platform/darwin/test/MGLAttributionInfoTests.m | 115 +++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 platform/darwin/test/MGLAttributionInfoTests.m (limited to 'platform/darwin/test') diff --git a/platform/darwin/test/MGLAttributionInfoTests.m b/platform/darwin/test/MGLAttributionInfoTests.m new file mode 100644 index 0000000000..003637bf1b --- /dev/null +++ b/platform/darwin/test/MGLAttributionInfoTests.m @@ -0,0 +1,115 @@ +#import +#import + +#import "MGLAttributionInfo.h" + +@interface MGLAttributionInfoTests : XCTestCase + +@end + +@implementation MGLAttributionInfoTests + +- (void)testParsing { + static NSString * const htmlStrings[] = { + @"© Mapbox " + @"©️ OpenStreetMap " + @"CC BY-SA " + @"Improve this map", + }; + + NS_MUTABLE_ARRAY_OF(MGLAttributionInfo *) *infos = [NSMutableArray array]; + for (NSUInteger i = 0; i < sizeof(htmlStrings) / sizeof(htmlStrings[0]); i++) { + NSArray *subinfos = [MGLAttributionInfo attributionInfosFromHTMLString:htmlStrings[i] + fontSize:0 + linkColor:nil]; + [infos growArrayByAddingAttributionInfosFromArray:subinfos]; + } + + XCTAssertEqual(infos.count, 4); + + CLLocationCoordinate2D mapbox = CLLocationCoordinate2DMake(12.9810816, 77.6368034); + XCTAssertEqualObjects(infos[0].title.string, @"© Mapbox"); + XCTAssertEqualObjects(infos[0].URL, [NSURL URLWithString:@"https://www.mapbox.com/about/maps/"]); + XCTAssertFalse(infos[0].feedbackLink); + XCTAssertNil([infos[0] feedbackURLAtCenterCoordinate:mapbox zoomLevel:14]); + + XCTAssertEqualObjects(infos[1].title.string, @"©️ OpenStreetMap"); + XCTAssertEqualObjects(infos[1].URL, [NSURL URLWithString:@"http://www.openstreetmap.org/about/"]); + XCTAssertFalse(infos[1].feedbackLink); + XCTAssertNil([infos[1] feedbackURLAtCenterCoordinate:mapbox zoomLevel:14]); + + XCTAssertEqualObjects(infos[2].title.string, @"CC\u00a0BY-SA"); + XCTAssertNil(infos[2].URL); + XCTAssertFalse(infos[2].feedbackLink); + XCTAssertNil([infos[2] feedbackURLAtCenterCoordinate:mapbox zoomLevel:14]); + + XCTAssertEqualObjects(infos[3].title.string, @"Improve this map"); + XCTAssertEqualObjects(infos[3].URL, [NSURL URLWithString:@"https://www.mapbox.com/map-feedback/"]); + XCTAssertTrue(infos[3].feedbackLink); + XCTAssertEqualObjects([infos[3] feedbackURLAtCenterCoordinate:mapbox zoomLevel:14], + [NSURL URLWithString:@"https://www.mapbox.com/map-feedback/#/77.63680/12.98108/15"]); +} + +- (void)testStyle { + static NSString * const htmlStrings[] = { + @"Mapbox", + }; + + CGFloat fontSize = 72; + MGLColor *color = [MGLColor redColor]; + NS_MUTABLE_ARRAY_OF(MGLAttributionInfo *) *infos = [NSMutableArray array]; + for (NSUInteger i = 0; i < sizeof(htmlStrings) / sizeof(htmlStrings[0]); i++) { + NSArray *subinfos = [MGLAttributionInfo attributionInfosFromHTMLString:htmlStrings[i] + fontSize:72 + linkColor:color]; + [infos growArrayByAddingAttributionInfosFromArray:subinfos]; + } + + XCTAssertEqual(infos.count, 1); + + XCTAssertEqualObjects(infos[0].title.string, @"Mapbox"); + XCTAssertEqualObjects([infos[0].title attribute:NSLinkAttributeName atIndex:0 effectiveRange:nil], [NSURL URLWithString:@"https://www.mapbox.com/"]); + XCTAssertEqualObjects([infos[0].title attribute:NSUnderlineStyleAttributeName atIndex:0 effectiveRange:nil], @(NSUnderlineStyleSingle)); + +#if TARGET_OS_IPHONE + UIFont *font; +#else + NSFont *font; +#endif + font = [infos[0].title attribute:NSFontAttributeName atIndex:0 effectiveRange:nil]; + XCTAssertEqual(font.pointSize, fontSize); + + CGFloat r, g, b, a; + [color getRed:&r green:&g blue:&b alpha:&a]; + MGLColor *linkColor = [infos[0].title attribute:NSForegroundColorAttributeName atIndex:0 effectiveRange:nil]; + CGFloat linkR, linkG, linkB, linkA; + [linkColor getRed:&linkR green:&linkG blue:&linkB alpha:&linkA]; + XCTAssertEqual(r, linkR); + XCTAssertEqual(g, linkG); + XCTAssertEqual(b, linkB); + XCTAssertEqual(a, linkA); +} + +- (void)testDedupe { + static NSString * const htmlStrings[] = { + @"World", + @"Hello World", + @"Another Source", + @"Hello", + @"Hello World", + }; + + NS_MUTABLE_ARRAY_OF(MGLAttributionInfo *) *infos = [NSMutableArray array]; + for (NSUInteger i = 0; i < sizeof(htmlStrings) / sizeof(htmlStrings[0]); i++) { + NSArray *subinfos = [MGLAttributionInfo attributionInfosFromHTMLString:htmlStrings[i] + fontSize:0 + linkColor:nil]; + [infos growArrayByAddingAttributionInfosFromArray:subinfos]; + } + + XCTAssertEqual(infos.count, 2); + XCTAssertEqualObjects(infos[0].title.string, @"Hello World"); + XCTAssertEqualObjects(infos[1].title.string, @"Another Source"); +} + +@end -- cgit v1.2.1