OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/bind_helpers.h" |
| 7 #include "base/logging.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/time/time.h" |
| 13 #include "media/base/mock_filters.h" |
| 14 #include "media/base/pipeline.h" |
| 15 #include "media/filters/pipeline_controller.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using ::testing::_; |
| 20 using ::testing::DoAll; |
| 21 using ::testing::Mock; |
| 22 using ::testing::Return; |
| 23 using ::testing::SaveArg; |
| 24 using ::testing::StrictMock; |
| 25 |
| 26 namespace media { |
| 27 |
| 28 class PipelineControllerTest : public ::testing::Test { |
| 29 public: |
| 30 PipelineControllerTest() |
| 31 : pipeline_controller_(&pipeline_, |
| 32 base::Bind(&PipelineControllerTest::CreateRenderer, |
| 33 base::Unretained(this)), |
| 34 base::Bind(&PipelineControllerTest::OnSeeked, |
| 35 base::Unretained(this)), |
| 36 base::Bind(&PipelineControllerTest::OnSuspended, |
| 37 base::Unretained(this)), |
| 38 base::Bind(&PipelineControllerTest::OnError, |
| 39 base::Unretained(this))) {} |
| 40 |
| 41 ~PipelineControllerTest() override {} |
| 42 |
| 43 PipelineStatusCB StartPipeline() { |
| 44 EXPECT_FALSE(pipeline_controller_.IsStable()); |
| 45 PipelineStatusCB start_cb; |
| 46 EXPECT_CALL(pipeline_, Start(_, _, _, _, _, _, _, _, _, _)) |
| 47 .WillOnce(SaveArg<4>(&start_cb)); |
| 48 pipeline_controller_.Start( |
| 49 nullptr, nullptr, false, base::Closure(), PipelineMetadataCB(), |
| 50 BufferingStateCB(), base::Closure(), AddTextTrackCB(), base::Closure()); |
| 51 Mock::VerifyAndClear(&pipeline_); |
| 52 EXPECT_FALSE(pipeline_controller_.IsStable()); |
| 53 return start_cb; |
| 54 } |
| 55 |
| 56 PipelineStatusCB SeekPipeline(base::TimeDelta time) { |
| 57 EXPECT_TRUE(pipeline_controller_.IsStable()); |
| 58 PipelineStatusCB seek_cb; |
| 59 EXPECT_CALL(pipeline_, Seek(time, _)).WillOnce(SaveArg<1>(&seek_cb)); |
| 60 pipeline_controller_.Seek(time, true); |
| 61 Mock::VerifyAndClear(&pipeline_); |
| 62 EXPECT_FALSE(pipeline_controller_.IsStable()); |
| 63 return seek_cb; |
| 64 } |
| 65 |
| 66 PipelineStatusCB SuspendPipeline() { |
| 67 EXPECT_TRUE(pipeline_controller_.IsStable()); |
| 68 PipelineStatusCB suspend_cb; |
| 69 EXPECT_CALL(pipeline_, Suspend(_)).WillOnce(SaveArg<0>(&suspend_cb)); |
| 70 pipeline_controller_.Suspend(); |
| 71 Mock::VerifyAndClear(&pipeline_); |
| 72 EXPECT_FALSE(pipeline_controller_.IsStable()); |
| 73 EXPECT_FALSE(pipeline_controller_.IsSuspended()); |
| 74 return suspend_cb; |
| 75 } |
| 76 |
| 77 PipelineStatusCB ResumePipeline() { |
| 78 EXPECT_TRUE(pipeline_controller_.IsSuspended()); |
| 79 PipelineStatusCB resume_cb; |
| 80 EXPECT_CALL(pipeline_, Resume(_, _, _)) |
| 81 .WillOnce( |
| 82 DoAll(SaveArg<1>(&last_resume_time_), SaveArg<2>(&resume_cb))); |
| 83 EXPECT_CALL(pipeline_, GetMediaTime()) |
| 84 .WillRepeatedly(Return(base::TimeDelta())); |
| 85 pipeline_controller_.Resume(); |
| 86 Mock::VerifyAndClear(&pipeline_); |
| 87 EXPECT_FALSE(pipeline_controller_.IsStable()); |
| 88 EXPECT_FALSE(pipeline_controller_.IsSuspended()); |
| 89 return resume_cb; |
| 90 } |
| 91 |
| 92 void Complete(const PipelineStatusCB& cb) { |
| 93 cb.Run(PIPELINE_OK); |
| 94 message_loop_.RunUntilIdle(); |
| 95 } |
| 96 |
| 97 protected: |
| 98 scoped_ptr<Renderer> CreateRenderer() { return scoped_ptr<Renderer>(); } |
| 99 |
| 100 void OnSeeked(bool time_updated) { |
| 101 was_seeked_ = true; |
| 102 last_seeked_time_updated_ = time_updated; |
| 103 } |
| 104 |
| 105 void OnSuspended() { was_suspended_ = true; } |
| 106 |
| 107 void OnError(PipelineStatus status) { NOTREACHED(); } |
| 108 |
| 109 base::MessageLoop message_loop_; |
| 110 |
| 111 StrictMock<MockPipeline> pipeline_; |
| 112 PipelineController pipeline_controller_; |
| 113 |
| 114 bool was_seeked_ = false; |
| 115 bool last_seeked_time_updated_ = false; |
| 116 bool was_suspended_ = false; |
| 117 base::TimeDelta last_resume_time_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(PipelineControllerTest); |
| 120 }; |
| 121 |
| 122 TEST_F(PipelineControllerTest, Startup) { |
| 123 PipelineStatusCB start_cb = StartPipeline(); |
| 124 EXPECT_FALSE(was_seeked_); |
| 125 |
| 126 Complete(start_cb); |
| 127 EXPECT_TRUE(was_seeked_); |
| 128 EXPECT_FALSE(last_seeked_time_updated_); |
| 129 EXPECT_TRUE(pipeline_controller_.IsStable()); |
| 130 } |
| 131 |
| 132 TEST_F(PipelineControllerTest, SuspendResume) { |
| 133 Complete(StartPipeline()); |
| 134 EXPECT_TRUE(was_seeked_); |
| 135 was_seeked_ = false; |
| 136 |
| 137 Complete(SuspendPipeline()); |
| 138 EXPECT_TRUE(was_suspended_); |
| 139 EXPECT_FALSE(pipeline_controller_.IsStable()); |
| 140 |
| 141 Complete(ResumePipeline()); |
| 142 EXPECT_TRUE(pipeline_controller_.IsStable()); |
| 143 |
| 144 // |was_seeked_| should not be affected by Suspend()/Resume() at all. |
| 145 EXPECT_FALSE(was_seeked_); |
| 146 } |
| 147 |
| 148 TEST_F(PipelineControllerTest, PendingSuspend) { |
| 149 Complete(StartPipeline()); |
| 150 |
| 151 base::TimeDelta seek_time = base::TimeDelta::FromSeconds(5); |
| 152 PipelineStatusCB seek_cb = SeekPipeline(seek_time); |
| 153 message_loop_.RunUntilIdle(); |
| 154 |
| 155 // While the seek is ongoing, request a suspend. |
| 156 // It will be a mock failure if pipeline_.Suspend() is called. |
| 157 pipeline_controller_.Suspend(); |
| 158 message_loop_.RunUntilIdle(); |
| 159 |
| 160 // Expect the suspend to trigger when the seek is completed. |
| 161 EXPECT_CALL(pipeline_, Suspend(_)); |
| 162 Complete(seek_cb); |
| 163 } |
| 164 |
| 165 TEST_F(PipelineControllerTest, SeekMergesWithResume) { |
| 166 Complete(StartPipeline()); |
| 167 Complete(SuspendPipeline()); |
| 168 |
| 169 // Request a seek while suspended. |
| 170 // It will be a mock failure if pipeline_.Seek() is called. |
| 171 base::TimeDelta seek_time = base::TimeDelta::FromSeconds(5); |
| 172 pipeline_controller_.Seek(seek_time, true); |
| 173 message_loop_.RunUntilIdle(); |
| 174 |
| 175 // Resume and verify the resume time includes the seek. |
| 176 Complete(ResumePipeline()); |
| 177 EXPECT_EQ(seek_time, last_resume_time_); |
| 178 EXPECT_TRUE(last_seeked_time_updated_); |
| 179 } |
| 180 |
| 181 TEST_F(PipelineControllerTest, SeekMergesWithSeek) { |
| 182 Complete(StartPipeline()); |
| 183 |
| 184 base::TimeDelta seek_time_1 = base::TimeDelta::FromSeconds(5); |
| 185 PipelineStatusCB seek_cb_1 = SeekPipeline(seek_time_1); |
| 186 message_loop_.RunUntilIdle(); |
| 187 |
| 188 // Request another seek while the first is ongoing. |
| 189 base::TimeDelta seek_time_2 = base::TimeDelta::FromSeconds(10); |
| 190 pipeline_controller_.Seek(seek_time_2, true); |
| 191 message_loop_.RunUntilIdle(); |
| 192 |
| 193 // Request a third seek. (It should replace the second.) |
| 194 base::TimeDelta seek_time_3 = base::TimeDelta::FromSeconds(15); |
| 195 pipeline_controller_.Seek(seek_time_3, true); |
| 196 message_loop_.RunUntilIdle(); |
| 197 |
| 198 // Expect the third seek to trigger when the first seek completes. |
| 199 EXPECT_CALL(pipeline_, Seek(seek_time_3, _)); |
| 200 Complete(seek_cb_1); |
| 201 } |
| 202 |
| 203 } // namespace media |
OLD | NEW |