blob: 946635ee524cafafd6ad7aad09c79f567f7e9a36 [file] [log] [blame]
sergeyu1afb35a12015-02-13 18:45:301// 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
sergeyu1417e0132015-12-23 19:01:227#include <utility>
8
avic5960f32015-12-22 22:49:489#include "base/macros.h"
dcheng0765c492016-04-06 22:41:5310#include "base/memory/ptr_util.h"
sergeyu1afb35a12015-02-13 18:45:3011#include "base/message_loop/message_loop.h"
12#include "base/run_loop.h"
13#include "base/single_thread_task_runner.h"
sergeyu1afb35a12015-02-13 18:45:3014#include "remoting/host/host_mock_objects.h"
sergeyu1afb35a12015-02-13 18:45:3015#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
23using ::remoting::protocol::MockClientStub;
24
25using ::testing::_;
sergeyu1afb35a12015-02-13 18:45:3026using ::testing::InvokeWithoutArgs;
27
28namespace remoting {
29
30static const int kCursorWidth = 64;
31static const int kCursorHeight = 32;
32static const int kHotspotX = 11;
33static const int kHotspotY = 12;
34
sergeyu30cb640e2016-02-05 21:19:5535class TestMouseCursorMonitor : public webrtc::MouseCursorMonitor {
sergeyu1afb35a12015-02-13 18:45:3036 public:
sergeyu30cb640e2016-02-05 21:19:5537 TestMouseCursorMonitor() : callback_(nullptr) {}
38 ~TestMouseCursorMonitor() override {}
sergeyu1afb35a12015-02-13 18:45:3039
40 void Init(Callback* callback, Mode mode) override {
sergeyu1afb35a12015-02-13 18:45:3041 EXPECT_FALSE(callback_);
42 EXPECT_TRUE(callback);
43
44 callback_ = callback;
45 }
46
47 void Capture() override {
sergeyu1afb35a12015-02-13 18:45:3048 ASSERT_TRUE(callback_);
49
dcheng0765c492016-04-06 22:41:5350 std::unique_ptr<webrtc::MouseCursor> mouse_cursor(new webrtc::MouseCursor(
sergeyu1afb35a12015-02-13 18:45:3051 new webrtc::BasicDesktopFrame(
52 webrtc::DesktopSize(kCursorWidth, kCursorHeight)),
53 webrtc::DesktopVector(kHotspotX, kHotspotY)));
54
55 callback_->OnMouseCursor(mouse_cursor.release());
56 }
57
58 private:
sergeyu1afb35a12015-02-13 18:45:3059 Callback* callback_;
60
sergeyu30cb640e2016-02-05 21:19:5561 DISALLOW_COPY_AND_ASSIGN(TestMouseCursorMonitor);
sergeyu1afb35a12015-02-13 18:45:3062};
63
64class MouseShapePumpTest : public testing::Test {
65 public:
sergeyu1afb35a12015-02-13 18:45:3066 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape);
67
68 protected:
69 base::MessageLoop message_loop_;
70 base::RunLoop run_loop_;
dcheng0765c492016-04-06 22:41:5371 std::unique_ptr<MouseShapePump> pump_;
sergeyu1afb35a12015-02-13 18:45:3072
73 MockClientStub client_stub_;
74};
75
sergeyu1afb35a12015-02-13 18:45:3076void 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.
93TEST_F(MouseShapePumpTest, FirstCursor) {
sergeyu30cb640e2016-02-05 21:19:5594 // Stop the |run_loop_| once it has captured the cursor.
sergeyu1afb35a12015-02-13 18:45:3095 EXPECT_CALL(client_stub_, SetCursorShape(_))
96 .WillOnce(DoAll(
97 Invoke(this, &MouseShapePumpTest::SetCursorShape),
sergeyu30cb640e2016-02-05 21:19:5598 InvokeWithoutArgs(&run_loop_, &base::RunLoop::Quit)))
sergeyu1afb35a12015-02-13 18:45:3099 .RetiresOnSaturation();
100
101 // Start the pump.
dcheng0765c492016-04-06 22:41:53102 pump_.reset(new MouseShapePump(base::WrapUnique(new TestMouseCursorMonitor()),
sergeyu30cb640e2016-02-05 21:19:55103 &client_stub_));
sergeyu1afb35a12015-02-13 18:45:30104
sergeyu30cb640e2016-02-05 21:19:55105 run_loop_.Run();
sergeyu1afb35a12015-02-13 18:45:30106}
107
108} // namespace remoting