| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "media/base/filters.h" | |
| 7 #include "media/base/mock_callback.h" | |
| 8 #include "media/base/mock_filter_host.h" | |
| 9 #include "media/base/mock_filters.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 16 #include "webkit/glue/media/simple_data_source.h" | |
| 17 #include "webkit/mocks/mock_webframeclient.h" | |
| 18 #include "webkit/mocks/mock_weburlloader.h" | |
| 19 | |
| 20 using ::testing::_; | |
| 21 using ::testing::DoAll; | |
| 22 using ::testing::InSequence; | |
| 23 using ::testing::Invoke; | |
| 24 using ::testing::NiceMock; | |
| 25 using ::testing::NotNull; | |
| 26 using ::testing::Return; | |
| 27 using ::testing::SetArgumentPointee; | |
| 28 using ::testing::StrictMock; | |
| 29 using ::testing::WithArgs; | |
| 30 | |
| 31 using WebKit::WebURLError; | |
| 32 using WebKit::WebURLLoader; | |
| 33 using WebKit::WebURLRequest; | |
| 34 using WebKit::WebURLResponse; | |
| 35 using WebKit::WebView; | |
| 36 | |
| 37 namespace webkit_glue { | |
| 38 | |
| 39 static const int kDataSize = 1024; | |
| 40 static const char kHttpUrl[] = "http://test"; | |
| 41 static const char kHttpsUrl[] = "https://test"; | |
| 42 static const char kFileUrl[] = "file://test"; | |
| 43 static const char kDataUrl[] = | |
| 44 "data:text/plain;base64,YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK"; | |
| 45 static const char kDataUrlDecoded[] = "abcdefghijklmnopqrstuvwxyz"; | |
| 46 static const char kInvalidUrl[] = "whatever://test"; | |
| 47 static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing"; | |
| 48 static const char kHttpRedirectToSameDomainUrl2[] = "http://test/ing2"; | |
| 49 static const char kHttpRedirectToDifferentDomainUrl1[] = "http://test2"; | |
| 50 static const char kHttpRedirectToDifferentDomainUrl2[] = "http://test2/ing"; | |
| 51 | |
| 52 class SimpleDataSourceTest : public testing::Test { | |
| 53 public: | |
| 54 SimpleDataSourceTest() | |
| 55 : view_(WebView::create(NULL)) { | |
| 56 view_->initializeMainFrame(&client_); | |
| 57 | |
| 58 for (int i = 0; i < kDataSize; ++i) { | |
| 59 data_[i] = i; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 virtual ~SimpleDataSourceTest() { | |
| 64 view_->close(); | |
| 65 } | |
| 66 | |
| 67 void InitializeDataSource(const char* url, | |
| 68 const media::PipelineStatusCB& callback) { | |
| 69 gurl_ = GURL(url); | |
| 70 | |
| 71 url_loader_ = new NiceMock<MockWebURLLoader>(); | |
| 72 | |
| 73 data_source_ = new SimpleDataSource(MessageLoop::current(), | |
| 74 view_->mainFrame()); | |
| 75 | |
| 76 // There is no need to provide a message loop to data source. | |
| 77 data_source_->set_host(&host_); | |
| 78 data_source_->SetURLLoaderForTest(url_loader_); | |
| 79 | |
| 80 data_source_->Initialize(url, callback); | |
| 81 MessageLoop::current()->RunAllPending(); | |
| 82 } | |
| 83 | |
| 84 void RequestSucceeded(bool is_loaded) { | |
| 85 WebURLResponse response(gurl_); | |
| 86 response.setExpectedContentLength(kDataSize); | |
| 87 | |
| 88 data_source_->didReceiveResponse(NULL, response); | |
| 89 int64 size; | |
| 90 EXPECT_TRUE(data_source_->GetSize(&size)); | |
| 91 EXPECT_EQ(kDataSize, size); | |
| 92 | |
| 93 for (int i = 0; i < kDataSize; ++i) { | |
| 94 data_source_->didReceiveData(NULL, data_ + i, 1, 1); | |
| 95 } | |
| 96 | |
| 97 EXPECT_CALL(host_, SetLoaded(is_loaded)); | |
| 98 | |
| 99 InSequence s; | |
| 100 EXPECT_CALL(host_, SetTotalBytes(kDataSize)); | |
| 101 EXPECT_CALL(host_, SetBufferedBytes(kDataSize)); | |
| 102 | |
| 103 data_source_->didFinishLoading(NULL, 0); | |
| 104 | |
| 105 // Let the tasks to be executed. | |
| 106 MessageLoop::current()->RunAllPending(); | |
| 107 } | |
| 108 | |
| 109 void RequestFailed() { | |
| 110 InSequence s; | |
| 111 | |
| 112 WebURLError error; | |
| 113 error.reason = net::ERR_FAILED; | |
| 114 data_source_->didFail(NULL, error); | |
| 115 | |
| 116 // Let the tasks to be executed. | |
| 117 MessageLoop::current()->RunAllPending(); | |
| 118 } | |
| 119 | |
| 120 void Redirect(const char* url) { | |
| 121 GURL redirectUrl(url); | |
| 122 WebURLRequest newRequest(redirectUrl); | |
| 123 WebURLResponse redirectResponse(gurl_); | |
| 124 | |
| 125 data_source_->willSendRequest(url_loader_, newRequest, redirectResponse); | |
| 126 | |
| 127 MessageLoop::current()->RunAllPending(); | |
| 128 } | |
| 129 | |
| 130 void DestroyDataSource() { | |
| 131 data_source_->Stop(media::NewExpectedClosure()); | |
| 132 MessageLoop::current()->RunAllPending(); | |
| 133 | |
| 134 data_source_ = NULL; | |
| 135 } | |
| 136 | |
| 137 void AsyncRead() { | |
| 138 for (int i = 0; i < kDataSize; ++i) { | |
| 139 uint8 buffer[1]; | |
| 140 | |
| 141 EXPECT_CALL(*this, ReadCallback(1)); | |
| 142 data_source_->Read( | |
| 143 i, 1, buffer, | |
| 144 base::Bind(&SimpleDataSourceTest::ReadCallback, | |
| 145 base::Unretained(this))); | |
| 146 EXPECT_EQ(static_cast<uint8>(data_[i]), buffer[0]); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 MOCK_METHOD1(ReadCallback, void(size_t size)); | |
| 151 | |
| 152 protected: | |
| 153 GURL gurl_; | |
| 154 scoped_ptr<MessageLoop> message_loop_; | |
| 155 NiceMock<MockWebURLLoader>* url_loader_; | |
| 156 scoped_refptr<SimpleDataSource> data_source_; | |
| 157 StrictMock<media::MockFilterHost> host_; | |
| 158 | |
| 159 MockWebFrameClient client_; | |
| 160 WebView* view_; | |
| 161 | |
| 162 char data_[kDataSize]; | |
| 163 | |
| 164 DISALLOW_COPY_AND_ASSIGN(SimpleDataSourceTest); | |
| 165 }; | |
| 166 | |
| 167 TEST_F(SimpleDataSourceTest, InitializeHTTP) { | |
| 168 InitializeDataSource(kHttpUrl, | |
| 169 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 170 RequestSucceeded(false); | |
| 171 DestroyDataSource(); | |
| 172 } | |
| 173 | |
| 174 TEST_F(SimpleDataSourceTest, InitializeHTTPS) { | |
| 175 InitializeDataSource(kHttpsUrl, | |
| 176 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 177 RequestSucceeded(false); | |
| 178 DestroyDataSource(); | |
| 179 } | |
| 180 | |
| 181 TEST_F(SimpleDataSourceTest, InitializeFile) { | |
| 182 InitializeDataSource(kFileUrl, | |
| 183 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 184 RequestSucceeded(true); | |
| 185 DestroyDataSource(); | |
| 186 } | |
| 187 | |
| 188 TEST_F(SimpleDataSourceTest, InitializeData) { | |
| 189 url_loader_ = new NiceMock<MockWebURLLoader>(); | |
| 190 | |
| 191 data_source_ = new SimpleDataSource(MessageLoop::current(), | |
| 192 view_->mainFrame()); | |
| 193 // There is no need to provide a message loop to data source. | |
| 194 data_source_->set_host(&host_); | |
| 195 data_source_->SetURLLoaderForTest(url_loader_); | |
| 196 | |
| 197 EXPECT_CALL(host_, SetLoaded(true)); | |
| 198 EXPECT_CALL(host_, SetTotalBytes(sizeof(kDataUrlDecoded))); | |
| 199 EXPECT_CALL(host_, SetBufferedBytes(sizeof(kDataUrlDecoded))); | |
| 200 | |
| 201 data_source_->Initialize(kDataUrl, | |
| 202 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 203 MessageLoop::current()->RunAllPending(); | |
| 204 | |
| 205 DestroyDataSource(); | |
| 206 } | |
| 207 | |
| 208 TEST_F(SimpleDataSourceTest, RequestFailed) { | |
| 209 InitializeDataSource(kHttpUrl, | |
| 210 media::NewExpectedStatusCB(media::PIPELINE_ERROR_NETWORK)); | |
| 211 RequestFailed(); | |
| 212 DestroyDataSource(); | |
| 213 } | |
| 214 | |
| 215 static void OnStatusCB(bool* called, media::PipelineStatus status) { | |
| 216 *called = true; | |
| 217 } | |
| 218 | |
| 219 TEST_F(SimpleDataSourceTest, StopWhenDownloading) { | |
| 220 // The callback should be deleted, but not executed. | |
| 221 // TODO(scherkus): should this really be the behaviour? Seems strange... | |
| 222 bool was_called = false; | |
| 223 InitializeDataSource(kHttpUrl, base::Bind(&OnStatusCB, &was_called)); | |
| 224 | |
| 225 EXPECT_CALL(*url_loader_, cancel()); | |
| 226 DestroyDataSource(); | |
| 227 EXPECT_FALSE(was_called); | |
| 228 } | |
| 229 | |
| 230 TEST_F(SimpleDataSourceTest, AsyncRead) { | |
| 231 InitializeDataSource(kFileUrl, | |
| 232 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 233 RequestSucceeded(true); | |
| 234 AsyncRead(); | |
| 235 DestroyDataSource(); | |
| 236 } | |
| 237 | |
| 238 // NOTE: This test will need to be reworked a little once | |
| 239 // http://code.google.com/p/chromium/issues/detail?id=72578 | |
| 240 // is fixed. | |
| 241 TEST_F(SimpleDataSourceTest, HasSingleOrigin) { | |
| 242 // Make sure no redirect case works as expected. | |
| 243 InitializeDataSource(kHttpUrl, | |
| 244 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 245 RequestSucceeded(false); | |
| 246 EXPECT_TRUE(data_source_->HasSingleOrigin()); | |
| 247 DestroyDataSource(); | |
| 248 | |
| 249 // Test redirect to the same domain. | |
| 250 InitializeDataSource(kHttpUrl, | |
| 251 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 252 Redirect(kHttpRedirectToSameDomainUrl1); | |
| 253 RequestSucceeded(false); | |
| 254 EXPECT_TRUE(data_source_->HasSingleOrigin()); | |
| 255 DestroyDataSource(); | |
| 256 | |
| 257 // Test redirect twice to the same domain. | |
| 258 InitializeDataSource(kHttpUrl, | |
| 259 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 260 Redirect(kHttpRedirectToSameDomainUrl1); | |
| 261 Redirect(kHttpRedirectToSameDomainUrl2); | |
| 262 RequestSucceeded(false); | |
| 263 EXPECT_TRUE(data_source_->HasSingleOrigin()); | |
| 264 DestroyDataSource(); | |
| 265 | |
| 266 // Test redirect to a different domain. | |
| 267 InitializeDataSource(kHttpUrl, | |
| 268 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 269 Redirect(kHttpRedirectToDifferentDomainUrl1); | |
| 270 RequestSucceeded(false); | |
| 271 EXPECT_FALSE(data_source_->HasSingleOrigin()); | |
| 272 DestroyDataSource(); | |
| 273 | |
| 274 // Test redirect to the same domain and then to a different domain. | |
| 275 InitializeDataSource(kHttpUrl, | |
| 276 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 277 Redirect(kHttpRedirectToSameDomainUrl1); | |
| 278 Redirect(kHttpRedirectToDifferentDomainUrl1); | |
| 279 RequestSucceeded(false); | |
| 280 EXPECT_FALSE(data_source_->HasSingleOrigin()); | |
| 281 DestroyDataSource(); | |
| 282 } | |
| 283 | |
| 284 } // namespace webkit_glue | |
| OLD | NEW |