sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 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 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 9 | #include "base/macros.h" |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
| 12 | #include "base/run_loop.h" |
| 13 | #include "base/single_thread_task_runner.h" |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 14 | #include "remoting/host/host_mock_objects.h" |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 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" |
| 22 | |
| 23 | using ::remoting::protocol::MockClientStub; |
| 24 | |
| 25 | using ::testing::_; |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 26 | using ::testing::InvokeWithoutArgs; |
| 27 | |
| 28 | namespace remoting { |
| 29 | |
| 30 | static const int kCursorWidth = 64; |
| 31 | static const int kCursorHeight = 32; |
| 32 | static const int kHotspotX = 11; |
| 33 | static const int kHotspotY = 12; |
| 34 | |
sergeyu | 30cb640e | 2016-02-05 21:19:55 | [diff] [blame] | 35 | class TestMouseCursorMonitor : public webrtc::MouseCursorMonitor { |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 36 | public: |
sergeyu | 30cb640e | 2016-02-05 21:19:55 | [diff] [blame] | 37 | TestMouseCursorMonitor() : callback_(nullptr) {} |
| 38 | ~TestMouseCursorMonitor() override {} |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 39 | |
| 40 | void Init(Callback* callback, Mode mode) override { |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 41 | EXPECT_FALSE(callback_); |
| 42 | EXPECT_TRUE(callback); |
| 43 | |
| 44 | callback_ = callback; |
| 45 | } |
| 46 | |
| 47 | void Capture() override { |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 48 | ASSERT_TRUE(callback_); |
| 49 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 50 | std::unique_ptr<webrtc::MouseCursor> mouse_cursor(new webrtc::MouseCursor( |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 51 | new webrtc::BasicDesktopFrame( |
| 52 | webrtc::DesktopSize(kCursorWidth, kCursorHeight)), |
| 53 | webrtc::DesktopVector(kHotspotX, kHotspotY))); |
| 54 | |
| 55 | callback_->OnMouseCursor(mouse_cursor.release()); |
| 56 | } |
| 57 | |
| 58 | private: |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 59 | Callback* callback_; |
| 60 | |
sergeyu | 30cb640e | 2016-02-05 21:19:55 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(TestMouseCursorMonitor); |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | class MouseShapePumpTest : public testing::Test { |
| 65 | public: |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 66 | void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape); |
| 67 | |
| 68 | protected: |
| 69 | base::MessageLoop message_loop_; |
| 70 | base::RunLoop run_loop_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 71 | std::unique_ptr<MouseShapePump> pump_; |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 72 | |
| 73 | MockClientStub client_stub_; |
| 74 | }; |
| 75 | |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 76 | void MouseShapePumpTest::SetCursorShape( |
| 77 | const protocol::CursorShapeInfo& cursor_shape) { |
| 78 | EXPECT_TRUE(cursor_shape.has_width()); |
| 79 | EXPECT_EQ(kCursorWidth, cursor_shape.width()); |
| 80 | EXPECT_TRUE(cursor_shape.has_height()); |
| 81 | EXPECT_EQ(kCursorHeight, cursor_shape.height()); |
| 82 | EXPECT_TRUE(cursor_shape.has_hotspot_x()); |
| 83 | EXPECT_EQ(kHotspotX, cursor_shape.hotspot_x()); |
| 84 | EXPECT_TRUE(cursor_shape.has_hotspot_y()); |
| 85 | EXPECT_EQ(kHotspotY, cursor_shape.hotspot_y()); |
| 86 | EXPECT_TRUE(cursor_shape.has_data()); |
| 87 | EXPECT_EQ(kCursorWidth * kCursorHeight * webrtc::DesktopFrame::kBytesPerPixel, |
| 88 | static_cast<int>(cursor_shape.data().size())); |
| 89 | } |
| 90 | |
| 91 | // This test mocks MouseCursorMonitor and ClientStub to verify that the |
| 92 | // MouseShapePump sends the cursor successfully. |
| 93 | TEST_F(MouseShapePumpTest, FirstCursor) { |
sergeyu | 30cb640e | 2016-02-05 21:19:55 | [diff] [blame] | 94 | // Stop the |run_loop_| once it has captured the cursor. |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 95 | EXPECT_CALL(client_stub_, SetCursorShape(_)) |
| 96 | .WillOnce(DoAll( |
| 97 | Invoke(this, &MouseShapePumpTest::SetCursorShape), |
sergeyu | 30cb640e | 2016-02-05 21:19:55 | [diff] [blame] | 98 | InvokeWithoutArgs(&run_loop_, &base::RunLoop::Quit))) |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 99 | .RetiresOnSaturation(); |
| 100 | |
| 101 | // Start the pump. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 102 | pump_.reset(new MouseShapePump(base::WrapUnique(new TestMouseCursorMonitor()), |
sergeyu | 30cb640e | 2016-02-05 21:19:55 | [diff] [blame] | 103 | &client_stub_)); |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 104 | |
sergeyu | 30cb640e | 2016-02-05 21:19:55 | [diff] [blame] | 105 | run_loop_.Run(); |
sergeyu | 1afb35a1 | 2015-02-13 18:45:30 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace remoting |