From 2c4c17243f99527bece85a8a5e66a58d20dc93dd Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 9 Jul 2019 14:36:41 +0800 Subject: [iOS, macOS] add plist value test && filter for invalid string --- platform/darwin/src/MGLRendererConfiguration.mm | 21 +++++- .../darwin/test/MGLRendererConfigurationTests.mm | 81 +++++++++++++++++++++- 2 files changed, 96 insertions(+), 6 deletions(-) diff --git a/platform/darwin/src/MGLRendererConfiguration.mm b/platform/darwin/src/MGLRendererConfiguration.mm index 9fde9cca9a..754985b113 100644 --- a/platform/darwin/src/MGLRendererConfiguration.mm +++ b/platform/darwin/src/MGLRendererConfiguration.mm @@ -9,6 +9,7 @@ #endif static NSString * const MGLCollisionBehaviorPre4_0Key = @"MGLCollisionBehaviorPre4_0"; +static NSString * const MGLIdeographicFontFamilyNameKey = @"MGLIdeographicFontFamilyName"; @interface MGLRendererConfiguration () @property (nonatomic, readwrite) BOOL perSourceCollisions; @@ -70,6 +71,10 @@ static NSString * const MGLCollisionBehaviorPre4_0Key = @"MGLCollisionBehaviorPr } - (mbgl::optional)localFontFamilyName { + return [self _localFontFamilyNameWithPropertyDictionary:[[NSBundle mainBundle]infoDictionary]]; +} + +- (mbgl::optional)_localFontFamilyNameWithPropertyDictionary:(nonnull NSDictionary *)properties { std::string systemFontFamilyName; #if TARGET_OS_IPHONE @@ -77,8 +82,8 @@ static NSString * const MGLCollisionBehaviorPre4_0Key = @"MGLCollisionBehaviorPr #else systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]); #endif - - id fontFamilyName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLIdeographicFontFamilyName"]; + + id fontFamilyName = properties[MGLIdeographicFontFamilyNameKey]; if([fontFamilyName isKindOfClass:[NSNumber class]] && ![fontFamilyName boolValue]) { @@ -86,7 +91,17 @@ static NSString * const MGLCollisionBehaviorPre4_0Key = @"MGLCollisionBehaviorPr } else if([fontFamilyName isKindOfClass:[NSString class]]) { - return fontFamilyName ? std::string([fontFamilyName UTF8String]) : systemFontFamilyName; + BOOL isValidFont = NO; +#if TARGET_OS_IPHONE + if([[UIFont familyNames] containsObject:fontFamilyName]){ + isValidFont = YES; + } +#else + if([[[NSFontManager sharedFontManager] availableFontFamilies] containsObject:fontFamilyName]){ + isValidFont = YES; + } +#endif + return (fontFamilyName && isValidFont) ? std::string([fontFamilyName UTF8String]) : systemFontFamilyName; } // Ability to specify an array of fonts for fallbacks for `localIdeographicFontFamily` else if ([fontFamilyName isKindOfClass:[NSArray class]]){ diff --git a/platform/darwin/test/MGLRendererConfigurationTests.mm b/platform/darwin/test/MGLRendererConfigurationTests.mm index 52401deee1..c06d0a6f64 100644 --- a/platform/darwin/test/MGLRendererConfigurationTests.mm +++ b/platform/darwin/test/MGLRendererConfigurationTests.mm @@ -6,6 +6,7 @@ static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"M @interface MGLRendererConfiguration (Tests) - (instancetype)initWithPropertyDictionary:(nonnull NSDictionary*)bundle; +- (mbgl::optional)_localFontFamilyNameWithPropertyDictionary:(nonnull NSDictionary *)properties; @end @@ -77,14 +78,14 @@ static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"M } - (void)testOverridingMGLCollisionBehaviorPre40 { - + // Dictionary = NO, NSUserDefaults = YES { [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:MGLRendererConfigurationTests_collisionBehaviorKey]; MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{MGLRendererConfigurationTests_collisionBehaviorKey:@(NO)}]; XCTAssert(config.perSourceCollisions); } - + // Dictionary = YES, NSUserDefaults = NO { [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:MGLRendererConfigurationTests_collisionBehaviorKey]; @@ -93,7 +94,8 @@ static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"M } } -- (void)testDefaultLocalFontFamilyName{ +- (void)testDefaultLocalFontFamilyName { + MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] init]; std::string localFontFamilyName = config.localFontFamilyName.value(); @@ -107,4 +109,77 @@ static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"M XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Default local font family name should match default system font"); } +- (void)testSettingMGLIdeographicFontFamilyNameWithPlistValue { + + MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] init]; + NSDictionary *dic; + + // `MGLIdeographicFontFamilyName` set to bool value `YES` + { + dic = @{@"MGLIdeographicFontFamilyName": @(YES)}; + std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value(); + + std::string systemFontFamilyName; +#if TARGET_OS_IPHONE + systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]); +#else + systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]); +#endif + XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Local font family name should match default system font name when setting `YES`"); + } + + // `MGLIdeographicFontFamilyName` set to bool value `NO` + { + dic = @{@"MGLIdeographicFontFamilyName": @(NO)}; + mbgl::optional localFontFamilyName = [config _localFontFamilyNameWithPropertyDictionary:dic]; + XCTAssertFalse(localFontFamilyName.has_value(), @"Client rendering font should use remote font when setting `NO`"); + } + + // `MGLIdeographicFontFamilyName` set to a valid font string value + { + dic = @{@"MGLIdeographicFontFamilyName": @"PingFang TC"}; + std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value(); + std::string targetFontFamilyName = std::string([@"PingFang TC" UTF8String]); + XCTAssertEqual(localFontFamilyName, targetFontFamilyName, @"Local font family name should match a custom valid font name"); + } + + // `MGLIdeographicFontFamilyName` set to an invalid font string value + { + dic = @{@"MGLIdeographicFontFamilyName": @"test font"}; + std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value(); + + std::string systemFontFamilyName; +#if TARGET_OS_IPHONE + systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]); +#else + systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]); +#endif + XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Local font family name should match default system font name when setting an invalid font string"); + } + + // `MGLIdeographicFontFamilyName` set to a valid font family names array value + { + dic = @{@"MGLIdeographicFontFamilyName": @[@"test font 1", @"PingFang TC", @"test font 2"]}; + std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value(); + std::string targetFontFamilyName = std::string([@"PingFang TC" UTF8String]); + XCTAssertEqual(localFontFamilyName, targetFontFamilyName, @"Local font family name should match a custom valid font name in a font family names array"); + } + + // `MGLIdeographicFontFamilyName` set to an invalid font family names array value + { + dic = @{@"MGLIdeographicFontFamilyName": @[@"test font 1", @"test font 2", @"test font 3"]}; + std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value(); + + std::string systemFontFamilyName; +#if TARGET_OS_IPHONE + systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]); +#else + systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]); +#endif + XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Local font family name should match default system font name when setting an invalid font family names array"); + } +} + + + @end -- cgit v1.2.1