Process Alt-Svc headers.
[chromium-blink-merge.git] / content / renderer / media / android / media_info_loader_unittest.cc
blob94af96cf54b124068ef89e66de6769ba242efdf1
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.
5 #include "base/bind.h"
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"
17 using ::testing::_;
18 using ::testing::InSequence;
19 using ::testing::NiceMock;
21 using blink::WebLocalFrame;
22 using blink::WebString;
23 using blink::WebURLError;
24 using blink::WebURLResponse;
25 using blink::WebView;
27 namespace content {
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 {
39 public:
40 MediaInfoLoaderTest()
41 : view_(WebView::create(NULL)),
42 frame_(WebLocalFrame::create(blink::WebTreeScopeType::Document,
43 &client_)) {
44 view_->setMainFrame(frame_);
47 virtual ~MediaInfoLoaderTest() {
48 view_->close();
49 frame_->close();
52 void Initialize(
53 const char* url,
54 blink::WebMediaPlayer::CORSMode cors_mode) {
55 gurl_ = GURL(url);
57 loader_.reset(new MediaInfoLoader(
58 gurl_, cors_mode,
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_);
67 void Start() {
68 InSequence s;
69 EXPECT_CALL(*url_loader_, loadAsynchronously(_, _));
70 loader_->Start(view_->mainFrame());
73 void Stop() {
74 InSequence s;
75 EXPECT_CALL(*url_loader_, cancel());
76 loader_.reset();
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();
89 void SendResponse(
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);
102 void FailLoad() {
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));
111 protected:
112 GURL gurl_;
114 scoped_ptr<MediaInfoLoader> loader_;
115 NiceMock<MockWebURLLoader>* url_loader_;
117 MockWebFrameClient client_;
118 WebView* view_;
119 WebLocalFrame* frame_;
121 base::MessageLoop message_loop_;
123 private:
124 DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest);
127 TEST_F(MediaInfoLoaderTest, StartStop) {
128 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
129 Start();
130 Stop();
133 TEST_F(MediaInfoLoaderTest, LoadFailure) {
134 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
135 Start();
136 FailLoad();
139 TEST_F(MediaInfoLoaderTest, DataUri) {
140 Initialize(kHttpDataUrl, blink::WebMediaPlayer::CORSModeUnspecified);
141 Start();
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);
148 Start();
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);
156 Start();
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);
165 Start();
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);
175 Start();
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);
184 Start();
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);
193 Start();
194 SendResponse(kHttpOK, MediaInfoLoader::kOk);
195 EXPECT_TRUE(loader_->DidPassCORSAccessCheck());
198 TEST_F(MediaInfoLoaderTest, CORSAccessCheckFailed) {
199 Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUseCredentials);
200 Start();
201 SendResponse(kHttpNotFound, MediaInfoLoader::kFailed);
202 EXPECT_FALSE(loader_->DidPassCORSAccessCheck());
205 } // namespace content