This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
/
FontDiskLoaderTests.m
310 lines (251 loc) · 11.3 KB
/
FontDiskLoaderTests.m
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright 2015-present Google Inc.. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import <XCTest/XCTest.h>
#import "MaterialFontDiskLoader.h"
static const CGFloat kEpsilonAccuracy = 0.001f;
// We use the bundle from MDFRobotoFontLoader to test loading functionality
static NSString *MDFRobotoFontLoaderClassname = @"FontDiskLoaderSimpleExample";
static NSString *MDFRobotoRegularFontName = @"Roboto-Regular";
static NSString *MDFRobotoRegularFontFilename = @"Roboto-Regular.ttf";
static NSString *MDFRobotoBundle = @"CustomFont.bundle";
static NSString *kBlackPixel = @"BlackPixel.png";
/**
For our tests we are following a Given When Then structure as defined in
http://martinfowler.com/bliki/GivenWhenThen.html
The essential idea is to break down writing a scenario (or test) into three sections:
The |given| part describes the state of the world before you begin the behavior you're specifying
in this scenario. You can think of it as the pre-conditions to the test.
The |when| section is that behavior that you're specifying.
Finally the |then| section describes the changes you expect due to the specified behavior.
For us this just means that we have the Given When Then guide posts as comments for each unit test.
*/
@interface MDFFontDiskLoader (Testing)
@property(nonatomic, assign) BOOL disableSanityChecks;
@end
@interface FontDiskLoaderTests : XCTestCase
@end
@implementation FontDiskLoaderTests
- (MDFFontDiskLoader *)validFontLoader {
NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(MDFRobotoFontLoaderClassname)];
return [[MDFFontDiskLoader alloc] initWithFontName:MDFRobotoRegularFontName
filename:MDFRobotoRegularFontFilename
bundleFileName:MDFRobotoBundle
baseBundle:bundle];
}
- (MDFFontDiskLoader *)invalidFontLoader {
NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(MDFRobotoFontLoaderClassname)];
// This attempts to use a png asset instead of a valid font file.
return [[MDFFontDiskLoader alloc] initWithFontName:@"some invalid font name"
filename:kBlackPixel
bundleFileName:MDFRobotoBundle
baseBundle:bundle];
}
- (void)testConvenienceInitCreatesAFontURL {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
// Then
XCTAssertNotNil(loader.fontURL, @"The font URL must not be nil when using the convenience init.");
XCTAssertTrue([[loader.fontURL path] containsString:MDFRobotoRegularFontFilename],
@"The font URL's path must contain %@", MDFRobotoRegularFontFilename);
XCTAssertTrue([[loader.fontURL path] containsString:MDFRobotoBundle],
@"The font URL's path must contain %@", MDFRobotoBundle);
}
- (void)testload {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
// When
[loader load];
// Then
XCTAssertTrue(loader.loaded, @"Loading a valid font must mark its state loaded.");
}
- (void)testUnload {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
[loader load];
XCTAssertTrue(loader.loaded, @"Loading a valid font must mark its state loaded.");
// When
BOOL success = [loader unload];
// Then
XCTAssertFalse(loader.loaded, @"Unloading a loaded font must not mark its state loaded.");
XCTAssertTrue(success, @"Unloading a loaded font must return success.");
}
- (void)testUnloadFailedRegistration {
// Given
MDFFontDiskLoader *loader = [self invalidFontLoader];
[loader load];
// When
[loader unload];
// Then
XCTAssertFalse(loader.loaded,
@"Unloading a previously failed fontloader must not mark it loaded");
XCTAssertFalse(loader.loadFailed,
@"Unloading a previously failed fontloader must reset its loadFailed.");
}
- (void)testUnloadNotloaded {
// Given
MDFFontDiskLoader *loader = [self invalidFontLoader];
// When
[loader unload];
// Then
XCTAssertFalse(loader.loaded,
@"Unloading a fontLoader that was never loaded must not mark it loaded.");
XCTAssertFalse(loader.loadFailed, @"Unloading a fontloader must reset its loadFailed flag.");
}
- (void)testloadFailure {
// Given
MDFFontDiskLoader *loader = [self invalidFontLoader];
loader.disableSanityChecks = YES;
// When
[loader load];
// Then
XCTAssertNil([loader fontOfSize:10],
@"Asking for a font with an invalid font url must not return a UIFont.");
XCTAssertTrue(loader.loadFailed,
@"Asking for a font with an invalid font url must set the loadFailed flag to YES");
}
- (void)testCopy {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
// When
MDFFontDiskLoader *secondFontLoader = [loader copy];
// Then
XCTAssertEqualObjects(secondFontLoader, loader,
@"A copy of a fontloader must equal the original");
}
- (void)testloadedOfSecondFontLoader {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
MDFFontDiskLoader *secondFontLoader =
[[MDFFontDiskLoader alloc] initWithFontName:loader.fontName fontURL:loader.fontURL];
// When
[loader load];
// Then
XCTAssertEqual(secondFontLoader.loaded, loader.loaded, @"Loading the fontloader with the same "
@"url and name must also update the "
@"loaded state of both fontloaders.");
}
- (void)testProvidesACustomFont {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
CGFloat randomSize = arc4random() * 100 / CGFLOAT_MAX;
// When
UIFont *font = [loader fontOfSize:randomSize];
// Then
XCTAssertNotNil(font, @"Asking for a font of any size of a valid font url and name must return a"
@"font.");
XCTAssertEqualWithAccuracy(font.pointSize, randomSize, kEpsilonAccuracy,
@"The font size of the font returned from fontOfSize: must match ");
XCTAssertEqualObjects(font.fontName, MDFRobotoRegularFontName);
}
- (void)testReturnNilWhenTheCustomFontCanNotBeFound {
// Given
MDFFontDiskLoader *validLoader = [self validFontLoader];
MDFFontDiskLoader *loader = [[MDFFontDiskLoader alloc] initWithFontName:@"some invalid font name"
fontURL:validLoader.fontURL];
loader.disableSanityChecks = YES;
CGFloat randomSize = arc4random() * 100 / CGFLOAT_MAX;
// When
UIFont *font = [loader fontOfSize:randomSize];
// Then
XCTAssertNil(font, @"Asking for a font must not return a font when the font name is not found.");
}
- (void)testDescriptionNotloaded {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
[loader unload];
NSString *expected =
[NSString stringWithFormat:@"font name: %@; font url: %@;", loader.fontName, loader.fontURL];
// When
NSString *actual = [loader description];
// Then
XCTAssertTrue([actual hasSuffix:expected],
@"When not loaded the description of the font loader `%@` must end with: `%@`",
actual, expected);
}
- (void)testDescriptionloaded {
// Given
MDFFontDiskLoader *loader = [self validFontLoader];
[loader load];
NSString *expected = [NSString stringWithFormat:@"font name: %@; loaded = YES; font url: %@;",
loader.fontName, loader.fontURL];
UIView *view = [UIView new];
view.userInteractionEnabled = NO;
// When
NSString *actual = [loader description];
// Then
XCTAssertTrue([actual hasSuffix:expected],
@"When loaded the description of the font loader `%@` must end with: %@", actual,
expected);
}
- (void)testDescriptionFailedRegistration {
// Given
MDFFontDiskLoader *loader = [self invalidFontLoader];
[loader load];
NSString *expected = [NSString stringWithFormat:@"font name: %@; failed registration = YES; "
@"font url: %@;",
loader.fontName, loader.fontURL];
UIView *view = [UIView new];
view.userInteractionEnabled = NO;
// When
NSString *actual = [loader description];
// Then
XCTAssertTrue([actual hasSuffix:expected],
@"When load fails the description of the font loader %@ must end with: %@", actual,
expected);
}
- (void)testNotEquals {
// Given
NSString *name = @"some name";
NSString *otherName = @"some other name";
NSURL *url = [NSURL fileURLWithPath:@"some url string"];
NSURL *otherUrl = [NSURL fileURLWithPath:@"some other url string"];
MDFFontDiskLoader *loader = [[MDFFontDiskLoader alloc] initWithFontName:name fontURL:url];
MDFFontDiskLoader *secondLoader =
[[MDFFontDiskLoader alloc] initWithFontName:otherName fontURL:url];
MDFFontDiskLoader *thirdLoader =
[[MDFFontDiskLoader alloc] initWithFontName:name fontURL:otherUrl];
NSObject *object = [[NSObject alloc] init];
// Then
XCTAssertNotEqualObjects(loader, secondLoader,
@"Fontloaders with different font names must not equal eachother.");
XCTAssertNotEqual([loader hash], [secondLoader hash],
@"Fontloaders with different font names must not have equal hashes.");
XCTAssertNotEqualObjects(loader, thirdLoader,
@"Fontloaders with different font urls must not equal eachother.");
XCTAssertNotEqual([loader hash], [secondLoader hash],
@"Fontloaders with different font urls must not have equal hashes.");
XCTAssertNotEqualObjects(secondLoader, thirdLoader,
@"Fontloaders with different font names and different names must not"
@"equal eachother.");
XCTAssertNotEqual([loader hash], [secondLoader hash],
@"Fontloaders with different font names and different names must not have equal"
@"hashes.");
XCTAssertNotEqualObjects(loader, object, @"A fontloader must not equal a object instance.");
XCTAssertNotEqual([loader hash], [object hash],
@"A fontloader must not have the same hash as a object instance.");
XCTAssertNotEqualObjects(loader, nil, @"A font loader must not equal nil");
}
- (void)testEquals {
// Given
NSURL *url = [NSURL fileURLWithPath:@"some url string"];
MDFFontDiskLoader *loader = [[MDFFontDiskLoader alloc] initWithFontName:@"some name" fontURL:url];
MDFFontDiskLoader *secondLoader =
[[MDFFontDiskLoader alloc] initWithFontName:@"some name" fontURL:url];
// Then
XCTAssertEqualObjects(loader, secondLoader,
@"Fontloaders with the same font names and font urls must equal"
@"eachother.");
XCTAssertEqual([loader hash], [secondLoader hash],
@"Fontloaders with the same font name and font urls must have the same hash.");
}
@end