Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / base / test / test_clipboard.cc
blob530037d2ff75fceb192199680ee975a70012b219
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"
7 #include <stddef.h>
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
11 namespace ui {
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()] =
24 clipboard;
25 return clipboard;
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 {
45 types->clear();
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 {
61 std::string result8;
62 ReadAsciiText(type, &result8);
63 *result = base::UTF8ToUTF16(result8);
66 void TestClipboard::ReadAsciiText(ClipboardType type,
67 std::string* result) const {
68 result->clear();
69 const DataStore& store = GetStore(type);
70 auto it = store.data.find(GetPlainTextFormatType());
71 if (it != store.data.end())
72 *result = it->second;
75 void TestClipboard::ReadHTML(ClipboardType type,
76 base::string16* markup,
77 std::string* src_url,
78 uint32* fragment_start,
79 uint32* fragment_end) const {
80 markup->clear();
81 src_url->clear();
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;
87 *fragment_start = 0;
88 *fragment_end = base::checked_cast<uint32>(markup->size());
91 void TestClipboard::ReadRTF(ClipboardType type, std::string* result) const {
92 result->clear();
93 const DataStore& store = GetStore(type);
94 auto it = store.data.find(GetRtfFormatType());
95 if (it != store.data.end())
96 *result = it->second;
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())
113 *url = it->second;
114 *title = base::UTF8ToUTF16(store.url_title);
117 void TestClipboard::ReadData(const FormatType& format,
118 std::string* result) const {
119 result->clear();
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) {
127 Clear(type);
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,
144 size_t markup_len,
145 const char* url_data,
146 size_t url_len) {
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,
158 size_t title_len,
159 const char* url_data,
160 size_t url_len) {
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,
178 size_t data_len) {
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() {
189 data.clear();
190 url_title.clear();
191 html_src_url.clear();
192 image = SkBitmap();
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;
205 return store;
208 const TestClipboard::DataStore& TestClipboard::GetDefaultStore() const {
209 return GetStore(default_store_type_);
212 TestClipboard::DataStore& TestClipboard::GetDefaultStore() {
213 return GetStore(default_store_type_);
216 } // namespace ui