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