| 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 #include "webkit/fileapi/local_file_system_operation.h" | 5 #include "webkit/fileapi/local_file_system_operation.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webkit/blob/shareable_file_reference.h" | 17 #include "webkit/blob/shareable_file_reference.h" |
| 18 #include "webkit/browser/fileapi/file_system_file_util.h" | 18 #include "webkit/browser/fileapi/file_system_file_util.h" |
| 19 #include "webkit/fileapi/async_file_test_helper.h" | 19 #include "webkit/fileapi/async_file_test_helper.h" |
| 20 #include "webkit/fileapi/file_system_context.h" | 20 #include "webkit/fileapi/file_system_context.h" |
| 21 #include "webkit/fileapi/file_system_operation_context.h" | 21 #include "webkit/fileapi/file_system_operation_context.h" |
| 22 #include "webkit/fileapi/file_system_util.h" | 22 #include "webkit/fileapi/file_system_util.h" |
| 23 #include "webkit/fileapi/local_file_system_test_helper.h" | |
| 24 #include "webkit/fileapi/mock_file_change_observer.h" | 23 #include "webkit/fileapi/mock_file_change_observer.h" |
| 24 #include "webkit/fileapi/sandbox_file_system_test_helper.h" |
| 25 #include "webkit/quota/mock_quota_manager.h" | 25 #include "webkit/quota/mock_quota_manager.h" |
| 26 #include "webkit/quota/quota_manager.h" | 26 #include "webkit/quota/quota_manager.h" |
| 27 | 27 |
| 28 using quota::QuotaManager; | 28 using quota::QuotaManager; |
| 29 using quota::QuotaManagerProxy; | 29 using quota::QuotaManagerProxy; |
| 30 using webkit_blob::ShareableFileReference; | 30 using webkit_blob::ShareableFileReference; |
| 31 | 31 |
| 32 namespace fileapi { | 32 namespace fileapi { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 58 | 58 |
| 59 base::FilePath base_dir = base_.path().AppendASCII("filesystem"); | 59 base::FilePath base_dir = base_.path().AppendASCII("filesystem"); |
| 60 quota_manager_ = new quota::MockQuotaManager( | 60 quota_manager_ = new quota::MockQuotaManager( |
| 61 false /* is_incognito */, base_dir, | 61 false /* is_incognito */, base_dir, |
| 62 base::MessageLoopProxy::current(), | 62 base::MessageLoopProxy::current(), |
| 63 base::MessageLoopProxy::current(), | 63 base::MessageLoopProxy::current(), |
| 64 NULL /* special storage policy */); | 64 NULL /* special storage policy */); |
| 65 quota_manager_proxy_ = new quota::MockQuotaManagerProxy( | 65 quota_manager_proxy_ = new quota::MockQuotaManagerProxy( |
| 66 quota_manager(), | 66 quota_manager(), |
| 67 base::MessageLoopProxy::current()); | 67 base::MessageLoopProxy::current()); |
| 68 test_helper_.SetUp(base_dir, quota_manager_proxy_.get()); | 68 sandbox_file_system_.SetUp(base_dir, quota_manager_proxy_.get()); |
| 69 } | 69 } |
| 70 | 70 |
| 71 virtual void TearDown() OVERRIDE { | 71 virtual void TearDown() OVERRIDE { |
| 72 // Let the client go away before dropping a ref of the quota manager proxy. | 72 // Let the client go away before dropping a ref of the quota manager proxy. |
| 73 quota_manager_proxy()->SimulateQuotaManagerDestroyed(); | 73 quota_manager_proxy()->SimulateQuotaManagerDestroyed(); |
| 74 quota_manager_ = NULL; | 74 quota_manager_ = NULL; |
| 75 quota_manager_proxy_ = NULL; | 75 quota_manager_proxy_ = NULL; |
| 76 test_helper_.TearDown(); | 76 sandbox_file_system_.TearDown(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 LocalFileSystemOperation* NewOperation() { | 79 LocalFileSystemOperation* NewOperation() { |
| 80 LocalFileSystemOperation* operation = test_helper_.NewOperation(); | 80 LocalFileSystemOperation* operation = sandbox_file_system_.NewOperation(); |
| 81 operation->operation_context()->set_change_observers(change_observers()); | 81 operation->operation_context()->set_change_observers(change_observers()); |
| 82 return operation; | 82 return operation; |
| 83 } | 83 } |
| 84 | 84 |
| 85 int status() const { return status_; } | 85 int status() const { return status_; } |
| 86 const base::PlatformFileInfo& info() const { return info_; } | 86 const base::PlatformFileInfo& info() const { return info_; } |
| 87 const base::FilePath& path() const { return path_; } | 87 const base::FilePath& path() const { return path_; } |
| 88 const std::vector<DirectoryEntry>& entries() const { | 88 const std::vector<DirectoryEntry>& entries() const { |
| 89 return entries_; | 89 return entries_; |
| 90 } | 90 } |
| 91 | 91 |
| 92 const ShareableFileReference* shareable_file_ref() const { | 92 const ShareableFileReference* shareable_file_ref() const { |
| 93 return shareable_file_ref_; | 93 return shareable_file_ref_; |
| 94 } | 94 } |
| 95 | 95 |
| 96 quota::MockQuotaManager* quota_manager() { | 96 quota::MockQuotaManager* quota_manager() { |
| 97 return static_cast<quota::MockQuotaManager*>(quota_manager_.get()); | 97 return static_cast<quota::MockQuotaManager*>(quota_manager_.get()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 quota::MockQuotaManagerProxy* quota_manager_proxy() { | 100 quota::MockQuotaManagerProxy* quota_manager_proxy() { |
| 101 return static_cast<quota::MockQuotaManagerProxy*>( | 101 return static_cast<quota::MockQuotaManagerProxy*>( |
| 102 quota_manager_proxy_.get()); | 102 quota_manager_proxy_.get()); |
| 103 } | 103 } |
| 104 | 104 |
| 105 FileSystemFileUtil* file_util() { | 105 FileSystemFileUtil* file_util() { |
| 106 return test_helper_.file_util(); | 106 return sandbox_file_system_.file_util(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 const ChangeObserverList& change_observers() const { | 109 const ChangeObserverList& change_observers() const { |
| 110 return change_observers_; | 110 return change_observers_; |
| 111 } | 111 } |
| 112 | 112 |
| 113 MockFileChangeObserver* change_observer() { | 113 MockFileChangeObserver* change_observer() { |
| 114 return &change_observer_; | 114 return &change_observer_; |
| 115 } | 115 } |
| 116 | 116 |
| 117 scoped_ptr<FileSystemOperationContext> NewContext() { | 117 scoped_ptr<FileSystemOperationContext> NewContext() { |
| 118 FileSystemOperationContext* context = test_helper_.NewOperationContext(); | 118 FileSystemOperationContext* context = |
| 119 sandbox_file_system_.NewOperationContext(); |
| 119 // Grant enough quota for all test cases. | 120 // Grant enough quota for all test cases. |
| 120 context->set_allowed_bytes_growth(1000000); | 121 context->set_allowed_bytes_growth(1000000); |
| 121 return make_scoped_ptr(context); | 122 return make_scoped_ptr(context); |
| 122 } | 123 } |
| 123 | 124 |
| 124 FileSystemURL URLForPath(const std::string& path) const { | 125 FileSystemURL URLForPath(const std::string& path) const { |
| 125 return test_helper_.CreateURLFromUTF8(path); | 126 return sandbox_file_system_.CreateURLFromUTF8(path); |
| 126 } | 127 } |
| 127 | 128 |
| 128 base::FilePath PlatformPath(const std::string& path) { | 129 base::FilePath PlatformPath(const std::string& path) { |
| 129 return test_helper_.GetLocalPath(base::FilePath::FromUTF8Unsafe(path)); | 130 return sandbox_file_system_.GetLocalPath( |
| 131 base::FilePath::FromUTF8Unsafe(path)); |
| 130 } | 132 } |
| 131 | 133 |
| 132 bool FileExists(const std::string& path) { | 134 bool FileExists(const std::string& path) { |
| 133 return AsyncFileTestHelper::FileExists( | 135 return AsyncFileTestHelper::FileExists( |
| 134 test_helper_.file_system_context(), URLForPath(path), | 136 sandbox_file_system_.file_system_context(), URLForPath(path), |
| 135 AsyncFileTestHelper::kDontCheckSize); | 137 AsyncFileTestHelper::kDontCheckSize); |
| 136 } | 138 } |
| 137 | 139 |
| 138 bool DirectoryExists(const std::string& path) { | 140 bool DirectoryExists(const std::string& path) { |
| 139 return AsyncFileTestHelper::DirectoryExists( | 141 return AsyncFileTestHelper::DirectoryExists( |
| 140 test_helper_.file_system_context(), URLForPath(path)); | 142 sandbox_file_system_.file_system_context(), URLForPath(path)); |
| 141 } | 143 } |
| 142 | 144 |
| 143 FileSystemURL CreateFile(const std::string& path) { | 145 FileSystemURL CreateFile(const std::string& path) { |
| 144 FileSystemURL url = URLForPath(path); | 146 FileSystemURL url = URLForPath(path); |
| 145 bool created = false; | 147 bool created = false; |
| 146 EXPECT_EQ(base::PLATFORM_FILE_OK, | 148 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 147 file_util()->EnsureFileExists(NewContext().get(), url, &created)); | 149 file_util()->EnsureFileExists(NewContext().get(), |
| 150 url, &created)); |
| 148 EXPECT_TRUE(created); | 151 EXPECT_TRUE(created); |
| 149 return url; | 152 return url; |
| 150 } | 153 } |
| 151 | 154 |
| 152 FileSystemURL CreateDirectory(const std::string& path) { | 155 FileSystemURL CreateDirectory(const std::string& path) { |
| 153 FileSystemURL url = URLForPath(path); | 156 FileSystemURL url = URLForPath(path); |
| 154 EXPECT_EQ(base::PLATFORM_FILE_OK, | 157 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 155 file_util()->CreateDirectory(NewContext().get(), url, | 158 file_util()->CreateDirectory(NewContext().get(), url, |
| 156 false /* exclusive */, true)); | 159 false /* exclusive */, true)); |
| 157 return url; | 160 return url; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 const base::PlatformFileInfo& info, | 212 const base::PlatformFileInfo& info, |
| 210 const base::FilePath& platform_path, | 213 const base::FilePath& platform_path, |
| 211 const scoped_refptr<ShareableFileReference>& shareable_file_ref) { | 214 const scoped_refptr<ShareableFileReference>& shareable_file_ref) { |
| 212 info_ = info; | 215 info_ = info; |
| 213 path_ = platform_path; | 216 path_ = platform_path; |
| 214 status_ = status; | 217 status_ = status; |
| 215 shareable_file_ref_ = shareable_file_ref; | 218 shareable_file_ref_ = shareable_file_ref; |
| 216 } | 219 } |
| 217 | 220 |
| 218 int64 GetDataSizeOnDisk() { | 221 int64 GetDataSizeOnDisk() { |
| 219 return test_helper_.ComputeCurrentOriginUsage() - | 222 return sandbox_file_system_.ComputeCurrentOriginUsage() - |
| 220 test_helper_.ComputeCurrentDirectoryDatabaseUsage(); | 223 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage(); |
| 221 } | 224 } |
| 222 | 225 |
| 223 void GetUsageAndQuota(int64* usage, int64* quota) { | 226 void GetUsageAndQuota(int64* usage, int64* quota) { |
| 224 quota::QuotaStatusCode status = | 227 quota::QuotaStatusCode status = |
| 225 AsyncFileTestHelper::GetUsageAndQuota( | 228 AsyncFileTestHelper::GetUsageAndQuota( |
| 226 quota_manager_, test_helper_.origin(), test_helper_.type(), | 229 quota_manager_, |
| 230 sandbox_file_system_.origin(), |
| 231 sandbox_file_system_.type(), |
| 227 usage, quota); | 232 usage, quota); |
| 228 base::MessageLoop::current()->RunUntilIdle(); | 233 base::MessageLoop::current()->RunUntilIdle(); |
| 229 ASSERT_EQ(quota::kQuotaStatusOk, status); | 234 ASSERT_EQ(quota::kQuotaStatusOk, status); |
| 230 } | 235 } |
| 231 | 236 |
| 232 int64 ComputePathCost(const FileSystemURL& url) { | 237 int64 ComputePathCost(const FileSystemURL& url) { |
| 233 int64 base_usage; | 238 int64 base_usage; |
| 234 GetUsageAndQuota(&base_usage, NULL); | 239 GetUsageAndQuota(&base_usage, NULL); |
| 235 | 240 |
| 236 AsyncFileTestHelper::CreateFile(test_helper_.file_system_context(), url); | 241 AsyncFileTestHelper::CreateFile( |
| 242 sandbox_file_system_.file_system_context(), url); |
| 237 NewOperation()->Remove(url, false /* recursive */, | 243 NewOperation()->Remove(url, false /* recursive */, |
| 238 base::Bind(&AssertFileErrorEq, FROM_HERE, | 244 base::Bind(&AssertFileErrorEq, FROM_HERE, |
| 239 base::PLATFORM_FILE_OK)); | 245 base::PLATFORM_FILE_OK)); |
| 240 base::MessageLoop::current()->RunUntilIdle(); | 246 base::MessageLoop::current()->RunUntilIdle(); |
| 241 change_observer()->ResetCount(); | 247 change_observer()->ResetCount(); |
| 242 | 248 |
| 243 int64 total_usage; | 249 int64 total_usage; |
| 244 GetUsageAndQuota(&total_usage, NULL); | 250 GetUsageAndQuota(&total_usage, NULL); |
| 245 return total_usage - base_usage; | 251 return total_usage - base_usage; |
| 246 } | 252 } |
| 247 | 253 |
| 248 void GrantQuotaForCurrentUsage() { | 254 void GrantQuotaForCurrentUsage() { |
| 249 int64 usage; | 255 int64 usage; |
| 250 GetUsageAndQuota(&usage, NULL); | 256 GetUsageAndQuota(&usage, NULL); |
| 251 quota_manager()->SetQuota(test_helper_.origin(), | 257 quota_manager()->SetQuota(sandbox_file_system_.origin(), |
| 252 test_helper_.storage_type(), | 258 sandbox_file_system_.storage_type(), |
| 253 usage); | 259 usage); |
| 254 } | 260 } |
| 255 | 261 |
| 256 int64 GetUsage() { | 262 int64 GetUsage() { |
| 257 int64 usage = 0; | 263 int64 usage = 0; |
| 258 GetUsageAndQuota(&usage, NULL); | 264 GetUsageAndQuota(&usage, NULL); |
| 259 return usage; | 265 return usage; |
| 260 } | 266 } |
| 261 | 267 |
| 262 void AddQuota(int64 quota_delta) { | 268 void AddQuota(int64 quota_delta) { |
| 263 int64 quota; | 269 int64 quota; |
| 264 GetUsageAndQuota(NULL, "a); | 270 GetUsageAndQuota(NULL, "a); |
| 265 quota_manager()->SetQuota(test_helper_.origin(), | 271 quota_manager()->SetQuota(sandbox_file_system_.origin(), |
| 266 test_helper_.storage_type(), | 272 sandbox_file_system_.storage_type(), |
| 267 quota + quota_delta); | 273 quota + quota_delta); |
| 268 } | 274 } |
| 269 | 275 |
| 270 base::MessageLoop message_loop_; | 276 base::MessageLoop message_loop_; |
| 271 scoped_refptr<QuotaManager> quota_manager_; | 277 scoped_refptr<QuotaManager> quota_manager_; |
| 272 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_; | 278 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_; |
| 273 | 279 |
| 274 // Common temp base for nondestructive uses. | 280 // Common temp base for nondestructive uses. |
| 275 base::ScopedTempDir base_; | 281 base::ScopedTempDir base_; |
| 276 | 282 |
| 277 LocalFileSystemTestOriginHelper test_helper_; | 283 SandboxFileSystemTestHelper sandbox_file_system_; |
| 278 | 284 |
| 279 // For post-operation status. | 285 // For post-operation status. |
| 280 int status_; | 286 int status_; |
| 281 base::PlatformFileInfo info_; | 287 base::PlatformFileInfo info_; |
| 282 base::FilePath path_; | 288 base::FilePath path_; |
| 283 std::vector<DirectoryEntry> entries_; | 289 std::vector<DirectoryEntry> entries_; |
| 284 scoped_refptr<ShareableFileReference> shareable_file_ref_; | 290 scoped_refptr<ShareableFileReference> shareable_file_ref_; |
| 285 | 291 |
| 286 MockFileChangeObserver change_observer_; | 292 MockFileChangeObserver change_observer_; |
| 287 ChangeObserverList change_observers_; | 293 ChangeObserverList change_observers_; |
| (...skipping 886 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1174 base::MessageLoop::current()->RunUntilIdle(); | 1180 base::MessageLoop::current()->RunUntilIdle(); |
| 1175 | 1181 |
| 1176 expected_usage += grandchild_file_size + grandchild_path_cost; | 1182 expected_usage += grandchild_file_size + grandchild_path_cost; |
| 1177 usage = GetUsage(); | 1183 usage = GetUsage(); |
| 1178 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, | 1184 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, |
| 1179 GetDataSizeOnDisk()); | 1185 GetDataSizeOnDisk()); |
| 1180 EXPECT_EQ(expected_usage, usage); | 1186 EXPECT_EQ(expected_usage, usage); |
| 1181 } | 1187 } |
| 1182 | 1188 |
| 1183 } // namespace fileapi | 1189 } // namespace fileapi |
| OLD | NEW |