Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 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/protocol/monitored_video_stub.h" |
| 6 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 9 | #include <memory> |
sergeyu | 89d088b | 2015-12-24 00:22:44 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 14 | #include "base/task/single_thread_task_runner.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 15 | #include "base/test/task_environment.h" |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 16 | #include "base/test/test_timeouts.h" |
Sebastien Marchand | efda77e53 | 2019-01-25 22:53:52 | [diff] [blame] | 17 | #include "base/timer/timer.h" |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 18 | #include "remoting/protocol/protocol_mock_objects.h" |
| 19 | #include "testing/gmock/include/gmock/gmock.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
| 22 | using ::testing::_; |
| 23 | using ::testing::AnyNumber; |
[email protected] | 356eb066 | 2014-05-02 09:44:27 | [diff] [blame] | 24 | using ::testing::AtMost; |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 25 | using ::testing::InvokeWithoutArgs; |
| 26 | |
Joe Downing | 39d710e | 2022-08-25 20:11:45 | [diff] [blame] | 27 | namespace remoting::protocol { |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 28 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 29 | static const int64_t kTestOverrideDelayMilliseconds = 1; |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 30 | |
| 31 | class MonitoredVideoStubTest : public testing::Test { |
| 32 | protected: |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 33 | void SetUp() override { |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 34 | packet_ = std::make_unique<VideoPacket>(); |
| 35 | monitor_ = std::make_unique<MonitoredVideoStub>( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 36 | &video_stub_, base::Milliseconds(kTestOverrideDelayMilliseconds), |
Evan Stade | c7ae0190 | 2020-07-06 16:50:40 | [diff] [blame] | 37 | base::BindRepeating(&MonitoredVideoStubTest::OnVideoChannelStatus, |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 38 | base::Unretained(this))); |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 39 | EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)).Times(AnyNumber()); |
| 40 | } |
| 41 | |
| 42 | MOCK_METHOD1(OnVideoChannelStatus, void(bool connected)); |
| 43 | |
Gabriel Charette | df0a744 | 2019-09-05 17:32:35 | [diff] [blame] | 44 | base::test::SingleThreadTaskEnvironment task_environment_; |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 45 | MockVideoStub video_stub_; |
| 46 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 47 | std::unique_ptr<MonitoredVideoStub> monitor_; |
| 48 | std::unique_ptr<VideoPacket> packet_; |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 49 | base::OneShotTimer timer_end_test_; |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | TEST_F(MonitoredVideoStubTest, OnChannelConnected) { |
| 53 | EXPECT_CALL(*this, OnVideoChannelStatus(true)); |
[email protected] | 356eb066 | 2014-05-02 09:44:27 | [diff] [blame] | 54 | // 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 Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 58 | monitor_->ProcessVideoPacket(std::move(packet_), {}); |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 59 | base::RunLoop().RunUntilIdle(); |
| 60 | } |
| 61 | |
| 62 | TEST_F(MonitoredVideoStubTest, OnChannelDisconnected) { |
| 63 | EXPECT_CALL(*this, OnVideoChannelStatus(true)); |
Evan Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 64 | monitor_->ProcessVideoPacket(std::move(packet_), {}); |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 65 | |
Gabriel Charette | 35dfc26 | 2017-07-27 16:56:47 | [diff] [blame] | 66 | base::RunLoop run_loop; |
ki.stfu | 659c758f | 2015-10-12 20:10:06 | [diff] [blame] | 67 | EXPECT_CALL(*this, OnVideoChannelStatus(false)) |
Gabriel Charette | 35dfc26 | 2017-07-27 16:56:47 | [diff] [blame] | 68 | .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::QuitWhenIdle)); |
| 69 | run_loop.Run(); |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST_F(MonitoredVideoStubTest, OnChannelStayConnected) { |
| 73 | // Verify no extra connected events are fired when packets are received |
| 74 | // frequently |
[email protected] | 356eb066 | 2014-05-02 09:44:27 | [diff] [blame] | 75 | 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] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 79 | |
Evan Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 80 | monitor_->ProcessVideoPacket(std::move(packet_), {}); |
| 81 | monitor_->ProcessVideoPacket(std::move(packet_), {}); |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 82 | base::RunLoop().RunUntilIdle(); |
| 83 | } |
| 84 | |
| 85 | TEST_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 Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 90 | monitor_->ProcessVideoPacket(std::move(packet_), {}); |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 91 | |
Gabriel Charette | dfa3604 | 2019-08-19 17:30:11 | [diff] [blame] | 92 | task_environment_.GetMainThreadTaskRunner()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 93 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 94 | // The delay should be much greater than |kTestOverrideDelayMilliseconds|. |
| 95 | TestTimeouts::tiny_timeout()); |
fdoray | 6d056ff | 2016-07-04 21:56:42 | [diff] [blame] | 96 | base::RunLoop().Run(); |
[email protected] | 324868c2 | 2014-05-01 00:19:05 | [diff] [blame] | 97 | } |
| 98 | |
Joe Downing | 39d710e | 2022-08-25 20:11:45 | [diff] [blame] | 99 | } // namespace remoting::protocol |