Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: media/filters/ffmpeg_demuxer_unittest.cc

Issue 6350001: Replace MockFilterCallback with MockCallback and simplify unit tests. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src
Patch Set: forgot some files Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/filters/decoder_base_unittest.cc ('k') | media/filters/ffmpeg_video_decoder_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <deque> 5 #include <deque>
6 6
7 #include "base/callback.h"
8 #include "base/threading/thread.h" 7 #include "base/threading/thread.h"
9 #include "media/base/filters.h" 8 #include "media/base/filters.h"
9 #include "media/base/mock_callback.h"
10 #include "media/base/mock_ffmpeg.h" 10 #include "media/base/mock_ffmpeg.h"
11 #include "media/base/mock_filter_host.h" 11 #include "media/base/mock_filter_host.h"
12 #include "media/base/mock_filters.h" 12 #include "media/base/mock_filters.h"
13 #include "media/base/mock_reader.h" 13 #include "media/base/mock_reader.h"
14 #include "media/ffmpeg/ffmpeg_common.h" 14 #include "media/ffmpeg/ffmpeg_common.h"
15 #include "media/filters/ffmpeg_demuxer.h" 15 #include "media/filters/ffmpeg_demuxer.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 using ::testing::AnyNumber; 18 using ::testing::AnyNumber;
19 using ::testing::DoAll; 19 using ::testing::DoAll;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 streams_[i].time_base.den = 1 * base::Time::kMicrosecondsPerSecond; 106 streams_[i].time_base.den = 1 * base::Time::kMicrosecondsPerSecond;
107 streams_[i].time_base.num = 1; 107 streams_[i].time_base.num = 1;
108 } 108 }
109 109
110 // Initialize MockFFmpeg. 110 // Initialize MockFFmpeg.
111 MockFFmpeg::set(&mock_ffmpeg_); 111 MockFFmpeg::set(&mock_ffmpeg_);
112 } 112 }
113 113
114 virtual ~FFmpegDemuxerTest() { 114 virtual ~FFmpegDemuxerTest() {
115 // Call Stop() to shut down internal threads. 115 // Call Stop() to shut down internal threads.
116 EXPECT_CALL(callback_, OnFilterCallback()); 116 demuxer_->Stop(NewExpectedCallback());
117 EXPECT_CALL(callback_, OnCallbackDestroyed());
118 demuxer_->Stop(callback_.NewCallback());
119 117
120 // Finish up any remaining tasks. 118 // Finish up any remaining tasks.
121 message_loop_.RunAllPending(); 119 message_loop_.RunAllPending();
122 120
123 // Release the reference to the demuxer. 121 // Release the reference to the demuxer.
124 demuxer_ = NULL; 122 demuxer_ = NULL;
125 123
126 // Reset MockFFmpeg. 124 // Reset MockFFmpeg.
127 MockFFmpeg::set(NULL); 125 MockFFmpeg::set(NULL);
128 } 126 }
129 127
130 // Sets up MockFFmpeg to allow FFmpegDemuxer to successfully initialize. 128 // Sets up MockFFmpeg to allow FFmpegDemuxer to successfully initialize.
131 void InitializeDemuxerMocks() { 129 void InitializeDemuxerMocks() {
132 EXPECT_CALL(*MockFFmpeg::get(), AVOpenInputFile(_, _, NULL, 0, NULL)) 130 EXPECT_CALL(*MockFFmpeg::get(), AVOpenInputFile(_, _, NULL, 0, NULL))
133 .WillOnce(DoAll(SetArgumentPointee<0>(&format_context_), Return(0))); 131 .WillOnce(DoAll(SetArgumentPointee<0>(&format_context_), Return(0)));
134 EXPECT_CALL(*MockFFmpeg::get(), AVFindStreamInfo(&format_context_)) 132 EXPECT_CALL(*MockFFmpeg::get(), AVFindStreamInfo(&format_context_))
135 .WillOnce(Return(0)); 133 .WillOnce(Return(0));
136 EXPECT_CALL(*MockFFmpeg::get(), AVCloseInputFile(&format_context_)); 134 EXPECT_CALL(*MockFFmpeg::get(), AVCloseInputFile(&format_context_));
137 } 135 }
138 136
139 // Initializes both MockFFmpeg and FFmpegDemuxer. 137 // Initializes both MockFFmpeg and FFmpegDemuxer.
140 void InitializeDemuxer() { 138 void InitializeDemuxer() {
141 InitializeDemuxerMocks(); 139 InitializeDemuxerMocks();
142 140
143 // We expect a successful initialization.
144 EXPECT_CALL(callback_, OnFilterCallback());
145 EXPECT_CALL(callback_, OnCallbackDestroyed());
146
147 // Since we ignore data streams, the duration should be equal to the longest 141 // Since we ignore data streams, the duration should be equal to the longest
148 // supported stream's duration (audio, in this case). 142 // supported stream's duration (audio, in this case).
149 base::TimeDelta expected_duration = 143 base::TimeDelta expected_duration =
150 base::TimeDelta::FromMicroseconds(kDurations[AV_STREAM_AUDIO]); 144 base::TimeDelta::FromMicroseconds(kDurations[AV_STREAM_AUDIO]);
151 EXPECT_CALL(host_, SetDuration(expected_duration)); 145 EXPECT_CALL(host_, SetDuration(expected_duration));
152 146
153 demuxer_->Initialize(data_source_.get(), callback_.NewCallback()); 147 demuxer_->Initialize(data_source_.get(), NewExpectedCallback());
154 message_loop_.RunAllPending(); 148 message_loop_.RunAllPending();
155 } 149 }
156 150
157 // Fixture members. 151 // Fixture members.
158 scoped_refptr<FFmpegDemuxer> demuxer_; 152 scoped_refptr<FFmpegDemuxer> demuxer_;
159 scoped_refptr<StrictMock<MockDataSource> > data_source_; 153 scoped_refptr<StrictMock<MockDataSource> > data_source_;
160 StrictMock<MockFilterHost> host_; 154 StrictMock<MockFilterHost> host_;
161 StrictMock<MockFilterCallback> callback_;
162 MessageLoop message_loop_; 155 MessageLoop message_loop_;
163 156
164 // FFmpeg fixtures. 157 // FFmpeg fixtures.
165 AVFormatContext format_context_; 158 AVFormatContext format_context_;
166 AVInputFormat input_format_; 159 AVInputFormat input_format_;
167 AVCodecContext codecs_[AV_STREAM_MAX]; 160 AVCodecContext codecs_[AV_STREAM_MAX];
168 AVStream streams_[AV_STREAM_MAX]; 161 AVStream streams_[AV_STREAM_MAX];
169 MockFFmpeg mock_ffmpeg_; 162 MockFFmpeg mock_ffmpeg_;
170 163
171 private: 164 private:
(...skipping 11 matching lines...) Expand all
183 const size_t FFmpegDemuxerTest::kDataSize = 4; 176 const size_t FFmpegDemuxerTest::kDataSize = 4;
184 const uint8 FFmpegDemuxerTest::kAudioData[kDataSize] = {0, 1, 2, 3}; 177 const uint8 FFmpegDemuxerTest::kAudioData[kDataSize] = {0, 1, 2, 3};
185 const uint8 FFmpegDemuxerTest::kVideoData[kDataSize] = {4, 5, 6, 7}; 178 const uint8 FFmpegDemuxerTest::kVideoData[kDataSize] = {4, 5, 6, 7};
186 const uint8* FFmpegDemuxerTest::kNullData = NULL; 179 const uint8* FFmpegDemuxerTest::kNullData = NULL;
187 180
188 TEST_F(FFmpegDemuxerTest, Initialize_OpenFails) { 181 TEST_F(FFmpegDemuxerTest, Initialize_OpenFails) {
189 // Simulate av_open_input_file() failing. 182 // Simulate av_open_input_file() failing.
190 EXPECT_CALL(*MockFFmpeg::get(), AVOpenInputFile(_, _, NULL, 0, NULL)) 183 EXPECT_CALL(*MockFFmpeg::get(), AVOpenInputFile(_, _, NULL, 0, NULL))
191 .WillOnce(Return(-1)); 184 .WillOnce(Return(-1));
192 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_COULD_NOT_OPEN)); 185 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_COULD_NOT_OPEN));
193 EXPECT_CALL(callback_, OnFilterCallback());
194 EXPECT_CALL(callback_, OnCallbackDestroyed());
195 186
196 demuxer_->Initialize(data_source_.get(), callback_.NewCallback()); 187 demuxer_->Initialize(data_source_.get(), NewExpectedCallback());
197 message_loop_.RunAllPending(); 188 message_loop_.RunAllPending();
198 } 189 }
199 190
200 TEST_F(FFmpegDemuxerTest, Initialize_ParseFails) { 191 TEST_F(FFmpegDemuxerTest, Initialize_ParseFails) {
201 // Simulate av_find_stream_info() failing. 192 // Simulate av_find_stream_info() failing.
202 EXPECT_CALL(*MockFFmpeg::get(), AVOpenInputFile(_, _, NULL, 0, NULL)) 193 EXPECT_CALL(*MockFFmpeg::get(), AVOpenInputFile(_, _, NULL, 0, NULL))
203 .WillOnce(DoAll(SetArgumentPointee<0>(&format_context_), Return(0))); 194 .WillOnce(DoAll(SetArgumentPointee<0>(&format_context_), Return(0)));
204 EXPECT_CALL(*MockFFmpeg::get(), AVFindStreamInfo(&format_context_)) 195 EXPECT_CALL(*MockFFmpeg::get(), AVFindStreamInfo(&format_context_))
205 .WillOnce(Return(AVERROR_IO)); 196 .WillOnce(Return(AVERROR_IO));
206 EXPECT_CALL(*MockFFmpeg::get(), AVCloseInputFile(&format_context_)); 197 EXPECT_CALL(*MockFFmpeg::get(), AVCloseInputFile(&format_context_));
207 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_COULD_NOT_PARSE)); 198 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_COULD_NOT_PARSE));
208 EXPECT_CALL(callback_, OnFilterCallback());
209 EXPECT_CALL(callback_, OnCallbackDestroyed());
210 199
211 demuxer_->Initialize(data_source_.get(), callback_.NewCallback()); 200 demuxer_->Initialize(data_source_.get(), NewExpectedCallback());
212 message_loop_.RunAllPending(); 201 message_loop_.RunAllPending();
213 } 202 }
214 203
215 TEST_F(FFmpegDemuxerTest, Initialize_NoStreams) { 204 TEST_F(FFmpegDemuxerTest, Initialize_NoStreams) {
216 // Simulate media with no parseable streams. 205 // Simulate media with no parseable streams.
217 { 206 {
218 SCOPED_TRACE(""); 207 SCOPED_TRACE("");
219 InitializeDemuxerMocks(); 208 InitializeDemuxerMocks();
220 } 209 }
221 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_NO_SUPPORTED_STREAMS)); 210 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_NO_SUPPORTED_STREAMS));
222 EXPECT_CALL(callback_, OnFilterCallback());
223 EXPECT_CALL(callback_, OnCallbackDestroyed());
224 format_context_.nb_streams = 0; 211 format_context_.nb_streams = 0;
225 212
226 demuxer_->Initialize(data_source_.get(), callback_.NewCallback()); 213 demuxer_->Initialize(data_source_.get(), NewExpectedCallback());
227 message_loop_.RunAllPending(); 214 message_loop_.RunAllPending();
228 } 215 }
229 216
230 TEST_F(FFmpegDemuxerTest, Initialize_DataStreamOnly) { 217 TEST_F(FFmpegDemuxerTest, Initialize_DataStreamOnly) {
231 // Simulate media with a data stream but no audio or video streams. 218 // Simulate media with a data stream but no audio or video streams.
232 { 219 {
233 SCOPED_TRACE(""); 220 SCOPED_TRACE("");
234 InitializeDemuxerMocks(); 221 InitializeDemuxerMocks();
235 } 222 }
236 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_NO_SUPPORTED_STREAMS)); 223 EXPECT_CALL(host_, SetError(DEMUXER_ERROR_NO_SUPPORTED_STREAMS));
237 EXPECT_CALL(callback_, OnFilterCallback());
238 EXPECT_CALL(callback_, OnCallbackDestroyed());
239 EXPECT_EQ(format_context_.streams[0], &streams_[AV_STREAM_DATA]); 224 EXPECT_EQ(format_context_.streams[0], &streams_[AV_STREAM_DATA]);
240 format_context_.nb_streams = 1; 225 format_context_.nb_streams = 1;
241 226
242 demuxer_->Initialize(data_source_.get(), callback_.NewCallback()); 227 demuxer_->Initialize(data_source_.get(), NewExpectedCallback());
243 message_loop_.RunAllPending(); 228 message_loop_.RunAllPending();
244 } 229 }
245 230
246 TEST_F(FFmpegDemuxerTest, Initialize_Successful) { 231 TEST_F(FFmpegDemuxerTest, Initialize_Successful) {
247 { 232 {
248 SCOPED_TRACE(""); 233 SCOPED_TRACE("");
249 InitializeDemuxer(); 234 InitializeDemuxer();
250 } 235 }
251 236
252 // Verify that our demuxer streams were created from our AVStream structures. 237 // Verify that our demuxer streams were created from our AVStream structures.
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 .WillOnce(Return(0)); 425 .WillOnce(Return(0));
441 426
442 EXPECT_CALL(*MockFFmpeg::get(), CheckPoint(1)); 427 EXPECT_CALL(*MockFFmpeg::get(), CheckPoint(1));
443 428
444 // ...then we'll expect a seek call... 429 // ...then we'll expect a seek call...
445 EXPECT_CALL(*MockFFmpeg::get(), 430 EXPECT_CALL(*MockFFmpeg::get(),
446 AVSeekFrame(&format_context_, -1, kExpectedTimestamp, kExpectedFlags)) 431 AVSeekFrame(&format_context_, -1, kExpectedTimestamp, kExpectedFlags))
447 .WillOnce(Return(0)); 432 .WillOnce(Return(0));
448 433
449 // ...then our callback will be executed... 434 // ...then our callback will be executed...
450 StrictMock<MockFilterCallback> seek_callback; 435 FilterCallback* seek_callback = NewExpectedCallback();
451 EXPECT_CALL(seek_callback, OnFilterCallback());
452 EXPECT_CALL(seek_callback, OnCallbackDestroyed());
453 EXPECT_CALL(*MockFFmpeg::get(), CheckPoint(2)); 436 EXPECT_CALL(*MockFFmpeg::get(), CheckPoint(2));
454 437
455 // ...followed by two audio packet reads we'll trigger... 438 // ...followed by two audio packet reads we'll trigger...
456 EXPECT_CALL(*MockFFmpeg::get(), AVReadFrame(&format_context_, _)) 439 EXPECT_CALL(*MockFFmpeg::get(), AVReadFrame(&format_context_, _))
457 .WillOnce(CreatePacketNoCount(AV_STREAM_AUDIO, kAudioData, kDataSize)); 440 .WillOnce(CreatePacketNoCount(AV_STREAM_AUDIO, kAudioData, kDataSize));
458 EXPECT_CALL(*MockFFmpeg::get(), AVDupPacket(_)) 441 EXPECT_CALL(*MockFFmpeg::get(), AVDupPacket(_))
459 .WillOnce(Return(0)); 442 .WillOnce(Return(0));
460 EXPECT_CALL(*MockFFmpeg::get(), AVReadFrame(&format_context_, _)) 443 EXPECT_CALL(*MockFFmpeg::get(), AVReadFrame(&format_context_, _))
461 .WillOnce(CreatePacketNoCount(AV_STREAM_AUDIO, kAudioData, kDataSize)); 444 .WillOnce(CreatePacketNoCount(AV_STREAM_AUDIO, kAudioData, kDataSize));
462 EXPECT_CALL(*MockFFmpeg::get(), AVDupPacket(_)) 445 EXPECT_CALL(*MockFFmpeg::get(), AVDupPacket(_))
(...skipping 22 matching lines...) Expand all
485 EXPECT_EQ(0, memcmp(kVideoData, reader->buffer()->GetData(), 468 EXPECT_EQ(0, memcmp(kVideoData, reader->buffer()->GetData(),
486 reader->buffer()->GetDataSize())); 469 reader->buffer()->GetDataSize()));
487 470
488 // Release the video packet and verify the other packets are still queued. 471 // Release the video packet and verify the other packets are still queued.
489 reader->Reset(); 472 reader->Reset();
490 message_loop_.RunAllPending(); 473 message_loop_.RunAllPending();
491 MockFFmpeg::get()->CheckPoint(1); 474 MockFFmpeg::get()->CheckPoint(1);
492 475
493 // Issue a simple forward seek, which should discard queued packets. 476 // Issue a simple forward seek, which should discard queued packets.
494 demuxer_->Seek(base::TimeDelta::FromMicroseconds(kExpectedTimestamp), 477 demuxer_->Seek(base::TimeDelta::FromMicroseconds(kExpectedTimestamp),
495 seek_callback.NewCallback()); 478 seek_callback);
496 message_loop_.RunAllPending(); 479 message_loop_.RunAllPending();
497 MockFFmpeg::get()->CheckPoint(2); 480 MockFFmpeg::get()->CheckPoint(2);
498 481
499 // Audio read #1. 482 // Audio read #1.
500 reader->Read(audio); 483 reader->Read(audio);
501 message_loop_.RunAllPending(); 484 message_loop_.RunAllPending();
502 EXPECT_TRUE(reader->called()); 485 EXPECT_TRUE(reader->called());
503 ASSERT_TRUE(reader->buffer()); 486 ASSERT_TRUE(reader->buffer());
504 ASSERT_EQ(kDataSize, reader->buffer()->GetDataSize()); 487 ASSERT_EQ(kDataSize, reader->buffer()->GetDataSize());
505 EXPECT_EQ(0, memcmp(kAudioData, reader->buffer()->GetData(), 488 EXPECT_EQ(0, memcmp(kAudioData, reader->buffer()->GetData(),
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 // Create our mocked callback. The demuxer will take ownership of this 554 // Create our mocked callback. The demuxer will take ownership of this
572 // pointer. 555 // pointer.
573 scoped_ptr<StrictMock<MockReadCallback> > callback( 556 scoped_ptr<StrictMock<MockReadCallback> > callback(
574 new StrictMock<MockReadCallback>()); 557 new StrictMock<MockReadCallback>());
575 558
576 // Get our stream. 559 // Get our stream.
577 scoped_refptr<DemuxerStream> audio = demuxer_->GetStream(DS_STREAM_AUDIO); 560 scoped_refptr<DemuxerStream> audio = demuxer_->GetStream(DS_STREAM_AUDIO);
578 ASSERT_TRUE(audio); 561 ASSERT_TRUE(audio);
579 562
580 // Stop the demuxer. 563 // Stop the demuxer.
581 EXPECT_CALL(callback_, OnFilterCallback()); 564 demuxer_->Stop(NewExpectedCallback());
582 EXPECT_CALL(callback_, OnCallbackDestroyed());
583 demuxer_->Stop(callback_.NewCallback());
584 565
585 // Expect all calls in sequence. 566 // Expect all calls in sequence.
586 InSequence s; 567 InSequence s;
587 568
588 // The callback should be immediately deleted. We'll use a checkpoint to 569 // The callback should be immediately deleted. We'll use a checkpoint to
589 // verify that it has indeed been deleted. 570 // verify that it has indeed been deleted.
590 EXPECT_CALL(*callback, OnDelete()); 571 EXPECT_CALL(*callback, OnDelete());
591 EXPECT_CALL(*MockFFmpeg::get(), CheckPoint(1)); 572 EXPECT_CALL(*MockFFmpeg::get(), CheckPoint(1));
592 573
593 // Attempt the read... 574 // Attempt the read...
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 // Second read. 679 // Second read.
699 EXPECT_EQ(512, demuxer->Read(512, kBuffer)); 680 EXPECT_EQ(512, demuxer->Read(512, kBuffer));
700 EXPECT_TRUE(demuxer->GetPosition(&position)); 681 EXPECT_TRUE(demuxer->GetPosition(&position));
701 EXPECT_EQ(1024, position); 682 EXPECT_EQ(1024, position);
702 683
703 // Third read will get an end-of-file error. 684 // Third read will get an end-of-file error.
704 EXPECT_EQ(AVERROR_EOF, demuxer->Read(512, kBuffer)); 685 EXPECT_EQ(AVERROR_EOF, demuxer->Read(512, kBuffer));
705 686
706 // This read complete signal is generated when demuxer is stopped. 687 // This read complete signal is generated when demuxer is stopped.
707 EXPECT_CALL(*demuxer, SignalReadCompleted(DataSource::kReadError)); 688 EXPECT_CALL(*demuxer, SignalReadCompleted(DataSource::kReadError));
708 EXPECT_CALL(callback_, OnFilterCallback()); 689 demuxer->Stop(NewExpectedCallback());
709 EXPECT_CALL(callback_, OnCallbackDestroyed());
710 demuxer->Stop(callback_.NewCallback());
711 message_loop_.RunAllPending(); 690 message_loop_.RunAllPending();
712 } 691 }
713 692
714 TEST_F(FFmpegDemuxerTest, ProtocolGetSetPosition) { 693 TEST_F(FFmpegDemuxerTest, ProtocolGetSetPosition) {
715 { 694 {
716 SCOPED_TRACE(""); 695 SCOPED_TRACE("");
717 InitializeDemuxer(); 696 InitializeDemuxer();
718 } 697 }
719 698
720 InSequence s; 699 InSequence s;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
755 { 734 {
756 SCOPED_TRACE(""); 735 SCOPED_TRACE("");
757 InitializeDemuxer(); 736 InitializeDemuxer();
758 } 737 }
759 EXPECT_CALL(*data_source_, IsStreaming()) 738 EXPECT_CALL(*data_source_, IsStreaming())
760 .WillOnce(Return(false)); 739 .WillOnce(Return(false));
761 EXPECT_FALSE(demuxer_->IsStreaming()); 740 EXPECT_FALSE(demuxer_->IsStreaming());
762 } 741 }
763 742
764 } // namespace media 743 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/decoder_base_unittest.cc ('k') | media/filters/ffmpeg_video_decoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698