1 // Copyright 2014 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.
5 #include "ui/base/test/test_clipboard.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
13 TestClipboard::TestClipboard()
14 : default_store_type_(CLIPBOARD_TYPE_COPY_PASTE
) {
17 TestClipboard::~TestClipboard() {
20 Clipboard
* TestClipboard::CreateForCurrentThread() {
21 base::AutoLock
lock(Clipboard::clipboard_map_lock_
.Get());
22 Clipboard
* clipboard
= new TestClipboard
;
23 Clipboard::clipboard_map_
.Get()[base::PlatformThread::CurrentId()] =
28 uint64
TestClipboard::GetSequenceNumber(ClipboardType type
) const {
29 return GetStore(type
).sequence_number
;
32 bool TestClipboard::IsFormatAvailable(const FormatType
& format
,
33 ClipboardType type
) const {
34 const DataStore
& store
= GetStore(type
);
35 return store
.data
.find(format
) != store
.data
.end();
38 void TestClipboard::Clear(ClipboardType type
) {
39 GetStore(type
).Clear();
42 void TestClipboard::ReadAvailableTypes(ClipboardType type
,
43 std::vector
<base::string16
>* types
,
44 bool* contains_filenames
) const {
47 void TestClipboard::ReadText(ClipboardType type
, base::string16
* result
) const {
49 ReadAsciiText(type
, &result8
);
50 *result
= base::UTF8ToUTF16(result8
);
53 void TestClipboard::ReadAsciiText(ClipboardType type
,
54 std::string
* result
) const {
56 const DataStore
& store
= GetStore(type
);
57 auto it
= store
.data
.find(GetPlainTextFormatType());
58 if (it
!= store
.data
.end())
62 void TestClipboard::ReadHTML(ClipboardType type
,
63 base::string16
* markup
,
65 uint32
* fragment_start
,
66 uint32
* fragment_end
) const {
69 const DataStore
& store
= GetStore(type
);
70 auto it
= store
.data
.find(GetHtmlFormatType());
71 if (it
!= store
.data
.end())
72 *markup
= base::UTF8ToUTF16(it
->second
);
73 *src_url
= store
.html_src_url
;
75 *fragment_end
= base::checked_cast
<uint32
>(markup
->size());
78 void TestClipboard::ReadRTF(ClipboardType type
, std::string
* result
) const {
80 const DataStore
& store
= GetStore(type
);
81 auto it
= store
.data
.find(GetRtfFormatType());
82 if (it
!= store
.data
.end())
86 SkBitmap
TestClipboard::ReadImage(ClipboardType type
) const {
87 return GetStore(type
).image
;
90 void TestClipboard::ReadCustomData(ClipboardType clipboard_type
,
91 const base::string16
& type
,
92 base::string16
* result
) const {
95 void TestClipboard::ReadBookmark(base::string16
* title
,
96 std::string
* url
) const {
97 const DataStore
& store
= GetDefaultStore();
98 auto it
= store
.data
.find(GetUrlWFormatType());
99 if (it
!= store
.data
.end())
101 *title
= base::UTF8ToUTF16(store
.url_title
);
104 void TestClipboard::ReadData(const FormatType
& format
,
105 std::string
* result
) const {
107 const DataStore
& store
= GetDefaultStore();
108 auto it
= store
.data
.find(format
);
109 if (it
!= store
.data
.end())
110 *result
= it
->second
;
113 void TestClipboard::WriteObjects(ClipboardType type
, const ObjectMap
& objects
) {
115 default_store_type_
= type
;
116 for (const auto& kv
: objects
)
117 DispatchObject(static_cast<ObjectType
>(kv
.first
), kv
.second
);
118 default_store_type_
= CLIPBOARD_TYPE_COPY_PASTE
;
121 void TestClipboard::WriteText(const char* text_data
, size_t text_len
) {
122 std::string
text(text_data
, text_len
);
123 GetDefaultStore().data
[GetPlainTextFormatType()] = text
;
124 // Create a dummy entry.
125 GetDefaultStore().data
[GetPlainTextWFormatType()];
126 if (IsSupportedClipboardType(CLIPBOARD_TYPE_SELECTION
))
127 GetStore(CLIPBOARD_TYPE_SELECTION
).data
[GetPlainTextFormatType()] = text
;
130 void TestClipboard::WriteHTML(const char* markup_data
,
132 const char* url_data
,
134 base::string16 markup
;
135 base::UTF8ToUTF16(markup_data
, markup_len
, &markup
);
136 GetDefaultStore().data
[GetHtmlFormatType()] = base::UTF16ToUTF8(markup
);
137 GetDefaultStore().html_src_url
= std::string(url_data
, url_len
);
140 void TestClipboard::WriteRTF(const char* rtf_data
, size_t data_len
) {
141 GetDefaultStore().data
[GetRtfFormatType()] = std::string(rtf_data
, data_len
);
144 void TestClipboard::WriteBookmark(const char* title_data
,
146 const char* url_data
,
148 GetDefaultStore().data
[GetUrlWFormatType()] = std::string(url_data
, url_len
);
149 GetDefaultStore().url_title
= std::string(title_data
, title_len
);
152 void TestClipboard::WriteWebSmartPaste() {
153 // Create a dummy entry.
154 GetDefaultStore().data
[GetWebKitSmartPasteFormatType()];
157 void TestClipboard::WriteBitmap(const SkBitmap
& bitmap
) {
158 // Create a dummy entry.
159 GetDefaultStore().data
[GetBitmapFormatType()];
160 bitmap
.copyTo(&GetDefaultStore().image
);
163 void TestClipboard::WriteData(const FormatType
& format
,
164 const char* data_data
,
166 GetDefaultStore().data
[format
] = std::string(data_data
, data_len
);
169 TestClipboard::DataStore::DataStore() : sequence_number(0) {
172 TestClipboard::DataStore::~DataStore() {
175 void TestClipboard::DataStore::Clear() {
178 html_src_url
.clear();
182 const TestClipboard::DataStore
& TestClipboard::GetStore(
183 ClipboardType type
) const {
184 CHECK(IsSupportedClipboardType(type
));
185 return stores_
[type
];
188 TestClipboard::DataStore
& TestClipboard::GetStore(ClipboardType type
) {
189 CHECK(IsSupportedClipboardType(type
));
190 DataStore
& store
= stores_
[type
];
191 ++store
.sequence_number
;
195 const TestClipboard::DataStore
& TestClipboard::GetDefaultStore() const {
196 return GetStore(default_store_type_
);
199 TestClipboard::DataStore
& TestClipboard::GetDefaultStore() {
200 return GetStore(default_store_type_
);