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

Side by Side Diff: webkit/glue/media/simple_data_source_unittest.cc

Issue 3863002: Refactoring BufferedDataSource to work with WebURLLoader instead of a MediaResourceLoaderBridge. (Closed) Base URL: http://git.chromium.org/git/chromium.git
Patch Set: . Created 10 years, 1 month 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
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/callback.h" 5 #include "base/callback.h"
6 #include "media/base/filters.h" 6 #include "media/base/filters.h"
7 #include "media/base/mock_filter_host.h" 7 #include "media/base/mock_filter_host.h"
8 #include "media/base/mock_filters.h" 8 #include "media/base/mock_filters.h"
9 #include "webkit/glue/media/mock_media_resource_loader_bridge_factory.h" 9 #include "net/base/net_errors.h"
10 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
11 #include "third_party/WebKit/WebKit/chromium/public/WebURLError.h"
12 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h"
13 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h"
14 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h"
10 #include "webkit/glue/media/simple_data_source.h" 15 #include "webkit/glue/media/simple_data_source.h"
11 #include "webkit/glue/mock_resource_loader_bridge.h" 16 #include "webkit/glue/mock_webframe.h"
17 #include "webkit/glue/mock_weburlloader_impl.h"
12 18
13 using ::testing::_; 19 using ::testing::_;
14 using ::testing::DoAll; 20 using ::testing::DoAll;
15 using ::testing::InSequence; 21 using ::testing::InSequence;
16 using ::testing::Invoke; 22 using ::testing::Invoke;
17 using ::testing::NiceMock; 23 using ::testing::NiceMock;
18 using ::testing::NotNull; 24 using ::testing::NotNull;
19 using ::testing::Return; 25 using ::testing::Return;
20 using ::testing::SetArgumentPointee; 26 using ::testing::SetArgumentPointee;
21 using ::testing::StrictMock; 27 using ::testing::StrictMock;
22 using ::testing::WithArgs; 28 using ::testing::WithArgs;
23 29
30 using WebKit::WebURLError;
31 using WebKit::WebURLLoader;
32 using WebKit::WebURLRequest;
33 using WebKit::WebURLResponse;
34
24 namespace { 35 namespace {
25 36
26 const int kDataSize = 1024; 37 const int kDataSize = 1024;
27 const char kHttpUrl[] = "http://test"; 38 const char kHttpUrl[] = "http://test";
28 const char kHttpsUrl[] = "https://test"; 39 const char kHttpsUrl[] = "https://test";
29 const char kFileUrl[] = "file://test"; 40 const char kFileUrl[] = "file://test";
30 const char kDataUrl[] = 41 const char kDataUrl[] =
31 "data:text/plain;base64,YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK"; 42 "data:text/plain;base64,YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK";
32 const char kDataUrlDecoded[] = "abcdefghijklmnopqrstuvwxyz"; 43 const char kDataUrlDecoded[] = "abcdefghijklmnopqrstuvwxyz";
33 const char kInvalidUrl[] = "whatever://test"; 44 const char kInvalidUrl[] = "whatever://test";
34 45
35 } // namespace 46 } // namespace
36 47
37 namespace webkit_glue { 48 namespace webkit_glue {
38 49
39 class SimpleDataSourceTest : public testing::Test { 50 class SimpleDataSourceTest : public testing::Test {
40 public: 51 public:
41 SimpleDataSourceTest() { 52 SimpleDataSourceTest() {
42 bridge_factory_.reset(
43 new NiceMock<MockMediaResourceLoaderBridgeFactory>());
44 bridge_.reset(new NiceMock<MockResourceLoaderBridge>());
45
46 for (int i = 0; i < kDataSize; ++i) { 53 for (int i = 0; i < kDataSize; ++i) {
47 data_[i] = i; 54 data_[i] = i;
48 } 55 }
49 } 56 }
50 57
51 virtual ~SimpleDataSourceTest() { 58 virtual ~SimpleDataSourceTest() {
52 if (bridge_.get()) 59 if (frame_.get())
53 EXPECT_CALL(*bridge_, OnDestroy()); 60 EXPECT_CALL(*frame_, OnFrameDestroy())
scherkus (not reviewing) 2010/11/22 21:27:29 try to use {} for non-one-liner ifs
annacc 2010/11/24 21:34:44 Changed to no longer expect a OnDestroy(). Now ju
54 if (bridge_factory_.get()) 61 .WillOnce(Invoke(this,
55 EXPECT_CALL(*bridge_factory_, OnDestroy()); 62 &SimpleDataSourceTest::ReleaseWebFrame));
56 } 63 }
57 64
58 void InitializeDataSource(const char* url) { 65 void InitializeDataSource(const char* url) {
66 gurl_ = GURL(url);
67
68 frame_.reset(new NiceMock<MockWebFrame>());
69 url_loader_ = new NiceMock<MockWebURLLoader>();
70
59 data_source_ = new SimpleDataSource(MessageLoop::current(), 71 data_source_ = new SimpleDataSource(MessageLoop::current(),
60 bridge_factory_.get()); 72 frame_.get());
61 CHECK(data_source_);
62 73
63 // There is no need to provide a message loop to data source. 74 // There is no need to provide a message loop to data source.
64 data_source_->set_host(&host_); 75 data_source_->set_host(&host_);
76 data_source_->SetURLLoaderForTest(url_loader_);
65 77
66 // First a bridge is created.
67 InSequence s; 78 InSequence s;
68 EXPECT_CALL(*bridge_factory_, CreateBridge(GURL(url), _, -1, -1))
69 .WillOnce(Return(bridge_.get()));
70 EXPECT_CALL(*bridge_, Start(data_source_.get()))
71 .WillOnce(Return(true));
72 79
73 data_source_->Initialize(url, callback_.NewCallback()); 80 data_source_->Initialize(url, callback_.NewCallback());
74
75 MessageLoop::current()->RunAllPending(); 81 MessageLoop::current()->RunAllPending();
76 } 82 }
77 83
78 void RequestSucceeded(bool is_loaded) { 84 void RequestSucceeded(bool is_loaded) {
79 ResourceResponseInfo info; 85 WebURLResponse response(gurl_);
80 info.content_length = kDataSize; 86 response.setExpectedContentLength(kDataSize);
81 87
82 data_source_->OnReceivedResponse(info, false); 88 data_source_->didReceiveResponse(NULL, response);
83 int64 size; 89 int64 size;
84 EXPECT_TRUE(data_source_->GetSize(&size)); 90 EXPECT_TRUE(data_source_->GetSize(&size));
85 EXPECT_EQ(kDataSize, size); 91 EXPECT_EQ(kDataSize, size);
86 92
87 for (int i = 0; i < kDataSize; ++i) 93 for (int i = 0; i < kDataSize; ++i) {
88 data_source_->OnReceivedData(data_ + i, 1); 94 data_source_->didReceiveData(NULL, data_ + i, 1);
95 }
89 96
90 EXPECT_CALL(host_, SetLoaded(is_loaded)); 97 EXPECT_CALL(host_, SetLoaded(is_loaded));
91 98
92 InSequence s; 99 InSequence s;
93 EXPECT_CALL(*bridge_, OnDestroy())
94 .WillOnce(Invoke(this, &SimpleDataSourceTest::ReleaseBridge));
95 EXPECT_CALL(host_, SetTotalBytes(kDataSize)); 100 EXPECT_CALL(host_, SetTotalBytes(kDataSize));
96 EXPECT_CALL(host_, SetBufferedBytes(kDataSize)); 101 EXPECT_CALL(host_, SetBufferedBytes(kDataSize));
97 EXPECT_CALL(callback_, OnFilterCallback()); 102 EXPECT_CALL(callback_, OnFilterCallback());
98 EXPECT_CALL(callback_, OnCallbackDestroyed()); 103 EXPECT_CALL(callback_, OnCallbackDestroyed());
99 104
100 URLRequestStatus status; 105 data_source_->didFinishLoading(NULL, 0);
101 status.set_status(URLRequestStatus::SUCCESS);
102 status.set_os_error(0);
103 data_source_->OnCompletedRequest(status, "", base::Time());
104 106
105 // Let the tasks to be executed. 107 // Let the tasks to be executed.
106 MessageLoop::current()->RunAllPending(); 108 MessageLoop::current()->RunAllPending();
107 } 109 }
108 110
109 void RequestFailed() { 111 void RequestFailed() {
110 InSequence s; 112 InSequence s;
111 EXPECT_CALL(*bridge_, OnDestroy())
112 .WillOnce(Invoke(this, &SimpleDataSourceTest::ReleaseBridge));
113 EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK)); 113 EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK));
114 EXPECT_CALL(callback_, OnFilterCallback()); 114 EXPECT_CALL(callback_, OnFilterCallback());
115 EXPECT_CALL(callback_, OnCallbackDestroyed()); 115 EXPECT_CALL(callback_, OnCallbackDestroyed());
116 116
117 URLRequestStatus status; 117 WebURLError error;
118 status.set_status(URLRequestStatus::FAILED); 118 error.reason = net::ERR_FAILED;
119 status.set_os_error(100); 119 data_source_->didFail(NULL, error);
120 data_source_->OnCompletedRequest(status, "", base::Time());
121 120
122 // Let the tasks to be executed. 121 // Let the tasks to be executed.
123 MessageLoop::current()->RunAllPending(); 122 MessageLoop::current()->RunAllPending();
124 } 123 }
125 124
126 void DestroyDataSource() { 125 void DestroyDataSource() {
127 EXPECT_CALL(*bridge_factory_, OnDestroy())
128 .WillOnce(Invoke(this, &SimpleDataSourceTest::ReleaseBridgeFactory));
129
130 StrictMock<media::MockFilterCallback> callback; 126 StrictMock<media::MockFilterCallback> callback;
131 EXPECT_CALL(callback, OnFilterCallback()); 127 EXPECT_CALL(callback, OnFilterCallback());
132 EXPECT_CALL(callback, OnCallbackDestroyed()); 128 EXPECT_CALL(callback, OnCallbackDestroyed());
129 EXPECT_CALL(*url_loader_, OnURLDestroy());
130
133 data_source_->Stop(callback.NewCallback()); 131 data_source_->Stop(callback.NewCallback());
134 MessageLoop::current()->RunAllPending(); 132 MessageLoop::current()->RunAllPending();
135 133
136 data_source_ = NULL; 134 data_source_ = NULL;
137 } 135 }
138 136
137 void ReleaseWebFrame() {
138 ignore_result(frame_.release());
139 }
140
139 void AsyncRead() { 141 void AsyncRead() {
140 for (int i = 0; i < kDataSize; ++i) { 142 for (int i = 0; i < kDataSize; ++i) {
141 uint8 buffer[1]; 143 uint8 buffer[1];
142 144
143 EXPECT_CALL(*this, ReadCallback(1)); 145 EXPECT_CALL(*this, ReadCallback(1));
144 data_source_->Read( 146 data_source_->Read(
145 i, 1, buffer, 147 i, 1, buffer,
146 NewCallback(this, &SimpleDataSourceTest::ReadCallback)); 148 NewCallback(this, &SimpleDataSourceTest::ReadCallback));
147 EXPECT_EQ(static_cast<uint8>(data_[i]), buffer[0]); 149 EXPECT_EQ(static_cast<uint8>(data_[i]), buffer[0]);
148 } 150 }
149 } 151 }
150 152
151 void ReleaseBridge() {
152 ignore_result(bridge_.release());
153 }
154
155 void ReleaseBridgeFactory() {
156 ignore_result(bridge_factory_.release());
157 }
158
159 MOCK_METHOD1(ReadCallback, void(size_t size)); 153 MOCK_METHOD1(ReadCallback, void(size_t size));
160 154
161 protected: 155 protected:
156 GURL gurl_;
162 scoped_ptr<MessageLoop> message_loop_; 157 scoped_ptr<MessageLoop> message_loop_;
163 scoped_ptr<NiceMock<MockMediaResourceLoaderBridgeFactory> > bridge_factory_; 158 NiceMock<MockWebURLLoader>* url_loader_;
164 scoped_ptr<NiceMock<MockResourceLoaderBridge> > bridge_;
165 scoped_refptr<SimpleDataSource> data_source_; 159 scoped_refptr<SimpleDataSource> data_source_;
166 StrictMock<media::MockFilterHost> host_; 160 StrictMock<media::MockFilterHost> host_;
167 StrictMock<media::MockFilterCallback> callback_; 161 StrictMock<media::MockFilterCallback> callback_;
162 scoped_ptr<NiceMock<MockWebFrame> > frame_;
163
168 char data_[kDataSize]; 164 char data_[kDataSize];
169 165
170 DISALLOW_COPY_AND_ASSIGN(SimpleDataSourceTest); 166 DISALLOW_COPY_AND_ASSIGN(SimpleDataSourceTest);
171 }; 167 };
172 168
173 TEST_F(SimpleDataSourceTest, InitializeHTTP) { 169 TEST_F(SimpleDataSourceTest, InitializeHTTP) {
174 InitializeDataSource(kHttpUrl); 170 InitializeDataSource(kHttpUrl);
175 RequestSucceeded(false); 171 RequestSucceeded(false);
176 DestroyDataSource(); 172 DestroyDataSource();
177 } 173 }
178 174
179 TEST_F(SimpleDataSourceTest, InitializeHTTPS) { 175 TEST_F(SimpleDataSourceTest, InitializeHTTPS) {
180 InitializeDataSource(kHttpsUrl); 176 InitializeDataSource(kHttpsUrl);
181 RequestSucceeded(false); 177 RequestSucceeded(false);
182 DestroyDataSource(); 178 DestroyDataSource();
183 } 179 }
184 180
185 TEST_F(SimpleDataSourceTest, InitializeFile) { 181 TEST_F(SimpleDataSourceTest, InitializeFile) {
186 InitializeDataSource(kFileUrl); 182 InitializeDataSource(kFileUrl);
187 RequestSucceeded(true); 183 RequestSucceeded(true);
188 DestroyDataSource(); 184 DestroyDataSource();
189 } 185 }
190 186
191 TEST_F(SimpleDataSourceTest, InitializeData) { 187 TEST_F(SimpleDataSourceTest, InitializeData) {
188 frame_.reset(new NiceMock<MockWebFrame>());
189 url_loader_ = new NiceMock<MockWebURLLoader>();
190
192 data_source_ = new SimpleDataSource(MessageLoop::current(), 191 data_source_ = new SimpleDataSource(MessageLoop::current(),
193 bridge_factory_.get()); 192 frame_.get());
194 EXPECT_TRUE(data_source_->IsUrlSupported(kDataUrl)); 193 EXPECT_TRUE(data_source_->IsUrlSupported(kDataUrl));
195 CHECK(data_source_);
196 194
197 // There is no need to provide a message loop to data source. 195 // There is no need to provide a message loop to data source.
198 data_source_->set_host(&host_); 196 data_source_->set_host(&host_);
197 data_source_->SetURLLoaderForTest(url_loader_);
199 198
200 EXPECT_CALL(host_, SetLoaded(true)); 199 EXPECT_CALL(host_, SetLoaded(true));
201 EXPECT_CALL(host_, SetTotalBytes(sizeof(kDataUrlDecoded))); 200 EXPECT_CALL(host_, SetTotalBytes(sizeof(kDataUrlDecoded)));
202 EXPECT_CALL(host_, SetBufferedBytes(sizeof(kDataUrlDecoded))); 201 EXPECT_CALL(host_, SetBufferedBytes(sizeof(kDataUrlDecoded)));
203 EXPECT_CALL(callback_, OnFilterCallback()); 202 EXPECT_CALL(callback_, OnFilterCallback());
204 EXPECT_CALL(callback_, OnCallbackDestroyed()); 203 EXPECT_CALL(callback_, OnCallbackDestroyed());
205 204
206 data_source_->Initialize(kDataUrl, callback_.NewCallback()); 205 data_source_->Initialize(kDataUrl, callback_.NewCallback());
207 MessageLoop::current()->RunAllPending(); 206 MessageLoop::current()->RunAllPending();
208 207
209 DestroyDataSource(); 208 DestroyDataSource();
210 } 209 }
211 210
212 TEST_F(SimpleDataSourceTest, RequestFailed) { 211 TEST_F(SimpleDataSourceTest, RequestFailed) {
213 InitializeDataSource(kHttpUrl); 212 InitializeDataSource(kHttpUrl);
214 RequestFailed(); 213 RequestFailed();
215 DestroyDataSource(); 214 DestroyDataSource();
216 } 215 }
217 216
218 TEST_F(SimpleDataSourceTest, StopWhenDownloading) { 217 TEST_F(SimpleDataSourceTest, StopWhenDownloading) {
219 InitializeDataSource(kHttpUrl); 218 InitializeDataSource(kHttpUrl);
220 219
221 EXPECT_CALL(*bridge_, Cancel()); 220 EXPECT_CALL(*url_loader_, cancel());
222 EXPECT_CALL(*bridge_, OnDestroy())
223 .WillOnce(Invoke(this, &SimpleDataSourceTest::ReleaseBridge));
224 EXPECT_CALL(callback_, OnCallbackDestroyed()); 221 EXPECT_CALL(callback_, OnCallbackDestroyed());
225 DestroyDataSource(); 222 DestroyDataSource();
226 } 223 }
227 224
228 TEST_F(SimpleDataSourceTest, AsyncRead) { 225 TEST_F(SimpleDataSourceTest, AsyncRead) {
229 InitializeDataSource(kFileUrl); 226 InitializeDataSource(kFileUrl);
230 RequestSucceeded(true); 227 RequestSucceeded(true);
231 AsyncRead(); 228 AsyncRead();
232 DestroyDataSource(); 229 DestroyDataSource();
233 } 230 }
234 231
235 } // namespace webkit_glue 232 } // namespace webkit_glue
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698