OLD | NEW |
---|---|
1 // Copyright (c) 2011 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 "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "media/base/mock_ffmpeg.h" | |
8 #include "media/base/mock_filters.h" | 7 #include "media/base/mock_filters.h" |
9 #include "media/ffmpeg/ffmpeg_common.h" | 8 #include "media/ffmpeg/ffmpeg_common.h" |
10 #include "media/filters/ffmpeg_glue.h" | 9 #include "media/filters/ffmpeg_glue.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
12 | 11 |
13 using ::testing::_; | 12 using ::testing::_; |
14 using ::testing::DoAll; | 13 using ::testing::DoAll; |
15 using ::testing::InSequence; | 14 using ::testing::InSequence; |
16 using ::testing::Return; | 15 using ::testing::Return; |
17 using ::testing::SetArgumentPointee; | 16 using ::testing::SetArgumentPointee; |
(...skipping 11 matching lines...) Expand all Loading... | |
29 MOCK_METHOD1(SetPosition, bool(int64 position)); | 28 MOCK_METHOD1(SetPosition, bool(int64 position)); |
30 MOCK_METHOD1(GetSize, bool(int64* size_out)); | 29 MOCK_METHOD1(GetSize, bool(int64* size_out)); |
31 MOCK_METHOD0(IsStreaming, bool()); | 30 MOCK_METHOD0(IsStreaming, bool()); |
32 | 31 |
33 private: | 32 private: |
34 DISALLOW_COPY_AND_ASSIGN(MockProtocol); | 33 DISALLOW_COPY_AND_ASSIGN(MockProtocol); |
35 }; | 34 }; |
36 | 35 |
37 class FFmpegGlueTest : public ::testing::Test { | 36 class FFmpegGlueTest : public ::testing::Test { |
38 public: | 37 public: |
39 FFmpegGlueTest() {} | 38 FFmpegGlueTest() : protocol_(NULL) {} |
40 | 39 |
41 virtual void SetUp() { | 40 virtual void SetUp() { |
scherkus (not reviewing)
2011/08/11 01:26:29
Shouldn't this test initialize the media library?
acolwell GONE FROM CHROMIUM
2011/08/11 23:54:40
Done.
| |
42 // Singleton should initialize FFmpeg. | 41 // Singleton should initialize FFmpeg. |
43 CHECK(FFmpegGlue::GetInstance()); | 42 CHECK(FFmpegGlue::GetInstance()); |
44 | 43 |
45 // Assign our static copy of URLProtocol for the rest of the tests. | 44 // Assign our static copy of URLProtocol for the rest of the tests. |
46 protocol_ = MockFFmpeg::protocol(); | 45 protocol_ = FFmpegGlue::url_protocol(); |
47 CHECK(protocol_); | 46 CHECK(protocol_); |
48 } | 47 } |
49 | 48 |
49 MOCK_METHOD1(CheckPoint, void(int val)); | |
50 | |
50 // Helper to open a URLContext pointing to the given mocked protocol. | 51 // Helper to open a URLContext pointing to the given mocked protocol. |
51 // Callers are expected to close the context at the end of their test. | 52 // Callers are expected to close the context at the end of their test. |
52 virtual void OpenContext(MockProtocol* protocol, URLContext* context) { | 53 virtual void OpenContext(MockProtocol* protocol, URLContext* context) { |
53 // IsStreaming() is called when opening. | 54 // IsStreaming() is called when opening. |
54 EXPECT_CALL(*protocol, IsStreaming()).WillOnce(Return(true)); | 55 EXPECT_CALL(*protocol, IsStreaming()).WillOnce(Return(true)); |
55 | 56 |
56 // Add the protocol to the glue layer and open a context. | 57 // Add the protocol to the glue layer and open a context. |
57 std::string key = FFmpegGlue::GetInstance()->AddProtocol(protocol); | 58 std::string key = FFmpegGlue::GetInstance()->AddProtocol(protocol); |
58 memset(context, 0, sizeof(*context)); | 59 memset(context, 0, sizeof(*context)); |
59 EXPECT_EQ(0, protocol_->url_open(context, key.c_str(), 0)); | 60 EXPECT_EQ(0, protocol_->url_open(context, key.c_str(), 0)); |
60 FFmpegGlue::GetInstance()->RemoveProtocol(protocol); | 61 FFmpegGlue::GetInstance()->RemoveProtocol(protocol); |
61 } | 62 } |
62 | 63 |
63 protected: | 64 protected: |
64 // Fixture members. | 65 // Fixture members. |
65 MockFFmpeg mock_ffmpeg_; | 66 URLProtocol* protocol_; |
66 static URLProtocol* protocol_; | |
67 | 67 |
68 private: | 68 private: |
69 DISALLOW_COPY_AND_ASSIGN(FFmpegGlueTest); | 69 DISALLOW_COPY_AND_ASSIGN(FFmpegGlueTest); |
70 }; | 70 }; |
71 | 71 |
72 URLProtocol* FFmpegGlueTest::protocol_ = NULL; | |
73 | |
74 TEST_F(FFmpegGlueTest, InitializeFFmpeg) { | 72 TEST_F(FFmpegGlueTest, InitializeFFmpeg) { |
75 // Make sure URLProtocol was filled out correctly. | 73 // Make sure URLProtocol was filled out correctly. |
76 EXPECT_STREQ("http", protocol_->name); | 74 EXPECT_STREQ("http", protocol_->name); |
77 EXPECT_TRUE(protocol_->url_close); | 75 EXPECT_TRUE(protocol_->url_close); |
78 EXPECT_TRUE(protocol_->url_open); | 76 EXPECT_TRUE(protocol_->url_open); |
79 EXPECT_TRUE(protocol_->url_read); | 77 EXPECT_TRUE(protocol_->url_read); |
80 EXPECT_TRUE(protocol_->url_seek); | 78 EXPECT_TRUE(protocol_->url_seek); |
81 EXPECT_TRUE(protocol_->url_write); | 79 EXPECT_TRUE(protocol_->url_write); |
82 } | 80 } |
83 | 81 |
(...skipping 27 matching lines...) Expand all Loading... | |
111 std::string key_a2 = glue->AddProtocol(protocol_a.get()); | 109 std::string key_a2 = glue->AddProtocol(protocol_a.get()); |
112 EXPECT_EQ(key_a, key_a2); | 110 EXPECT_EQ(key_a, key_a2); |
113 glue->GetProtocol(key_a2, &protocol_c); | 111 glue->GetProtocol(key_a2, &protocol_c); |
114 EXPECT_EQ(protocol_a.get(), protocol_c); | 112 EXPECT_EQ(protocol_a.get(), protocol_c); |
115 | 113 |
116 // Removes the protocols then releases our references. They should be | 114 // Removes the protocols then releases our references. They should be |
117 // destroyed. | 115 // destroyed. |
118 InSequence s; | 116 InSequence s; |
119 EXPECT_CALL(*protocol_a, OnDestroy()); | 117 EXPECT_CALL(*protocol_a, OnDestroy()); |
120 EXPECT_CALL(*protocol_b, OnDestroy()); | 118 EXPECT_CALL(*protocol_b, OnDestroy()); |
121 EXPECT_CALL(mock_ffmpeg_, CheckPoint(0)); | 119 EXPECT_CALL(*this, CheckPoint(0)); |
122 | 120 |
123 glue->RemoveProtocol(protocol_a.get()); | 121 glue->RemoveProtocol(protocol_a.get()); |
124 glue->GetProtocol(key_a, &protocol_c); | 122 glue->GetProtocol(key_a, &protocol_c); |
125 EXPECT_FALSE(protocol_c); | 123 EXPECT_FALSE(protocol_c); |
126 glue->GetProtocol(key_b, &protocol_d); | 124 glue->GetProtocol(key_b, &protocol_d); |
127 EXPECT_EQ(protocol_b.get(), protocol_d); | 125 EXPECT_EQ(protocol_b.get(), protocol_d); |
128 glue->RemoveProtocol(protocol_b.get()); | 126 glue->RemoveProtocol(protocol_b.get()); |
129 glue->GetProtocol(key_b, &protocol_d); | 127 glue->GetProtocol(key_b, &protocol_d); |
130 EXPECT_FALSE(protocol_d); | 128 EXPECT_FALSE(protocol_d); |
131 protocol_a.reset(); | 129 protocol_a.reset(); |
132 protocol_b.reset(); | 130 protocol_b.reset(); |
133 | 131 |
134 // Data sources should be deleted by this point. | 132 // Data sources should be deleted by this point. |
135 mock_ffmpeg_.CheckPoint(0); | 133 CheckPoint(0); |
136 } | 134 } |
137 | 135 |
138 TEST_F(FFmpegGlueTest, OpenClose) { | 136 TEST_F(FFmpegGlueTest, OpenClose) { |
139 // Prepare testing data. | 137 // Prepare testing data. |
140 FFmpegGlue* glue = FFmpegGlue::GetInstance(); | 138 FFmpegGlue* glue = FFmpegGlue::GetInstance(); |
141 | 139 |
142 // Create our protocol and add them to the glue layer. | 140 // Create our protocol and add them to the glue layer. |
143 scoped_ptr<StrictMock<Destroyable<MockProtocol> > > protocol( | 141 scoped_ptr<StrictMock<Destroyable<MockProtocol> > > protocol( |
144 new StrictMock<Destroyable<MockProtocol> >()); | 142 new StrictMock<Destroyable<MockProtocol> >()); |
145 EXPECT_CALL(*protocol, IsStreaming()).WillOnce(Return(true)); | 143 EXPECT_CALL(*protocol, IsStreaming()).WillOnce(Return(true)); |
146 std::string key = glue->AddProtocol(protocol.get()); | 144 std::string key = glue->AddProtocol(protocol.get()); |
147 | 145 |
148 // Prepare FFmpeg URLContext structure. | 146 // Prepare FFmpeg URLContext structure. |
149 URLContext context; | 147 URLContext context; |
150 memset(&context, 0, sizeof(context)); | 148 memset(&context, 0, sizeof(context)); |
151 | 149 |
152 // Test opening a URLContext with a protocol that doesn't exist. | 150 // Test opening a URLContext with a protocol that doesn't exist. |
153 EXPECT_EQ(AVERROR(EIO), protocol_->url_open(&context, "foobar", 0)); | 151 EXPECT_EQ(AVERROR(EIO), protocol_->url_open(&context, "foobar", 0)); |
154 | 152 |
155 // Test opening a URLContext with our protocol. | 153 // Test opening a URLContext with our protocol. |
156 EXPECT_EQ(0, protocol_->url_open(&context, key.c_str(), 0)); | 154 EXPECT_EQ(0, protocol_->url_open(&context, key.c_str(), 0)); |
157 EXPECT_EQ(URL_RDONLY, context.flags); | 155 EXPECT_EQ(URL_RDONLY, context.flags); |
158 EXPECT_EQ(protocol.get(), context.priv_data); | 156 EXPECT_EQ(protocol.get(), context.priv_data); |
159 EXPECT_TRUE(context.is_streamed); | 157 EXPECT_TRUE(context.is_streamed); |
160 | 158 |
161 // We're going to remove references one by one until the last reference is | 159 // We're going to remove references one by one until the last reference is |
162 // held by FFmpeg. Once we close the URLContext, the protocol should be | 160 // held by FFmpeg. Once we close the URLContext, the protocol should be |
163 // destroyed. | 161 // destroyed. |
164 InSequence s; | 162 InSequence s; |
165 EXPECT_CALL(mock_ffmpeg_, CheckPoint(0)); | 163 EXPECT_CALL(*this, CheckPoint(0)); |
166 EXPECT_CALL(mock_ffmpeg_, CheckPoint(1)); | 164 EXPECT_CALL(*this, CheckPoint(1)); |
167 EXPECT_CALL(*protocol, OnDestroy()); | 165 EXPECT_CALL(*protocol, OnDestroy()); |
168 EXPECT_CALL(mock_ffmpeg_, CheckPoint(2)); | 166 EXPECT_CALL(*this, CheckPoint(2)); |
169 | 167 |
170 // Remove the protocol from the glue layer, releasing a reference. | 168 // Remove the protocol from the glue layer, releasing a reference. |
171 glue->RemoveProtocol(protocol.get()); | 169 glue->RemoveProtocol(protocol.get()); |
172 mock_ffmpeg_.CheckPoint(0); | 170 CheckPoint(0); |
173 | 171 |
174 // Remove our own reference -- URLContext should maintain a reference. | 172 // Remove our own reference -- URLContext should maintain a reference. |
175 mock_ffmpeg_.CheckPoint(1); | 173 CheckPoint(1); |
176 protocol.reset(); | 174 protocol.reset(); |
177 | 175 |
178 // Close the URLContext, which should release the final reference. | 176 // Close the URLContext, which should release the final reference. |
179 EXPECT_EQ(0, protocol_->url_close(&context)); | 177 EXPECT_EQ(0, protocol_->url_close(&context)); |
180 mock_ffmpeg_.CheckPoint(2); | 178 CheckPoint(2); |
181 } | 179 } |
182 | 180 |
183 TEST_F(FFmpegGlueTest, Write) { | 181 TEST_F(FFmpegGlueTest, Write) { |
184 scoped_ptr<StrictMock<MockProtocol> > protocol( | 182 scoped_ptr<StrictMock<MockProtocol> > protocol( |
185 new StrictMock<MockProtocol>()); | 183 new StrictMock<MockProtocol>()); |
186 URLContext context; | 184 URLContext context; |
187 OpenContext(protocol.get(), &context); | 185 OpenContext(protocol.get(), &context); |
188 | 186 |
189 const int kBufferSize = 16; | 187 const int kBufferSize = 16; |
190 uint8 buffer[kBufferSize]; | 188 uint8 buffer[kBufferSize]; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 | 300 |
303 TEST_F(FFmpegGlueTest, Destroy) { | 301 TEST_F(FFmpegGlueTest, Destroy) { |
304 // Create our protocol and add them to the glue layer. | 302 // Create our protocol and add them to the glue layer. |
305 scoped_ptr<StrictMock<Destroyable<MockProtocol> > > protocol( | 303 scoped_ptr<StrictMock<Destroyable<MockProtocol> > > protocol( |
306 new StrictMock<Destroyable<MockProtocol> >()); | 304 new StrictMock<Destroyable<MockProtocol> >()); |
307 std::string key = FFmpegGlue::GetInstance()->AddProtocol(protocol.get()); | 305 std::string key = FFmpegGlue::GetInstance()->AddProtocol(protocol.get()); |
308 | 306 |
309 // We should expect the protocol to get destroyed when the unit test | 307 // We should expect the protocol to get destroyed when the unit test |
310 // exits. | 308 // exits. |
311 InSequence s; | 309 InSequence s; |
312 EXPECT_CALL(mock_ffmpeg_, CheckPoint(0)); | 310 EXPECT_CALL(*this, CheckPoint(0)); |
313 EXPECT_CALL(*protocol, OnDestroy()); | 311 EXPECT_CALL(*protocol, OnDestroy()); |
314 | 312 |
315 // Remove our own reference, we shouldn't be destroyed yet. | 313 // Remove our own reference, we shouldn't be destroyed yet. |
316 mock_ffmpeg_.CheckPoint(0); | 314 CheckPoint(0); |
317 protocol.reset(); | 315 protocol.reset(); |
318 | 316 |
319 // ~FFmpegGlue() will be called when this unit test finishes execution. By | 317 // ~FFmpegGlue() will be called when this unit test finishes execution. By |
320 // leaving something inside FFmpegGlue's map we get to test our cleanup code. | 318 // leaving something inside FFmpegGlue's map we get to test our cleanup code. |
321 } | 319 } |
322 | 320 |
323 } // namespace media | 321 } // namespace media |
OLD | NEW |