Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: content/child/dwrite_font_proxy/dwrite_font_proxy_win_unittest.cc

Issue 1438603002: Create direct write font proxy classes and unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix bug that would not load the system font path in release. Also minor formatting tweaks and refac… Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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/child/dwrite_font_proxy/dwrite_font_proxy_win.h"
6
7 #include <dwrite.h>
8 #include <shlobj.h>
9 #include <wrl.h>
10
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/common/dwrite_font_proxy_messages.h"
14 #include "content/common/view_messages.h"
15 #include "content/test/dwrite_font_fake_sender_win.h"
16 #include "ipc/ipc_message_macros.h"
17 #include "ipc/ipc_sender.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace mswr = Microsoft::WRL;
21
22 namespace content {
23
24 void CreateDWriteFactory(IUnknown** factory) {
25 using DWriteCreateFactoryProc = decltype(DWriteCreateFactory)*;
26 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll");
27 if (!dwrite_dll)
28 return;
29
30 DWriteCreateFactoryProc dwrite_create_factory_proc =
31 reinterpret_cast<DWriteCreateFactoryProc>(
32 GetProcAddress(dwrite_dll, "DWriteCreateFactory"));
33 if (!dwrite_create_factory_proc)
34 return;
35
36 dwrite_create_factory_proc(DWRITE_FACTORY_TYPE_SHARED,
37 __uuidof(IDWriteFactory), factory);
38 }
39
40 class DWriteFontProxyUnitTest : public testing::Test {
41 public:
42 DWriteFontProxyUnitTest() {
43 if (!factory)
44 return;
45 fake_collection_ = new FakeFontCollection();
46 SetupFonts(fake_collection_.get());
47 mswr::MakeAndInitialize<DWriteFontCollectionProxy>(
48 &collection_, factory.Get(),
49 base::Bind(&FakeFontCollection::GetTrackingSender, fake_collection_));
50 }
51
52 ~DWriteFontProxyUnitTest() { collection_->Unregister(); }
53
54 static void SetupFonts(FakeFontCollection* fonts) {
55 fonts->AddFont(L"Aardvark")
56 .AddFamilyName(L"en-us", L"Aardvark")
57 .AddFamilyName(L"de-de", L"Erdferkel")
58 .AddFilePath(L"X:\\Nonexistent\\Folder\\Aardvark.ttf");
59 FakeFont& arial =
60 fonts->AddFont(L"Arial").AddFamilyName(L"en-us", L"Arial");
61 for (auto& path : arial_font_files)
62 arial.AddFilePath(path);
63 fonts->AddFont(L"Times New Roman")
64 .AddFamilyName(L"en-us", L"Times New Roman")
65 .AddFilePath(L"X:\\Nonexistent\\Folder\\Times.ttf");
66 }
67
68 static void SetUpTestCase() {
69 CreateDWriteFactory(&factory);
70
71 std::vector<base::char16> font_path;
72 font_path.resize(MAX_PATH);
73 SHGetSpecialFolderPath(NULL /* hwndOwner - reserved */, font_path.data(),
74 CSIDL_FONTS, FALSE /* fCreate*/);
75 base::string16 arial;
76 arial.append(font_path.data()).append(L"\\arial.ttf");
77 base::string16 arialbd;
78 arialbd.append(font_path.data()).append(L"\\arialbd.ttf");
79 arial_font_files.push_back(arial);
80 arial_font_files.push_back(arialbd);
81 }
82
83 protected:
84 scoped_refptr<FakeFontCollection> fake_collection_;
85 mswr::ComPtr<DWriteFontCollectionProxy> collection_;
86
87 static std::vector<base::string16> arial_font_files;
88 static mswr::ComPtr<IDWriteFactory> factory;
89 };
90 std::vector<base::string16> DWriteFontProxyUnitTest::arial_font_files;
91 mswr::ComPtr<IDWriteFactory> DWriteFontProxyUnitTest::factory;
92
93 TEST_F(DWriteFontProxyUnitTest, GetFontFamilyCount) {
94 if (!factory)
95 return;
96
97 uint32 family_count = collection_->GetFontFamilyCount();
98
99 EXPECT_EQ(3, family_count);
100 ASSERT_EQ(1, fake_collection_->MessageCount());
101 EXPECT_EQ(DWriteFontProxyMsg_GetFamilyCount::ID,
102 fake_collection_->GetMessage(0)->type());
103
104 // Calling again should not cause another message to be sent.
105 family_count = collection_->GetFontFamilyCount();
106 EXPECT_EQ(3, family_count);
107 ASSERT_EQ(1, fake_collection_->MessageCount());
108 }
109
110 TEST_F(DWriteFontProxyUnitTest, FindFamilyNameShouldFindFamily) {
111 HRESULT hr;
112 if (!factory)
113 return;
114
115 uint32 index = UINT_MAX;
116 BOOL exists = FALSE;
117 hr = collection_->FindFamilyName(L"Arial", &index, &exists);
118
119 EXPECT_EQ(S_OK, hr);
120 EXPECT_EQ(1, index);
121 EXPECT_TRUE(exists);
122 ASSERT_EQ(2, fake_collection_->MessageCount());
123 EXPECT_EQ(DWriteFontProxyMsg_FindFamily::ID,
124 fake_collection_->GetMessage(0)->type());
125 EXPECT_EQ(DWriteFontProxyMsg_GetFamilyCount::ID,
126 fake_collection_->GetMessage(1)->type());
127 }
128
129 TEST_F(DWriteFontProxyUnitTest, FindFamilyNameShouldReturnUINTMAXWhenNotFound) {
130 HRESULT hr;
131 if (!factory)
132 return;
133
134 uint32 index = UINT_MAX;
135 BOOL exists = FALSE;
136 hr = collection_->FindFamilyName(L"Not a font", &index, &exists);
137
138 EXPECT_EQ(S_OK, hr);
139 EXPECT_EQ(UINT32_MAX, index);
140 EXPECT_FALSE(exists);
141 ASSERT_EQ(1, fake_collection_->MessageCount());
142 EXPECT_EQ(DWriteFontProxyMsg_FindFamily::ID,
143 fake_collection_->GetMessage(0)->type());
144 }
145
146 TEST_F(DWriteFontProxyUnitTest, FindFamilyNameShouldNotSendDuplicateIPC) {
147 HRESULT hr;
148 if (!factory)
149 return;
150
151 uint32 index = UINT_MAX;
152 BOOL exists = FALSE;
153 hr = collection_->FindFamilyName(L"Arial", &index, &exists);
154 ASSERT_EQ(S_OK, hr);
155 ASSERT_EQ(2, fake_collection_->MessageCount());
156
157 hr = collection_->FindFamilyName(L"Arial", &index, &exists);
158
159 EXPECT_EQ(S_OK, hr);
160 EXPECT_EQ(2, fake_collection_->MessageCount());
161 }
162
163 TEST_F(DWriteFontProxyUnitTest, GetFontFamilyShouldCreateFamily) {
164 HRESULT hr;
165 if (!factory)
166 return;
167
168 uint32 index = UINT_MAX;
169 BOOL exists = FALSE;
170 hr = collection_->FindFamilyName(L"Arial", &index, &exists);
171 ASSERT_EQ(2, fake_collection_->MessageCount());
172
173 mswr::ComPtr<IDWriteFontFamily> family;
174 hr = collection_->GetFontFamily(2, &family);
175
176 EXPECT_EQ(S_OK, hr);
177 EXPECT_EQ(2, fake_collection_->MessageCount());
178 EXPECT_NE(nullptr, family.Get());
179 }
180
181 void CheckLocale(IDWriteLocalizedStrings* strings,
Alexei Svitkine (slow) 2015/11/24 16:42:36 Non-const params should be last. Also, should thi
Ilya Kulshin 2015/11/25 02:40:48 Done.
182 const base::string16& locale_name,
183 const base::string16& expected_value) {
184 UINT32 locale_index = 0;
185 BOOL locale_exists = FALSE;
186 strings->FindLocaleName(locale_name.data(), &locale_index, &locale_exists);
187 EXPECT_TRUE(locale_exists);
188
189 UINT32 name_length = 0;
190 strings->GetLocaleNameLength(locale_index, &name_length);
191 EXPECT_EQ(locale_name.size(), name_length);
192 base::string16 actual_name;
193 name_length++;
194 actual_name.resize(name_length);
195 strings->GetLocaleName(locale_index, const_cast<WCHAR*>(actual_name.data()),
196 name_length);
197 EXPECT_STREQ(locale_name.c_str(), actual_name.c_str());
198
199 UINT32 string_length = 0;
200 strings->GetStringLength(locale_index, &string_length);
201 EXPECT_EQ(expected_value.size(), string_length);
202 base::string16 actual_value;
203 string_length++;
204 actual_value.resize(string_length);
205 strings->GetString(locale_index, const_cast<WCHAR*>(actual_value.data()),
206 string_length);
207 EXPECT_STREQ(expected_value.c_str(), actual_value.c_str());
208 }
209
210 TEST_F(DWriteFontProxyUnitTest, GetFamilyNames) {
211 HRESULT hr;
212 if (!factory)
213 return;
214
215 uint32 index = UINT_MAX;
216 BOOL exists = FALSE;
217 hr = collection_->FindFamilyName(L"Aardvark", &index, &exists);
218 ASSERT_EQ(2, fake_collection_->MessageCount());
219
220 mswr::ComPtr<IDWriteFontFamily> family;
221 hr = collection_->GetFontFamily(index, &family);
222 EXPECT_EQ(S_OK, hr);
223 EXPECT_EQ(2, fake_collection_->MessageCount());
224
225 mswr::ComPtr<IDWriteLocalizedStrings> names;
226 hr = family->GetFamilyNames(&names);
227 EXPECT_EQ(S_OK, hr);
228 EXPECT_EQ(3, fake_collection_->MessageCount());
229 EXPECT_EQ(DWriteFontProxyMsg_GetFamilyNames::ID,
230 fake_collection_->GetMessage(2)->type());
231
232 EXPECT_EQ(2, names->GetCount());
233 UINT32 locale_index = 0;
234 BOOL locale_exists = FALSE;
235 hr = names->FindLocaleName(L"fr-fr", &locale_index, &locale_exists);
236 EXPECT_EQ(S_OK, hr);
237 EXPECT_FALSE(locale_exists);
238
239 CheckLocale(names.Get(), L"en-us", L"Aardvark");
240 CheckLocale(names.Get(), L"de-de", L"Erdferkel");
241
242 base::string16 unused;
243 unused.resize(25);
244 hr = names->GetLocaleName(15234, const_cast<WCHAR*>(unused.data()),
245 unused.size() - 1);
246 EXPECT_FALSE(SUCCEEDED(hr));
247 }
248
249 TEST_F(DWriteFontProxyUnitTest, GetFontCollection) {
250 HRESULT hr;
251 if (!factory)
252 return;
253
254 uint32 index = UINT_MAX;
255 BOOL exists = FALSE;
256 hr = collection_->FindFamilyName(L"Arial", &index, &exists);
257 ASSERT_EQ(2, fake_collection_->MessageCount());
258
259 mswr::ComPtr<IDWriteFontFamily> family;
260 hr = collection_->GetFontFamily(2, &family);
261 EXPECT_EQ(S_OK, hr);
262 EXPECT_EQ(2, fake_collection_->MessageCount());
263
264 mswr::ComPtr<IDWriteFontCollection> returned_collection;
265 hr = family->GetFontCollection(&returned_collection);
266 EXPECT_EQ(S_OK, hr);
267 EXPECT_EQ(2, fake_collection_->MessageCount());
268 EXPECT_EQ(collection_.Get(), returned_collection.Get());
269 }
270
271 TEST_F(DWriteFontProxyUnitTest, GetFamilyNamesShouldNotIPCAfterLoadingFamily) {
272 HRESULT hr;
273 if (!factory)
274 return;
275
276 uint32 index = UINT_MAX;
277 BOOL exists = FALSE;
278 collection_->FindFamilyName(L"Arial", &index, &exists);
279 mswr::ComPtr<IDWriteFontFamily> family;
280 collection_->GetFontFamily(index, &family);
281 family->GetFontCount();
282 EXPECT_EQ(fake_collection_->MessageCount(), 3);
283
284 mswr::ComPtr<IDWriteLocalizedStrings> names;
285 hr = family->GetFamilyNames(&names);
286 EXPECT_EQ(S_OK, hr);
287 EXPECT_EQ(3, fake_collection_->MessageCount());
288 }
289
290 TEST_F(DWriteFontProxyUnitTest,
291 GetFontFamilyShouldNotCreateFamilyWhenIndexIsInvalid) {
292 HRESULT hr;
293 if (!factory)
294 return;
295
296 uint32 index = UINT_MAX;
297 BOOL exists = FALSE;
298 hr = collection_->FindFamilyName(L"Arial", &index, &exists);
299 ASSERT_EQ(2, fake_collection_->MessageCount());
300
301 mswr::ComPtr<IDWriteFontFamily> family;
302 hr = collection_->GetFontFamily(1654, &family);
303
304 EXPECT_FALSE(SUCCEEDED(hr));
305 EXPECT_EQ(2, fake_collection_->MessageCount());
306 }
307
308 TEST_F(DWriteFontProxyUnitTest, LoadingFontFamily) {
309 HRESULT hr;
310 if (!factory)
311 return;
312
313 uint32 index = UINT_MAX;
314 BOOL exists = FALSE;
315 collection_->FindFamilyName(L"Arial", &index, &exists);
316 mswr::ComPtr<IDWriteFontFamily> family;
317 collection_->GetFontFamily(index, &family);
318 ASSERT_EQ(2, fake_collection_->MessageCount());
319
320 UINT32 font_count = family->GetFontCount();
321 EXPECT_LT(0u, font_count);
322 EXPECT_EQ(3, fake_collection_->MessageCount());
323 EXPECT_EQ(DWriteFontProxyMsg_GetFontFiles::ID,
324 fake_collection_->GetMessage(2)->type());
325 mswr::ComPtr<IDWriteFont> font;
326 hr = family->GetFirstMatchingFont(DWRITE_FONT_WEIGHT_NORMAL,
327 DWRITE_FONT_STRETCH_NORMAL,
328 DWRITE_FONT_STYLE_NORMAL, &font);
329 EXPECT_EQ(S_OK, hr);
330 EXPECT_EQ(3, fake_collection_->MessageCount());
331 mswr::ComPtr<IDWriteFont> font2;
332 hr = family->GetFont(0, &font2);
333 EXPECT_EQ(S_OK, hr);
334 EXPECT_EQ(3, fake_collection_->MessageCount());
335 mswr::ComPtr<IDWriteFontList> matching_fonts;
336 hr = family->GetMatchingFonts(DWRITE_FONT_WEIGHT_NORMAL,
337 DWRITE_FONT_STRETCH_NORMAL,
338 DWRITE_FONT_STYLE_NORMAL, &matching_fonts);
339 EXPECT_EQ(S_OK, hr);
340 EXPECT_EQ(3, fake_collection_->MessageCount());
341 EXPECT_NE(nullptr, matching_fonts.Get());
342 }
343
344 TEST_F(DWriteFontProxyUnitTest, GetFontFromFontFaceShouldFindFont) {
345 HRESULT hr;
346 if (!factory)
347 return;
348
349 uint32 index = UINT_MAX;
350 BOOL exists = FALSE;
351 collection_->FindFamilyName(L"Arial", &index, &exists);
352 mswr::ComPtr<IDWriteFontFamily> family;
353 collection_->GetFontFamily(index, &family);
354
355 mswr::ComPtr<IDWriteFont> font;
356 family->GetFirstMatchingFont(DWRITE_FONT_WEIGHT_NORMAL,
357 DWRITE_FONT_STRETCH_NORMAL,
358 DWRITE_FONT_STYLE_NORMAL, &font);
359
360 mswr::ComPtr<IDWriteFontFace> font_face;
361 hr = font->CreateFontFace(&font_face);
362 ASSERT_TRUE(SUCCEEDED(hr));
363 ASSERT_EQ(3, fake_collection_->MessageCount());
364
365 mswr::ComPtr<IDWriteFont> found_font;
366 collection_->GetFontFromFontFace(font_face.Get(), &found_font);
367 EXPECT_NE(nullptr, found_font.Get());
368 EXPECT_EQ(3, fake_collection_->MessageCount());
369 }
370
371 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698