OLD | NEW |
(Empty) | |
| 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 "base/bind.h" |
| 6 #include "base/callback_helpers.h" |
| 7 #include "base/logging.h" |
| 8 #include "chromecast/media/cma/base/buffering_controller.h" |
| 9 #include "chromecast/media/cma/base/buffering_state.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace media { |
| 14 namespace cma { |
| 15 |
| 16 namespace { |
| 17 |
| 18 class MockBufferingControllerClient { |
| 19 public: |
| 20 MockBufferingControllerClient(); |
| 21 ~MockBufferingControllerClient(); |
| 22 |
| 23 MOCK_METHOD1(OnBufferingNotification, void(bool is_buffering)); |
| 24 |
| 25 private: |
| 26 DISALLOW_COPY_AND_ASSIGN(MockBufferingControllerClient); |
| 27 }; |
| 28 |
| 29 MockBufferingControllerClient::MockBufferingControllerClient() { |
| 30 } |
| 31 |
| 32 MockBufferingControllerClient::~MockBufferingControllerClient() { |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class BufferingControllerTest : public testing::Test { |
| 38 public: |
| 39 BufferingControllerTest(); |
| 40 virtual ~BufferingControllerTest(); |
| 41 |
| 42 protected: |
| 43 scoped_ptr<BufferingController> buffering_controller_; |
| 44 |
| 45 MockBufferingControllerClient client_; |
| 46 |
| 47 // Buffer level under the low level threshold. |
| 48 base::TimeDelta d1_; |
| 49 |
| 50 // Buffer level between the low and the high level. |
| 51 base::TimeDelta d2_; |
| 52 |
| 53 // Buffer level above the high level. |
| 54 base::TimeDelta d3_; |
| 55 |
| 56 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(BufferingControllerTest); |
| 58 }; |
| 59 |
| 60 BufferingControllerTest::BufferingControllerTest() { |
| 61 base::TimeDelta low_level_threshold( |
| 62 base::TimeDelta::FromMilliseconds(2000)); |
| 63 base::TimeDelta high_level_threshold( |
| 64 base::TimeDelta::FromMilliseconds(6000)); |
| 65 |
| 66 d1_ = low_level_threshold - base::TimeDelta::FromMilliseconds(50); |
| 67 d2_ = (low_level_threshold + high_level_threshold) / 2; |
| 68 d3_ = high_level_threshold + base::TimeDelta::FromMilliseconds(50); |
| 69 |
| 70 scoped_refptr<BufferingConfig> buffering_config( |
| 71 new BufferingConfig(low_level_threshold, high_level_threshold)); |
| 72 buffering_controller_.reset(new BufferingController( |
| 73 buffering_config, |
| 74 base::Bind(&MockBufferingControllerClient::OnBufferingNotification, |
| 75 base::Unretained(&client_)))); |
| 76 } |
| 77 |
| 78 BufferingControllerTest::~BufferingControllerTest() { |
| 79 } |
| 80 |
| 81 TEST_F(BufferingControllerTest, OneStream_Typical) { |
| 82 EXPECT_CALL(client_, OnBufferingNotification(true)).Times(1); |
| 83 scoped_refptr<BufferingState> buffering_state = |
| 84 buffering_controller_->AddStream(); |
| 85 buffering_state->SetMediaTime(base::TimeDelta()); |
| 86 |
| 87 // Simulate pre-buffering. |
| 88 buffering_state->SetBufferedTime(d2_); |
| 89 EXPECT_EQ(buffering_state->GetState(), BufferingState::kMediumLevel); |
| 90 |
| 91 EXPECT_CALL(client_, OnBufferingNotification(false)).Times(1); |
| 92 buffering_state->SetBufferedTime(d3_); |
| 93 EXPECT_EQ(buffering_state->GetState(), BufferingState::kHighLevel); |
| 94 |
| 95 // Simulate some fluctuations of the buffering level. |
| 96 buffering_state->SetBufferedTime(d2_); |
| 97 EXPECT_EQ(buffering_state->GetState(), BufferingState::kMediumLevel); |
| 98 |
| 99 // Simulate an underrun. |
| 100 EXPECT_CALL(client_, OnBufferingNotification(true)).Times(1); |
| 101 buffering_state->SetBufferedTime(d1_); |
| 102 EXPECT_EQ(buffering_state->GetState(), BufferingState::kLowLevel); |
| 103 |
| 104 EXPECT_CALL(client_, OnBufferingNotification(false)).Times(1); |
| 105 buffering_state->SetBufferedTime(d3_); |
| 106 EXPECT_EQ(buffering_state->GetState(), BufferingState::kHighLevel); |
| 107 |
| 108 // Simulate the end of stream. |
| 109 buffering_state->NotifyEos(); |
| 110 EXPECT_EQ(buffering_state->GetState(), BufferingState::kEosReached); |
| 111 |
| 112 buffering_state->SetBufferedTime(d2_); |
| 113 EXPECT_EQ(buffering_state->GetState(), BufferingState::kEosReached); |
| 114 |
| 115 buffering_state->SetBufferedTime(d1_); |
| 116 EXPECT_EQ(buffering_state->GetState(), BufferingState::kEosReached); |
| 117 } |
| 118 |
| 119 TEST_F(BufferingControllerTest, OneStream_LeaveBufferingOnEos) { |
| 120 EXPECT_CALL(client_, OnBufferingNotification(true)).Times(1); |
| 121 scoped_refptr<BufferingState> buffering_state = |
| 122 buffering_controller_->AddStream(); |
| 123 buffering_state->SetMediaTime(base::TimeDelta()); |
| 124 |
| 125 EXPECT_CALL(client_, OnBufferingNotification(false)).Times(1); |
| 126 buffering_state->NotifyEos(); |
| 127 EXPECT_EQ(buffering_state->GetState(), BufferingState::kEosReached); |
| 128 } |
| 129 |
| 130 } // namespace cma |
| 131 } // namespace media |
| 132 |
OLD | NEW |