1 // Copyright 2015 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/mouse_shape_pump.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "remoting/base/auto_thread.h"
12 #include "remoting/base/auto_thread_task_runner.h"
13 #include "remoting/host/host_mock_objects.h"
14 #include "remoting/proto/control.pb.h"
15 #include "remoting/proto/video.pb.h"
16 #include "remoting/protocol/protocol_mock_objects.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
20 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
21 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
23 using ::remoting::protocol::MockClientStub
;
26 using ::testing::DoAll
;
27 using ::testing::InSequence
;
28 using ::testing::InvokeWithoutArgs
;
32 static const int kCursorWidth
= 64;
33 static const int kCursorHeight
= 32;
34 static const int kHotspotX
= 11;
35 static const int kHotspotY
= 12;
37 class ThreadCheckMouseCursorMonitor
: public webrtc::MouseCursorMonitor
{
39 ThreadCheckMouseCursorMonitor(
40 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
41 : task_runner_(task_runner
), callback_(nullptr) {
43 ~ThreadCheckMouseCursorMonitor() override
{
44 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
47 void Init(Callback
* callback
, Mode mode
) override
{
48 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
49 EXPECT_FALSE(callback_
);
50 EXPECT_TRUE(callback
);
55 void Capture() override
{
56 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
57 ASSERT_TRUE(callback_
);
59 scoped_ptr
<webrtc::MouseCursor
> mouse_cursor(new webrtc::MouseCursor(
60 new webrtc::BasicDesktopFrame(
61 webrtc::DesktopSize(kCursorWidth
, kCursorHeight
)),
62 webrtc::DesktopVector(kHotspotX
, kHotspotY
)));
64 callback_
->OnMouseCursor(mouse_cursor
.release());
68 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
72 DISALLOW_COPY_AND_ASSIGN(ThreadCheckMouseCursorMonitor
);
75 class MouseShapePumpTest
: public testing::Test
{
77 void SetUp() override
;
78 void TearDown() override
;
80 void SetCursorShape(const protocol::CursorShapeInfo
& cursor_shape
);
83 base::MessageLoop message_loop_
;
84 base::RunLoop run_loop_
;
85 scoped_refptr
<AutoThreadTaskRunner
> capture_task_runner_
;
86 scoped_refptr
<AutoThreadTaskRunner
> main_task_runner_
;
87 scoped_ptr
<MouseShapePump
> pump_
;
89 MockClientStub client_stub_
;
92 void MouseShapePumpTest::SetUp() {
93 main_task_runner_
= new AutoThreadTaskRunner(
94 message_loop_
.message_loop_proxy(), run_loop_
.QuitClosure());
95 capture_task_runner_
= AutoThread::Create("capture", main_task_runner_
);
98 void MouseShapePumpTest::TearDown() {
101 // Release the task runners, so that the test can quit.
102 capture_task_runner_
= nullptr;
103 main_task_runner_
= nullptr;
105 // Run the MessageLoop until everything has torn down.
109 void MouseShapePumpTest::SetCursorShape(
110 const protocol::CursorShapeInfo
& cursor_shape
) {
111 EXPECT_TRUE(cursor_shape
.has_width());
112 EXPECT_EQ(kCursorWidth
, cursor_shape
.width());
113 EXPECT_TRUE(cursor_shape
.has_height());
114 EXPECT_EQ(kCursorHeight
, cursor_shape
.height());
115 EXPECT_TRUE(cursor_shape
.has_hotspot_x());
116 EXPECT_EQ(kHotspotX
, cursor_shape
.hotspot_x());
117 EXPECT_TRUE(cursor_shape
.has_hotspot_y());
118 EXPECT_EQ(kHotspotY
, cursor_shape
.hotspot_y());
119 EXPECT_TRUE(cursor_shape
.has_data());
120 EXPECT_EQ(kCursorWidth
* kCursorHeight
* webrtc::DesktopFrame::kBytesPerPixel
,
121 static_cast<int>(cursor_shape
.data().size()));
124 // This test mocks MouseCursorMonitor and ClientStub to verify that the
125 // MouseShapePump sends the cursor successfully.
126 TEST_F(MouseShapePumpTest
, FirstCursor
) {
127 scoped_ptr
<ThreadCheckMouseCursorMonitor
> cursor_monitor(
128 new ThreadCheckMouseCursorMonitor(capture_task_runner_
));
130 base::RunLoop run_loop
;
132 // Stop the |run_loop| once it has captured the cursor.
133 EXPECT_CALL(client_stub_
, SetCursorShape(_
))
135 Invoke(this, &MouseShapePumpTest::SetCursorShape
),
136 InvokeWithoutArgs(&run_loop
, &base::RunLoop::Quit
)))
137 .RetiresOnSaturation();
140 pump_
.reset(new MouseShapePump(capture_task_runner_
, cursor_monitor
.Pass(),
146 } // namespace remoting