summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLRendererConfigurationTests.mm
blob: 6483aa35876132c0ed0e8b6f6018092abb4a55af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>
#import "MGLRendererConfiguration.h"

static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"MGLCollisionBehaviorPre4_0";

@interface MGLRendererConfiguration (Tests)
- (instancetype)initWithPropertyDictionary:(nonnull NSDictionary*)bundle;
- (mbgl::optional<std::string>)_localFontFamilyNameWithPropertyDictionary:(nonnull NSDictionary *)properties;
@end


@interface MGLRendererConfigurationTests : XCTestCase
@end

@implementation MGLRendererConfigurationTests
- (void)setUp {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:MGLRendererConfigurationTests_collisionBehaviorKey];
}

- (void)tearDown {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:MGLRendererConfigurationTests_collisionBehaviorKey];
}

// Emulate what would happen with an Info.plist.
- (void)testSettingMGLCollisionBehaviorPre40WithEmptyDictionary
{
    MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{}];
    XCTAssertFalse(config.perSourceCollisions);
}

- (void)testSettingMGLCollisionBehaviorPre40WithYESDictionary
{
    MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{MGLRendererConfigurationTests_collisionBehaviorKey:@(NO)}];
    XCTAssertFalse(config.perSourceCollisions);
}

- (void)testSettingMGLCollisionBehaviorPre40WithNODictionary
{
    MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{MGLRendererConfigurationTests_collisionBehaviorKey:@(YES)}];
    XCTAssert(config.perSourceCollisions);
}

- (void)testSettingMGLCollisionBehaviorPre40InNSUserDefaults {
    {
        XCTAssertNil([[NSUserDefaults standardUserDefaults] objectForKey:MGLRendererConfigurationTests_collisionBehaviorKey]);
        MGLRendererConfiguration *config = [MGLRendererConfiguration currentConfiguration];
        XCTAssertFalse(config.perSourceCollisions);
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:MGLRendererConfigurationTests_collisionBehaviorKey];
    {
        XCTAssertNotNil([[NSUserDefaults standardUserDefaults] objectForKey:MGLRendererConfigurationTests_collisionBehaviorKey]);
        MGLRendererConfiguration *config = [MGLRendererConfiguration currentConfiguration];
        XCTAssertFalse(config.perSourceCollisions);
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:MGLRendererConfigurationTests_collisionBehaviorKey];
    {
        XCTAssertNotNil([[NSUserDefaults standardUserDefaults] objectForKey:MGLRendererConfigurationTests_collisionBehaviorKey]);
        MGLRendererConfiguration *config = [MGLRendererConfiguration currentConfiguration];
        XCTAssert(config.perSourceCollisions);
    }
}

- (void)testSettingMGLCollisionBehaviorPre40PListValueUsingString {
    // Dictionary = "NO"
    {
        MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{MGLRendererConfigurationTests_collisionBehaviorKey:@"NO"}];
        XCTAssertFalse(config.perSourceCollisions);
    }
    
    // Dictionary = "YES"
    {
        MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{MGLRendererConfigurationTests_collisionBehaviorKey:@"YES"}];
        XCTAssert(config.perSourceCollisions);
    }
}

- (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];
        MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{MGLRendererConfigurationTests_collisionBehaviorKey:@(YES)}];
        XCTAssertFalse(config.perSourceCollisions);
    }
}

- (void)testDefaultLocalFontFamilyName {
    
    MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] init];
    std::string localFontFamilyName = config.localFontFamilyName.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, @"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<std::string> 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");
    }
    
    // `MGLIdeographicFontFamilyName` set to an invalid value type: NSDictionary, NSNumber, NSData, etc.
    {
        dic = @{@"MGLIdeographicFontFamilyName": [@"test font 1" dataUsingEncoding:NSUTF8StringEncoding]};
        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 value type");
    }
}



@end