forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont_warmup_win_unittest.cc
432 lines (376 loc) · 13 KB
/
font_warmup_win_unittest.cc
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/child/font_warmup_win.h"
#include <stddef.h>
#include <stdint.h>
#include <windows.h>
#include <memory>
#include <vector>
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/sys_byteorder.h"
#include "base/win/windows_version.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkString.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "third_party/skia/include/ports/SkFontMgr.h"
namespace content {
namespace {
class TestSkTypeface : public SkTypeface {
public:
TestSkTypeface(const SkFontStyle& style,
const char* familyName,
SkFontTableTag tag,
const char* data,
size_t dataLength)
: SkTypeface(style, 0),
familyName_(familyName),
tag_(tag),
data_(data, data + dataLength) {}
protected:
SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
const SkDescriptor*) const override {
ADD_FAILURE();
return nullptr;
}
void onFilterRec(SkScalerContextRec*) const override { ADD_FAILURE(); }
SkStreamAsset* onOpenStream(int* ttcIndex) const override {
ADD_FAILURE();
return nullptr;
}
int onGetVariationDesignPosition(
SkFontArguments::VariationPosition::Coordinate coordinates[],
int coordinateCount) const override {
ADD_FAILURE();
return -1;
}
void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const override {
ADD_FAILURE();
}
int onCharsToGlyphs(const void* chars,
Encoding,
uint16_t glyphs[],
int glyphCount) const override {
ADD_FAILURE();
return 0;
}
int onCountGlyphs() const override {
ADD_FAILURE();
return 0;
}
int onGetUPEM() const override {
ADD_FAILURE();
return 0;
}
bool onGetKerningPairAdjustments(const uint16_t glyphs[],
int count,
int32_t adjustments[]) const override {
ADD_FAILURE();
return false;
}
void onGetFamilyName(SkString* familyName) const override {
*familyName = familyName_;
}
LocalizedStrings* onCreateFamilyNameIterator() const override {
ADD_FAILURE();
return nullptr;
}
int onGetTableTags(SkFontTableTag tags[]) const override {
ADD_FAILURE();
return 0;
}
size_t onGetTableData(SkFontTableTag tag,
size_t offset,
size_t length,
void* data) const override {
size_t retsize = 0;
if (tag == tag_) {
retsize = length > data_.size() ? data_.size() : length;
if (data)
memcpy(data, &data_[0], retsize);
}
return retsize;
}
bool onComputeBounds(SkRect*) const override {
ADD_FAILURE();
return false;
}
private:
SkString familyName_;
SkFontTableTag tag_;
std::vector<char> data_;
};
const char* kTestFontFamily = "GDITest";
const wchar_t* kTestFontFamilyW = L"GDITest";
const SkFontTableTag kTestFontTableTag = 0x11223344;
const char* kTestFontData = "GDITestGDITest";
const wchar_t* kTestFontFamilyInvalid = L"InvalidFont";
class TestSkFontMgr : public SkFontMgr {
public:
TestSkFontMgr() {
content::SetPreSandboxWarmupFontMgrForTesting(sk_ref_sp(this));
}
~TestSkFontMgr() override {
content::SetPreSandboxWarmupFontMgrForTesting(nullptr);
}
protected:
int onCountFamilies() const override { return 1; }
void onGetFamilyName(int index, SkString* familyName) const override {
if (index == 0)
*familyName = kTestFontFamily;
}
SkFontStyleSet* onCreateStyleSet(int index) const override {
ADD_FAILURE();
return nullptr;
}
SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
ADD_FAILURE();
return nullptr;
}
SkTypeface* onMatchFamilyStyle(const char familyName[],
const SkFontStyle&) const override {
if (strcmp(familyName, kTestFontFamily) == 0)
return createTypeface();
return nullptr;
}
SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
const SkFontStyle&,
const char* bcp47[],
int bcp47Count,
SkUnichar character) const override {
ADD_FAILURE();
return nullptr;
}
SkTypeface* onMatchFaceStyle(const SkTypeface*,
const SkFontStyle&) const override {
ADD_FAILURE();
return nullptr;
}
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override {
ADD_FAILURE();
return nullptr;
}
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
int ttcIndex) const override {
ADD_FAILURE();
return nullptr;
}
sk_sp<SkTypeface> onMakeFromFile(const char path[],
int ttcIndex) const override {
ADD_FAILURE();
return nullptr;
}
sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
SkFontStyle style) const override {
ADD_FAILURE();
return nullptr;
}
private:
SkTypeface* createTypeface() const {
SkFontStyle style(400, 100, SkFontStyle::kUpright_Slant);
return new TestSkTypeface(style, kTestFontFamily,
base::ByteSwap(kTestFontTableTag), kTestFontData,
strlen(kTestFontData));
}
};
void InitLogFont(LOGFONTW* logfont, const wchar_t* fontname) {
size_t length = std::min(sizeof(logfont->lfFaceName),
(wcslen(fontname) + 1) * sizeof(wchar_t));
memcpy(logfont->lfFaceName, fontname, length);
}
content::GdiFontPatchData* SetupTest() {
HMODULE module_handle;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<LPCWSTR>(SetupTest),
&module_handle)) {
WCHAR module_path[MAX_PATH];
if (GetModuleFileNameW(module_handle, module_path, MAX_PATH) > 0) {
base::FilePath path(module_path);
content::ResetEmulatedGdiHandlesForTesting();
return content::PatchGdiFontEnumeration(path);
}
}
return nullptr;
}
int CALLBACK EnumFontCallbackTest(const LOGFONT* log_font,
const TEXTMETRIC* text_metric,
DWORD font_type,
LPARAM param) {
const NEWTEXTMETRICEX* new_text_metric =
reinterpret_cast<const NEWTEXTMETRICEX*>(text_metric);
return !(font_type & TRUETYPE_FONTTYPE) &&
!(new_text_metric->ntmTm.ntmFlags & NTM_PS_OPENTYPE);
}
} // namespace
TEST(GDIFontEmulationTest, CreateDeleteDCSuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_FALSE(!patch_data);
HDC hdc = CreateCompatibleDC(0);
EXPECT_NE(hdc, nullptr);
EXPECT_EQ(1u, GetEmulatedGdiHandleCountForTesting());
EXPECT_TRUE(DeleteDC(hdc));
EXPECT_EQ(0u, GetEmulatedGdiHandleCountForTesting());
}
TEST(GDIFontEmulationTest, CreateUniqueDCSuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc1 = CreateCompatibleDC(0);
EXPECT_NE(hdc1, nullptr);
HDC hdc2 = CreateCompatibleDC(0);
EXPECT_NE(hdc2, nullptr);
EXPECT_NE(hdc1, hdc2);
EXPECT_TRUE(DeleteDC(hdc2));
EXPECT_EQ(1u, GetEmulatedGdiHandleCountForTesting());
EXPECT_TRUE(DeleteDC(hdc1));
EXPECT_EQ(0u, GetEmulatedGdiHandleCountForTesting());
}
TEST(GDIFontEmulationTest, CreateFontSuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
LOGFONTW logfont = {0};
InitLogFont(&logfont, kTestFontFamilyW);
HFONT font = CreateFontIndirectW(&logfont);
EXPECT_NE(font, nullptr);
EXPECT_TRUE(DeleteObject(font));
EXPECT_EQ(0u, GetEmulatedGdiHandleCountForTesting());
}
TEST(GDIFontEmulationTest, CreateFontFailure) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
LOGFONTW logfont = {0};
InitLogFont(&logfont, kTestFontFamilyInvalid);
HFONT font = CreateFontIndirectW(&logfont);
EXPECT_EQ(font, nullptr);
}
TEST(GDIFontEmulationTest, EnumFontFamilySuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc = CreateCompatibleDC(0);
EXPECT_NE(hdc, nullptr);
LOGFONTW logfont = {0};
InitLogFont(&logfont, kTestFontFamilyW);
int res = EnumFontFamiliesExW(hdc, &logfont, EnumFontCallbackTest, 0, 0);
EXPECT_FALSE(res);
EXPECT_TRUE(DeleteDC(hdc));
}
TEST(GDIFontEmulationTest, EnumFontFamilyFailure) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc = CreateCompatibleDC(0);
EXPECT_NE(hdc, nullptr);
LOGFONTW logfont = {0};
InitLogFont(&logfont, kTestFontFamilyInvalid);
int res = EnumFontFamiliesExW(hdc, &logfont, EnumFontCallbackTest, 0, 0);
EXPECT_TRUE(res);
EXPECT_TRUE(DeleteDC(hdc));
}
TEST(GDIFontEmulationTest, DeleteDCFailure) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc = reinterpret_cast<HDC>(0x55667788);
EXPECT_FALSE(DeleteDC(hdc));
}
TEST(GDIFontEmulationTest, DeleteObjectFailure) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HFONT font = reinterpret_cast<HFONT>(0x88aabbcc);
EXPECT_FALSE(DeleteObject(font));
}
TEST(GDIFontEmulationTest, GetFontDataSizeSuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc = CreateCompatibleDC(0);
EXPECT_NE(hdc, nullptr);
LOGFONTW logfont = {0};
InitLogFont(&logfont, kTestFontFamilyW);
HFONT font = CreateFontIndirectW(&logfont);
EXPECT_NE(font, nullptr);
EXPECT_EQ(SelectObject(hdc, font), nullptr);
DWORD size = GetFontData(hdc, kTestFontTableTag, 0, nullptr, 0);
DWORD data_size = static_cast<DWORD>(strlen(kTestFontData));
EXPECT_EQ(size, data_size);
EXPECT_TRUE(DeleteObject(font));
EXPECT_TRUE(DeleteDC(hdc));
}
TEST(GDIFontEmulationTest, GetFontDataInvalidTagSuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc = CreateCompatibleDC(0);
EXPECT_NE(hdc, nullptr);
LOGFONTW logfont = {0};
InitLogFont(&logfont, kTestFontFamilyW);
HFONT font = CreateFontIndirectW(&logfont);
EXPECT_NE(font, nullptr);
EXPECT_EQ(SelectObject(hdc, font), nullptr);
DWORD size = GetFontData(hdc, kTestFontTableTag + 1, 0, nullptr, 0);
EXPECT_EQ(size, GDI_ERROR);
EXPECT_TRUE(DeleteObject(font));
EXPECT_TRUE(DeleteDC(hdc));
}
TEST(GDIFontEmulationTest, GetFontDataInvalidFontSuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc = CreateCompatibleDC(0);
EXPECT_NE(hdc, nullptr);
DWORD size = GetFontData(hdc, kTestFontTableTag, 0, nullptr, 0);
EXPECT_EQ(size, GDI_ERROR);
EXPECT_TRUE(DeleteDC(hdc));
}
TEST(GDIFontEmulationTest, GetFontDataDataSuccess) {
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return;
TestSkFontMgr fontmgr;
std::unique_ptr<GdiFontPatchData> patch_data(SetupTest());
EXPECT_NE(patch_data, nullptr);
HDC hdc = CreateCompatibleDC(0);
EXPECT_NE(hdc, nullptr);
LOGFONTW logfont = {0};
InitLogFont(&logfont, kTestFontFamilyW);
HFONT font = CreateFontIndirectW(&logfont);
EXPECT_NE(font, nullptr);
EXPECT_EQ(SelectObject(hdc, font), nullptr);
DWORD data_size = static_cast<DWORD>(strlen(kTestFontData));
std::vector<char> data(data_size);
DWORD size = GetFontData(hdc, kTestFontTableTag, 0, &data[0], data.size());
EXPECT_EQ(size, data_size);
EXPECT_EQ(memcmp(&data[0], kTestFontData, data.size()), 0);
EXPECT_TRUE(DeleteObject(font));
EXPECT_TRUE(DeleteDC(hdc));
}
} // namespace content