Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / android / media_info_loader_unittest.cc
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.
4
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"
16
17 using ::testing::_;
18 using ::testing::InSequence;
19 using ::testing::NiceMock;
20
21 using blink::WebLocalFrame;
22 using blink::WebString;
23 using blink::WebURLError;
24 using blink::WebURLResponse;
25 using blink::WebView;
26
27 namespace content {
28
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";
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)), frame_(WebLocalFrame::create(&client_)) {
42     view_->setMainFrame(frame_);
43   }
44
45   virtual ~MediaInfoLoaderTest() {
46     view_->close();
47     frame_->close();
48   }
49
50   void Initialize(
51       const char* url,
52       blink::WebMediaPlayer::CORSMode cors_mode) {
53     gurl_ = GURL(url);
54
55     loader_.reset(new MediaInfoLoader(
56         gurl_, cors_mode,
57         base::Bind(&MediaInfoLoaderTest::ReadyCallback,
58                    base::Unretained(this))));
59
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_);
63   }
64
65   void Start() {
66     InSequence s;
67     EXPECT_CALL(*url_loader_, loadAsynchronously(_, _));
68     loader_->Start(view_->mainFrame());
69   }
70
71   void Stop() {
72     InSequence s;
73     EXPECT_CALL(*url_loader_, cancel());
74     loader_.reset();
75   }
76
77   void Redirect(const char* url) {
78     GURL redirect_url(url);
79     blink::WebURLRequest new_request(redirect_url);
80     blink::WebURLResponse redirect_response(gurl_);
81
82     loader_->willSendRequest(url_loader_, new_request, redirect_response);
83
84     base::MessageLoop::current()->RunUntilIdle();
85   }
86
87   void SendResponse(
88       int http_status, MediaInfoLoader::Status expected_status) {
89     EXPECT_CALL(*this, ReadyCallback(expected_status));
90     EXPECT_CALL(*url_loader_, cancel());
91
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);
98   }
99
100   void FailLoad() {
101     EXPECT_CALL(*this, ReadyCallback(MediaInfoLoader::kFailed));
102     loader_->didFail(url_loader_, WebURLError());
103   }
104
105   MOCK_METHOD1(ReadyCallback, void(MediaInfoLoader::Status));
106
107  protected:
108   GURL gurl_;
109
110   scoped_ptr<MediaInfoLoader> loader_;
111   NiceMock<MockWebURLLoader>* url_loader_;
112
113   MockWebFrameClient client_;
114   WebView* view_;
115   WebLocalFrame* frame_;
116
117   base::MessageLoop message_loop_;
118
119  private:
120   DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest);
121 };
122
123 TEST_F(MediaInfoLoaderTest, StartStop) {
124   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
125   Start();
126   Stop();
127 }
128
129 TEST_F(MediaInfoLoaderTest, LoadFailure) {
130   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
131   Start();
132   FailLoad();
133 }
134
135 TEST_F(MediaInfoLoaderTest, DataUri) {
136   Initialize(kHttpDataUrl, blink::WebMediaPlayer::CORSModeUnspecified);
137   Start();
138   SendResponse(0, MediaInfoLoader::kOk);
139 }
140
141 TEST_F(MediaInfoLoaderTest, HasSingleOriginNoRedirect) {
142   // Make sure no redirect case works as expected.
143   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
144   Start();
145   SendResponse(kHttpOK, MediaInfoLoader::kOk);
146   EXPECT_TRUE(loader_->HasSingleOrigin());
147 }
148
149 TEST_F(MediaInfoLoaderTest, HasSingleOriginSingleRedirect) {
150   // Test redirect to the same domain.
151   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
152   Start();
153   Redirect(kHttpRedirectToSameDomainUrl1);
154   SendResponse(kHttpOK, MediaInfoLoader::kOk);
155   EXPECT_TRUE(loader_->HasSingleOrigin());
156 }
157
158 TEST_F(MediaInfoLoaderTest, HasSingleOriginDoubleRedirect) {
159   // Test redirect twice to the same domain.
160   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
161   Start();
162   Redirect(kHttpRedirectToSameDomainUrl1);
163   Redirect(kHttpRedirectToSameDomainUrl2);
164   SendResponse(kHttpOK, MediaInfoLoader::kOk);
165   EXPECT_TRUE(loader_->HasSingleOrigin());
166 }
167
168 TEST_F(MediaInfoLoaderTest, HasSingleOriginDifferentDomain) {
169   // Test redirect to a different domain.
170   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
171   Start();
172   Redirect(kHttpRedirectToDifferentDomainUrl1);
173   SendResponse(kHttpOK, MediaInfoLoader::kOk);
174   EXPECT_FALSE(loader_->HasSingleOrigin());
175 }
176
177 TEST_F(MediaInfoLoaderTest, HasSingleOriginMultipleDomains) {
178   // Test redirect to the same domain and then to a different domain.
179   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
180   Start();
181   Redirect(kHttpRedirectToSameDomainUrl1);
182   Redirect(kHttpRedirectToDifferentDomainUrl1);
183   SendResponse(kHttpOK, MediaInfoLoader::kOk);
184   EXPECT_FALSE(loader_->HasSingleOrigin());
185 }
186
187 TEST_F(MediaInfoLoaderTest, CORSAccessCheckPassed) {
188   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUseCredentials);
189   Start();
190   SendResponse(kHttpOK, MediaInfoLoader::kOk);
191   EXPECT_TRUE(loader_->DidPassCORSAccessCheck());
192 }
193
194 TEST_F(MediaInfoLoaderTest, CORSAccessCheckFailed) {
195   Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUseCredentials);
196   Start();
197   SendResponse(kHttpNotFound, MediaInfoLoader::kFailed);
198   EXPECT_FALSE(loader_->DidPassCORSAccessCheck());
199 }
200
201 }  // namespace content