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