Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) | 5 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) |
| 6 // rather than as part of test_shell_tests because they rely on being able | 6 // rather than as part of test_shell_tests because they rely on being able |
| 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses | 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses |
| 8 // TYPE_UI, which URLRequest doesn't allow. | 8 // TYPE_UI, which URLRequest doesn't allow. |
| 9 // | 9 // |
| 10 | 10 |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/message_loop_proxy.h" | 16 #include "base/message_loop_proxy.h" |
| 17 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
| 18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 19 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 20 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
| 21 #include "net/url_request/url_request_job.h" | 21 #include "net/url_request/url_request_job.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "webkit/blob/blob_data.h" | 23 #include "webkit/blob/blob_data.h" |
| 24 #include "webkit/blob/blob_storage_controller.h" | 24 #include "webkit/blob/blob_storage_controller.h" |
| 25 #include "webkit/blob/blob_url_request_job.h" | 25 #include "webkit/blob/blob_url_request_job.h" |
| 26 #include "webkit/fileapi/file_system_callback_dispatcher.h" | |
| 27 #include "webkit/fileapi/file_system_context.h" | 26 #include "webkit/fileapi/file_system_context.h" |
| 28 #include "webkit/fileapi/file_system_file_util.h" | 27 #include "webkit/fileapi/file_system_file_util.h" |
| 29 #include "webkit/fileapi/file_system_operation.h" | 28 #include "webkit/fileapi/file_system_operation.h" |
| 30 #include "webkit/fileapi/file_system_test_helper.h" | 29 #include "webkit/fileapi/file_system_test_helper.h" |
| 31 #include "webkit/fileapi/file_system_util.h" | 30 #include "webkit/fileapi/file_system_util.h" |
| 32 #include "webkit/fileapi/local_file_util.h" | 31 #include "webkit/fileapi/local_file_util.h" |
| 33 #include "webkit/fileapi/quota_file_util.h" | 32 #include "webkit/fileapi/quota_file_util.h" |
| 34 #include "webkit/quota/quota_manager.h" | 33 #include "webkit/quota/quota_manager.h" |
| 35 | 34 |
| 36 using quota::QuotaManager; | 35 using quota::QuotaManager; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 bool complete() const { return complete_; } | 92 bool complete() const { return complete_; } |
| 94 | 93 |
| 95 virtual void SetUp(); | 94 virtual void SetUp(); |
| 96 virtual void TearDown(); | 95 virtual void TearDown(); |
| 97 | 96 |
| 98 protected: | 97 protected: |
| 99 GURL URLForPath(const FilePath& path) const { | 98 GURL URLForPath(const FilePath& path) const { |
| 100 return test_helper_.GetURLForPath(path); | 99 return test_helper_.GetURLForPath(path); |
| 101 } | 100 } |
| 102 | 101 |
| 102 // Callback function for recording test results. | |
| 103 FileSystemOperationInterface::WriteCallback RecordWrite() { | |
|
kinuko
2012/02/10 05:34:09
ditto. (I slightly prefer RecordwriteCallback())
kinaba
2012/02/10 08:22:58
Done.
| |
| 104 return base::Bind(&FileSystemOperationWriteTest::DidWrite, | |
| 105 base::Unretained(this)); | |
| 106 } | |
| 107 | |
| 108 void DidWrite(base::PlatformFileError status, int64 bytes, bool complete) { | |
| 109 if (status == base::PLATFORM_FILE_OK) { | |
| 110 add_bytes_written(bytes, complete); | |
| 111 if (complete) | |
| 112 MessageLoop::current()->Quit(); | |
| 113 } else { | |
| 114 set_failure_status(status); | |
| 115 MessageLoop::current()->Quit(); | |
| 116 } | |
| 117 } | |
| 118 | |
| 103 scoped_ptr<LocalFileUtil> local_file_util_; | 119 scoped_ptr<LocalFileUtil> local_file_util_; |
| 104 scoped_refptr<MockQuotaManager> quota_manager_; | 120 scoped_refptr<MockQuotaManager> quota_manager_; |
| 105 FileSystemTestOriginHelper test_helper_; | 121 FileSystemTestOriginHelper test_helper_; |
| 106 | 122 |
| 107 MessageLoop loop_; | 123 MessageLoop loop_; |
| 108 | 124 |
| 109 ScopedTempDir dir_; | 125 ScopedTempDir dir_; |
| 110 FilePath filesystem_dir_; | 126 FilePath filesystem_dir_; |
| 111 FilePath file_; | 127 FilePath file_; |
| 112 FilePath virtual_path_; | 128 FilePath virtual_path_; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 140 const std::string& scheme) { | 156 const std::string& scheme) { |
| 141 webkit_blob::BlobStorageController* blob_storage_controller = | 157 webkit_blob::BlobStorageController* blob_storage_controller = |
| 142 static_cast<const TestURLRequestContext*>(request->context())-> | 158 static_cast<const TestURLRequestContext*>(request->context())-> |
| 143 blob_storage_controller(); | 159 blob_storage_controller(); |
| 144 return new webkit_blob::BlobURLRequestJob( | 160 return new webkit_blob::BlobURLRequestJob( |
| 145 request, | 161 request, |
| 146 blob_storage_controller->GetBlobDataFromUrl(request->url()), | 162 blob_storage_controller->GetBlobDataFromUrl(request->url()), |
| 147 base::MessageLoopProxy::current()); | 163 base::MessageLoopProxy::current()); |
| 148 } | 164 } |
| 149 | 165 |
| 150 class MockDispatcher : public FileSystemCallbackDispatcher { | |
| 151 public: | |
| 152 MockDispatcher(FileSystemOperationWriteTest* test) : test_(test) { } | |
| 153 | |
| 154 virtual void DidFail(base::PlatformFileError status) { | |
| 155 test_->set_failure_status(status); | |
| 156 MessageLoop::current()->Quit(); | |
| 157 } | |
| 158 | |
| 159 virtual void DidSucceed() { | |
| 160 ADD_FAILURE(); | |
| 161 } | |
| 162 | |
| 163 virtual void DidReadMetadata( | |
| 164 const base::PlatformFileInfo& info, | |
| 165 const FilePath& platform_path) { | |
| 166 ADD_FAILURE(); | |
| 167 } | |
| 168 | |
| 169 virtual void DidReadDirectory( | |
| 170 const std::vector<base::FileUtilProxy::Entry>& entries, | |
| 171 bool /* has_more */) { | |
| 172 ADD_FAILURE(); | |
| 173 } | |
| 174 | |
| 175 virtual void DidOpenFileSystem(const std::string&, const GURL&) { | |
| 176 ADD_FAILURE(); | |
| 177 } | |
| 178 | |
| 179 virtual void DidWrite(int64 bytes, bool complete) { | |
| 180 test_->add_bytes_written(bytes, complete); | |
| 181 if (complete) | |
| 182 MessageLoop::current()->Quit(); | |
| 183 } | |
| 184 | |
| 185 private: | |
| 186 FileSystemOperationWriteTest* test_; | |
| 187 }; | |
| 188 | |
| 189 } // namespace (anonymous) | 166 } // namespace (anonymous) |
| 190 | 167 |
| 191 void FileSystemOperationWriteTest::SetUp() { | 168 void FileSystemOperationWriteTest::SetUp() { |
| 192 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 169 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 193 FilePath base_dir = dir_.path().AppendASCII("filesystem"); | 170 FilePath base_dir = dir_.path().AppendASCII("filesystem"); |
| 194 | 171 |
| 195 quota_manager_ = new MockQuotaManager(base_dir, 1024); | 172 quota_manager_ = new MockQuotaManager(base_dir, 1024); |
| 196 test_helper_.SetUp(base_dir, | 173 test_helper_.SetUp(base_dir, |
| 197 false /* unlimited quota */, | 174 false /* unlimited quota */, |
| 198 quota_manager_->proxy(), | 175 quota_manager_->proxy(), |
| 199 local_file_util_.get()); | 176 local_file_util_.get()); |
| 200 filesystem_dir_ = test_helper_.GetOriginRootPath(); | 177 filesystem_dir_ = test_helper_.GetOriginRootPath(); |
| 201 | 178 |
| 202 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(filesystem_dir_, &file_)); | 179 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(filesystem_dir_, &file_)); |
| 203 virtual_path_ = file_.BaseName(); | 180 virtual_path_ = file_.BaseName(); |
| 204 | 181 |
| 205 net::URLRequest::Deprecated::RegisterProtocolFactory( | 182 net::URLRequest::Deprecated::RegisterProtocolFactory( |
| 206 "blob", &BlobURLRequestJobFactory); | 183 "blob", &BlobURLRequestJobFactory); |
| 207 } | 184 } |
| 208 | 185 |
| 209 void FileSystemOperationWriteTest::TearDown() { | 186 void FileSystemOperationWriteTest::TearDown() { |
| 210 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); | 187 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); |
| 211 quota_manager_ = NULL; | 188 quota_manager_ = NULL; |
| 212 test_helper_.TearDown(); | 189 test_helper_.TearDown(); |
| 213 } | 190 } |
| 214 | 191 |
| 215 FileSystemOperation* FileSystemOperationWriteTest::operation() { | 192 FileSystemOperation* FileSystemOperationWriteTest::operation() { |
| 216 return test_helper_.NewOperation(new MockDispatcher(this)); | 193 return test_helper_.NewOperation(); |
| 217 } | 194 } |
| 218 | 195 |
| 219 TEST_F(FileSystemOperationWriteTest, TestWriteSuccess) { | 196 TEST_F(FileSystemOperationWriteTest, TestWriteSuccess) { |
| 220 GURL blob_url("blob:success"); | 197 GURL blob_url("blob:success"); |
| 221 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 198 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 222 blob_data->AppendData("Hello, world!\n"); | 199 blob_data->AppendData("Hello, world!\n"); |
| 223 | 200 |
| 224 scoped_refptr<TestURLRequestContext> url_request_context( | 201 scoped_refptr<TestURLRequestContext> url_request_context( |
| 225 new TestURLRequestContext()); | 202 new TestURLRequestContext()); |
| 226 url_request_context->blob_storage_controller()->AddFinishedBlob( | 203 url_request_context->blob_storage_controller()->AddFinishedBlob( |
| 227 blob_url, blob_data); | 204 blob_url, blob_data); |
| 228 | 205 |
| 229 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, | 206 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, |
| 230 0); | 207 0, RecordWrite()); |
| 231 MessageLoop::current()->Run(); | 208 MessageLoop::current()->Run(); |
| 232 | 209 |
| 233 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 210 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); |
| 234 | 211 |
| 235 EXPECT_EQ(14, bytes_written()); | 212 EXPECT_EQ(14, bytes_written()); |
| 236 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 213 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| 237 EXPECT_TRUE(complete()); | 214 EXPECT_TRUE(complete()); |
| 238 } | 215 } |
| 239 | 216 |
| 240 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { | 217 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { |
| 241 GURL blob_url("blob:zero"); | 218 GURL blob_url("blob:zero"); |
| 242 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 219 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 243 blob_data->AppendData(""); | 220 blob_data->AppendData(""); |
| 244 | 221 |
| 245 scoped_refptr<TestURLRequestContext> url_request_context( | 222 scoped_refptr<TestURLRequestContext> url_request_context( |
| 246 new TestURLRequestContext()); | 223 new TestURLRequestContext()); |
| 247 url_request_context->blob_storage_controller()->AddFinishedBlob( | 224 url_request_context->blob_storage_controller()->AddFinishedBlob( |
| 248 blob_url, blob_data); | 225 blob_url, blob_data); |
| 249 | 226 |
| 250 operation()->Write(url_request_context, URLForPath(virtual_path_), | 227 operation()->Write(url_request_context, URLForPath(virtual_path_), |
| 251 blob_url, 0); | 228 blob_url, 0, RecordWrite()); |
| 252 MessageLoop::current()->Run(); | 229 MessageLoop::current()->Run(); |
| 253 | 230 |
| 254 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 231 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); |
| 255 | 232 |
| 256 EXPECT_EQ(0, bytes_written()); | 233 EXPECT_EQ(0, bytes_written()); |
| 257 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 234 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| 258 EXPECT_TRUE(complete()); | 235 EXPECT_TRUE(complete()); |
| 259 } | 236 } |
| 260 | 237 |
| 261 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidBlobUrl) { | 238 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidBlobUrl) { |
| 262 scoped_refptr<TestURLRequestContext> url_request_context( | 239 scoped_refptr<TestURLRequestContext> url_request_context( |
| 263 new TestURLRequestContext()); | 240 new TestURLRequestContext()); |
| 264 | 241 |
| 265 operation()->Write(url_request_context, URLForPath(virtual_path_), | 242 operation()->Write(url_request_context, URLForPath(virtual_path_), |
| 266 GURL("blob:invalid"), 0); | 243 GURL("blob:invalid"), 0, RecordWrite()); |
| 267 MessageLoop::current()->Run(); | 244 MessageLoop::current()->Run(); |
| 268 | 245 |
| 269 EXPECT_EQ(0, bytes_written()); | 246 EXPECT_EQ(0, bytes_written()); |
| 270 EXPECT_EQ(base::PLATFORM_FILE_ERROR_FAILED, status()); | 247 EXPECT_EQ(base::PLATFORM_FILE_ERROR_FAILED, status()); |
| 271 EXPECT_TRUE(complete()); | 248 EXPECT_TRUE(complete()); |
| 272 } | 249 } |
| 273 | 250 |
| 274 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { | 251 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { |
| 275 GURL blob_url("blob:writeinvalidfile"); | 252 GURL blob_url("blob:writeinvalidfile"); |
| 276 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 253 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 277 blob_data->AppendData("It\'ll not be written."); | 254 blob_data->AppendData("It\'ll not be written."); |
| 278 | 255 |
| 279 scoped_refptr<TestURLRequestContext> url_request_context( | 256 scoped_refptr<TestURLRequestContext> url_request_context( |
| 280 new TestURLRequestContext()); | 257 new TestURLRequestContext()); |
| 281 url_request_context->blob_storage_controller()->AddFinishedBlob( | 258 url_request_context->blob_storage_controller()->AddFinishedBlob( |
| 282 blob_url, blob_data); | 259 blob_url, blob_data); |
| 283 | 260 |
| 284 operation()->Write(url_request_context, | 261 operation()->Write(url_request_context, |
| 285 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), | 262 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), |
| 286 blob_url, 0); | 263 blob_url, 0, RecordWrite()); |
| 287 MessageLoop::current()->Run(); | 264 MessageLoop::current()->Run(); |
| 288 | 265 |
| 289 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 266 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); |
| 290 | 267 |
| 291 EXPECT_EQ(0, bytes_written()); | 268 EXPECT_EQ(0, bytes_written()); |
| 292 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); | 269 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); |
| 293 EXPECT_TRUE(complete()); | 270 EXPECT_TRUE(complete()); |
| 294 } | 271 } |
| 295 | 272 |
| 296 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { | 273 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { |
| 297 FilePath subdir; | 274 FilePath subdir; |
| 298 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, | 275 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, |
| 299 FILE_PATH_LITERAL("d"), | 276 FILE_PATH_LITERAL("d"), |
| 300 &subdir)); | 277 &subdir)); |
| 301 FilePath virtual_subdir_path = subdir.BaseName(); | 278 FilePath virtual_subdir_path = subdir.BaseName(); |
| 302 | 279 |
| 303 GURL blob_url("blob:writedir"); | 280 GURL blob_url("blob:writedir"); |
| 304 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 281 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 305 blob_data->AppendData("It\'ll not be written, too."); | 282 blob_data->AppendData("It\'ll not be written, too."); |
| 306 | 283 |
| 307 scoped_refptr<TestURLRequestContext> url_request_context( | 284 scoped_refptr<TestURLRequestContext> url_request_context( |
| 308 new TestURLRequestContext()); | 285 new TestURLRequestContext()); |
| 309 url_request_context->blob_storage_controller()->AddFinishedBlob( | 286 url_request_context->blob_storage_controller()->AddFinishedBlob( |
| 310 blob_url, blob_data); | 287 blob_url, blob_data); |
| 311 | 288 |
| 312 operation()->Write(url_request_context, URLForPath(virtual_subdir_path), | 289 operation()->Write(url_request_context, URLForPath(virtual_subdir_path), |
| 313 blob_url, 0); | 290 blob_url, 0, RecordWrite()); |
| 314 MessageLoop::current()->Run(); | 291 MessageLoop::current()->Run(); |
| 315 | 292 |
| 316 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 293 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); |
| 317 | 294 |
| 318 EXPECT_EQ(0, bytes_written()); | 295 EXPECT_EQ(0, bytes_written()); |
| 319 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); | 296 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); |
| 320 EXPECT_TRUE(complete()); | 297 EXPECT_TRUE(complete()); |
| 321 } | 298 } |
| 322 | 299 |
| 323 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { | 300 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { |
| 324 GURL blob_url("blob:success"); | 301 GURL blob_url("blob:success"); |
| 325 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 302 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 326 blob_data->AppendData("Hello, world!\n"); | 303 blob_data->AppendData("Hello, world!\n"); |
| 327 | 304 |
| 328 scoped_refptr<TestURLRequestContext> url_request_context( | 305 scoped_refptr<TestURLRequestContext> url_request_context( |
| 329 new TestURLRequestContext()); | 306 new TestURLRequestContext()); |
| 330 url_request_context->blob_storage_controller()->AddFinishedBlob( | 307 url_request_context->blob_storage_controller()->AddFinishedBlob( |
| 331 blob_url, blob_data); | 308 blob_url, blob_data); |
| 332 | 309 |
| 333 quota_manager_->set_quota(10); | 310 quota_manager_->set_quota(10); |
| 334 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, | 311 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, |
| 335 0); | 312 0, RecordWrite()); |
| 336 MessageLoop::current()->Run(); | 313 MessageLoop::current()->Run(); |
| 337 | 314 |
| 338 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 315 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); |
| 339 | 316 |
| 340 EXPECT_EQ(10, bytes_written()); | 317 EXPECT_EQ(10, bytes_written()); |
| 341 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); | 318 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); |
| 342 EXPECT_TRUE(complete()); | 319 EXPECT_TRUE(complete()); |
| 343 } | 320 } |
| 344 | 321 |
| 345 // TODO(ericu,dmikurube): Add tests for Cancel. | 322 // TODO(ericu,dmikurube): Add tests for Cancel. |
| 346 | 323 |
| 347 } // namespace fileapi | 324 } // namespace fileapi |
| OLD | NEW |