Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(625)

Side by Side Diff: webkit/renderer/media/media_info_loader_unittest.cc

Issue 16327002: android: Implement single origin and CORS check for video (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address nits. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/renderer/media/media_info_loader.cc ('k') | webkit/renderer/media/webkit_media.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #include "base/bind.h"
6 #include "base/message_loop.h"
7 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h"
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
9 #include "third_party/WebKit/public/platform/WebURLError.h"
10 #include "third_party/WebKit/public/platform/WebURLRequest.h"
11 #include "third_party/WebKit/public/platform/WebURLResponse.h"
12 #include "webkit/mocks/mock_webframeclient.h"
13 #include "webkit/mocks/mock_weburlloader.h"
14 #include "webkit/renderer/media/media_info_loader.h"
15
16 using ::testing::_;
17 using ::testing::InSequence;
18 using ::testing::NiceMock;
19
20 using WebKit::WebString;
21 using WebKit::WebURLError;
22 using WebKit::WebURLResponse;
23 using WebKit::WebView;
24
25 using webkit_glue::MockWebFrameClient;
26 using webkit_glue::MockWebURLLoader;
27
28 namespace webkit_media {
29
30 static const char* kHttpUrl = "http://test";
31 static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing";
32 static const char kHttpRedirectToSameDomainUrl2[] = "http://test/ing2";
33 static const char kHttpRedirectToDifferentDomainUrl1[] = "http://test2";
34
35 static const int kHttpOK = 200;
36 static const int kHttpNotFound = 404;
37
38 class MediaInfoLoaderTest : public testing::Test {
39 public:
40 MediaInfoLoaderTest()
41 : view_(WebView::create(NULL)) {
42 view_->initializeMainFrame(&client_);
43 }
44
45 virtual ~MediaInfoLoaderTest() {
46 view_->close();
47 }
48
49 void Initialize(
50 const char* url,
51 WebKit::WebMediaPlayer::CORSMode cors_mode) {
52 gurl_ = GURL(url);
53
54 loader_.reset(new MediaInfoLoader(
55 gurl_, cors_mode,
56 base::Bind(&MediaInfoLoaderTest::ReadyCallback,
57 base::Unretained(this))));
58
59 // |test_loader_| will be used when Start() is called.
60 url_loader_ = new NiceMock<MockWebURLLoader>();
61 loader_->test_loader_ = scoped_ptr<WebKit::WebURLLoader>(url_loader_);
62 }
63
64 void Start() {
65 InSequence s;
66 EXPECT_CALL(*url_loader_, loadAsynchronously(_, _));
67 loader_->Start(view_->mainFrame());
68 }
69
70 void Stop() {
71 InSequence s;
72 EXPECT_CALL(*url_loader_, cancel());
73 loader_.reset();
74 }
75
76 void Redirect(const char* url) {
77 GURL redirect_url(url);
78 WebKit::WebURLRequest new_request(redirect_url);
79 WebKit::WebURLResponse redirect_response(gurl_);
80
81 loader_->willSendRequest(url_loader_, new_request, redirect_response);
82
83 base::MessageLoop::current()->RunUntilIdle();
84 }
85
86 void SendResponse(
87 int http_status, MediaInfoLoader::Status expected_status) {
88 EXPECT_CALL(*this, ReadyCallback(expected_status));
89
90 WebURLResponse response(gurl_);
91 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
92 WebString::fromUTF8("0"));
93 response.setExpectedContentLength(0);
94 response.setHTTPStatusCode(http_status);
95 loader_->didReceiveResponse(url_loader_, response);
96 }
97
98 void FailLoad() {
99 EXPECT_CALL(*this, ReadyCallback(MediaInfoLoader::kFailed));
100 loader_->didFail(url_loader_, WebURLError());
101 }
102
103 MOCK_METHOD1(ReadyCallback, void(MediaInfoLoader::Status));
104
105 protected:
106 GURL gurl_;
107
108 scoped_ptr<MediaInfoLoader> loader_;
109 NiceMock<MockWebURLLoader>* url_loader_;
110
111 MockWebFrameClient client_;
112 WebView* view_;
113
114 base::MessageLoop message_loop_;
115
116 private:
117 DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest);
118 };
119
120 TEST_F(MediaInfoLoaderTest, StartStop) {
121 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
122 Start();
123 Stop();
124 }
125
126 TEST_F(MediaInfoLoaderTest, LoadFailure) {
127 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
128 Start();
129 FailLoad();
130 Stop();
131 }
132
133 TEST_F(MediaInfoLoaderTest, HasSingleOriginNoRedirect) {
134 // Make sure no redirect case works as expected.
135 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
136 Start();
137 SendResponse(kHttpOK, MediaInfoLoader::kOk);
138 EXPECT_TRUE(loader_->HasSingleOrigin());
139 Stop();
140 }
141
142 TEST_F(MediaInfoLoaderTest, HasSingleOriginSingleRedirect) {
143 // Test redirect to the same domain.
144 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
145 Start();
146 Redirect(kHttpRedirectToSameDomainUrl1);
147 SendResponse(kHttpOK, MediaInfoLoader::kOk);
148 EXPECT_TRUE(loader_->HasSingleOrigin());
149 Stop();
150 }
151
152 TEST_F(MediaInfoLoaderTest, HasSingleOriginDoubleRedirect) {
153 // Test redirect twice to the same domain.
154 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
155 Start();
156 Redirect(kHttpRedirectToSameDomainUrl1);
157 Redirect(kHttpRedirectToSameDomainUrl2);
158 SendResponse(kHttpOK, MediaInfoLoader::kOk);
159 EXPECT_TRUE(loader_->HasSingleOrigin());
160 Stop();
161 }
162
163 TEST_F(MediaInfoLoaderTest, HasSingleOriginDifferentDomain) {
164 // Test redirect to a different domain.
165 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
166 Start();
167 Redirect(kHttpRedirectToDifferentDomainUrl1);
168 SendResponse(kHttpOK, MediaInfoLoader::kOk);
169 EXPECT_FALSE(loader_->HasSingleOrigin());
170 Stop();
171 }
172
173 TEST_F(MediaInfoLoaderTest, HasSingleOriginMultipleDomains) {
174 // Test redirect to the same domain and then to a different domain.
175 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
176 Start();
177 Redirect(kHttpRedirectToSameDomainUrl1);
178 Redirect(kHttpRedirectToDifferentDomainUrl1);
179 SendResponse(kHttpOK, MediaInfoLoader::kOk);
180 EXPECT_FALSE(loader_->HasSingleOrigin());
181 Stop();
182 }
183
184 TEST_F(MediaInfoLoaderTest, CORSAccessCheckPassed) {
185 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials);
186 Start();
187 SendResponse(kHttpOK, MediaInfoLoader::kOk);
188 EXPECT_TRUE(loader_->DidPassCORSAccessCheck());
189 Stop();
190 }
191
192 TEST_F(MediaInfoLoaderTest, CORSAccessCheckFailed) {
193 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials);
194 Start();
195 SendResponse(kHttpNotFound, MediaInfoLoader::kFailed);
196 EXPECT_FALSE(loader_->DidPassCORSAccessCheck());
197 Stop();
198 }
199
200 } // namespace webkit_media
OLDNEW
« no previous file with comments | « webkit/renderer/media/media_info_loader.cc ('k') | webkit/renderer/media/webkit_media.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698