summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/dwrite_font_proxy_message_filter_win_unittest.cc
blob: 65359e9b92355b8d05345e7785fe67d76314dc2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 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 "content/browser/renderer_host/dwrite_font_proxy_message_filter_win.h"

#include <dwrite.h>

#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "content/common/dwrite_font_proxy_messages.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "ipc/ipc_message_macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/win/direct_write.h"

namespace mswr = Microsoft::WRL;

namespace content {

namespace {

class FilterWithFakeSender : public DWriteFontProxyMessageFilter {
 public:
  bool Send(IPC::Message* msg) override {
    EXPECT_EQ(nullptr, reply_message_.get());
    reply_message_.reset(msg);
    return true;
  }

  IPC::Message* GetReply() { return reply_message_.get(); }

  void ResetReply() { reply_message_.reset(nullptr); }

 private:
  ~FilterWithFakeSender() override = default;

  scoped_ptr<IPC::Message> reply_message_;
};

class DWriteFontProxyMessageFilterUnitTest : public testing::Test {
 public:
  DWriteFontProxyMessageFilterUnitTest() {
    filter_ = new FilterWithFakeSender();
  }

  void Send(IPC::SyncMessage* message) {
    std::unique_ptr<IPC::SyncMessage> deleter(message);
    scoped_ptr<IPC::MessageReplyDeserializer> serializer(
        message->GetReplyDeserializer());
    filter_->OnMessageReceived(*message);
    base::RunLoop().RunUntilIdle();
    ASSERT_NE(nullptr, filter_->GetReply());
    serializer->SerializeOutputParameters(*(filter_->GetReply()));
  }

  scoped_refptr<FilterWithFakeSender> filter_;
  content::TestBrowserThreadBundle thread_bundle_;
};

TEST_F(DWriteFontProxyMessageFilterUnitTest, GetFamilyCount) {
  if (!gfx::win::ShouldUseDirectWrite())
    return;
  UINT32 family_count = 0;
  Send(new DWriteFontProxyMsg_GetFamilyCount(&family_count));
  EXPECT_NE(0u, family_count);  // Assume there's some fonts on the test system.
}

TEST_F(DWriteFontProxyMessageFilterUnitTest, FindFamily) {
  if (!gfx::win::ShouldUseDirectWrite())
    return;
  UINT32 arial_index = 0;
  Send(new DWriteFontProxyMsg_FindFamily(L"Arial", &arial_index));
  EXPECT_NE(UINT_MAX, arial_index);

  filter_->ResetReply();
  UINT32 times_index = 0;
  Send(new DWriteFontProxyMsg_FindFamily(L"Times New Roman", &times_index));
  EXPECT_NE(UINT_MAX, times_index);
  EXPECT_NE(arial_index, times_index);

  filter_->ResetReply();
  UINT32 unknown_index = 0;
  Send(new DWriteFontProxyMsg_FindFamily(L"Not a font family", &unknown_index));
  EXPECT_EQ(UINT_MAX, unknown_index);
}

TEST_F(DWriteFontProxyMessageFilterUnitTest, GetFamilyNames) {
  if (!gfx::win::ShouldUseDirectWrite())
    return;
  UINT32 arial_index = 0;
  Send(new DWriteFontProxyMsg_FindFamily(L"Arial", &arial_index));
  filter_->ResetReply();

  std::vector<DWriteStringPair> names;
  Send(new DWriteFontProxyMsg_GetFamilyNames(arial_index, &names));

  EXPECT_LT(0u, names.size());
  for (const auto& pair : names) {
    EXPECT_STRNE(L"", pair.first.c_str());
    EXPECT_STRNE(L"", pair.second.c_str());
  }
}

TEST_F(DWriteFontProxyMessageFilterUnitTest, GetFamilyNamesIndexOutOfBounds) {
  if (!gfx::win::ShouldUseDirectWrite())
    return;
  std::vector<DWriteStringPair> names;
  UINT32 invalid_index = 1000000;
  Send(new DWriteFontProxyMsg_GetFamilyNames(invalid_index, &names));

  EXPECT_EQ(0u, names.size());
}

TEST_F(DWriteFontProxyMessageFilterUnitTest, GetFontFiles) {
  if (!gfx::win::ShouldUseDirectWrite())
    return;
  UINT32 arial_index = 0;
  Send(new DWriteFontProxyMsg_FindFamily(L"Arial", &arial_index));
  filter_->ResetReply();

  std::vector<base::string16> files;
  Send(new DWriteFontProxyMsg_GetFontFiles(arial_index, &files));

  EXPECT_LT(0u, files.size());
  for (const base::string16& file : files) {
    EXPECT_STRNE(L"", file.c_str());
  }
}

TEST_F(DWriteFontProxyMessageFilterUnitTest, GetFontFilesIndexOutOfBounds) {
  if (!gfx::win::ShouldUseDirectWrite())
    return;
  std::vector<base::string16> files;
  UINT32 invalid_index = 1000000;
  Send(new DWriteFontProxyMsg_GetFontFiles(invalid_index, &files));

  EXPECT_EQ(0u, files.size());
}

}  // namespace

}  // namespace content