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/run_loop.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "webkit/fileapi/async_file_test_helper.h" |
| 9 #include "webkit/fileapi/file_system_context.h" |
| 10 #include "webkit/fileapi/file_system_mount_point_provider.h" |
| 11 #include "webkit/fileapi/file_system_url.h" |
| 12 #include "webkit/fileapi/file_system_util.h" |
| 13 #include "webkit/quota/quota_manager.h" |
| 14 |
| 15 namespace fileapi { |
| 16 |
| 17 namespace { |
| 18 |
| 19 typedef FileSystemOperation::FileEntryList FileEntryList; |
| 20 |
| 21 void AssignAndQuit(base::RunLoop* run_loop, |
| 22 base::PlatformFileError* result_out, |
| 23 base::PlatformFileError result) { |
| 24 *result_out = result; |
| 25 run_loop->Quit(); |
| 26 } |
| 27 |
| 28 base::Callback<void(base::PlatformFileError)> |
| 29 AssignAndQuitCallback(base::RunLoop* run_loop, |
| 30 base::PlatformFileError* result) { |
| 31 return base::Bind(&AssignAndQuit, run_loop, base::Unretained(result)); |
| 32 } |
| 33 |
| 34 void GetMetadataCallback(base::RunLoop* run_loop, |
| 35 base::PlatformFileError* result_out, |
| 36 base::PlatformFileInfo* file_info_out, |
| 37 base::PlatformFileError result, |
| 38 const base::PlatformFileInfo& file_info, |
| 39 const base::FilePath& /* platform_path */) { |
| 40 *result_out = result; |
| 41 *file_info_out = file_info; |
| 42 run_loop->Quit(); |
| 43 } |
| 44 |
| 45 void ReadDirectoryCallback(base::RunLoop* run_loop, |
| 46 base::PlatformFileError* result_out, |
| 47 FileEntryList* entries_out, |
| 48 base::PlatformFileError result, |
| 49 const FileEntryList& entries, |
| 50 bool has_more) { |
| 51 *result_out = result; |
| 52 *entries_out = entries; |
| 53 if (result != base::PLATFORM_FILE_OK || !has_more) |
| 54 run_loop->Quit(); |
| 55 } |
| 56 |
| 57 void DidGetUsageAndQuota(quota::QuotaStatusCode* status_out, |
| 58 int64* usage_out, |
| 59 int64* quota_out, |
| 60 quota::QuotaStatusCode status, |
| 61 int64 usage, |
| 62 int64 quota) { |
| 63 if (status_out) |
| 64 *status_out = status; |
| 65 if (usage_out) |
| 66 *usage_out = usage; |
| 67 if (quota_out) |
| 68 *quota_out = quota; |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 const int64 AsyncFileTestHelper::kDontCheckSize = -1; |
| 74 |
| 75 base::PlatformFileError AsyncFileTestHelper::Copy( |
| 76 FileSystemContext* context, |
| 77 const FileSystemURL& src, |
| 78 const FileSystemURL& dest) { |
| 79 DCHECK(context); |
| 80 FileSystemOperation* operation = |
| 81 context->CreateFileSystemOperation(dest, NULL); |
| 82 EXPECT_TRUE(operation != NULL); |
| 83 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 84 base::RunLoop run_loop; |
| 85 operation->Copy(src, dest, AssignAndQuitCallback(&run_loop, &result)); |
| 86 run_loop.Run(); |
| 87 return result; |
| 88 } |
| 89 |
| 90 base::PlatformFileError AsyncFileTestHelper::Move( |
| 91 FileSystemContext* context, |
| 92 const FileSystemURL& src, |
| 93 const FileSystemURL& dest) { |
| 94 FileSystemOperation* operation = |
| 95 context->CreateFileSystemOperation(dest, NULL); |
| 96 EXPECT_TRUE(operation != NULL); |
| 97 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 98 base::RunLoop run_loop; |
| 99 operation->Move(src, dest, AssignAndQuitCallback(&run_loop, &result)); |
| 100 run_loop.Run(); |
| 101 return result; |
| 102 } |
| 103 |
| 104 base::PlatformFileError AsyncFileTestHelper::Remove( |
| 105 FileSystemContext* context, |
| 106 const FileSystemURL& url, |
| 107 bool recursive) { |
| 108 FileSystemOperation* operation = |
| 109 context->CreateFileSystemOperation(url, NULL); |
| 110 EXPECT_TRUE(operation != NULL); |
| 111 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 112 base::RunLoop run_loop; |
| 113 operation->Remove(url, recursive, AssignAndQuitCallback(&run_loop, &result)); |
| 114 run_loop.Run(); |
| 115 return result; |
| 116 } |
| 117 |
| 118 base::PlatformFileError AsyncFileTestHelper::ReadDirectory( |
| 119 FileSystemContext* context, |
| 120 const FileSystemURL& url, |
| 121 FileEntryList* entries) { |
| 122 DCHECK(entries); |
| 123 entries->clear(); |
| 124 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 125 FileSystemOperation* operation = |
| 126 context->CreateFileSystemOperation(url, NULL); |
| 127 EXPECT_TRUE(operation != NULL); |
| 128 base::RunLoop run_loop; |
| 129 operation->ReadDirectory( |
| 130 url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries)); |
| 131 run_loop.Run(); |
| 132 return result; |
| 133 } |
| 134 |
| 135 base::PlatformFileError AsyncFileTestHelper::CreateDirectory( |
| 136 FileSystemContext* context, |
| 137 const FileSystemURL& url) { |
| 138 FileSystemOperation* operation = |
| 139 context->CreateFileSystemOperation(url, NULL); |
| 140 EXPECT_TRUE(operation != NULL); |
| 141 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 142 base::RunLoop run_loop; |
| 143 operation->CreateDirectory(url, |
| 144 false /* exclusive */, |
| 145 false /* recursive */, |
| 146 AssignAndQuitCallback(&run_loop, &result)); |
| 147 run_loop.Run(); |
| 148 return result; |
| 149 } |
| 150 |
| 151 base::PlatformFileError AsyncFileTestHelper::CreateFile( |
| 152 FileSystemContext* context, |
| 153 const FileSystemURL& url) { |
| 154 FileSystemOperation* operation = |
| 155 context->CreateFileSystemOperation(url, NULL); |
| 156 EXPECT_TRUE(operation != NULL); |
| 157 base::RunLoop run_loop; |
| 158 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 159 operation->CreateFile(url, false /* exclusive */, |
| 160 AssignAndQuitCallback(&run_loop, &result)); |
| 161 run_loop.Run(); |
| 162 return result; |
| 163 } |
| 164 |
| 165 base::PlatformFileError AsyncFileTestHelper::TruncateFile( |
| 166 FileSystemContext* context, |
| 167 const FileSystemURL& url, |
| 168 size_t size) { |
| 169 FileSystemOperation* operation = |
| 170 context->CreateFileSystemOperation(url, NULL); |
| 171 EXPECT_TRUE(operation != NULL); |
| 172 base::RunLoop run_loop; |
| 173 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 174 operation->Truncate(url, size, |
| 175 AssignAndQuitCallback(&run_loop, &result)); |
| 176 run_loop.Run(); |
| 177 return result; |
| 178 } |
| 179 |
| 180 base::PlatformFileError AsyncFileTestHelper::GetMetadata( |
| 181 FileSystemContext* context, |
| 182 const FileSystemURL& url, |
| 183 base::PlatformFileInfo* file_info) { |
| 184 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; |
| 185 base::RunLoop run_loop; |
| 186 FileSystemOperation* operation = |
| 187 context->CreateFileSystemOperation(url, NULL); |
| 188 EXPECT_TRUE(operation != NULL); |
| 189 operation->GetMetadata(url, base::Bind(&GetMetadataCallback, |
| 190 &run_loop, &result, file_info)); |
| 191 run_loop.Run(); |
| 192 return result; |
| 193 } |
| 194 |
| 195 bool AsyncFileTestHelper::FileExists( |
| 196 FileSystemContext* context, |
| 197 const FileSystemURL& url, |
| 198 int64 expected_size) { |
| 199 base::PlatformFileInfo file_info; |
| 200 base::PlatformFileError result = GetMetadata(context, url, &file_info); |
| 201 if (result != base::PLATFORM_FILE_OK || file_info.is_directory) |
| 202 return false; |
| 203 return expected_size == kDontCheckSize || file_info.size == expected_size; |
| 204 } |
| 205 |
| 206 bool AsyncFileTestHelper::DirectoryExists( |
| 207 FileSystemContext* context, |
| 208 const FileSystemURL& url) { |
| 209 base::PlatformFileInfo file_info; |
| 210 base::PlatformFileError result = GetMetadata(context, url, &file_info); |
| 211 return (result == base::PLATFORM_FILE_OK) && file_info.is_directory; |
| 212 } |
| 213 |
| 214 quota::QuotaStatusCode AsyncFileTestHelper::GetUsageAndQuota( |
| 215 quota::QuotaManager* quota_manager, |
| 216 const GURL& origin, |
| 217 FileSystemType type, |
| 218 int64* usage, |
| 219 int64* quota) { |
| 220 quota::QuotaStatusCode status = quota::kQuotaStatusUnknown; |
| 221 quota_manager->GetUsageAndQuota( |
| 222 origin, |
| 223 FileSystemTypeToQuotaStorageType(type), |
| 224 base::Bind(&DidGetUsageAndQuota, &status, usage, quota)); |
| 225 MessageLoop::current()->RunUntilIdle(); |
| 226 return status; |
| 227 } |
| 228 |
| 229 } // namespace fileapi |
OLD | NEW |