OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "remoting/host/mouse_shape_pump.h" | |
6 | |
7 #include "base/bind.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/fake_mouse_cursor_monitor.h" | |
14 #include "remoting/host/host_mock_objects.h" | |
15 #include "remoting/proto/control.pb.h" | |
16 #include "remoting/proto/video.pb.h" | |
17 #include "remoting/protocol/protocol_mock_objects.h" | |
18 #include "testing/gmock/include/gmock/gmock.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | |
21 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" | |
22 | |
23 using ::remoting::protocol::MockClientStub; | |
24 | |
25 using ::testing::_; | |
26 using ::testing::DoAll; | |
27 using ::testing::InSequence; | |
28 using ::testing::InvokeWithoutArgs; | |
29 | |
30 namespace remoting { | |
31 | |
32 static const int kCursorWidth = 64; | |
33 static const int kCursorHeight = 32; | |
34 static const int kHotspotX = 11; | |
35 static const int kHotspotY = 12; | |
36 | |
37 class ThreadCheckMouseCursorMonitor : public FakeMouseCursorMonitor { | |
38 public: | |
39 ThreadCheckMouseCursorMonitor( | |
40 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
41 : task_runner_(task_runner) { | |
42 } | |
43 ~ThreadCheckMouseCursorMonitor() override { | |
44 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
45 } | |
46 | |
47 private: | |
48 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(ThreadCheckMouseCursorMonitor); | |
51 }; | |
52 | |
53 class MouseShapePumpTest : public testing::Test { | |
54 public: | |
55 MouseShapePumpTest(); | |
56 | |
57 void SetUp() override; | |
58 void TearDown() override; | |
59 | |
60 void StartMouseShapePump( | |
61 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor); | |
62 void StopMouseShapePump(); | |
63 | |
64 // webrtc::MouseCursorMonitor mocks. | |
65 void OnMouseCursorMonitorInit( | |
66 webrtc::MouseCursorMonitor::Callback* callback, | |
67 webrtc::MouseCursorMonitor::Mode mode); | |
68 void OnCaptureMouse(); | |
69 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape); | |
70 | |
71 protected: | |
72 base::MessageLoop message_loop_; | |
73 base::RunLoop run_loop_; | |
74 scoped_refptr<AutoThreadTaskRunner> capture_task_runner_; | |
75 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; | |
76 scoped_ptr<MouseShapePump> pump_; | |
77 | |
78 MockClientStub client_stub_; | |
79 | |
80 // Points to the callback passed to webrtc::MouseCursor::Init(). | |
81 webrtc::MouseCursorMonitor::Callback* mouse_monitor_callback_; | |
82 | |
83 private: | |
84 DISALLOW_COPY_AND_ASSIGN(MouseShapePumpTest); | |
85 }; | |
86 | |
87 MouseShapePumpTest::MouseShapePumpTest() | |
88 : mouse_monitor_callback_(nullptr) { | |
89 } | |
90 | |
91 void MouseShapePumpTest::SetUp() { | |
92 main_task_runner_ = new AutoThreadTaskRunner( | |
93 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); | |
94 capture_task_runner_ = main_task_runner_; | |
95 } | |
96 | |
97 void MouseShapePumpTest::TearDown() { | |
98 // Release the task runners, so that the test can quit. | |
99 capture_task_runner_ = nullptr; | |
100 main_task_runner_ = nullptr; | |
101 | |
102 // Run the MessageLoop until everything has torn down. | |
103 run_loop_.Run(); | |
104 } | |
105 | |
106 void MouseShapePumpTest::StartMouseShapePump( | |
107 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor) { | |
108 pump_.reset(new MouseShapePump(main_task_runner_, capture_task_runner_, | |
109 mouse_monitor.Pass(), &client_stub_)); | |
110 } | |
111 | |
112 void MouseShapePumpTest::StopMouseShapePump() { | |
113 pump_.reset(); | |
114 } | |
115 | |
116 void MouseShapePumpTest::OnCaptureMouse() { | |
117 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
| |
118 | |
119 scoped_ptr<webrtc::MouseCursor> mouse_cursor( | |
120 new webrtc::MouseCursor( | |
121 new webrtc::BasicDesktopFrame( | |
122 webrtc::DesktopSize(kCursorWidth, kCursorHeight)), | |
123 webrtc::DesktopVector(kHotspotX, kHotspotY))); | |
124 | |
125 mouse_monitor_callback_->OnMouseCursor(mouse_cursor.release()); | |
126 } | |
127 | |
128 void MouseShapePumpTest::OnMouseCursorMonitorInit( | |
129 webrtc::MouseCursorMonitor::Callback* callback, | |
130 webrtc::MouseCursorMonitor::Mode mode) { | |
131 EXPECT_FALSE(mouse_monitor_callback_); | |
132 EXPECT_TRUE(callback); | |
133 | |
134 mouse_monitor_callback_ = callback; | |
135 } | |
136 | |
137 void MouseShapePumpTest::SetCursorShape( | |
138 const protocol::CursorShapeInfo& cursor_shape) { | |
139 EXPECT_TRUE(cursor_shape.has_width()); | |
140 EXPECT_EQ(kCursorWidth, cursor_shape.width()); | |
141 EXPECT_TRUE(cursor_shape.has_height()); | |
142 EXPECT_EQ(kCursorHeight, cursor_shape.height()); | |
143 EXPECT_TRUE(cursor_shape.has_hotspot_x()); | |
144 EXPECT_EQ(kHotspotX, cursor_shape.hotspot_x()); | |
145 EXPECT_TRUE(cursor_shape.has_hotspot_y()); | |
146 EXPECT_EQ(kHotspotY, cursor_shape.hotspot_y()); | |
147 EXPECT_TRUE(cursor_shape.has_data()); | |
148 EXPECT_EQ(kCursorWidth * kCursorHeight * webrtc::DesktopFrame::kBytesPerPixel, | |
149 static_cast<int>(cursor_shape.data().size())); | |
150 } | |
151 | |
152 // 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.
| |
153 // MouseShapePump sends the cursor successfully. | |
154 TEST_F(MouseShapePumpTest, FirstCursor) { | |
155 scoped_ptr<MockMouseCursorMonitor> cursor_monitor( | |
156 new MockMouseCursorMonitor()); | |
157 | |
158 { | |
159 InSequence s; | |
160 | |
161 EXPECT_CALL(*cursor_monitor, Init(_, _)) | |
162 .WillOnce( | |
163 Invoke(this, &MouseShapePumpTest::OnMouseCursorMonitorInit)); | |
164 | |
165 EXPECT_CALL(*cursor_monitor, Capture()) | |
166 .WillRepeatedly(Invoke(this, &MouseShapePumpTest::OnCaptureMouse)); | |
167 } | |
168 | |
169 // Stop the pump once it has captured the cursor. | |
170 EXPECT_CALL(client_stub_, SetCursorShape(_)) | |
171 .WillOnce(DoAll( | |
172 Invoke(this, &MouseShapePumpTest::SetCursorShape), | |
173 InvokeWithoutArgs(this, &MouseShapePumpTest::StopMouseShapePump))) | |
174 .RetiresOnSaturation(); | |
175 | |
176 // Start the pump. | |
177 StartMouseShapePump(cursor_monitor.Pass()); | |
178 | |
179 // Run until there are no more pending tasks from the MouseShapePump. | |
180 base::RunLoop().RunUntilIdle(); | |
181 } | |
182 | |
183 // Verify that the mouse monitor is torn down on the correct thread. | |
184 TEST_F(MouseShapePumpTest, DeleteOnThreads) { | |
185 capture_task_runner_ = AutoThread::Create("capture", main_task_runner_); | |
186 | |
187 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( | |
188 new ThreadCheckMouseCursorMonitor(capture_task_runner_)); | |
189 | |
190 // Start and stop the pump, so it will tear down the mouse monitor. | |
191 StartMouseShapePump(mouse_cursor_monitor.Pass()); | |
192 StopMouseShapePump(); | |
193 } | |
194 | |
195 } // namespace remoting | |
OLD | NEW |