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
3 // found in the LICENSE file.
6 #include "media/base/filters.h"
7 #include "media/base/mock_callback.h"
8 #include "media/base/mock_filter_host.h"
9 #include "media/base/mock_filters.h"
10 #include "net/base/net_errors.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
16 #include "webkit/glue/media/simple_data_source.h"
17 #include "webkit/mocks/mock_webframeclient.h"
18 #include "webkit/mocks/mock_weburlloader.h"
21 using ::testing::DoAll
;
22 using ::testing::InSequence
;
23 using ::testing::Invoke
;
24 using ::testing::NiceMock
;
25 using ::testing::NotNull
;
26 using ::testing::Return
;
27 using ::testing::SetArgumentPointee
;
28 using ::testing::StrictMock
;
29 using ::testing::WithArgs
;
31 using WebKit::WebURLError
;
32 using WebKit::WebURLLoader
;
33 using WebKit::WebURLRequest
;
34 using WebKit::WebURLResponse
;
35 using WebKit::WebView
;
37 namespace webkit_glue
{
39 static const int kDataSize
= 1024;
40 static const char kHttpUrl
[] = "http://test";
41 static const char kHttpsUrl
[] = "https://test";
42 static const char kFileUrl
[] = "file://test";
43 static const char kDataUrl
[] =
44 "data:text/plain;base64,YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK";
45 static const char kDataUrlDecoded
[] = "abcdefghijklmnopqrstuvwxyz";
46 static const char kInvalidUrl
[] = "whatever://test";
47 static const char kHttpRedirectToSameDomainUrl1
[] = "http://test/ing";
48 static const char kHttpRedirectToSameDomainUrl2
[] = "http://test/ing2";
49 static const char kHttpRedirectToDifferentDomainUrl1
[] = "http://test2";
50 static const char kHttpRedirectToDifferentDomainUrl2
[] = "http://test2/ing";
52 class SimpleDataSourceTest
: public testing::Test
{
54 SimpleDataSourceTest()
55 : view_(WebView::create(NULL
)) {
56 view_
->initializeMainFrame(&client_
);
58 for (int i
= 0; i
< kDataSize
; ++i
) {
63 virtual ~SimpleDataSourceTest() {
67 void InitializeDataSource(const char* url
,
68 const media::PipelineStatusCB
& callback
) {
71 url_loader_
= new NiceMock
<MockWebURLLoader
>();
73 data_source_
= new SimpleDataSource(MessageLoop::current(),
76 // There is no need to provide a message loop to data source.
77 data_source_
->set_host(&host_
);
78 data_source_
->SetURLLoaderForTest(url_loader_
);
80 data_source_
->Initialize(url
, callback
);
81 MessageLoop::current()->RunAllPending();
84 void RequestSucceeded(bool is_loaded
) {
85 WebURLResponse
response(gurl_
);
86 response
.setExpectedContentLength(kDataSize
);
88 data_source_
->didReceiveResponse(NULL
, response
);
90 EXPECT_TRUE(data_source_
->GetSize(&size
));
91 EXPECT_EQ(kDataSize
, size
);
93 for (int i
= 0; i
< kDataSize
; ++i
) {
94 data_source_
->didReceiveData(NULL
, data_
+ i
, 1, 1);
97 EXPECT_CALL(host_
, SetLoaded(is_loaded
));
100 EXPECT_CALL(host_
, SetTotalBytes(kDataSize
));
101 EXPECT_CALL(host_
, SetBufferedBytes(kDataSize
));
103 data_source_
->didFinishLoading(NULL
, 0);
105 // Let the tasks to be executed.
106 MessageLoop::current()->RunAllPending();
109 void RequestFailed() {
113 error
.reason
= net::ERR_FAILED
;
114 data_source_
->didFail(NULL
, error
);
116 // Let the tasks to be executed.
117 MessageLoop::current()->RunAllPending();
120 void Redirect(const char* url
) {
121 GURL
redirectUrl(url
);
122 WebURLRequest
newRequest(redirectUrl
);
123 WebURLResponse
redirectResponse(gurl_
);
125 data_source_
->willSendRequest(url_loader_
, newRequest
, redirectResponse
);
127 MessageLoop::current()->RunAllPending();
130 void DestroyDataSource() {
131 data_source_
->Stop(media::NewExpectedClosure());
132 MessageLoop::current()->RunAllPending();
138 for (int i
= 0; i
< kDataSize
; ++i
) {
141 EXPECT_CALL(*this, ReadCallback(1));
144 base::Bind(&SimpleDataSourceTest::ReadCallback
,
145 base::Unretained(this)));
146 EXPECT_EQ(static_cast<uint8
>(data_
[i
]), buffer
[0]);
150 MOCK_METHOD1(ReadCallback
, void(size_t size
));
154 scoped_ptr
<MessageLoop
> message_loop_
;
155 NiceMock
<MockWebURLLoader
>* url_loader_
;
156 scoped_refptr
<SimpleDataSource
> data_source_
;
157 StrictMock
<media::MockFilterHost
> host_
;
159 MockWebFrameClient client_
;
162 char data_
[kDataSize
];
164 DISALLOW_COPY_AND_ASSIGN(SimpleDataSourceTest
);
167 TEST_F(SimpleDataSourceTest
, InitializeHTTP
) {
168 InitializeDataSource(kHttpUrl
,
169 media::NewExpectedStatusCB(media::PIPELINE_OK
));
170 RequestSucceeded(false);
174 TEST_F(SimpleDataSourceTest
, InitializeHTTPS
) {
175 InitializeDataSource(kHttpsUrl
,
176 media::NewExpectedStatusCB(media::PIPELINE_OK
));
177 RequestSucceeded(false);
181 TEST_F(SimpleDataSourceTest
, InitializeFile
) {
182 InitializeDataSource(kFileUrl
,
183 media::NewExpectedStatusCB(media::PIPELINE_OK
));
184 RequestSucceeded(true);
188 TEST_F(SimpleDataSourceTest
, InitializeData
) {
189 url_loader_
= new NiceMock
<MockWebURLLoader
>();
191 data_source_
= new SimpleDataSource(MessageLoop::current(),
193 // There is no need to provide a message loop to data source.
194 data_source_
->set_host(&host_
);
195 data_source_
->SetURLLoaderForTest(url_loader_
);
197 EXPECT_CALL(host_
, SetLoaded(true));
198 EXPECT_CALL(host_
, SetTotalBytes(sizeof(kDataUrlDecoded
)));
199 EXPECT_CALL(host_
, SetBufferedBytes(sizeof(kDataUrlDecoded
)));
201 data_source_
->Initialize(kDataUrl
,
202 media::NewExpectedStatusCB(media::PIPELINE_OK
));
203 MessageLoop::current()->RunAllPending();
208 TEST_F(SimpleDataSourceTest
, RequestFailed
) {
209 InitializeDataSource(kHttpUrl
,
210 media::NewExpectedStatusCB(media::PIPELINE_ERROR_NETWORK
));
215 static void OnStatusCB(bool* called
, media::PipelineStatus status
) {
219 TEST_F(SimpleDataSourceTest
, StopWhenDownloading
) {
220 // The callback should be deleted, but not executed.
221 // TODO(scherkus): should this really be the behaviour? Seems strange...
222 bool was_called
= false;
223 InitializeDataSource(kHttpUrl
, base::Bind(&OnStatusCB
, &was_called
));
225 EXPECT_CALL(*url_loader_
, cancel());
227 EXPECT_FALSE(was_called
);
230 TEST_F(SimpleDataSourceTest
, AsyncRead
) {
231 InitializeDataSource(kFileUrl
,
232 media::NewExpectedStatusCB(media::PIPELINE_OK
));
233 RequestSucceeded(true);
238 // NOTE: This test will need to be reworked a little once
239 // http://code.google.com/p/chromium/issues/detail?id=72578
241 TEST_F(SimpleDataSourceTest
, HasSingleOrigin
) {
242 // Make sure no redirect case works as expected.
243 InitializeDataSource(kHttpUrl
,
244 media::NewExpectedStatusCB(media::PIPELINE_OK
));
245 RequestSucceeded(false);
246 EXPECT_TRUE(data_source_
->HasSingleOrigin());
249 // Test redirect to the same domain.
250 InitializeDataSource(kHttpUrl
,
251 media::NewExpectedStatusCB(media::PIPELINE_OK
));
252 Redirect(kHttpRedirectToSameDomainUrl1
);
253 RequestSucceeded(false);
254 EXPECT_TRUE(data_source_
->HasSingleOrigin());
257 // Test redirect twice to the same domain.
258 InitializeDataSource(kHttpUrl
,
259 media::NewExpectedStatusCB(media::PIPELINE_OK
));
260 Redirect(kHttpRedirectToSameDomainUrl1
);
261 Redirect(kHttpRedirectToSameDomainUrl2
);
262 RequestSucceeded(false);
263 EXPECT_TRUE(data_source_
->HasSingleOrigin());
266 // Test redirect to a different domain.
267 InitializeDataSource(kHttpUrl
,
268 media::NewExpectedStatusCB(media::PIPELINE_OK
));
269 Redirect(kHttpRedirectToDifferentDomainUrl1
);
270 RequestSucceeded(false);
271 EXPECT_FALSE(data_source_
->HasSingleOrigin());
274 // Test redirect to the same domain and then to a different domain.
275 InitializeDataSource(kHttpUrl
,
276 media::NewExpectedStatusCB(media::PIPELINE_OK
));
277 Redirect(kHttpRedirectToSameDomainUrl1
);
278 Redirect(kHttpRedirectToDifferentDomainUrl1
);
279 RequestSucceeded(false);
280 EXPECT_FALSE(data_source_
->HasSingleOrigin());
284 } // namespace webkit_glue