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