OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/base/test/test_clipboard.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 #include "base/numerics/safe_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 |
| 11 namespace ui { |
| 12 |
| 13 TestClipboard::TestClipboard() |
| 14 : default_store_type_(CLIPBOARD_TYPE_COPY_PASTE) { |
| 15 } |
| 16 |
| 17 TestClipboard::~TestClipboard() { |
| 18 } |
| 19 |
| 20 void TestClipboard::UseForCurrentThread() { |
| 21 base::AutoLock lock(Clipboard::clipboard_map_lock_.Get()); |
| 22 Clipboard::clipboard_map_.Get()[base::PlatformThread::CurrentId()] = |
| 23 new TestClipboard; |
| 24 } |
| 25 |
| 26 uint64 TestClipboard::GetSequenceNumber(ClipboardType type) const { |
| 27 return GetStore(type).sequence_number; |
| 28 } |
| 29 |
| 30 bool TestClipboard::IsFormatAvailable(const FormatType& format, |
| 31 ClipboardType type) const { |
| 32 const DataStore& store = GetStore(type); |
| 33 return store.data.find(format) != store.data.end(); |
| 34 } |
| 35 |
| 36 void TestClipboard::Clear(ClipboardType type) { |
| 37 GetStore(type).Clear(); |
| 38 } |
| 39 |
| 40 void TestClipboard::ReadAvailableTypes(ClipboardType type, |
| 41 std::vector<base::string16>* types, |
| 42 bool* contains_filenames) const { |
| 43 } |
| 44 |
| 45 void TestClipboard::ReadText(ClipboardType type, base::string16* result) const { |
| 46 std::string result8; |
| 47 ReadAsciiText(type, &result8); |
| 48 *result = base::UTF8ToUTF16(result8); |
| 49 } |
| 50 |
| 51 void TestClipboard::ReadAsciiText(ClipboardType type, |
| 52 std::string* result) const { |
| 53 result->clear(); |
| 54 const DataStore& store = GetStore(type); |
| 55 auto it = store.data.find(GetPlainTextFormatType()); |
| 56 if (it != store.data.end()) |
| 57 *result = it->second; |
| 58 } |
| 59 |
| 60 void TestClipboard::ReadHTML(ClipboardType type, |
| 61 base::string16* markup, |
| 62 std::string* src_url, |
| 63 uint32* fragment_start, |
| 64 uint32* fragment_end) const { |
| 65 markup->clear(); |
| 66 src_url->clear(); |
| 67 const DataStore& store = GetStore(type); |
| 68 auto it = store.data.find(GetHtmlFormatType()); |
| 69 if (it != store.data.end()) |
| 70 *markup = base::UTF8ToUTF16(it->second); |
| 71 *src_url = store.html_src_url; |
| 72 *fragment_start = 0; |
| 73 *fragment_end = base::checked_cast<uint32>(markup->size()); |
| 74 } |
| 75 |
| 76 void TestClipboard::ReadRTF(ClipboardType type, std::string* result) const { |
| 77 result->clear(); |
| 78 const DataStore& store = GetStore(type); |
| 79 auto it = store.data.find(GetRtfFormatType()); |
| 80 if (it != store.data.end()) |
| 81 *result = it->second; |
| 82 } |
| 83 |
| 84 SkBitmap TestClipboard::ReadImage(ClipboardType type) const { |
| 85 return GetStore(type).image; |
| 86 } |
| 87 |
| 88 void TestClipboard::ReadCustomData(ClipboardType clipboard_type, |
| 89 const base::string16& type, |
| 90 base::string16* result) const { |
| 91 } |
| 92 |
| 93 void TestClipboard::ReadBookmark(base::string16* title, |
| 94 std::string* url) const { |
| 95 const DataStore& store = GetDefaultStore(); |
| 96 auto it = store.data.find(GetUrlWFormatType()); |
| 97 if (it != store.data.end()) |
| 98 *url = it->second; |
| 99 *title = base::UTF8ToUTF16(store.url_title); |
| 100 } |
| 101 |
| 102 void TestClipboard::ReadData(const FormatType& format, |
| 103 std::string* result) const { |
| 104 result->clear(); |
| 105 const DataStore& store = GetDefaultStore(); |
| 106 auto it = store.data.find(format); |
| 107 if (it != store.data.end()) |
| 108 *result = it->second; |
| 109 } |
| 110 |
| 111 void TestClipboard::WriteObjects(ClipboardType type, const ObjectMap& objects) { |
| 112 Clear(type); |
| 113 default_store_type_ = type; |
| 114 for (const auto& kv : objects) |
| 115 DispatchObject(static_cast<ObjectType>(kv.first), kv.second); |
| 116 default_store_type_ = CLIPBOARD_TYPE_COPY_PASTE; |
| 117 } |
| 118 |
| 119 void TestClipboard::WriteText(const char* text_data, size_t text_len) { |
| 120 std::string text(text_data, text_len); |
| 121 GetDefaultStore().data[GetPlainTextFormatType()] = text; |
| 122 // Create a dummy entry. |
| 123 GetDefaultStore().data[GetPlainTextWFormatType()]; |
| 124 if (IsSupportedClipboardType(CLIPBOARD_TYPE_SELECTION)) |
| 125 GetStore(CLIPBOARD_TYPE_SELECTION).data[GetPlainTextFormatType()] = text; |
| 126 } |
| 127 |
| 128 void TestClipboard::WriteHTML(const char* markup_data, |
| 129 size_t markup_len, |
| 130 const char* url_data, |
| 131 size_t url_len) { |
| 132 base::string16 markup; |
| 133 base::UTF8ToUTF16(markup_data, markup_len, &markup); |
| 134 GetDefaultStore().data[GetHtmlFormatType()] = base::UTF16ToUTF8(markup); |
| 135 GetDefaultStore().html_src_url = std::string(url_data, url_len); |
| 136 } |
| 137 |
| 138 void TestClipboard::WriteRTF(const char* rtf_data, size_t data_len) { |
| 139 GetDefaultStore().data[GetRtfFormatType()] = std::string(rtf_data, data_len); |
| 140 } |
| 141 |
| 142 void TestClipboard::WriteBookmark(const char* title_data, |
| 143 size_t title_len, |
| 144 const char* url_data, |
| 145 size_t url_len) { |
| 146 GetDefaultStore().data[GetUrlWFormatType()] = std::string(url_data, url_len); |
| 147 GetDefaultStore().url_title = std::string(title_data, title_len); |
| 148 } |
| 149 |
| 150 void TestClipboard::WriteWebSmartPaste() { |
| 151 // Create a dummy entry. |
| 152 GetDefaultStore().data[GetWebKitSmartPasteFormatType()]; |
| 153 } |
| 154 |
| 155 void TestClipboard::WriteBitmap(const SkBitmap& bitmap) { |
| 156 // Create a dummy entry. |
| 157 GetDefaultStore().data[GetBitmapFormatType()]; |
| 158 bitmap.copyTo(&GetDefaultStore().image); |
| 159 } |
| 160 |
| 161 void TestClipboard::WriteData(const FormatType& format, |
| 162 const char* data_data, |
| 163 size_t data_len) { |
| 164 GetDefaultStore().data[format] = std::string(data_data, data_len); |
| 165 } |
| 166 |
| 167 TestClipboard::DataStore::DataStore() : sequence_number(0) { |
| 168 } |
| 169 |
| 170 TestClipboard::DataStore::~DataStore() { |
| 171 } |
| 172 |
| 173 void TestClipboard::DataStore::Clear() { |
| 174 data.clear(); |
| 175 url_title.clear(); |
| 176 html_src_url.clear(); |
| 177 image = SkBitmap(); |
| 178 } |
| 179 |
| 180 const TestClipboard::DataStore& TestClipboard::GetStore( |
| 181 ClipboardType type) const { |
| 182 CHECK(IsSupportedClipboardType(type)); |
| 183 return stores_[type]; |
| 184 } |
| 185 |
| 186 TestClipboard::DataStore& TestClipboard::GetStore(ClipboardType type) { |
| 187 CHECK(IsSupportedClipboardType(type)); |
| 188 DataStore& store = stores_[type]; |
| 189 ++store.sequence_number; |
| 190 return store; |
| 191 } |
| 192 |
| 193 const TestClipboard::DataStore& TestClipboard::GetDefaultStore() const { |
| 194 return GetStore(default_store_type_); |
| 195 } |
| 196 |
| 197 TestClipboard::DataStore& TestClipboard::GetDefaultStore() { |
| 198 return GetStore(default_store_type_); |
| 199 } |
| 200 |
| 201 } // namespace ui |
OLD | NEW |