1// Copyright 2014 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/protocol/monitored_video_stub.h"
6
7#include "base/message_loop/message_loop.h"
8#include "base/message_loop/message_loop_proxy.h"
9#include "base/run_loop.h"
10#include "base/test/test_timeouts.h"
11#include "remoting/protocol/protocol_mock_objects.h"
12#include "testing/gmock/include/gmock/gmock.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15using ::testing::_;
16using ::testing::AnyNumber;
17using ::testing::AtMost;
18using ::testing::InvokeWithoutArgs;
19
20namespace remoting {
21namespace protocol {
22
23static const int64 kTestOverrideDelayMilliseconds = 1;
24
25class MonitoredVideoStubTest : public testing::Test {
26 protected:
27  virtual void SetUp() OVERRIDE {
28    packet_.reset(new VideoPacket());
29    monitor_.reset(new MonitoredVideoStub(
30        &video_stub_,
31        base::TimeDelta::FromMilliseconds(kTestOverrideDelayMilliseconds),
32        base::Bind(
33            &MonitoredVideoStubTest::OnVideoChannelStatus,
34            base::Unretained(this))));
35    EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)).Times(AnyNumber());
36  }
37
38  MOCK_METHOD1(OnVideoChannelStatus, void(bool connected));
39
40  base::MessageLoop message_loop_;
41  MockVideoStub video_stub_;
42
43  scoped_ptr<MonitoredVideoStub> monitor_;
44  scoped_ptr<VideoPacket> packet_;
45  base::OneShotTimer<MonitoredVideoStubTest> timer_end_test_;
46};
47
48TEST_F(MonitoredVideoStubTest, OnChannelConnected) {
49  EXPECT_CALL(*this, OnVideoChannelStatus(true));
50  // On slow machines, the connectivity check timer may fire before the test
51  // finishes, so we expect to see at most one transition to not ready.
52  EXPECT_CALL(*this, OnVideoChannelStatus(false)).Times(AtMost(1));
53
54  monitor_->ProcessVideoPacket(packet_.Pass(), base::Closure());
55  base::RunLoop().RunUntilIdle();
56}
57
58TEST_F(MonitoredVideoStubTest, OnChannelDisconnected) {
59  EXPECT_CALL(*this, OnVideoChannelStatus(true));
60  monitor_->ProcessVideoPacket(packet_.Pass(), base::Closure());
61
62  EXPECT_CALL(*this, OnVideoChannelStatus(false)).WillOnce(
63    InvokeWithoutArgs(
64      &message_loop_,
65      &base::MessageLoop::Quit));
66  message_loop_.Run();
67}
68
69TEST_F(MonitoredVideoStubTest, OnChannelStayConnected) {
70  // Verify no extra connected events are fired when packets are received
71  // frequently
72  EXPECT_CALL(*this, OnVideoChannelStatus(true));
73  // On slow machines, the connectivity check timer may fire before the test
74  // finishes, so we expect to see at most one transition to not ready.
75  EXPECT_CALL(*this, OnVideoChannelStatus(false)).Times(AtMost(1));
76
77  monitor_->ProcessVideoPacket(packet_.Pass(), base::Closure());
78  monitor_->ProcessVideoPacket(packet_.Pass(), base::Closure());
79  base::RunLoop().RunUntilIdle();
80}
81
82TEST_F(MonitoredVideoStubTest, OnChannelStayDisconnected) {
83  // Verify no extra disconnected events are fired.
84  EXPECT_CALL(*this, OnVideoChannelStatus(true)).Times(1);
85  EXPECT_CALL(*this, OnVideoChannelStatus(false)).Times(1);
86
87  monitor_->ProcessVideoPacket(packet_.Pass(), base::Closure());
88
89  message_loop_.PostDelayedTask(
90      FROM_HERE,
91      base::MessageLoop::QuitClosure(),
92      // The delay should be much greater than |kTestOverrideDelayMilliseconds|.
93      TestTimeouts::tiny_timeout());
94  message_loop_.Run();
95}
96
97}  // namespace protocol
98}  // namespace remoting
99