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