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