Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/android/media_info_loader_android.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 MediaInfoLoaderAndroidTest : public testing::Test { | |
| 39 public: | |
| 40 MediaInfoLoaderAndroidTest() | |
| 41 : view_(WebView::create(NULL)) { | |
| 42 view_->initializeMainFrame(&client_); | |
| 43 } | |
| 44 | |
| 45 virtual ~MediaInfoLoaderAndroidTest() { | |
| 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 MediaInfoLoaderAndroid( | |
| 55 gurl_, cors_mode, | |
| 56 base::Bind(&MediaInfoLoaderAndroidTest::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 redirectUrl(url); | |
|
acolwell GONE FROM CHROMIUM
2013/06/10 19:17:32
nit: s/redirectUrl/redirect_url/
Sami
2013/06/11 13:51:25
Done.
| |
| 78 WebKit::WebURLRequest newRequest(redirectUrl); | |
|
acolwell GONE FROM CHROMIUM
2013/06/10 19:17:32
nit: s/newRequest/new_request/
Sami
2013/06/11 13:51:25
Done.
| |
| 79 WebKit::WebURLResponse redirectResponse(gurl_); | |
|
acolwell GONE FROM CHROMIUM
2013/06/10 19:17:32
nit: s/redirectResponse/redirect_response/
Sami
2013/06/11 13:51:25
Done.
| |
| 80 | |
| 81 loader_->willSendRequest(url_loader_, newRequest, redirectResponse); | |
| 82 | |
| 83 base::MessageLoop::current()->RunUntilIdle(); | |
| 84 } | |
| 85 | |
| 86 void SendResponse( | |
| 87 int http_status, MediaInfoLoaderAndroid::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(MediaInfoLoaderAndroid::kFailed)); | |
| 100 loader_->didFail(url_loader_, WebURLError()); | |
| 101 } | |
| 102 | |
| 103 MOCK_METHOD1(ReadyCallback, void(MediaInfoLoaderAndroid::Status)); | |
| 104 | |
| 105 protected: | |
| 106 GURL gurl_; | |
| 107 | |
| 108 scoped_ptr<MediaInfoLoaderAndroid> 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(MediaInfoLoaderAndroidTest); | |
| 118 }; | |
| 119 | |
| 120 TEST_F(MediaInfoLoaderAndroidTest, StartStop) { | |
| 121 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified); | |
| 122 Start(); | |
| 123 Stop(); | |
| 124 } | |
| 125 | |
| 126 TEST_F(MediaInfoLoaderAndroidTest, LoadFailure) { | |
| 127 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified); | |
| 128 Start(); | |
| 129 FailLoad(); | |
| 130 Stop(); | |
| 131 } | |
| 132 | |
| 133 TEST_F(MediaInfoLoaderAndroidTest, HasSingleOrigin) { | |
| 134 // Make sure no redirect case works as expected. | |
| 135 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified); | |
| 136 Start(); | |
| 137 SendResponse(kHttpOK, MediaInfoLoaderAndroid::kOk); | |
| 138 EXPECT_TRUE(loader_->HasSingleOrigin()); | |
| 139 Stop(); | |
| 140 | |
| 141 // Test redirect to the same domain. | |
| 142 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified); | |
|
acolwell GONE FROM CHROMIUM
2013/06/10 19:17:32
nit: split these out into seperate tests so that t
Sami
2013/06/11 13:51:25
Good idea, done.
| |
| 143 Start(); | |
| 144 Redirect(kHttpRedirectToSameDomainUrl1); | |
| 145 SendResponse(kHttpOK, MediaInfoLoaderAndroid::kOk); | |
| 146 EXPECT_TRUE(loader_->HasSingleOrigin()); | |
| 147 Stop(); | |
| 148 | |
| 149 // Test redirect twice to the same domain. | |
| 150 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified); | |
| 151 Start(); | |
| 152 Redirect(kHttpRedirectToSameDomainUrl1); | |
| 153 Redirect(kHttpRedirectToSameDomainUrl2); | |
| 154 SendResponse(kHttpOK, MediaInfoLoaderAndroid::kOk); | |
| 155 EXPECT_TRUE(loader_->HasSingleOrigin()); | |
| 156 Stop(); | |
| 157 | |
| 158 // Test redirect to a different domain. | |
| 159 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified); | |
| 160 Start(); | |
| 161 Redirect(kHttpRedirectToDifferentDomainUrl1); | |
| 162 SendResponse(kHttpOK, MediaInfoLoaderAndroid::kOk); | |
| 163 EXPECT_FALSE(loader_->HasSingleOrigin()); | |
| 164 Stop(); | |
| 165 | |
| 166 // Test redirect to the same domain and then to a different domain. | |
| 167 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUnspecified); | |
| 168 Start(); | |
| 169 Redirect(kHttpRedirectToSameDomainUrl1); | |
| 170 Redirect(kHttpRedirectToDifferentDomainUrl1); | |
| 171 SendResponse(kHttpOK, MediaInfoLoaderAndroid::kOk); | |
| 172 EXPECT_FALSE(loader_->HasSingleOrigin()); | |
| 173 Stop(); | |
| 174 } | |
| 175 | |
| 176 TEST_F(MediaInfoLoaderAndroidTest, CORSAccessCheckPassed) { | |
| 177 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials); | |
| 178 Start(); | |
| 179 SendResponse(kHttpOK, MediaInfoLoaderAndroid::kOk); | |
| 180 EXPECT_TRUE(loader_->DidPassCORSAccessCheck()); | |
| 181 Stop(); | |
| 182 } | |
| 183 | |
| 184 TEST_F(MediaInfoLoaderAndroidTest, CORSAccessCheckFailed) { | |
| 185 Initialize(kHttpUrl, WebKit::WebMediaPlayer::CORSModeUseCredentials); | |
| 186 Start(); | |
| 187 SendResponse(kHttpNotFound, MediaInfoLoaderAndroid::kFailed); | |
| 188 EXPECT_FALSE(loader_->DidPassCORSAccessCheck()); | |
| 189 Stop(); | |
| 190 } | |
| 191 | |
| 192 } // namespace webkit_media | |
| OLD | NEW |