Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / remoting / host / chromeos / clipboard_aura_unittest.cc
blobb375e8a601dbf2b6f6a0cb2b2a4746c6221aedda
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/chromeos/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 ClientClipboard* client_clipboard_;
59 scoped_ptr<ClipboardAura> clipboard_;
62 void ClipboardAuraTest::SetUp() {
63 // Alert the clipboard class to which threads are allowed to access the
64 // clipboard.
65 std::vector<base::PlatformThreadId> allowed_clipboard_threads;
66 allowed_clipboard_threads.push_back(base::PlatformThread::CurrentId());
67 ui::Clipboard::SetAllowedThreads(allowed_clipboard_threads);
69 // Setup the clipboard.
70 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
71 message_loop_.message_loop_proxy();
72 client_clipboard_ = new ClientClipboard();
73 clipboard_.reset(new ClipboardAura());
75 EXPECT_GT(TestTimeouts::tiny_timeout(), kTestOverridePollingInterval * 10)
76 << "The test timeout should be greater than the polling interval";
77 clipboard_->SetPollingIntervalForTesting(kTestOverridePollingInterval);
79 clipboard_->Start(make_scoped_ptr(client_clipboard_));
82 void ClipboardAuraTest::TearDown() {
83 ui::Clipboard::DestroyClipboardForCurrentThread();
86 void ClipboardAuraTest::StopAndResetClipboard() {
87 clipboard_.reset();
90 TEST_F(ClipboardAuraTest, WriteToClipboard) {
91 protocol::ClipboardEvent event;
92 event.set_mime_type(kMimeTypeTextUtf8);
93 event.set_data("Test data.");
95 clipboard_->InjectClipboardEvent(event);
96 StopAndResetClipboard();
97 base::RunLoop().RunUntilIdle();
99 std::string clipboard_data;
100 ui::Clipboard* aura_clipboard = ui::Clipboard::GetForCurrentThread();
101 aura_clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_data);
103 EXPECT_EQ(clipboard_data, "Test data.")
104 << "InjectClipboardEvent should write to aura clipboard";
107 TEST_F(ClipboardAuraTest, MonitorClipboardChanges) {
108 base::RunLoop().RunUntilIdle();
111 // |clipboard_writer| will write to the clipboard when it goes out of scope.
112 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
113 clipboard_writer.WriteText(base::UTF8ToUTF16("Test data."));
116 EXPECT_CALL(*client_clipboard_,
117 InjectClipboardEvent(Property(&protocol::ClipboardEvent::data,
118 Eq("Test data.")))).Times(1);
120 base::RunLoop run_loop;
121 message_loop_.PostDelayedTask(
122 FROM_HERE, base::Bind(&ClipboardAuraTest_MonitorClipboardChanges_Test::
123 StopAndResetClipboard,
124 base::Unretained(this)),
125 TestTimeouts::tiny_timeout());
126 message_loop_.PostDelayedTask(FROM_HERE, run_loop.QuitClosure(),
127 TestTimeouts::tiny_timeout());
128 run_loop.Run();
131 } // namespace remoting