OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/font_warmup_win.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "base/files/file_path.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/sys_byteorder.h" | |
16 #include "base/win/windows_version.h" | |
17 #include "skia/ext/refptr.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "third_party/skia/include/core/SkString.h" | |
20 #include "third_party/skia/include/core/SkTypeface.h" | |
21 #include "third_party/skia/include/ports/SkFontMgr.h" | |
22 | |
23 namespace content { | |
24 | |
25 namespace { | |
26 | |
27 class TestSkTypeface : public SkTypeface { | |
28 public: | |
29 TestSkTypeface(const SkFontStyle& style, | |
30 const char* familyName, | |
31 SkFontTableTag tag, | |
32 const char* data, | |
33 size_t dataLength) | |
34 : SkTypeface(style, 0), | |
35 familyName_(familyName), | |
36 tag_(tag), | |
37 data_(data, data + dataLength) {} | |
38 | |
39 protected: | |
40 SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override { | |
41 ADD_FAILURE(); | |
42 return nullptr; | |
43 } | |
44 void onFilterRec(SkScalerContextRec*) const override { ADD_FAILURE(); } | |
45 SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( | |
46 PerGlyphInfo, | |
47 const uint32_t* glyphIDs, | |
48 uint32_t glyphIDsCount) const override { | |
49 ADD_FAILURE(); | |
50 return nullptr; | |
51 } | |
52 | |
53 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | |
54 ADD_FAILURE(); | |
55 return nullptr; | |
56 } | |
57 | |
58 SkFontData* onCreateFontData() const override { | |
59 ADD_FAILURE(); | |
60 return nullptr; | |
61 } | |
62 | |
63 void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const override { | |
64 ADD_FAILURE(); | |
65 } | |
66 | |
67 int onCharsToGlyphs(const void* chars, | |
68 Encoding, | |
69 uint16_t glyphs[], | |
70 int glyphCount) const override { | |
71 ADD_FAILURE(); | |
72 return 0; | |
73 } | |
74 | |
75 int onCountGlyphs() const override { | |
76 ADD_FAILURE(); | |
77 return 0; | |
78 } | |
79 | |
80 int onGetUPEM() const override { | |
81 ADD_FAILURE(); | |
82 return 0; | |
83 } | |
84 bool onGetKerningPairAdjustments(const uint16_t glyphs[], | |
85 int count, | |
86 int32_t adjustments[]) const override { | |
87 ADD_FAILURE(); | |
88 return false; | |
89 } | |
90 | |
91 void onGetFamilyName(SkString* familyName) const override { | |
92 *familyName = familyName_; | |
93 } | |
94 | |
95 LocalizedStrings* onCreateFamilyNameIterator() const override { | |
96 ADD_FAILURE(); | |
97 return nullptr; | |
98 } | |
99 | |
100 int onGetTableTags(SkFontTableTag tags[]) const override { | |
101 ADD_FAILURE(); | |
102 return 0; | |
103 } | |
104 | |
105 size_t onGetTableData(SkFontTableTag tag, | |
106 size_t offset, | |
107 size_t length, | |
108 void* data) const override { | |
109 size_t retsize = 0; | |
110 if (tag == tag_) { | |
111 retsize = length > data_.size() ? data_.size() : length; | |
112 if (data) | |
113 memcpy(data, &data_[0], retsize); | |
114 } | |
115 return retsize; | |
116 } | |
117 | |
118 bool onComputeBounds(SkRect*) const override { | |
119 ADD_FAILURE(); | |
120 return false; | |
121 } | |
122 | |
123 private: | |
124 SkString familyName_; | |
125 SkFontTableTag tag_; | |
126 std::vector<char> data_; | |
127 }; | |
128 | |
129 const char* kTestFontFamily = "GDITest"; | |
130 const wchar_t* kTestFontFamilyW = L"GDITest"; | |
131 const SkFontTableTag kTestFontTableTag = 0x11223344; | |
132 const char* kTestFontData = "GDITestGDITest"; | |
133 const wchar_t* kTestFontFamilyInvalid = L"InvalidFont"; | |
134 | |
135 class TestSkFontMgr : public SkFontMgr { | |
136 public: | |
137 TestSkFontMgr() { content::SetPreSandboxWarmupFontMgrForTesting(this); } | |
138 ~TestSkFontMgr() override { | |
139 content::SetPreSandboxWarmupFontMgrForTesting(nullptr); | |
140 } | |
141 | |
142 protected: | |
143 int onCountFamilies() const override { return 1; } | |
144 | |
145 void onGetFamilyName(int index, SkString* familyName) const override { | |
146 if (index == 0) | |
147 *familyName = kTestFontFamily; | |
148 } | |
149 | |
150 SkFontStyleSet* onCreateStyleSet(int index) const override { | |
151 ADD_FAILURE(); | |
152 return nullptr; | |
153 } | |
154 | |
155 SkFontStyleSet* onMatchFamily(const char familyName[]) const override { | |
156 ADD_FAILURE(); | |
157 return nullptr; | |
158 } | |
159 | |
160 SkTypeface* onMatchFamilyStyle(const char familyName[], | |
161 const SkFontStyle&) const override { | |
162 if (strcmp(familyName, kTestFontFamily) == 0) | |
163 return createTypeface(); | |
164 return nullptr; | |
165 } | |
166 | |
167 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], | |
168 const SkFontStyle&, | |
169 const char* bcp47[], | |
170 int bcp47Count, | |
171 SkUnichar character) const override { | |
172 ADD_FAILURE(); | |
173 return nullptr; | |
174 } | |
175 | |
176 SkTypeface* onMatchFaceStyle(const SkTypeface*, | |
177 const SkFontStyle&) const override { | |
178 ADD_FAILURE(); | |
179 return nullptr; | |
180 } | |
181 | |
182 SkTypeface* onCreateFromData(SkData*, int ttcIndex) const override { | |
183 ADD_FAILURE(); | |
184 return nullptr; | |
185 } | |
186 | |
187 SkTypeface* onCreateFromStream(SkStreamAsset*, int ttcIndex) const override { | |
188 ADD_FAILURE(); | |
189 return nullptr; | |
190 } | |
191 | |
192 SkTypeface* onCreateFromFontData(SkFontData*) const override { | |
193 ADD_FAILURE(); | |
194 return nullptr; | |
195 } | |
196 | |
197 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { | |
198 ADD_FAILURE(); | |
199 return nullptr; | |
200 } | |
201 | |
202 SkTypeface* onLegacyCreateTypeface(const char familyName[], | |
203 unsigned styleBits) const override { | |
204 ADD_FAILURE(); | |
205 return nullptr; | |
206 } | |
207 | |
208 private: | |
209 SkTypeface* createTypeface() const { | |
210 SkFontStyle style(400, 100, SkFontStyle::kUpright_Slant); | |
211 | |
212 return new TestSkTypeface(style, kTestFontFamily, | |
213 base::ByteSwap(kTestFontTableTag), kTestFontData, | |
214 strlen(kTestFontData)); | |
215 } | |
216 }; | |
217 | |
218 void InitLogFont(LOGFONTW* logfont, const wchar_t* fontname) { | |
219 size_t length = std::min(sizeof(logfont->lfFaceName), | |
220 (wcslen(fontname) + 1) * sizeof(wchar_t)); | |
221 memcpy(logfont->lfFaceName, fontname, length); | |
222 } | |
223 | |
224 content::GdiFontPatchData* SetupTest() { | |
225 HMODULE module_handle; | |
226 if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | |
227 reinterpret_cast<LPCWSTR>(SetupTest), | |
228 &module_handle)) { | |
229 WCHAR module_path[MAX_PATH]; | |
230 | |
231 if (GetModuleFileNameW(module_handle, module_path, MAX_PATH) > 0) { | |
232 base::FilePath path(module_path); | |
233 content::ResetEmulatedGdiHandlesForTesting(); | |
234 return content::PatchGdiFontEnumeration(path); | |
235 } | |
236 } | |
237 return nullptr; | |
238 } | |
239 | |
240 int CALLBACK EnumFontCallbackTest(const LOGFONT* log_font, | |
241 const TEXTMETRIC* text_metric, | |
242 DWORD font_type, | |
243 LPARAM param) { | |
244 const NEWTEXTMETRICEX* new_text_metric = | |
245 reinterpret_cast<const NEWTEXTMETRICEX*>(text_metric); | |
246 | |
247 return !(font_type & TRUETYPE_FONTTYPE) && | |
248 !(new_text_metric->ntmTm.ntmFlags & NTM_PS_OPENTYPE); | |
249 } | |
250 | |
251 } // namespace | |
252 | |
253 TEST(GDIFontEmulationTest, CreateDeleteDCSuccess) { | |
254 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
255 return; | |
256 TestSkFontMgr fontmgr; | |
257 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
258 EXPECT_FALSE(!patch_data); | |
259 | |
260 HDC hdc = CreateCompatibleDC(0); | |
261 EXPECT_NE(hdc, nullptr); | |
262 EXPECT_EQ(1u, GetEmulatedGdiHandleCountForTesting()); | |
263 EXPECT_TRUE(DeleteDC(hdc)); | |
264 EXPECT_EQ(0u, GetEmulatedGdiHandleCountForTesting()); | |
265 } | |
266 | |
267 TEST(GDIFontEmulationTest, CreateUniqueDCSuccess) { | |
268 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
269 return; | |
270 TestSkFontMgr fontmgr; | |
271 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
272 EXPECT_NE(patch_data, nullptr); | |
273 | |
274 HDC hdc1 = CreateCompatibleDC(0); | |
275 EXPECT_NE(hdc1, nullptr); | |
276 HDC hdc2 = CreateCompatibleDC(0); | |
277 EXPECT_NE(hdc2, nullptr); | |
278 EXPECT_NE(hdc1, hdc2); | |
279 EXPECT_TRUE(DeleteDC(hdc2)); | |
280 EXPECT_EQ(1u, GetEmulatedGdiHandleCountForTesting()); | |
281 EXPECT_TRUE(DeleteDC(hdc1)); | |
282 EXPECT_EQ(0u, GetEmulatedGdiHandleCountForTesting()); | |
283 } | |
284 | |
285 TEST(GDIFontEmulationTest, CreateFontSuccess) { | |
286 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
287 return; | |
288 TestSkFontMgr fontmgr; | |
289 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
290 EXPECT_NE(patch_data, nullptr); | |
291 LOGFONTW logfont = {0}; | |
292 InitLogFont(&logfont, kTestFontFamilyW); | |
293 HFONT font = CreateFontIndirectW(&logfont); | |
294 EXPECT_NE(font, nullptr); | |
295 EXPECT_TRUE(DeleteObject(font)); | |
296 EXPECT_EQ(0u, GetEmulatedGdiHandleCountForTesting()); | |
297 } | |
298 | |
299 TEST(GDIFontEmulationTest, CreateFontFailure) { | |
300 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
301 return; | |
302 TestSkFontMgr fontmgr; | |
303 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
304 EXPECT_NE(patch_data, nullptr); | |
305 LOGFONTW logfont = {0}; | |
306 InitLogFont(&logfont, kTestFontFamilyInvalid); | |
307 HFONT font = CreateFontIndirectW(&logfont); | |
308 EXPECT_EQ(font, nullptr); | |
309 } | |
310 | |
311 TEST(GDIFontEmulationTest, EnumFontFamilySuccess) { | |
312 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
313 return; | |
314 TestSkFontMgr fontmgr; | |
315 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
316 EXPECT_NE(patch_data, nullptr); | |
317 HDC hdc = CreateCompatibleDC(0); | |
318 EXPECT_NE(hdc, nullptr); | |
319 LOGFONTW logfont = {0}; | |
320 InitLogFont(&logfont, kTestFontFamilyW); | |
321 int res = EnumFontFamiliesExW(hdc, &logfont, EnumFontCallbackTest, 0, 0); | |
322 EXPECT_FALSE(res); | |
323 EXPECT_TRUE(DeleteDC(hdc)); | |
324 } | |
325 | |
326 TEST(GDIFontEmulationTest, EnumFontFamilyFailure) { | |
327 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
328 return; | |
329 TestSkFontMgr fontmgr; | |
330 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
331 EXPECT_NE(patch_data, nullptr); | |
332 HDC hdc = CreateCompatibleDC(0); | |
333 EXPECT_NE(hdc, nullptr); | |
334 LOGFONTW logfont = {0}; | |
335 InitLogFont(&logfont, kTestFontFamilyInvalid); | |
336 int res = EnumFontFamiliesExW(hdc, &logfont, EnumFontCallbackTest, 0, 0); | |
337 EXPECT_TRUE(res); | |
338 EXPECT_TRUE(DeleteDC(hdc)); | |
339 } | |
340 | |
341 TEST(GDIFontEmulationTest, DeleteDCFailure) { | |
342 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
343 return; | |
344 TestSkFontMgr fontmgr; | |
345 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
346 EXPECT_NE(patch_data, nullptr); | |
347 HDC hdc = reinterpret_cast<HDC>(0x55667788); | |
348 EXPECT_FALSE(DeleteDC(hdc)); | |
349 } | |
350 | |
351 TEST(GDIFontEmulationTest, DeleteObjectFailure) { | |
352 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
353 return; | |
354 TestSkFontMgr fontmgr; | |
355 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
356 EXPECT_NE(patch_data, nullptr); | |
357 HFONT font = reinterpret_cast<HFONT>(0x88aabbcc); | |
358 EXPECT_FALSE(DeleteObject(font)); | |
359 } | |
360 | |
361 TEST(GDIFontEmulationTest, GetFontDataSizeSuccess) { | |
362 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
363 return; | |
364 TestSkFontMgr fontmgr; | |
365 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
366 EXPECT_NE(patch_data, nullptr); | |
367 HDC hdc = CreateCompatibleDC(0); | |
368 EXPECT_NE(hdc, nullptr); | |
369 LOGFONTW logfont = {0}; | |
370 InitLogFont(&logfont, kTestFontFamilyW); | |
371 HFONT font = CreateFontIndirectW(&logfont); | |
372 EXPECT_NE(font, nullptr); | |
373 EXPECT_EQ(SelectObject(hdc, font), nullptr); | |
374 DWORD size = GetFontData(hdc, kTestFontTableTag, 0, nullptr, 0); | |
375 DWORD data_size = static_cast<DWORD>(strlen(kTestFontData)); | |
376 EXPECT_EQ(size, data_size); | |
377 EXPECT_TRUE(DeleteObject(font)); | |
378 EXPECT_TRUE(DeleteDC(hdc)); | |
379 } | |
380 | |
381 TEST(GDIFontEmulationTest, GetFontDataInvalidTagSuccess) { | |
382 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
383 return; | |
384 TestSkFontMgr fontmgr; | |
385 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
386 EXPECT_NE(patch_data, nullptr); | |
387 HDC hdc = CreateCompatibleDC(0); | |
388 EXPECT_NE(hdc, nullptr); | |
389 LOGFONTW logfont = {0}; | |
390 InitLogFont(&logfont, kTestFontFamilyW); | |
391 HFONT font = CreateFontIndirectW(&logfont); | |
392 EXPECT_NE(font, nullptr); | |
393 EXPECT_EQ(SelectObject(hdc, font), nullptr); | |
394 DWORD size = GetFontData(hdc, kTestFontTableTag + 1, 0, nullptr, 0); | |
395 EXPECT_EQ(size, GDI_ERROR); | |
396 EXPECT_TRUE(DeleteObject(font)); | |
397 EXPECT_TRUE(DeleteDC(hdc)); | |
398 } | |
399 | |
400 TEST(GDIFontEmulationTest, GetFontDataInvalidFontSuccess) { | |
401 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
402 return; | |
403 TestSkFontMgr fontmgr; | |
404 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
405 EXPECT_NE(patch_data, nullptr); | |
406 HDC hdc = CreateCompatibleDC(0); | |
407 EXPECT_NE(hdc, nullptr); | |
408 DWORD size = GetFontData(hdc, kTestFontTableTag, 0, nullptr, 0); | |
409 EXPECT_EQ(size, GDI_ERROR); | |
410 EXPECT_TRUE(DeleteDC(hdc)); | |
411 } | |
412 | |
413 TEST(GDIFontEmulationTest, GetFontDataDataSuccess) { | |
414 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
415 return; | |
416 TestSkFontMgr fontmgr; | |
417 scoped_ptr<GdiFontPatchData> patch_data(SetupTest()); | |
418 EXPECT_NE(patch_data, nullptr); | |
419 HDC hdc = CreateCompatibleDC(0); | |
420 EXPECT_NE(hdc, nullptr); | |
421 LOGFONTW logfont = {0}; | |
422 InitLogFont(&logfont, kTestFontFamilyW); | |
423 HFONT font = CreateFontIndirectW(&logfont); | |
424 EXPECT_NE(font, nullptr); | |
425 EXPECT_EQ(SelectObject(hdc, font), nullptr); | |
426 DWORD data_size = static_cast<DWORD>(strlen(kTestFontData)); | |
427 std::vector<char> data(data_size); | |
428 DWORD size = GetFontData(hdc, kTestFontTableTag, 0, &data[0], data.size()); | |
429 EXPECT_EQ(size, data_size); | |
430 EXPECT_EQ(memcmp(&data[0], kTestFontData, data.size()), 0); | |
431 EXPECT_TRUE(DeleteObject(font)); | |
432 EXPECT_TRUE(DeleteDC(hdc)); | |
433 } | |
434 | |
435 } // namespace content | |
OLD | NEW |