OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/test/test_clipboard.h" | 5 #include "ui/base/test/test_clipboard.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 9 #include "base/auto_reset.h" |
8 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/run_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
10 | 13 |
11 namespace ui { | 14 namespace ui { |
12 | 15 |
13 TestClipboard::TestClipboard() | 16 TestClipboard::TestClipboard() |
14 : default_store_type_(CLIPBOARD_TYPE_COPY_PASTE) { | 17 : default_store_type_(CLIPBOARD_TYPE_COPY_PASTE), |
| 18 waiting_for_write_text_(false), |
| 19 write_text_called_(false) { |
15 } | 20 } |
16 | 21 |
17 TestClipboard::~TestClipboard() { | 22 TestClipboard::~TestClipboard() { |
18 } | 23 } |
19 | 24 |
20 Clipboard* TestClipboard::CreateForCurrentThread() { | 25 Clipboard* TestClipboard::CreateForCurrentThread() { |
21 base::AutoLock lock(Clipboard::clipboard_map_lock_.Get()); | 26 base::AutoLock lock(Clipboard::clipboard_map_lock_.Get()); |
22 Clipboard* clipboard = new TestClipboard; | 27 Clipboard* clipboard = new TestClipboard; |
23 Clipboard::clipboard_map_.Get()[base::PlatformThread::CurrentId()] = | 28 Clipboard::clipboard_map_.Get()[base::PlatformThread::CurrentId()] = |
24 clipboard; | 29 clipboard; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 default_store_type_ = CLIPBOARD_TYPE_COPY_PASTE; | 123 default_store_type_ = CLIPBOARD_TYPE_COPY_PASTE; |
119 } | 124 } |
120 | 125 |
121 void TestClipboard::WriteText(const char* text_data, size_t text_len) { | 126 void TestClipboard::WriteText(const char* text_data, size_t text_len) { |
122 std::string text(text_data, text_len); | 127 std::string text(text_data, text_len); |
123 GetDefaultStore().data[GetPlainTextFormatType()] = text; | 128 GetDefaultStore().data[GetPlainTextFormatType()] = text; |
124 // Create a dummy entry. | 129 // Create a dummy entry. |
125 GetDefaultStore().data[GetPlainTextWFormatType()]; | 130 GetDefaultStore().data[GetPlainTextWFormatType()]; |
126 if (IsSupportedClipboardType(CLIPBOARD_TYPE_SELECTION)) | 131 if (IsSupportedClipboardType(CLIPBOARD_TYPE_SELECTION)) |
127 GetStore(CLIPBOARD_TYPE_SELECTION).data[GetPlainTextFormatType()] = text; | 132 GetStore(CLIPBOARD_TYPE_SELECTION).data[GetPlainTextFormatType()] = text; |
| 133 |
| 134 write_text_called_ = true; |
| 135 if (waiting_for_write_text_) { |
| 136 waiting_for_write_text_ = false; |
| 137 write_text_wait_quit_closure_.Run(); |
| 138 } |
128 } | 139 } |
129 | 140 |
130 void TestClipboard::WriteHTML(const char* markup_data, | 141 void TestClipboard::WriteHTML(const char* markup_data, |
131 size_t markup_len, | 142 size_t markup_len, |
132 const char* url_data, | 143 const char* url_data, |
133 size_t url_len) { | 144 size_t url_len) { |
134 base::string16 markup; | 145 base::string16 markup; |
135 base::UTF8ToUTF16(markup_data, markup_len, &markup); | 146 base::UTF8ToUTF16(markup_data, markup_len, &markup); |
136 GetDefaultStore().data[GetHtmlFormatType()] = base::UTF16ToUTF8(markup); | 147 GetDefaultStore().data[GetHtmlFormatType()] = base::UTF16ToUTF8(markup); |
137 GetDefaultStore().html_src_url = std::string(url_data, url_len); | 148 GetDefaultStore().html_src_url = std::string(url_data, url_len); |
(...skipping 21 matching lines...) Expand all Loading... |
159 GetDefaultStore().data[GetBitmapFormatType()]; | 170 GetDefaultStore().data[GetBitmapFormatType()]; |
160 bitmap.copyTo(&GetDefaultStore().image); | 171 bitmap.copyTo(&GetDefaultStore().image); |
161 } | 172 } |
162 | 173 |
163 void TestClipboard::WriteData(const FormatType& format, | 174 void TestClipboard::WriteData(const FormatType& format, |
164 const char* data_data, | 175 const char* data_data, |
165 size_t data_len) { | 176 size_t data_len) { |
166 GetDefaultStore().data[format] = std::string(data_data, data_len); | 177 GetDefaultStore().data[format] = std::string(data_data, data_len); |
167 } | 178 } |
168 | 179 |
| 180 void TestClipboard::ResetWaits() { |
| 181 waiting_for_write_text_ = false; |
| 182 write_text_called_ = false; |
| 183 } |
| 184 |
| 185 void TestClipboard::WaitForWriteText() { |
| 186 DCHECK(!waiting_for_write_text_); |
| 187 if (write_text_called_) |
| 188 return; |
| 189 base::RunLoop run_loop; |
| 190 base::AutoReset<base::Closure> reset_quit_closure( |
| 191 &write_text_wait_quit_closure_, run_loop.QuitClosure()); |
| 192 base::AutoReset<bool> reset_waiting_for_write_text_(&waiting_for_write_text_, |
| 193 true); |
| 194 run_loop.Run(); |
| 195 } |
| 196 |
169 TestClipboard::DataStore::DataStore() : sequence_number(0) { | 197 TestClipboard::DataStore::DataStore() : sequence_number(0) { |
170 } | 198 } |
171 | 199 |
172 TestClipboard::DataStore::~DataStore() { | 200 TestClipboard::DataStore::~DataStore() { |
173 } | 201 } |
174 | 202 |
175 void TestClipboard::DataStore::Clear() { | 203 void TestClipboard::DataStore::Clear() { |
176 data.clear(); | 204 data.clear(); |
177 url_title.clear(); | 205 url_title.clear(); |
178 html_src_url.clear(); | 206 html_src_url.clear(); |
(...skipping 15 matching lines...) Expand all Loading... |
194 | 222 |
195 const TestClipboard::DataStore& TestClipboard::GetDefaultStore() const { | 223 const TestClipboard::DataStore& TestClipboard::GetDefaultStore() const { |
196 return GetStore(default_store_type_); | 224 return GetStore(default_store_type_); |
197 } | 225 } |
198 | 226 |
199 TestClipboard::DataStore& TestClipboard::GetDefaultStore() { | 227 TestClipboard::DataStore& TestClipboard::GetDefaultStore() { |
200 return GetStore(default_store_type_); | 228 return GetStore(default_store_type_); |
201 } | 229 } |
202 | 230 |
203 } // namespace ui | 231 } // namespace ui |
OLD | NEW |