Index: content/common/dwrite_font_proxy_win_unittest.cc |
diff --git a/content/common/dwrite_font_proxy_win_unittest.cc b/content/common/dwrite_font_proxy_win_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0a715d44a9c4d8403fd04b9ad623f7ac314fb201 |
--- /dev/null |
+++ b/content/common/dwrite_font_proxy_win_unittest.cc |
@@ -0,0 +1,370 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <dwrite.h> |
+#include <shlobj.h> |
+#include <wrl.h> |
+ |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "content/common/dwrite_font_proxy_messages.h" |
+#include "content/common/dwrite_font_proxy_win.h" |
+#include "content/common/view_messages.h" |
+#include "content/test/dwrite_font_fake_sender_win.h" |
+#include "ipc/ipc_message_macros.h" |
+#include "ipc/ipc_sender.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace mswr = Microsoft::WRL; |
+ |
+namespace content { |
+ |
+void CreateDWriteFactory(IUnknown** factory) { |
+ using DWriteCreateFactoryProc = decltype(DWriteCreateFactory)*; |
+ HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll"); |
+ if (!dwrite_dll) |
+ return; |
+ |
+ DWriteCreateFactoryProc dwrite_create_factory_proc = |
+ reinterpret_cast<DWriteCreateFactoryProc>( |
+ GetProcAddress(dwrite_dll, "DWriteCreateFactory")); |
+ if (!dwrite_create_factory_proc) |
+ return; |
+ |
+ dwrite_create_factory_proc(DWRITE_FACTORY_TYPE_SHARED, |
+ __uuidof(IDWriteFactory), factory); |
+} |
+ |
+class DWriteFontProxyUnitTest : public testing::Test { |
+ public: |
+ DWriteFontProxyUnitTest() { |
+ if (factory == nullptr) |
+ return; |
+ fakeCollection_ = new FakeFontCollection(); |
+ SetupFonts(fakeCollection_.get()); |
+ mswr::MakeAndInitialize<DWriteFontCollectionProxy>( |
+ &collection_, factory.Get(), |
+ base::Bind(&FakeFontCollection::GetTrackingSender, fakeCollection_)); |
+ } |
+ |
+ ~DWriteFontProxyUnitTest() { collection_->Unregister(); } |
+ |
+ static void SetupFonts(FakeFontCollection* fonts) { |
+ fonts->AddFont(L"Aardvark") |
+ .AddFamilyName(L"en-us", L"Aardvark") |
+ .AddFamilyName(L"de-de", L"Erdferkel") |
+ .AddFilePath(L"X:\\Nonexistent\\Folder\\Aardvark.ttf"); |
+ FakeFont& arial = |
+ fonts->AddFont(L"Arial").AddFamilyName(L"en-us", L"Arial"); |
+ for (auto& path : arialFontFiles) |
+ arial.AddFilePath(path); |
+ fonts->AddFont(L"Times New Roman") |
+ .AddFamilyName(L"en-us", L"Times New Roman") |
+ .AddFilePath(L"X:\\Nonexistent\\Folder\\Times.ttf"); |
+ } |
+ |
+ static void SetUpTestCase() { |
+ CreateDWriteFactory(&factory); |
+ |
+ std::vector<base::char16> fontPath; |
+ fontPath.resize(MAX_PATH); |
+ SHGetSpecialFolderPath(NULL /* hwndOwner - reserved */, fontPath.data(), |
+ CSIDL_FONTS, FALSE /* fCreate*/); |
+ base::string16 arial; |
+ arial.append(fontPath.data()).append(L"\\arial.ttf"); |
+ base::string16 arialBold; |
+ arialBold.append(fontPath.data()).append(L"\\arialbd.ttf"); |
+ arialFontFiles.push_back(arial); |
+ arialFontFiles.push_back(arialBold); |
+ } |
+ |
+ protected: |
+ scoped_refptr<FakeFontCollection> fakeCollection_; |
+ mswr::ComPtr<DWriteFontCollectionProxy> collection_; |
+ |
+ static std::vector<base::string16> arialFontFiles; |
+ static mswr::ComPtr<IDWriteFactory> factory; |
+}; |
+std::vector<base::string16> DWriteFontProxyUnitTest::arialFontFiles; |
+mswr::ComPtr<IDWriteFactory> DWriteFontProxyUnitTest::factory; |
+ |
+TEST_F(DWriteFontProxyUnitTest, GetFontFamilyCount) { |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 familyCount = collection_->GetFontFamilyCount(); |
+ |
+ EXPECT_EQ(3, familyCount); |
+ ASSERT_EQ(1, fakeCollection_->MessageCount()); |
+ EXPECT_EQ(DWriteFontProxyMsg_GetFamilyCount::ID, |
+ fakeCollection_->GetMessage(0)->type()); |
+ |
+ // Calling again should not cause another message to be sent. |
+ familyCount = collection_->GetFontFamilyCount(); |
+ EXPECT_EQ(3, familyCount); |
+ ASSERT_EQ(1, fakeCollection_->MessageCount()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, FindFamilyNameShouldFindFamily) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ hr = collection_->FindFamilyName(L"Arial", &index, &exists); |
+ |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(1, index); |
+ EXPECT_TRUE(exists); |
+ ASSERT_EQ(2, fakeCollection_->MessageCount()); |
+ EXPECT_EQ(DWriteFontProxyMsg_FindFamily::ID, |
+ fakeCollection_->GetMessage(0)->type()); |
+ EXPECT_EQ(DWriteFontProxyMsg_GetFamilyCount::ID, |
+ fakeCollection_->GetMessage(1)->type()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, FindFamilyNameShouldReturnUINTMAXWhenNotFound) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ hr = collection_->FindFamilyName(L"Not a font", &index, &exists); |
+ |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(UINT32_MAX, index); |
+ EXPECT_FALSE(exists); |
+ ASSERT_EQ(1, fakeCollection_->MessageCount()); |
+ EXPECT_EQ(DWriteFontProxyMsg_FindFamily::ID, |
+ fakeCollection_->GetMessage(0)->type()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, FindFamilyNameShouldNotSendDuplicateIPC) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ hr = collection_->FindFamilyName(L"Arial", &index, &exists); |
+ ASSERT_EQ(S_OK, hr); |
+ ASSERT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ hr = collection_->FindFamilyName(L"Arial", &index, &exists); |
+ |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(2, fakeCollection_->MessageCount()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, GetFontFamilyShouldCreateFamily) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ hr = collection_->FindFamilyName(L"Arial", &index, &exists); |
+ ASSERT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ mswr::ComPtr<IDWriteFontFamily> family; |
+ hr = collection_->GetFontFamily(2, &family); |
+ |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(2, fakeCollection_->MessageCount()); |
+ EXPECT_NE(nullptr, family.Get()); |
+} |
+ |
+void CheckLocale(IDWriteLocalizedStrings* strings, |
+ const base::string16& localeName, |
+ const base::string16& expectedValue) { |
+ UINT32 localeIndex = 0; |
+ BOOL localeExists = FALSE; |
+ strings->FindLocaleName(localeName.data(), &localeIndex, &localeExists); |
+ EXPECT_TRUE(localeExists); |
+ |
+ UINT32 nameLength = 0; |
+ strings->GetLocaleNameLength(localeIndex, &nameLength); |
+ EXPECT_EQ(localeName.size(), nameLength); |
+ base::string16 actualName; |
+ nameLength++; |
+ actualName.resize(nameLength); |
+ strings->GetLocaleName(localeIndex, const_cast<WCHAR*>(actualName.data()), |
+ nameLength); |
+ EXPECT_STREQ(localeName.c_str(), actualName.c_str()); |
+ |
+ UINT32 stringLength = 0; |
+ strings->GetStringLength(localeIndex, &stringLength); |
+ EXPECT_EQ(expectedValue.size(), stringLength); |
+ base::string16 actualValue; |
+ stringLength++; |
+ actualValue.resize(stringLength); |
+ strings->GetString(localeIndex, const_cast<WCHAR*>(actualValue.data()), |
+ stringLength); |
+ EXPECT_STREQ(expectedValue.c_str(), actualValue.c_str()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, GetFamilyNames) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ hr = collection_->FindFamilyName(L"Aardvark", &index, &exists); |
+ ASSERT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ mswr::ComPtr<IDWriteFontFamily> family; |
+ hr = collection_->GetFontFamily(index, &family); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ mswr::ComPtr<IDWriteLocalizedStrings> names; |
+ hr = family->GetFamilyNames(&names); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(3, fakeCollection_->MessageCount()); |
+ EXPECT_EQ(DWriteFontProxyMsg_GetFamilyNames::ID, |
+ fakeCollection_->GetMessage(2)->type()); |
+ |
+ EXPECT_EQ(2, names->GetCount()); |
+ UINT32 localeIndex = 0; |
+ BOOL localeExists = FALSE; |
+ hr = names->FindLocaleName(L"fr-fr", &localeIndex, &localeExists); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_FALSE(localeExists); |
+ |
+ CheckLocale(names.Get(), L"en-us", L"Aardvark"); |
+ CheckLocale(names.Get(), L"de-de", L"Erdferkel"); |
+ |
+ base::string16 unused; |
+ unused.resize(25); |
+ hr = names->GetLocaleName(15234, const_cast<WCHAR*>(unused.data()), |
+ unused.size() - 1); |
+ EXPECT_FALSE(SUCCEEDED(hr)); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, GetFontCollection) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ hr = collection_->FindFamilyName(L"Arial", &index, &exists); |
+ ASSERT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ mswr::ComPtr<IDWriteFontFamily> family; |
+ hr = collection_->GetFontFamily(2, &family); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ mswr::ComPtr<IDWriteFontCollection> returnedCollection; |
+ hr = family->GetFontCollection(&returnedCollection); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(2, fakeCollection_->MessageCount()); |
+ EXPECT_EQ(collection_.Get(), returnedCollection.Get()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, GetFamilyNamesShouldNotIPCAfterLoadingFamily) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ collection_->FindFamilyName(L"Arial", &index, &exists); |
+ mswr::ComPtr<IDWriteFontFamily> family; |
+ collection_->GetFontFamily(index, &family); |
+ family->GetFontCount(); |
+ EXPECT_EQ(fakeCollection_->MessageCount(), 3); |
+ |
+ mswr::ComPtr<IDWriteLocalizedStrings> names; |
+ hr = family->GetFamilyNames(&names); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(3, fakeCollection_->MessageCount()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, |
+ GetFontFamilyShouldNotCreateFamilyWhenIndexIsInvalid) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ hr = collection_->FindFamilyName(L"Arial", &index, &exists); |
+ ASSERT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ mswr::ComPtr<IDWriteFontFamily> family; |
+ hr = collection_->GetFontFamily(1654, &family); |
+ |
+ EXPECT_FALSE(SUCCEEDED(hr)); |
+ EXPECT_EQ(2, fakeCollection_->MessageCount()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, LoadingFontFamily) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ collection_->FindFamilyName(L"Arial", &index, &exists); |
+ mswr::ComPtr<IDWriteFontFamily> family; |
+ collection_->GetFontFamily(index, &family); |
+ ASSERT_EQ(2, fakeCollection_->MessageCount()); |
+ |
+ UINT32 fontCount = family->GetFontCount(); |
+ EXPECT_LT(0u, fontCount); |
+ EXPECT_EQ(3, fakeCollection_->MessageCount()); |
+ EXPECT_EQ(DWriteFontProxyMsg_GetFontFiles::ID, |
+ fakeCollection_->GetMessage(2)->type()); |
+ mswr::ComPtr<IDWriteFont> font; |
+ hr = family->GetFirstMatchingFont(DWRITE_FONT_WEIGHT_NORMAL, |
+ DWRITE_FONT_STRETCH_NORMAL, |
+ DWRITE_FONT_STYLE_NORMAL, &font); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(3, fakeCollection_->MessageCount()); |
+ mswr::ComPtr<IDWriteFont> font2; |
+ hr = family->GetFont(0, &font2); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(3, fakeCollection_->MessageCount()); |
+ mswr::ComPtr<IDWriteFontList> matchingFonts; |
+ hr = family->GetMatchingFonts(DWRITE_FONT_WEIGHT_NORMAL, |
+ DWRITE_FONT_STRETCH_NORMAL, |
+ DWRITE_FONT_STYLE_NORMAL, &matchingFonts); |
+ EXPECT_EQ(S_OK, hr); |
+ EXPECT_EQ(3, fakeCollection_->MessageCount()); |
+ EXPECT_NE(nullptr, matchingFonts.Get()); |
+} |
+ |
+TEST_F(DWriteFontProxyUnitTest, GetFontFromFontFaceShouldFindFont) { |
+ HRESULT hr; |
+ if (factory == nullptr) |
+ return; |
+ |
+ uint32 index = UINT_MAX; |
+ BOOL exists = FALSE; |
+ collection_->FindFamilyName(L"Arial", &index, &exists); |
+ mswr::ComPtr<IDWriteFontFamily> family; |
+ collection_->GetFontFamily(index, &family); |
+ |
+ mswr::ComPtr<IDWriteFont> font; |
+ family->GetFirstMatchingFont(DWRITE_FONT_WEIGHT_NORMAL, |
+ DWRITE_FONT_STRETCH_NORMAL, |
+ DWRITE_FONT_STYLE_NORMAL, &font); |
+ |
+ mswr::ComPtr<IDWriteFontFace> fontFace; |
+ hr = font->CreateFontFace(&fontFace); |
+ ASSERT_TRUE(SUCCEEDED(hr)); |
+ ASSERT_EQ(3, fakeCollection_->MessageCount()); |
+ |
+ mswr::ComPtr<IDWriteFont> foundFont; |
+ collection_->GetFontFromFontFace(fontFace.Get(), &foundFont); |
+ EXPECT_NE(nullptr, foundFont.Get()); |
+ EXPECT_EQ(3, fakeCollection_->MessageCount()); |
+} |
+ |
+} // namespace content |