forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
skia_font_collection_test.dart
185 lines (158 loc) · 6.42 KB
/
skia_font_collection_test.dart
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart';
void main() {
internalBootstrapBrowserTest(() => testMain);
}
void testMain() {
group('$SkiaFontCollection', () {
final List<String> warnings = <String>[];
late void Function(String) oldPrintWarning;
setUpAll(() async {
ensureFlutterViewEmbedderInitialized();
await renderer.initialize();
oldPrintWarning = printWarning;
printWarning = (String warning) {
warnings.add(warning);
};
});
tearDownAll(() {
printWarning = oldPrintWarning;
});
setUp(() {
mockHttpFetchResponseFactory = null;
warnings.clear();
});
tearDown(() {
mockHttpFetchResponseFactory = null;
});
test('logs no warnings with the default mock asset manager', () async {
final SkiaFontCollection fontCollection = SkiaFontCollection();
final WebOnlyMockAssetManager mockAssetManager =
WebOnlyMockAssetManager();
await fontCollection.downloadAssetFonts(mockAssetManager);
fontCollection.registerDownloadedFonts();
expect(warnings, isEmpty);
});
test('logs a warning if one of the registered fonts is invalid', () async {
mockHttpFetchResponseFactory = (String url) async {
final ByteBuffer bogusData = Uint8List.fromList('this is not valid font data'.codeUnits).buffer;
return MockHttpFetchResponse(
status: 200,
url: url,
contentLength: bogusData.lengthInBytes,
payload: MockHttpFetchPayload(byteBuffer: bogusData),
);
};
final SkiaFontCollection fontCollection = SkiaFontCollection();
final WebOnlyMockAssetManager mockAssetManager =
WebOnlyMockAssetManager();
mockAssetManager.defaultFontManifest = '''
[
{
"family":"Roboto",
"fonts":[{"asset":"/fonts/Roboto-Regular.ttf"}]
},
{
"family": "BrokenFont",
"fonts":[{"asset":"packages/bogus/BrokenFont.ttf"}]
}
]
''';
// It should complete without error, but emit a warning about BrokenFont.
await fontCollection.downloadAssetFonts(mockAssetManager);
fontCollection.registerDownloadedFonts();
expect(
warnings,
containsAllInOrder(
<String>[
'Failed to load font BrokenFont at packages/bogus/BrokenFont.ttf',
'Verify that packages/bogus/BrokenFont.ttf contains a valid font.',
],
),
);
});
test('logs an HTTP warning if one of the registered fonts is missing (404 file not found)', () async {
final SkiaFontCollection fontCollection = SkiaFontCollection();
final WebOnlyMockAssetManager mockAssetManager =
WebOnlyMockAssetManager();
mockAssetManager.defaultFontManifest = '''
[
{
"family":"Roboto",
"fonts":[{"asset":"/fonts/Roboto-Regular.ttf"}]
},
{
"family": "ThisFontDoesNotExist",
"fonts":[{"asset":"packages/bogus/ThisFontDoesNotExist.ttf"}]
}
]
''';
// It should complete without error, but emit a warning about ThisFontDoesNotExist.
await fontCollection.downloadAssetFonts(mockAssetManager);
fontCollection.registerDownloadedFonts();
expect(
warnings,
containsAllInOrder(<String>[
'Failed to load font ThisFontDoesNotExist at packages/bogus/ThisFontDoesNotExist.ttf',
'Flutter Web engine failed to fetch "packages/bogus/ThisFontDoesNotExist.ttf". HTTP request succeeded, but the server responded with HTTP status 404.',
]),
);
});
test('prioritizes Ahem loaded via FontManifest.json', () async {
final SkiaFontCollection fontCollection = SkiaFontCollection();
final WebOnlyMockAssetManager mockAssetManager =
WebOnlyMockAssetManager();
mockAssetManager.defaultFontManifest = '''
[
{
"family":"Ahem",
"fonts":[{"asset":"/assets/fonts/Roboto-Regular.ttf"}]
}
]
'''.trim();
final ByteBuffer robotoData = await httpFetchByteBuffer('/assets/fonts/Roboto-Regular.ttf');
await fontCollection.downloadAssetFonts(mockAssetManager);
await fontCollection.debugDownloadTestFonts();
fontCollection.registerDownloadedFonts();
expect(warnings, isEmpty);
// Use `singleWhere` to make sure only one version of 'Ahem' is loaded.
final RegisteredFont ahem = fontCollection.debugRegisteredFonts!
.singleWhere((RegisteredFont font) => font.family == 'Ahem');
// Check that the contents of 'Ahem' is actually Roboto, because that's
// what's specified in the manifest, and the manifest takes precedence.
expect(ahem.bytes.length, robotoData.lengthInBytes);
});
test('falls back to default Ahem URL', () async {
final SkiaFontCollection fontCollection = SkiaFontCollection();
final WebOnlyMockAssetManager mockAssetManager =
WebOnlyMockAssetManager();
mockAssetManager.defaultFontManifest = '[]';
final ByteBuffer ahemData = await httpFetchByteBuffer('/assets/fonts/ahem.ttf');
await fontCollection.downloadAssetFonts(mockAssetManager);
await fontCollection.debugDownloadTestFonts();
fontCollection.registerDownloadedFonts();
expect(warnings, isEmpty);
// Use `singleWhere` to make sure only one version of 'Ahem' is loaded.
final RegisteredFont ahem = fontCollection.debugRegisteredFonts!
.singleWhere((RegisteredFont font) => font.family == 'Ahem');
// Check that the contents of 'Ahem' is actually Roboto, because that's
// what's specified in the manifest, and the manifest takes precedence.
expect(ahem.bytes.length, ahemData.lengthInBytes);
});
test('download fonts separately from registering', () async {
final SkiaFontCollection fontCollection = SkiaFontCollection();
await fontCollection.debugDownloadTestFonts();
/// Fonts should have been downloaded, but not yet registered
expect(fontCollection.debugRegisteredFonts, isEmpty);
fontCollection.registerDownloadedFonts();
/// Fonts should now be registered and _registeredFonts should be filled
expect(fontCollection.debugRegisteredFonts, isNotEmpty);
expect(warnings, isEmpty);
});
});
}