| 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 "remoting/host/chromeos/clipboard_aura.h" | |
| 6 | |
| 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" | |
| 20 | |
| 21 using testing::_; | |
| 22 using testing::Eq; | |
| 23 using testing::InvokeWithoutArgs; | |
| 24 using testing::Property; | |
| 25 | |
| 26 namespace remoting { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 const base::TimeDelta kTestOverridePollingInterval = | |
| 31 base::TimeDelta::FromMilliseconds(1); | |
| 32 | |
| 33 class ClientClipboard : public protocol::ClipboardStub { | |
| 34 public: | |
| 35 ClientClipboard(); | |
| 36 MOCK_METHOD1(InjectClipboardEvent, | |
| 37 void(const protocol::ClipboardEvent& event)); | |
| 38 | |
| 39 private: | |
| 40 DISALLOW_COPY_AND_ASSIGN(ClientClipboard); | |
| 41 }; | |
| 42 | |
| 43 ClientClipboard::ClientClipboard() { | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 class ClipboardAuraTest : public testing::Test { | |
| 49 public: | |
| 50 ClipboardAuraTest() {} | |
| 51 void SetUp() override; | |
| 52 void TearDown() override; | |
| 53 | |
| 54 protected: | |
| 55 void StopAndResetClipboard(); | |
| 56 | |
| 57 base::MessageLoopForUI message_loop_; | |
| 58 base::RunLoop run_loop_; | |
| 59 ClientClipboard* client_clipboard_; | |
| 60 scoped_ptr<ClipboardAura> clipboard_; | |
| 61 }; | |
| 62 | |
| 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); | |
| 69 | |
| 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_)); | |
| 76 | |
| 77 EXPECT_GT(TestTimeouts::tiny_timeout(), kTestOverridePollingInterval * 10) | |
| 78 << "The test timeout should be greater than the polling interval"; | |
| 79 clipboard_->SetPollingIntervalForTesting(kTestOverridePollingInterval); | |
| 80 } | |
| 81 | |
| 82 void ClipboardAuraTest::TearDown() { | |
| 83 ui::Clipboard::DestroyClipboardForCurrentThread(); | |
| 84 } | |
| 85 | |
| 86 void ClipboardAuraTest::StopAndResetClipboard() { | |
| 87 clipboard_->Stop(); | |
| 88 clipboard_.reset(); | |
| 89 } | |
| 90 | |
| 91 TEST_F(ClipboardAuraTest, WriteToClipboard) { | |
| 92 protocol::ClipboardEvent event; | |
| 93 event.set_mime_type(kMimeTypeTextUtf8); | |
| 94 event.set_data("Test data."); | |
| 95 | |
| 96 clipboard_->InjectClipboardEvent(event); | |
| 97 StopAndResetClipboard(); | |
| 98 run_loop_.RunUntilIdle(); | |
| 99 | |
| 100 std::string clipboard_data; | |
| 101 ui::Clipboard* aura_clipboard = ui::Clipboard::GetForCurrentThread(); | |
| 102 aura_clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_data); | |
| 103 | |
| 104 EXPECT_EQ(clipboard_data, "Test data.") | |
| 105 << "InjectClipboardEvent should write to aura clipboard"; | |
| 106 } | |
| 107 | |
| 108 TEST_F(ClipboardAuraTest, MonitorClipboardChanges) { | |
| 109 { | |
| 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.")); | |
| 113 } | |
| 114 | |
| 115 EXPECT_CALL(*client_clipboard_, | |
| 116 InjectClipboardEvent(Property(&protocol::ClipboardEvent::data, | |
| 117 Eq("Test data.")))).Times(1); | |
| 118 | |
| 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(); | |
| 127 } | |
| 128 | |
| 129 } // namespace remoting | |
| OLD | NEW |