- add sources.
[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/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"
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 namespace content {
26
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";
31
32 static const int kHttpOK = 200;
33 static const int kHttpNotFound = 404;
34
35 class MediaInfoLoaderTest : public testing::Test {
36  public:
37   MediaInfoLoaderTest()
38       : view_(WebView::create(NULL)) {
39     view_->initializeMainFrame(&client_);
40   }
41
42   virtual ~MediaInfoLoaderTest() {
43     view_->close();
44   }
45
46   void Initialize(
47       const char* url,
48       WebKit::WebMediaPlayer::CORSMode cors_mode) {
49     gurl_ = GURL(url);
50
51     loader_.reset(new MediaInfoLoader(
52         gurl_, cors_mode,
53         base::Bind(&MediaInfoLoaderTest::ReadyCallback,
54                    base::Unretained(this))));
55
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_);
59   }
60
61   void Start() {
62     InSequence s;
63     EXPECT_CALL(*url_loader_, loadAsynchronously(_, _));
64     loader_->Start(view_->mainFrame());
65   }
66
67   void Stop() {
68     InSequence s;
69     EXPECT_CALL(*url_loader_, cancel());
70     loader_.reset();
71   }
72
73   void Redirect(const char* url) {
74     GURL redirect_url(url);
75     WebKit::WebURLRequest new_request(redirect_url);
76     WebKit::WebURLResponse redirect_response(gurl_);
77
78     loader_->willSendRequest(url_loader_, new_request, redirect_response);
79
80     base::MessageLoop::current()->RunUntilIdle();
81   }
82
83   void SendResponse(
84       int http_status, MediaInfoLoader::Status expected_status) {
85     EXPECT_CALL(*this, ReadyCallback(expected_status));
86     EXPECT_CALL(*url_loader_, cancel());
87
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);
94   }
95
96   void FailLoad() {
97     EXPECT_CALL(*this, ReadyCallback(MediaInfoLoader::kFailed));
98     loader_->didFail(url_loader_, WebURLError());
99   }
100
101   MOCK_METHOD1(ReadyCallback, void(MediaInfoLoader::Status));
102
103  protected:
104   GURL gurl_;
105
106   scoped_ptr<MediaInfoLoader> loader_;
107   NiceMock<MockWebURLLoader>* url_loader_;
108
109   MockWebFrameClient client_;
110   WebView* view_;
111
112   base::MessageLoop message_loop_;
113
114  private:
115   DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest);
116 };
117
118 TEST_F(MediaInfoLoaderTest, StartStop) {
119   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
120   Start();
121   Stop();
122 }
123
124 TEST_F(MediaInfoLoaderTest, LoadFailure) {
125   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
126   Start();
127   FailLoad();
128 }
129
130 TEST_F(MediaInfoLoaderTest, HasSingleOriginNoRedirect) {
131   // Make sure no redirect case works as expected.
132   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
133   Start();
134   SendResponse(kHttpOK, MediaInfoLoader::kOk);
135   EXPECT_TRUE(loader_->HasSingleOrigin());
136 }
137
138 TEST_F(MediaInfoLoaderTest, HasSingleOriginSingleRedirect) {
139   // Test redirect to the same domain.
140   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
141   Start();
142   Redirect(kHttpRedirectToSameDomainUrl1);
143   SendResponse(kHttpOK, MediaInfoLoader::kOk);
144   EXPECT_TRUE(loader_->HasSingleOrigin());
145 }
146
147 TEST_F(MediaInfoLoaderTest, HasSingleOriginDoubleRedirect) {
148   // Test redirect twice to the same domain.
149   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
150   Start();
151   Redirect(kHttpRedirectToSameDomainUrl1);
152   Redirect(kHttpRedirectToSameDomainUrl2);
153   SendResponse(kHttpOK, MediaInfoLoader::kOk);
154   EXPECT_TRUE(loader_->HasSingleOrigin());
155 }
156
157 TEST_F(MediaInfoLoaderTest, HasSingleOriginDifferentDomain) {
158   // Test redirect to a different domain.
159   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
160   Start();
161   Redirect(kHttpRedirectToDifferentDomainUrl1);
162   SendResponse(kHttpOK, MediaInfoLoader::kOk);
163   EXPECT_FALSE(loader_->HasSingleOrigin());
164 }
165
166 TEST_F(MediaInfoLoaderTest, HasSingleOriginMultipleDomains) {
167   // Test redirect to the same domain and then to a different domain.
168   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified);
169   Start();
170   Redirect(kHttpRedirectToSameDomainUrl1);
171   Redirect(kHttpRedirectToDifferentDomainUrl1);
172   SendResponse(kHttpOK, MediaInfoLoader::kOk);
173   EXPECT_FALSE(loader_->HasSingleOrigin());
174 }
175
176 TEST_F(MediaInfoLoaderTest, CORSAccessCheckPassed) {
177   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials);
178   Start();
179   SendResponse(kHttpOK, MediaInfoLoader::kOk);
180   EXPECT_TRUE(loader_->DidPassCORSAccessCheck());
181 }
182
183 TEST_F(MediaInfoLoaderTest, CORSAccessCheckFailed) {
184   Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials);
185   Start();
186   SendResponse(kHttpNotFound, MediaInfoLoader::kFailed);
187   EXPECT_FALSE(loader_->DidPassCORSAccessCheck());
188 }
189
190 }  // namespace content