Index: content/public/test/browser_test_utils.cc |
diff --git a/content/public/test/browser_test_utils.cc b/content/public/test/browser_test_utils.cc |
index ca51b0575028171c30990b686532a6c518739d2a..aede81c9be4c29dc52271c9955bc341b9d9990a4 100644 |
--- a/content/public/test/browser_test_utils.cc |
+++ b/content/public/test/browser_test_utils.cc |
@@ -41,6 +41,7 @@ |
#include "content/common/view_messages.h" |
#include "content/public/browser/browser_context.h" |
#include "content/public/browser/browser_plugin_guest_manager.h" |
+#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/histogram_fetcher.h" |
#include "content/public/browser/navigation_entry.h" |
#include "content/public/browser/notification_service.h" |
@@ -62,7 +63,10 @@ |
#include "net/url_request/url_request_context.h" |
#include "net/url_request/url_request_context_getter.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/base/clipboard/clipboard.h" |
+#include "ui/base/clipboard/scoped_clipboard_writer.h" |
#include "ui/base/resource/resource_bundle.h" |
+#include "ui/base/test/test_clipboard.h" |
#include "ui/compositor/test/draw_waiter_for_test.h" |
#include "ui/events/gesture_detection/gesture_configuration.h" |
#include "ui/events/keycodes/dom/dom_code.h" |
@@ -1431,4 +1435,85 @@ uint32_t InputMsgWatcher::WaitForAck() { |
return ack_result_; |
} |
+BrowserTestClipboard::BrowserTestClipboard() : is_installed_(false) {} |
dcheng
2016/07/20 07:02:17
Nit: let's just merge SetUp and TearDown into the
|
+ |
+BrowserTestClipboard::~BrowserTestClipboard() { |
+ DCHECK(!is_installed_); |
+} |
+ |
+void BrowserTestClipboard::SetUp() { |
+ DCHECK(!is_installed_); |
+#if defined(OS_WIN) |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ RunTaskOnIOThreadAndWait( |
+ base::Bind(&BrowserTestClipboard::SetUp, base::Unretained(this))); |
+ is_installed_ = true; |
+ return; |
+ } |
+#endif |
+ ui::TestClipboard::CreateForCurrentThread(); |
+#if !defined(OS_WIN) |
+ is_installed_ = true; |
+#endif |
+} |
+ |
+void BrowserTestClipboard::TearDown() { |
+ DCHECK(is_installed_); |
+#if defined(OS_WIN) |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ RunTaskOnIOThreadAndWait( |
+ base::Bind(&BrowserTestClipboard::TearDown, base::Unretained(this))); |
+ is_installed_ = false; |
+ return; |
+ } |
+#endif |
+ ui::Clipboard::DestroyClipboardForCurrentThread(); |
+#if !defined(OS_WIN) |
+ is_installed_ = false; |
+#endif |
+} |
+ |
+void BrowserTestClipboard::SetRtf(const std::string& rtf) { |
+ DCHECK(is_installed_); |
+#if defined(OS_WIN) |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ RunTaskOnIOThreadAndWait(base::Bind(&BrowserTestClipboard::SetRtf, |
+ base::Unretained(this), rtf)); |
+ return; |
+ } |
+#endif |
+ ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); |
+ clipboard_writer.WriteRTF(rtf); |
+} |
+ |
+void BrowserTestClipboard::SetText(const std::string& text) { |
+ DCHECK(is_installed_); |
+#if defined(OS_WIN) |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ RunTaskOnIOThreadAndWait(base::Bind(&BrowserTestClipboard::SetText, |
+ base::Unretained(this), text)); |
dcheng
2016/07/20 07:02:17
Hmm... what a mess. I guess I need to clean up the
|
+ return; |
+ } |
+#endif |
+ ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); |
+ clipboard_writer.WriteText(base::ASCIIToUTF16(text)); |
+} |
+ |
+void BrowserTestClipboard::RunTaskOnIOThreadAndWait(const base::Closure& task) { |
+ base::WaitableEvent completion( |
+ base::WaitableEvent::ResetPolicy::AUTOMATIC, |
+ base::WaitableEvent::InitialState::NOT_SIGNALED); |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&BrowserTestClipboard::RunTask, base::Unretained(this), |
+ task, &completion)); |
+ completion.Wait(); |
+} |
+ |
+void BrowserTestClipboard::RunTask(const base::Closure& task, |
+ base::WaitableEvent* completion) { |
+ task.Run(); |
+ completion->Signal(); |
+} |
+ |
} // namespace content |