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/WebURLError.h"
11 #include "third_party/WebKit/public/platform/WebURLRequest.h"
12 #include "third_party/WebKit/public/platform/WebURLResponse.h"
13 #include "third_party/WebKit/public/web/WebMediaPlayer.h"
14 #include "third_party/WebKit/public/web/WebView.h"
17 using ::testing::InSequence;
18 using ::testing::NiceMock;
20 using WebKit::WebString;
21 using WebKit::WebURLError;
22 using WebKit::WebURLResponse;
23 using WebKit::WebView;
27 static const char* kHttpUrl = "http://test";
28 static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing";
29 static const char kHttpRedirectToSameDomainUrl2[] = "http://test/ing2";
30 static const char kHttpRedirectToDifferentDomainUrl1[] = "http://test2";
32 static const int kHttpOK = 200;
33 static const int kHttpNotFound = 404;
35 class MediaInfoLoaderTest : public testing::Test {
38 : view_(WebView::create(NULL)) {
39 view_->initializeMainFrame(&client_);
42 virtual ~MediaInfoLoaderTest() {
48 WebKit::WebMediaPlayer::CORSMode cors_mode) {
51 loader_.reset(new MediaInfoLoader(
53 base::Bind(&MediaInfoLoaderTest::ReadyCallback,
54 base::Unretained(this))));
56 // |test_loader_| will be used when Start() is called.
57 url_loader_ = new NiceMock<MockWebURLLoader>();
58 loader_->test_loader_ = scoped_ptr<WebKit::WebURLLoader>(url_loader_);
63 EXPECT_CALL(*url_loader_, loadAsynchronously(_, _));
64 loader_->Start(view_->mainFrame());
69 EXPECT_CALL(*url_loader_, cancel());
73 void Redirect(const char* url) {
74 GURL redirect_url(url);
75 WebKit::WebURLRequest new_request(redirect_url);
76 WebKit::WebURLResponse redirect_response(gurl_);
78 loader_->willSendRequest(url_loader_, new_request, redirect_response);
80 base::MessageLoop::current()->RunUntilIdle();
84 int http_status, MediaInfoLoader::Status expected_status) {
85 EXPECT_CALL(*this, ReadyCallback(expected_status));
86 EXPECT_CALL(*url_loader_, cancel());
88 WebURLResponse response(gurl_);
89 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
90 WebString::fromUTF8("0"));
91 response.setExpectedContentLength(0);
92 response.setHTTPStatusCode(http_status);
93 loader_->didReceiveResponse(url_loader_, response);
97 EXPECT_CALL(*this, ReadyCallback(MediaInfoLoader::kFailed));
98 loader_->didFail(url_loader_, WebURLError());
101 MOCK_METHOD1(ReadyCallback, void(MediaInfoLoader::Status));
106 scoped_ptr<MediaInfoLoader> loader_;
107 NiceMock<MockWebURLLoader>* url_loader_;
109 MockWebFrameClient client_;
112 base::MessageLoop message_loop_;
115 DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest);
118 TEST_F(MediaInfoLoaderTest, StartStop) {
119 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
124 TEST_F(MediaInfoLoaderTest, LoadFailure) {
125 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
130 TEST_F(MediaInfoLoaderTest, HasSingleOriginNoRedirect) {
131 // Make sure no redirect case works as expected.
132 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
134 SendResponse(kHttpOK, MediaInfoLoader::kOk);
135 EXPECT_TRUE(loader_->HasSingleOrigin());
138 TEST_F(MediaInfoLoaderTest, HasSingleOriginSingleRedirect) {
139 // Test redirect to the same domain.
140 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
142 Redirect(kHttpRedirectToSameDomainUrl1);
143 SendResponse(kHttpOK, MediaInfoLoader::kOk);
144 EXPECT_TRUE(loader_->HasSingleOrigin());
147 TEST_F(MediaInfoLoaderTest, HasSingleOriginDoubleRedirect) {
148 // Test redirect twice to the same domain.
149 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
151 Redirect(kHttpRedirectToSameDomainUrl1);
152 Redirect(kHttpRedirectToSameDomainUrl2);
153 SendResponse(kHttpOK, MediaInfoLoader::kOk);
154 EXPECT_TRUE(loader_->HasSingleOrigin());
157 TEST_F(MediaInfoLoaderTest, HasSingleOriginDifferentDomain) {
158 // Test redirect to a different domain.
159 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
161 Redirect(kHttpRedirectToDifferentDomainUrl1);
162 SendResponse(kHttpOK, MediaInfoLoader::kOk);
163 EXPECT_FALSE(loader_->HasSingleOrigin());
166 TEST_F(MediaInfoLoaderTest, HasSingleOriginMultipleDomains) {
167 // Test redirect to the same domain and then to a different domain.
168 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
170 Redirect(kHttpRedirectToSameDomainUrl1);
171 Redirect(kHttpRedirectToDifferentDomainUrl1);
172 SendResponse(kHttpOK, MediaInfoLoader::kOk);
173 EXPECT_FALSE(loader_->HasSingleOrigin());
176 TEST_F(MediaInfoLoaderTest, CORSAccessCheckPassed) {
177 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials);
179 SendResponse(kHttpOK, MediaInfoLoader::kOk);
180 EXPECT_TRUE(loader_->DidPassCORSAccessCheck());
183 TEST_F(MediaInfoLoaderTest, CORSAccessCheckFailed) {
184 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials);
186 SendResponse(kHttpNotFound, MediaInfoLoader::kFailed);
187 EXPECT_FALSE(loader_->DidPassCORSAccessCheck());
190 } // namespace content