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)), frame_(WebLocalFrame::create(&client_)) {
42 view_->setMainFrame(frame_);
45 virtual ~MediaInfoLoaderTest() {
52 blink::WebMediaPlayer::CORSMode cors_mode) {
55 loader_.reset(new MediaInfoLoader(
57 base::Bind(&MediaInfoLoaderTest::ReadyCallback,
58 base::Unretained(this))));
60 // |test_loader_| will be used when Start() is called.
61 url_loader_ = new NiceMock<MockWebURLLoader>();
62 loader_->test_loader_ = scoped_ptr<blink::WebURLLoader>(url_loader_);
67 EXPECT_CALL(*url_loader_, loadAsynchronously(_, _));
68 loader_->Start(view_->mainFrame());
73 EXPECT_CALL(*url_loader_, cancel());
77 void Redirect(const char* url) {
78 GURL redirect_url(url);
79 blink::WebURLRequest new_request(redirect_url);
80 blink::WebURLResponse redirect_response(gurl_);
82 loader_->willSendRequest(url_loader_, new_request, redirect_response);
84 base::MessageLoop::current()->RunUntilIdle();
88 int http_status, MediaInfoLoader::Status expected_status) {
89 EXPECT_CALL(*this, ReadyCallback(expected_status));
90 EXPECT_CALL(*url_loader_, cancel());
92 WebURLResponse response(gurl_);
93 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
94 WebString::fromUTF8("0"));
95 response.setExpectedContentLength(0);
96 response.setHTTPStatusCode(http_status);
97 loader_->didReceiveResponse(url_loader_, response);
101 EXPECT_CALL(*this, ReadyCallback(MediaInfoLoader::kFailed));
102 loader_->didFail(url_loader_, WebURLError());
105 MOCK_METHOD1(ReadyCallback, void(MediaInfoLoader::Status));
110 scoped_ptr<MediaInfoLoader> loader_;
111 NiceMock<MockWebURLLoader>* url_loader_;
113 MockWebFrameClient client_;
115 WebLocalFrame* frame_;
117 base::MessageLoop message_loop_;
120 DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest);
123 TEST_F(MediaInfoLoaderTest, StartStop) {
124 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
129 TEST_F(MediaInfoLoaderTest, LoadFailure) {
130 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
135 TEST_F(MediaInfoLoaderTest, DataUri) {
136 Initialize(kHttpDataUrl, blink::WebMediaPlayer::CORSModeUnspecified);
138 SendResponse(0, MediaInfoLoader::kOk);
141 TEST_F(MediaInfoLoaderTest, HasSingleOriginNoRedirect) {
142 // Make sure no redirect case works as expected.
143 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
145 SendResponse(kHttpOK, MediaInfoLoader::kOk);
146 EXPECT_TRUE(loader_->HasSingleOrigin());
149 TEST_F(MediaInfoLoaderTest, HasSingleOriginSingleRedirect) {
150 // Test redirect to the same domain.
151 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
153 Redirect(kHttpRedirectToSameDomainUrl1);
154 SendResponse(kHttpOK, MediaInfoLoader::kOk);
155 EXPECT_TRUE(loader_->HasSingleOrigin());
158 TEST_F(MediaInfoLoaderTest, HasSingleOriginDoubleRedirect) {
159 // Test redirect twice to the same domain.
160 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
162 Redirect(kHttpRedirectToSameDomainUrl1);
163 Redirect(kHttpRedirectToSameDomainUrl2);
164 SendResponse(kHttpOK, MediaInfoLoader::kOk);
165 EXPECT_TRUE(loader_->HasSingleOrigin());
168 TEST_F(MediaInfoLoaderTest, HasSingleOriginDifferentDomain) {
169 // Test redirect to a different domain.
170 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
172 Redirect(kHttpRedirectToDifferentDomainUrl1);
173 SendResponse(kHttpOK, MediaInfoLoader::kOk);
174 EXPECT_FALSE(loader_->HasSingleOrigin());
177 TEST_F(MediaInfoLoaderTest, HasSingleOriginMultipleDomains) {
178 // Test redirect to the same domain and then to a different domain.
179 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
181 Redirect(kHttpRedirectToSameDomainUrl1);
182 Redirect(kHttpRedirectToDifferentDomainUrl1);
183 SendResponse(kHttpOK, MediaInfoLoader::kOk);
184 EXPECT_FALSE(loader_->HasSingleOrigin());
187 TEST_F(MediaInfoLoaderTest, CORSAccessCheckPassed) {
188 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUseCredentials);
190 SendResponse(kHttpOK, MediaInfoLoader::kOk);
191 EXPECT_TRUE(loader_->DidPassCORSAccessCheck());
194 TEST_F(MediaInfoLoaderTest, CORSAccessCheckFailed) {
195 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUseCredentials);
197 SendResponse(kHttpNotFound, MediaInfoLoader::kFailed);
198 EXPECT_FALSE(loader_->DidPassCORSAccessCheck());
201 } // namespace content