blob: fae569ee748e808b073cc7890399d0d7b19be06e [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2015 The Chromium Authors
kulshin4039fd5b2015-12-14 23:12:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_TEST_DWRITE_FONT_FAKE_SENDER_WIN_H_
6#define CONTENT_TEST_DWRITE_FONT_FAKE_SENDER_WIN_H_
7
avi66a07722015-12-25 23:38:128#include <stddef.h>
9#include <stdint.h>
kulshin4039fd5b2015-12-14 23:12:5410#include <wrl.h>
11
Henrique Ferreiro5388077c2019-09-09 13:14:3612#include <string>
kulshin4039fd5b2015-12-14 23:12:5413#include <utility>
14#include <vector>
15
Sam McNallyae42d322018-01-09 00:47:5516#include "base/files/file_path.h"
Henrique Ferreiro5388077c2019-09-09 13:14:3617#include "mojo/public/cpp/bindings/pending_remote.h"
18#include "mojo/public/cpp/bindings/receiver_set.h"
Dominik Röttsches24f9d162019-01-04 10:43:5619#include "third_party/blink/public/mojom/dwrite_font_proxy/dwrite_font_proxy.mojom.h"
kulshin267bf5e2016-04-19 18:57:1620
kulshin4039fd5b2015-12-14 23:12:5421namespace content {
22
23class FakeFontCollection;
24
25// Creates a new FakeFontCollection, seeded with some basic data, and returns a
26// Sender that can be used to interact with the collection.
Henrique Ferreiro5388077c2019-09-09 13:14:3627base::RepeatingCallback<
28 mojo::PendingRemote<blink::mojom::DWriteFontProxy>(void)>
Sam McNallyae42d322018-01-09 00:47:5529CreateFakeCollectionSender();
kulshin4039fd5b2015-12-14 23:12:5430
31// Helper class for describing a font object. Use FakeFontCollection instead.
32class FakeFont {
33 public:
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5834 explicit FakeFont(const std::u16string& name);
kulshin4039fd5b2015-12-14 23:12:5435
Peter Boström9b036532021-10-28 23:37:2836 FakeFont& operator=(const FakeFont&) = delete;
37
Sam McNallyae42d322018-01-09 00:47:5538 FakeFont(FakeFont&& other);
vmpstrbcdec0d2016-04-14 01:24:5239
kulshin4039fd5b2015-12-14 23:12:5440 ~FakeFont();
41
Sam McNallyae42d322018-01-09 00:47:5542 FakeFont& AddFilePath(const base::FilePath& file_path) {
kulshin4039fd5b2015-12-14 23:12:5443 file_paths_.push_back(file_path);
44 return *this;
45 }
46
Sam McNallyae42d322018-01-09 00:47:5547 FakeFont& AddFileHandle(base::File handle) {
48 file_handles_.push_back(std::move(handle));
kulshin96ce8347f2016-07-26 00:46:0149 return *this;
50 }
51
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5852 FakeFont& AddFamilyName(const std::u16string& locale,
53 const std::u16string& family_name) {
kulshin4039fd5b2015-12-14 23:12:5454 family_names_.emplace_back(locale, family_name);
55 return *this;
56 }
57
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5858 const std::u16string& font_name() { return font_name_; }
kulshin267bf5e2016-04-19 18:57:1659
kulshin4039fd5b2015-12-14 23:12:5460 private:
61 friend FakeFontCollection;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5862 std::u16string font_name_;
Sam McNallyae42d322018-01-09 00:47:5563 std::vector<base::FilePath> file_paths_;
64 std::vector<base::File> file_handles_;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5865 std::vector<std::pair<std::u16string, std::u16string>> family_names_;
kulshin4039fd5b2015-12-14 23:12:5466};
67
68// Implements a font collection that supports interaction through sending IPC
69// messages from dwrite_font_proxy_messages.h.
70// Usage:
71// Create a new FakeFontCollection.
72// Call AddFont() to add fonts.
73// For each font, call methods on the font to configure the font.
74// Note: the indices of the fonts will correspond to the order they were
75// added. The collection will not sort or reorder fonts in any way.
76// Call GetSender()/GetTrackingSender() to obtain an IPC::Sender.
77// Call Send() with DWriteFontProxyMsg_* to interact with the collection.
Daniel Bratell9b550422017-12-11 16:30:5078// Call MessageCount()/GetIpcMessage() to inspect sent messages.
kulshin4039fd5b2015-12-14 23:12:5479//
80// The internal code flow for GetSender()/Send() is as follows:
81// GetSender() returns a new FakeSender, pointing back to the collection.
82// FakeSender::Send() will create a new ReplySender and call
83// ReplySender::OnMessageReceived()
84// ReplySender::OnMessageReceived() contains the message map, which will
85// internally call ReplySender::On*() and ReplySender::Send()
86// ReplySender::Send() will save the reply message, to be used later.
87// FakeSender::Send() will retrieve the reply message and save the output.
Dominik Röttsches24f9d162019-01-04 10:43:5688class FakeFontCollection : public blink::mojom::DWriteFontProxy {
kulshin4039fd5b2015-12-14 23:12:5489 public:
Sam McNallyae42d322018-01-09 00:47:5590 enum class MessageType {
91 kFindFamily,
92 kGetFamilyCount,
93 kGetFamilyNames,
94 kGetFontFiles,
95 kMapCharacters
96 };
kulshin4039fd5b2015-12-14 23:12:5497 FakeFontCollection();
Peter Boström828b9022021-09-21 02:28:4398
99 FakeFontCollection(const FakeFontCollection&) = delete;
100 FakeFontCollection& operator=(const FakeFontCollection&) = delete;
101
Sam McNallyae42d322018-01-09 00:47:55102 ~FakeFontCollection() override;
kulshin4039fd5b2015-12-14 23:12:54103
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58104 FakeFont& AddFont(const std::u16string& font_name);
kulshin4039fd5b2015-12-14 23:12:54105
106 size_t MessageCount();
Sam McNallyae42d322018-01-09 00:47:55107 MessageType GetMessageType(size_t id);
kulshin4039fd5b2015-12-14 23:12:54108
Henrique Ferreiro5388077c2019-09-09 13:14:36109 mojo::PendingRemote<blink::mojom::DWriteFontProxy> CreateRemote();
kulshin4039fd5b2015-12-14 23:12:54110
111 protected:
Dominik Röttsches24f9d162019-01-04 10:43:56112 // blink::mojom::DWriteFontProxy:
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58113 void FindFamily(const std::u16string& family_name,
Sam McNallyae42d322018-01-09 00:47:55114 FindFamilyCallback callback) override;
115 void GetFamilyCount(GetFamilyCountCallback callback) override;
116 void GetFamilyNames(uint32_t family_index,
117 GetFamilyNamesCallback callback) override;
118 void GetFontFiles(uint32_t family_index,
119 GetFontFilesCallback callback) override;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58120 void MapCharacters(const std::u16string& text,
Dominik Röttsches24f9d162019-01-04 10:43:56121 blink::mojom::DWriteFontStylePtr font_style,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58122 const std::u16string& locale_name,
Sam McNallyae42d322018-01-09 00:47:55123 uint32_t reading_direction,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58124 const std::u16string& base_family_name,
Sam McNallyae42d322018-01-09 00:47:55125 MapCharactersCallback callback) override;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58126 void MatchUniqueFont(const std::u16string& unique_font_name,
Dominik Röttsches11638282019-04-12 22:19:03127 MatchUniqueFontCallback callback) override;
128 void GetUniqueFontLookupMode(
129 GetUniqueFontLookupModeCallback callback) override;
Dominik Röttsches1c59996a2019-04-03 14:17:09130 void GetUniqueNameLookupTableIfAvailable(
131 GetUniqueNameLookupTableIfAvailableCallback callback) override;
Dominik Röttschesb0c644652019-01-09 15:18:37132 void GetUniqueNameLookupTable(
133 GetUniqueNameLookupTableCallback callback) override;
Dominik Röttschesaf596df2019-10-01 08:06:07134 void FallbackFamilyAndStyleForCodepoint(
Dominik Röttsches6323eeff2019-07-18 14:12:04135 const std::string& base_family_name,
136 const std::string& locale_name,
137 uint32_t codepoint,
Dominik Röttschesaf596df2019-10-01 08:06:07138 FallbackFamilyAndStyleForCodepointCallback callback) override;
kulshin4039fd5b2015-12-14 23:12:54139
140 private:
kulshin4039fd5b2015-12-14 23:12:54141 std::vector<FakeFont> fonts_;
Sam McNallyae42d322018-01-09 00:47:55142
143 std::vector<MessageType> message_types_;
144
Henrique Ferreiro5388077c2019-09-09 13:14:36145 mojo::ReceiverSet<blink::mojom::DWriteFontProxy> receivers_;
kulshin4039fd5b2015-12-14 23:12:54146};
147
148} // namespace content
149
150#endif // CONTENT_TEST_DWRITE_FONT_FAKE_SENDER_WIN_H_