1 // Copyright 2013 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 "base/message_loop/message_loop.h"
7 #include "content/renderer/media/android/media_info_loader.h"
8 #include "content/test/mock_webframeclient.h"
9 #include "content/test/mock_weburlloader.h"
10 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
11 #include "third_party/WebKit/public/platform/WebURLError.h"
12 #include "third_party/WebKit/public/platform/WebURLRequest.h"
13 #include "third_party/WebKit/public/platform/WebURLResponse.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 #include "third_party/WebKit/public/web/WebView.h"
18 using ::testing::InSequence
;
19 using ::testing::NiceMock
;
21 using blink::WebLocalFrame
;
22 using blink::WebString
;
23 using blink::WebURLError
;
24 using blink::WebURLResponse
;
29 static const char* kHttpUrl
= "http://test";
30 static const char kHttpRedirectToSameDomainUrl1
[] = "http://test/ing";
31 static const char kHttpRedirectToSameDomainUrl2
[] = "http://test/ing2";
32 static const char kHttpRedirectToDifferentDomainUrl1
[] = "http://test2";
33 static const char kHttpDataUrl
[] = "data:audio/wav;base64,UklGRhwMAABXQVZFZm10";
35 static const int kHttpOK
= 200;
36 static const int kHttpNotFound
= 404;
38 class MediaInfoLoaderTest
: public testing::Test
{
41 : view_(WebView::create(NULL
)),
42 frame_(WebLocalFrame::create(blink::WebTreeScopeType::Document
,
44 view_
->setMainFrame(frame_
);
47 virtual ~MediaInfoLoaderTest() {
54 blink::WebMediaPlayer::CORSMode cors_mode
) {
57 loader_
.reset(new MediaInfoLoader(
59 base::Bind(&MediaInfoLoaderTest::ReadyCallback
,
60 base::Unretained(this))));
62 // |test_loader_| will be used when Start() is called.
63 url_loader_
= new NiceMock
<MockWebURLLoader
>();
64 loader_
->test_loader_
= scoped_ptr
<blink::WebURLLoader
>(url_loader_
);
69 EXPECT_CALL(*url_loader_
, loadAsynchronously(_
, _
));
70 loader_
->Start(view_
->mainFrame());
75 EXPECT_CALL(*url_loader_
, cancel());
79 void Redirect(const char* url
) {
80 GURL
redirect_url(url
);
81 blink::WebURLRequest
new_request(redirect_url
);
82 blink::WebURLResponse
redirect_response(gurl_
);
84 loader_
->willSendRequest(url_loader_
, new_request
, redirect_response
);
86 base::MessageLoop::current()->RunUntilIdle();
90 int http_status
, MediaInfoLoader::Status expected_status
) {
91 EXPECT_CALL(*this, ReadyCallback(expected_status
, _
, _
, _
));
92 EXPECT_CALL(*url_loader_
, cancel());
94 WebURLResponse
response(gurl_
);
95 response
.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
96 WebString::fromUTF8("0"));
97 response
.setExpectedContentLength(0);
98 response
.setHTTPStatusCode(http_status
);
99 loader_
->didReceiveResponse(url_loader_
, response
);
103 EXPECT_CALL(*this, ReadyCallback(
104 MediaInfoLoader::kFailed
, _
, _
, _
));
105 loader_
->didFail(url_loader_
, WebURLError());
108 MOCK_METHOD4(ReadyCallback
,
109 void(MediaInfoLoader::Status
, const GURL
&, const GURL
&, bool));
114 scoped_ptr
<MediaInfoLoader
> loader_
;
115 NiceMock
<MockWebURLLoader
>* url_loader_
;
117 MockWebFrameClient client_
;
119 WebLocalFrame
* frame_
;
121 base::MessageLoop message_loop_
;
124 DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest
);
127 TEST_F(MediaInfoLoaderTest
, StartStop
) {
128 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
133 TEST_F(MediaInfoLoaderTest
, LoadFailure
) {
134 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
139 TEST_F(MediaInfoLoaderTest
, DataUri
) {
140 Initialize(kHttpDataUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
142 SendResponse(0, MediaInfoLoader::kOk
);
145 TEST_F(MediaInfoLoaderTest
, HasSingleOriginNoRedirect
) {
146 // Make sure no redirect case works as expected.
147 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
149 SendResponse(kHttpOK
, MediaInfoLoader::kOk
);
150 EXPECT_TRUE(loader_
->HasSingleOrigin());
153 TEST_F(MediaInfoLoaderTest
, HasSingleOriginSingleRedirect
) {
154 // Test redirect to the same domain.
155 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
157 Redirect(kHttpRedirectToSameDomainUrl1
);
158 SendResponse(kHttpOK
, MediaInfoLoader::kOk
);
159 EXPECT_TRUE(loader_
->HasSingleOrigin());
162 TEST_F(MediaInfoLoaderTest
, HasSingleOriginDoubleRedirect
) {
163 // Test redirect twice to the same domain.
164 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
166 Redirect(kHttpRedirectToSameDomainUrl1
);
167 Redirect(kHttpRedirectToSameDomainUrl2
);
168 SendResponse(kHttpOK
, MediaInfoLoader::kOk
);
169 EXPECT_TRUE(loader_
->HasSingleOrigin());
172 TEST_F(MediaInfoLoaderTest
, HasSingleOriginDifferentDomain
) {
173 // Test redirect to a different domain.
174 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
176 Redirect(kHttpRedirectToDifferentDomainUrl1
);
177 SendResponse(kHttpOK
, MediaInfoLoader::kOk
);
178 EXPECT_FALSE(loader_
->HasSingleOrigin());
181 TEST_F(MediaInfoLoaderTest
, HasSingleOriginMultipleDomains
) {
182 // Test redirect to the same domain and then to a different domain.
183 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUnspecified
);
185 Redirect(kHttpRedirectToSameDomainUrl1
);
186 Redirect(kHttpRedirectToDifferentDomainUrl1
);
187 SendResponse(kHttpOK
, MediaInfoLoader::kOk
);
188 EXPECT_FALSE(loader_
->HasSingleOrigin());
191 TEST_F(MediaInfoLoaderTest
, CORSAccessCheckPassed
) {
192 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUseCredentials
);
194 SendResponse(kHttpOK
, MediaInfoLoader::kOk
);
195 EXPECT_TRUE(loader_
->DidPassCORSAccessCheck());
198 TEST_F(MediaInfoLoaderTest
, CORSAccessCheckFailed
) {
199 Initialize(kHttpUrl
, blink::WebMediaPlayer::CORSModeUseCredentials
);
201 SendResponse(kHttpNotFound
, MediaInfoLoader::kFailed
);
202 EXPECT_FALSE(loader_
->DidPassCORSAccessCheck());
205 } // namespace content