blob: 3722cd1a487a270798a7fb9fc3cdca773773a29a [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2014 The Chromium Authors
[email protected]324868c22014-05-01 00:19:052// 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/protocol/monitored_video_stub.h"
6
avi5a080f012015-12-22 23:15:437#include <stdint.h>
8
Peter Boström42afa23f2021-04-02 22:10:469#include <memory>
sergeyu89d088b2015-12-24 00:22:4410#include <utility>
11
Sebastien Marchand6d0558fd2019-01-25 16:49:3712#include "base/bind.h"
[email protected]324868c22014-05-01 00:19:0513#include "base/run_loop.h"
Patrick Monette643cdf62021-10-15 19:13:4214#include "base/task/single_thread_task_runner.h"
Gabriel Charettec7108742019-08-23 03:31:4015#include "base/test/task_environment.h"
[email protected]324868c22014-05-01 00:19:0516#include "base/test/test_timeouts.h"
Sebastien Marchandefda77e532019-01-25 22:53:5217#include "base/timer/timer.h"
[email protected]324868c22014-05-01 00:19:0518#include "remoting/protocol/protocol_mock_objects.h"
19#include "testing/gmock/include/gmock/gmock.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22using ::testing::_;
23using ::testing::AnyNumber;
[email protected]356eb0662014-05-02 09:44:2724using ::testing::AtMost;
[email protected]324868c22014-05-01 00:19:0525using ::testing::InvokeWithoutArgs;
26
Joe Downing39d710e2022-08-25 20:11:4527namespace remoting::protocol {
[email protected]324868c22014-05-01 00:19:0528
avi5a080f012015-12-22 23:15:4329static const int64_t kTestOverrideDelayMilliseconds = 1;
[email protected]324868c22014-05-01 00:19:0530
31class MonitoredVideoStubTest : public testing::Test {
32 protected:
nick697f4292015-04-23 18:22:3133 void SetUp() override {
Peter Boström42afa23f2021-04-02 22:10:4634 packet_ = std::make_unique<VideoPacket>();
35 monitor_ = std::make_unique<MonitoredVideoStub>(
Peter Kastinge5a38ed2021-10-02 03:06:3536 &video_stub_, base::Milliseconds(kTestOverrideDelayMilliseconds),
Evan Stadec7ae01902020-07-06 16:50:4037 base::BindRepeating(&MonitoredVideoStubTest::OnVideoChannelStatus,
Peter Boström42afa23f2021-04-02 22:10:4638 base::Unretained(this)));
[email protected]324868c22014-05-01 00:19:0539 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)).Times(AnyNumber());
40 }
41
42 MOCK_METHOD1(OnVideoChannelStatus, void(bool connected));
43
Gabriel Charettedf0a7442019-09-05 17:32:3544 base::test::SingleThreadTaskEnvironment task_environment_;
[email protected]324868c22014-05-01 00:19:0545 MockVideoStub video_stub_;
46
dcheng0765c492016-04-06 22:41:5347 std::unique_ptr<MonitoredVideoStub> monitor_;
48 std::unique_ptr<VideoPacket> packet_;
danakj8c3eb802015-09-24 07:53:0049 base::OneShotTimer timer_end_test_;
[email protected]324868c22014-05-01 00:19:0550};
51
52TEST_F(MonitoredVideoStubTest, OnChannelConnected) {
53 EXPECT_CALL(*this, OnVideoChannelStatus(true));
[email protected]356eb0662014-05-02 09:44:2754 // On slow machines, the connectivity check timer may fire before the test
55 // finishes, so we expect to see at most one transition to not ready.
56 EXPECT_CALL(*this, OnVideoChannelStatus(false)).Times(AtMost(1));
57
Evan Stade86dadf152020-03-16 20:06:3358 monitor_->ProcessVideoPacket(std::move(packet_), {});
[email protected]324868c22014-05-01 00:19:0559 base::RunLoop().RunUntilIdle();
60}
61
62TEST_F(MonitoredVideoStubTest, OnChannelDisconnected) {
63 EXPECT_CALL(*this, OnVideoChannelStatus(true));
Evan Stade86dadf152020-03-16 20:06:3364 monitor_->ProcessVideoPacket(std::move(packet_), {});
[email protected]324868c22014-05-01 00:19:0565
Gabriel Charette35dfc262017-07-27 16:56:4766 base::RunLoop run_loop;
ki.stfu659c758f2015-10-12 20:10:0667 EXPECT_CALL(*this, OnVideoChannelStatus(false))
Gabriel Charette35dfc262017-07-27 16:56:4768 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::QuitWhenIdle));
69 run_loop.Run();
[email protected]324868c22014-05-01 00:19:0570}
71
72TEST_F(MonitoredVideoStubTest, OnChannelStayConnected) {
73 // Verify no extra connected events are fired when packets are received
74 // frequently
[email protected]356eb0662014-05-02 09:44:2775 EXPECT_CALL(*this, OnVideoChannelStatus(true));
76 // On slow machines, the connectivity check timer may fire before the test
77 // finishes, so we expect to see at most one transition to not ready.
78 EXPECT_CALL(*this, OnVideoChannelStatus(false)).Times(AtMost(1));
[email protected]324868c22014-05-01 00:19:0579
Evan Stade86dadf152020-03-16 20:06:3380 monitor_->ProcessVideoPacket(std::move(packet_), {});
81 monitor_->ProcessVideoPacket(std::move(packet_), {});
[email protected]324868c22014-05-01 00:19:0582 base::RunLoop().RunUntilIdle();
83}
84
85TEST_F(MonitoredVideoStubTest, OnChannelStayDisconnected) {
86 // Verify no extra disconnected events are fired.
87 EXPECT_CALL(*this, OnVideoChannelStatus(true)).Times(1);
88 EXPECT_CALL(*this, OnVideoChannelStatus(false)).Times(1);
89
Evan Stade86dadf152020-03-16 20:06:3390 monitor_->ProcessVideoPacket(std::move(packet_), {});
[email protected]324868c22014-05-01 00:19:0591
Gabriel Charettedfa36042019-08-19 17:30:1192 task_environment_.GetMainThreadTaskRunner()->PostDelayedTask(
Gabriel Charetteea918012018-05-16 11:53:4493 FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(),
[email protected]324868c22014-05-01 00:19:0594 // The delay should be much greater than |kTestOverrideDelayMilliseconds|.
95 TestTimeouts::tiny_timeout());
fdoray6d056ff2016-07-04 21:56:4296 base::RunLoop().Run();
[email protected]324868c22014-05-01 00:19:0597}
98
Joe Downing39d710e2022-08-25 20:11:4599} // namespace remoting::protocol