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 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), type
))
48 types
->push_back(base::UTF8ToUTF16(kMimeTypeText
));
49 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), type
))
50 types
->push_back(base::UTF8ToUTF16(kMimeTypeHTML
));
52 if (IsFormatAvailable(Clipboard::GetRtfFormatType(), type
))
53 types
->push_back(base::UTF8ToUTF16(kMimeTypeRTF
));
54 if (IsFormatAvailable(Clipboard::GetBitmapFormatType(), type
))
55 types
->push_back(base::UTF8ToUTF16(kMimeTypePNG
));
57 *contains_filenames
= false;
60 void TestClipboard::ReadText(ClipboardType type
, base::string16
* result
) const {
62 ReadAsciiText(type
, &result8
);
63 *result
= base::UTF8ToUTF16(result8
);
66 void TestClipboard::ReadAsciiText(ClipboardType type
,
67 std::string
* result
) const {
69 const DataStore
& store
= GetStore(type
);
70 auto it
= store
.data
.find(GetPlainTextFormatType());
71 if (it
!= store
.data
.end())
75 void TestClipboard::ReadHTML(ClipboardType type
,
76 base::string16
* markup
,
78 uint32
* fragment_start
,
79 uint32
* fragment_end
) const {
82 const DataStore
& store
= GetStore(type
);
83 auto it
= store
.data
.find(GetHtmlFormatType());
84 if (it
!= store
.data
.end())
85 *markup
= base::UTF8ToUTF16(it
->second
);
86 *src_url
= store
.html_src_url
;
88 *fragment_end
= base::checked_cast
<uint32
>(markup
->size());
91 void TestClipboard::ReadRTF(ClipboardType type
, std::string
* result
) const {
93 const DataStore
& store
= GetStore(type
);
94 auto it
= store
.data
.find(GetRtfFormatType());
95 if (it
!= store
.data
.end())
99 SkBitmap
TestClipboard::ReadImage(ClipboardType type
) const {
100 return GetStore(type
).image
;
103 void TestClipboard::ReadCustomData(ClipboardType clipboard_type
,
104 const base::string16
& type
,
105 base::string16
* result
) const {
108 void TestClipboard::ReadBookmark(base::string16
* title
,
109 std::string
* url
) const {
110 const DataStore
& store
= GetDefaultStore();
111 auto it
= store
.data
.find(GetUrlWFormatType());
112 if (it
!= store
.data
.end())
114 *title
= base::UTF8ToUTF16(store
.url_title
);
117 void TestClipboard::ReadData(const FormatType
& format
,
118 std::string
* result
) const {
120 const DataStore
& store
= GetDefaultStore();
121 auto it
= store
.data
.find(format
);
122 if (it
!= store
.data
.end())
123 *result
= it
->second
;
126 void TestClipboard::WriteObjects(ClipboardType type
, const ObjectMap
& objects
) {
128 default_store_type_
= type
;
129 for (const auto& kv
: objects
)
130 DispatchObject(static_cast<ObjectType
>(kv
.first
), kv
.second
);
131 default_store_type_
= CLIPBOARD_TYPE_COPY_PASTE
;
134 void TestClipboard::WriteText(const char* text_data
, size_t text_len
) {
135 std::string
text(text_data
, text_len
);
136 GetDefaultStore().data
[GetPlainTextFormatType()] = text
;
137 // Create a dummy entry.
138 GetDefaultStore().data
[GetPlainTextWFormatType()];
139 if (IsSupportedClipboardType(CLIPBOARD_TYPE_SELECTION
))
140 GetStore(CLIPBOARD_TYPE_SELECTION
).data
[GetPlainTextFormatType()] = text
;
143 void TestClipboard::WriteHTML(const char* markup_data
,
145 const char* url_data
,
147 base::string16 markup
;
148 base::UTF8ToUTF16(markup_data
, markup_len
, &markup
);
149 GetDefaultStore().data
[GetHtmlFormatType()] = base::UTF16ToUTF8(markup
);
150 GetDefaultStore().html_src_url
= std::string(url_data
, url_len
);
153 void TestClipboard::WriteRTF(const char* rtf_data
, size_t data_len
) {
154 GetDefaultStore().data
[GetRtfFormatType()] = std::string(rtf_data
, data_len
);
157 void TestClipboard::WriteBookmark(const char* title_data
,
159 const char* url_data
,
161 GetDefaultStore().data
[GetUrlWFormatType()] = std::string(url_data
, url_len
);
162 GetDefaultStore().url_title
= std::string(title_data
, title_len
);
165 void TestClipboard::WriteWebSmartPaste() {
166 // Create a dummy entry.
167 GetDefaultStore().data
[GetWebKitSmartPasteFormatType()];
170 void TestClipboard::WriteBitmap(const SkBitmap
& bitmap
) {
171 // Create a dummy entry.
172 GetDefaultStore().data
[GetBitmapFormatType()];
173 bitmap
.copyTo(&GetDefaultStore().image
);
176 void TestClipboard::WriteData(const FormatType
& format
,
177 const char* data_data
,
179 GetDefaultStore().data
[format
] = std::string(data_data
, data_len
);
182 TestClipboard::DataStore::DataStore() : sequence_number(0) {
185 TestClipboard::DataStore::~DataStore() {
188 void TestClipboard::DataStore::Clear() {
191 html_src_url
.clear();
195 const TestClipboard::DataStore
& TestClipboard::GetStore(
196 ClipboardType type
) const {
197 CHECK(IsSupportedClipboardType(type
));
198 return stores_
[type
];
201 TestClipboard::DataStore
& TestClipboard::GetStore(ClipboardType type
) {
202 CHECK(IsSupportedClipboardType(type
));
203 DataStore
& store
= stores_
[type
];
204 ++store
.sequence_number
;
208 const TestClipboard::DataStore
& TestClipboard::GetDefaultStore() const {
209 return GetStore(default_store_type_
);
212 TestClipboard::DataStore
& TestClipboard::GetDefaultStore() {
213 return GetStore(default_store_type_
);