Remote assistance on Chrome OS Part VII - Clipboard Injection
[chromium-blink-merge.git] / remoting / host / clipboard_aura_unittest.cc
blob8993592ee5c0edd64a2c2209e8043c36dad0b94a
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 "remoting/host/clipboard_aura.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/test_timeouts.h"
13 #include "remoting/base/constants.h"
14 #include "remoting/proto/event.pb.h"
15 #include "remoting/protocol/clipboard_stub.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/base/clipboard/clipboard.h"
19 #include "ui/base/clipboard/scoped_clipboard_writer.h"
21 using testing::_;
22 using testing::Eq;
23 using testing::InvokeWithoutArgs;
24 using testing::Property;
26 namespace remoting {
28 namespace {
30 const base::TimeDelta kTestOverridePollingInterval =
31 base::TimeDelta::FromMilliseconds(1);
33 class ClientClipboard : public protocol::ClipboardStub {
34 public:
35 ClientClipboard();
36 MOCK_METHOD1(InjectClipboardEvent,
37 void(const protocol::ClipboardEvent& event));
39 private:
40 DISALLOW_COPY_AND_ASSIGN(ClientClipboard);
43 ClientClipboard::ClientClipboard() {
46 } // namespace
48 class ClipboardAuraTest : public testing::Test {
49 public:
50 ClipboardAuraTest() {}
51 void SetUp() override;
52 void TearDown() override;
54 protected:
55 void StopAndResetClipboard();
57 base::MessageLoopForUI message_loop_;
58 base::RunLoop run_loop_;
59 ClientClipboard* client_clipboard_;
60 scoped_ptr<ClipboardAura> clipboard_;
63 void ClipboardAuraTest::SetUp() {
64 // Alert the clipboard class to which threads are allowed to access the
65 // clipboard.
66 std::vector<base::PlatformThreadId> allowed_clipboard_threads;
67 allowed_clipboard_threads.push_back(base::PlatformThread::CurrentId());
68 ui::Clipboard::SetAllowedThreads(allowed_clipboard_threads);
70 // Setup the clipboard.
71 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
72 message_loop_.message_loop_proxy();
73 client_clipboard_ = new ClientClipboard();
74 clipboard_.reset(new ClipboardAura(task_runner));
75 clipboard_->Start(make_scoped_ptr(client_clipboard_));
77 EXPECT_GT(TestTimeouts::tiny_timeout(), kTestOverridePollingInterval * 10)
78 << "The test timeout should be greater than the polling interval";
79 clipboard_->SetPollingIntervalForTesting(kTestOverridePollingInterval);
82 void ClipboardAuraTest::TearDown() {
83 ui::Clipboard::DestroyClipboardForCurrentThread();
86 void ClipboardAuraTest::StopAndResetClipboard() {
87 clipboard_->Stop();
88 clipboard_.reset();
91 TEST_F(ClipboardAuraTest, WriteToClipboard) {
92 protocol::ClipboardEvent event;
93 event.set_mime_type(kMimeTypeTextUtf8);
94 event.set_data("Test data.");
96 clipboard_->InjectClipboardEvent(event);
97 StopAndResetClipboard();
98 run_loop_.RunUntilIdle();
100 std::string clipboard_data;
101 ui::Clipboard* aura_clipboard = ui::Clipboard::GetForCurrentThread();
102 aura_clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_data);
104 EXPECT_EQ(clipboard_data, "Test data.")
105 << "InjectClipboardEvent should write to aura clipboard";
108 TEST_F(ClipboardAuraTest, MonitorClipboardChanges) {
110 // |clipboard_writer| will write to the clipboard when it goes out of scope.
111 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
112 clipboard_writer.WriteText(base::UTF8ToUTF16("Test data."));
115 EXPECT_CALL(*client_clipboard_,
116 InjectClipboardEvent(Property(&protocol::ClipboardEvent::data,
117 Eq("Test data.")))).Times(1);
119 message_loop_.PostDelayedTask(
120 FROM_HERE, base::Bind(&ClipboardAuraTest_MonitorClipboardChanges_Test::
121 StopAndResetClipboard,
122 base::Unretained(this)),
123 TestTimeouts::tiny_timeout());
124 message_loop_.PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(),
125 TestTimeouts::tiny_timeout());
126 message_loop_.Run();
129 } // namespace remoting