Chromium Code Reviews| Index: remoting/host/mouse_shape_pump_unittest.cc |
| diff --git a/remoting/host/mouse_shape_pump_unittest.cc b/remoting/host/mouse_shape_pump_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..acbb7f26670957846afb4317fa84f9dab52335c6 |
| --- /dev/null |
| +++ b/remoting/host/mouse_shape_pump_unittest.cc |
| @@ -0,0 +1,195 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/mouse_shape_pump.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "remoting/base/auto_thread.h" |
| +#include "remoting/base/auto_thread_task_runner.h" |
| +#include "remoting/host/fake_mouse_cursor_monitor.h" |
| +#include "remoting/host/host_mock_objects.h" |
| +#include "remoting/proto/control.pb.h" |
| +#include "remoting/proto/video.pb.h" |
| +#include "remoting/protocol/protocol_mock_objects.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| +#include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" |
| + |
| +using ::remoting::protocol::MockClientStub; |
| + |
| +using ::testing::_; |
| +using ::testing::DoAll; |
| +using ::testing::InSequence; |
| +using ::testing::InvokeWithoutArgs; |
| + |
| +namespace remoting { |
| + |
| +static const int kCursorWidth = 64; |
| +static const int kCursorHeight = 32; |
| +static const int kHotspotX = 11; |
| +static const int kHotspotY = 12; |
| + |
| +class ThreadCheckMouseCursorMonitor : public FakeMouseCursorMonitor { |
| + public: |
| + ThreadCheckMouseCursorMonitor( |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| + : task_runner_(task_runner) { |
| + } |
| + ~ThreadCheckMouseCursorMonitor() override { |
| + EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| + } |
| + |
| + private: |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ThreadCheckMouseCursorMonitor); |
| +}; |
| + |
| +class MouseShapePumpTest : public testing::Test { |
| + public: |
| + MouseShapePumpTest(); |
| + |
| + void SetUp() override; |
| + void TearDown() override; |
| + |
| + void StartMouseShapePump( |
| + scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor); |
| + void StopMouseShapePump(); |
| + |
| + // webrtc::MouseCursorMonitor mocks. |
| + void OnMouseCursorMonitorInit( |
| + webrtc::MouseCursorMonitor::Callback* callback, |
| + webrtc::MouseCursorMonitor::Mode mode); |
| + void OnCaptureMouse(); |
| + void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape); |
| + |
| + protected: |
| + base::MessageLoop message_loop_; |
| + base::RunLoop run_loop_; |
| + scoped_refptr<AutoThreadTaskRunner> capture_task_runner_; |
| + scoped_refptr<AutoThreadTaskRunner> main_task_runner_; |
| + scoped_ptr<MouseShapePump> pump_; |
| + |
| + MockClientStub client_stub_; |
| + |
| + // Points to the callback passed to webrtc::MouseCursor::Init(). |
| + webrtc::MouseCursorMonitor::Callback* mouse_monitor_callback_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MouseShapePumpTest); |
| +}; |
| + |
| +MouseShapePumpTest::MouseShapePumpTest() |
| + : mouse_monitor_callback_(nullptr) { |
| +} |
| + |
| +void MouseShapePumpTest::SetUp() { |
| + main_task_runner_ = new AutoThreadTaskRunner( |
| + message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); |
| + capture_task_runner_ = main_task_runner_; |
| +} |
| + |
| +void MouseShapePumpTest::TearDown() { |
| + // Release the task runners, so that the test can quit. |
| + capture_task_runner_ = nullptr; |
| + main_task_runner_ = nullptr; |
| + |
| + // Run the MessageLoop until everything has torn down. |
| + run_loop_.Run(); |
| +} |
| + |
| +void MouseShapePumpTest::StartMouseShapePump( |
| + scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor) { |
| + pump_.reset(new MouseShapePump(main_task_runner_, capture_task_runner_, |
| + mouse_monitor.Pass(), &client_stub_)); |
| +} |
| + |
| +void MouseShapePumpTest::StopMouseShapePump() { |
| + pump_.reset(); |
| +} |
| + |
| +void MouseShapePumpTest::OnCaptureMouse() { |
| + EXPECT_TRUE(mouse_monitor_callback_); |
|
Wez
2015/02/11 01:05:07
nit: Does EXPECT work here, or do you need ASSERT?
Sergey Ulanov
2015/02/11 19:21:35
replaced with ASSERT. the OnMouseCursor() call bel
|
| + |
| + scoped_ptr<webrtc::MouseCursor> mouse_cursor( |
| + new webrtc::MouseCursor( |
| + new webrtc::BasicDesktopFrame( |
| + webrtc::DesktopSize(kCursorWidth, kCursorHeight)), |
| + webrtc::DesktopVector(kHotspotX, kHotspotY))); |
| + |
| + mouse_monitor_callback_->OnMouseCursor(mouse_cursor.release()); |
| +} |
| + |
| +void MouseShapePumpTest::OnMouseCursorMonitorInit( |
| + webrtc::MouseCursorMonitor::Callback* callback, |
| + webrtc::MouseCursorMonitor::Mode mode) { |
| + EXPECT_FALSE(mouse_monitor_callback_); |
| + EXPECT_TRUE(callback); |
| + |
| + mouse_monitor_callback_ = callback; |
| +} |
| + |
| +void MouseShapePumpTest::SetCursorShape( |
| + const protocol::CursorShapeInfo& cursor_shape) { |
| + EXPECT_TRUE(cursor_shape.has_width()); |
| + EXPECT_EQ(kCursorWidth, cursor_shape.width()); |
| + EXPECT_TRUE(cursor_shape.has_height()); |
| + EXPECT_EQ(kCursorHeight, cursor_shape.height()); |
| + EXPECT_TRUE(cursor_shape.has_hotspot_x()); |
| + EXPECT_EQ(kHotspotX, cursor_shape.hotspot_x()); |
| + EXPECT_TRUE(cursor_shape.has_hotspot_y()); |
| + EXPECT_EQ(kHotspotY, cursor_shape.hotspot_y()); |
| + EXPECT_TRUE(cursor_shape.has_data()); |
| + EXPECT_EQ(kCursorWidth * kCursorHeight * webrtc::DesktopFrame::kBytesPerPixel, |
| + static_cast<int>(cursor_shape.data().size())); |
| +} |
| + |
| +// This test mocks MouseCursorMonitor and network layer to verify that the |
|
Wez
2015/02/11 01:05:07
What is it that it mocks from the network layer?
Sergey Ulanov
2015/02/11 19:21:35
ClientStub. updated the comment.
|
| +// MouseShapePump sends the cursor successfully. |
| +TEST_F(MouseShapePumpTest, FirstCursor) { |
| + scoped_ptr<MockMouseCursorMonitor> cursor_monitor( |
| + new MockMouseCursorMonitor()); |
| + |
| + { |
| + InSequence s; |
| + |
| + EXPECT_CALL(*cursor_monitor, Init(_, _)) |
| + .WillOnce( |
| + Invoke(this, &MouseShapePumpTest::OnMouseCursorMonitorInit)); |
| + |
| + EXPECT_CALL(*cursor_monitor, Capture()) |
| + .WillRepeatedly(Invoke(this, &MouseShapePumpTest::OnCaptureMouse)); |
| + } |
| + |
| + // Stop the pump once it has captured the cursor. |
| + EXPECT_CALL(client_stub_, SetCursorShape(_)) |
| + .WillOnce(DoAll( |
| + Invoke(this, &MouseShapePumpTest::SetCursorShape), |
| + InvokeWithoutArgs(this, &MouseShapePumpTest::StopMouseShapePump))) |
| + .RetiresOnSaturation(); |
| + |
| + // Start the pump. |
| + StartMouseShapePump(cursor_monitor.Pass()); |
| + |
| + // Run until there are no more pending tasks from the MouseShapePump. |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +// Verify that the mouse monitor is torn down on the correct thread. |
| +TEST_F(MouseShapePumpTest, DeleteOnThreads) { |
| + capture_task_runner_ = AutoThread::Create("capture", main_task_runner_); |
| + |
| + scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( |
| + new ThreadCheckMouseCursorMonitor(capture_task_runner_)); |
| + |
| + // Start and stop the pump, so it will tear down the mouse monitor. |
| + StartMouseShapePump(mouse_cursor_monitor.Pass()); |
| + StopMouseShapePump(); |
| +} |
| + |
| +} // namespace remoting |