Chromium Code Reviews| 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 "base/message_loop.h" | |
| 7 #include "base/platform_file.h" | |
| 8 #include "base/time.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "webkit/chromeos/fileapi/memory_file_util.h" | |
| 11 | |
| 12 namespace { | |
| 13 const FilePath::CharType kRootPath[] = "/mnt/memory"; | |
| 14 const char kTestString[] = "A test string. A test string."; | |
| 15 const char kTestStringLength = arraysize(kTestString) - 1; | |
| 16 } // namespace | |
| 17 | |
| 18 namespace fileapi { | |
| 19 | |
| 20 // This test is actually testing MemoryFileUtil — an async in-memory file | |
| 21 // system, based on FileUtilAsync. | |
| 22 class MemoryFileUtilTest : public testing::Test { | |
| 23 public: | |
| 24 MemoryFileUtilTest() { | |
| 25 } | |
| 26 | |
| 27 void SetUp() { | |
| 28 file_util_.reset(new MemoryFileUtil(FilePath(kRootPath))); | |
| 29 } | |
| 30 | |
| 31 MemoryFileUtil* file_util() { | |
| 32 return file_util_.get(); | |
| 33 } | |
| 34 | |
| 35 enum CallbackType { | |
| 36 CALLBACK_TYPE_ERROR, | |
| 37 CALLBACK_TYPE_STATUS, | |
| 38 CALLBACK_TYPE_GET_FILE_INFO, | |
| 39 CALLBACK_TYPE_OPEN, | |
| 40 CALLBACK_TYPE_READ_WRITE, | |
| 41 CALLBACK_TYPE_READ_DIRECTORY | |
| 42 }; | |
| 43 | |
| 44 struct CallbackStatus { | |
| 45 CallbackType type; | |
| 46 base::PlatformFileError result; | |
| 47 base::PlatformFileInfo file_info; | |
| 48 // This object should be deleted in the test code. | |
| 49 AsyncFileStream* file_stream; | |
| 50 int64 length; | |
| 51 | |
| 52 // Following 4 fields only for ReadDirectory. | |
| 53 FileUtilAsync::FileList entries; | |
| 54 bool completed; | |
| 55 bool called_after_completed; | |
| 56 int called; | |
| 57 }; | |
| 58 | |
| 59 StatusCallback GetStatusCallback(int request_id) { | |
| 60 if (request_id > max_request_id_) { | |
|
kinuko
2011/12/14 03:39:22
Please always omit (or use) the curly braces for s
satorux1
2011/12/14 04:43:26
Done.
| |
| 61 max_request_id_ = request_id; | |
| 62 } | |
| 63 return base::Bind(&MemoryFileUtilTest::StatusCallbackImpl, | |
| 64 base::Unretained(this), | |
| 65 request_id); | |
| 66 } | |
| 67 | |
| 68 FileUtilAsync::GetFileInfoCallback GetGetFileInfoCallback( | |
| 69 int request_id) { | |
| 70 if (request_id > max_request_id_) { | |
| 71 max_request_id_ = request_id; | |
| 72 } | |
| 73 return base::Bind(&MemoryFileUtilTest::GetFileInfoCallback, | |
| 74 base::Unretained(this), | |
| 75 request_id); | |
| 76 } | |
| 77 | |
| 78 FileUtilAsync::OpenCallback GetOpenCallback(int request_id) { | |
| 79 if (request_id > max_request_id_) { | |
| 80 max_request_id_ = request_id; | |
| 81 } | |
| 82 return base::Bind(&MemoryFileUtilTest::OpenCallback, | |
| 83 base::Unretained(this), | |
| 84 request_id); | |
| 85 } | |
| 86 | |
| 87 ReadWriteCallback GetReadWriteCallback(int request_id) { | |
| 88 if (request_id > max_request_id_) { | |
| 89 max_request_id_ = request_id; | |
| 90 } | |
| 91 return base::Bind(&MemoryFileUtilTest::ReadWriteCallbackImpl, | |
| 92 base::Unretained(this), | |
| 93 request_id); | |
| 94 } | |
| 95 | |
| 96 FileUtilAsync::ReadDirectoryCallback GetReadDirectoryCallback( | |
| 97 int request_id) { | |
| 98 if (request_id > max_request_id_) { | |
| 99 max_request_id_ = request_id; | |
| 100 } | |
| 101 return base::Bind(&MemoryFileUtilTest::ReadDirectoryCallback, | |
| 102 base::Unretained(this), | |
| 103 request_id); | |
| 104 } | |
| 105 | |
| 106 int CreateEmptyFile(const FilePath& file_path) { | |
| 107 int request_id = GetNextRequestId(); | |
| 108 file_util_->Create(file_path, GetStatusCallback(request_id)); | |
| 109 return request_id; | |
| 110 } | |
| 111 | |
| 112 int CreateNonEmptyFile(const FilePath& file_path, | |
| 113 const char* data, | |
| 114 int length) { | |
| 115 int request_id = GetNextRequestId(); | |
| 116 file_util_->Open( | |
| 117 file_path, | |
| 118 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, | |
| 119 base::Bind(&MemoryFileUtilTest::WriteToOpenedFile, | |
| 120 base::Unretained(this), | |
| 121 request_id, data, length)); | |
| 122 return request_id; | |
| 123 } | |
| 124 | |
| 125 CallbackType GetStatusType(int request_id) { | |
| 126 if (status_map_.find(request_id) == status_map_.end()) | |
| 127 return CALLBACK_TYPE_ERROR; | |
| 128 return status_map_[request_id].type; | |
| 129 } | |
| 130 | |
| 131 // Return the operation status. | |
| 132 CallbackStatus& GetStatus(int request_id) { | |
| 133 return status_map_[request_id]; | |
| 134 } | |
| 135 | |
| 136 int StatusQueueSize() { | |
| 137 return status_map_.size(); | |
| 138 } | |
| 139 | |
| 140 int GetNextRequestId() { | |
| 141 return ++max_request_id_; | |
| 142 } | |
| 143 | |
| 144 void set_read_directory_buffer_size(int size) { | |
| 145 file_util_->set_read_directory_buffer_size(size); | |
| 146 } | |
| 147 | |
| 148 private: | |
| 149 void StatusCallbackImpl(int request_id, PlatformFileError result) { | |
| 150 CallbackStatus status; | |
| 151 status.type = CALLBACK_TYPE_STATUS; | |
| 152 status.result = result; | |
| 153 status_map_[request_id] = status; | |
| 154 } | |
| 155 | |
| 156 void GetFileInfoCallback(int request_id, | |
| 157 PlatformFileError result, | |
| 158 const base::PlatformFileInfo& file_info) { | |
| 159 CallbackStatus status; | |
| 160 status.type = CALLBACK_TYPE_GET_FILE_INFO; | |
| 161 status.result = result; | |
| 162 status.file_info = file_info; | |
| 163 status_map_[request_id] = status; | |
| 164 } | |
| 165 | |
| 166 void OpenCallback(int request_id, | |
| 167 PlatformFileError result, | |
| 168 AsyncFileStream* file_stream) { | |
| 169 CallbackStatus status; | |
| 170 status.type = CALLBACK_TYPE_OPEN; | |
| 171 status.result = result; | |
| 172 status.file_stream = file_stream; | |
| 173 status_map_[request_id] = status; | |
| 174 } | |
| 175 | |
| 176 void ReadWriteCallbackImpl(int request_id, | |
| 177 PlatformFileError result, | |
| 178 int64 length) { | |
| 179 CallbackStatus status; | |
| 180 status.type = CALLBACK_TYPE_READ_WRITE; | |
| 181 status.result = result; | |
| 182 status.length = length; | |
| 183 status_map_[request_id] = status; | |
| 184 } | |
| 185 | |
| 186 void ReadDirectoryCallback(int request_id, | |
| 187 PlatformFileError result, | |
| 188 const FileUtilAsync::FileList& entries, | |
| 189 bool completed) { | |
| 190 if (status_map_.find(request_id) == status_map_.end()) { | |
| 191 CallbackStatus status; | |
| 192 status.type = CALLBACK_TYPE_READ_DIRECTORY; | |
| 193 status.called_after_completed = false; | |
| 194 status.result = result; | |
| 195 status.completed = completed; | |
| 196 status.called = 1; | |
| 197 status.entries = entries; | |
| 198 status_map_[request_id] = status; | |
| 199 } else { | |
| 200 CallbackStatus& status = status_map_[request_id]; | |
| 201 if (status.completed) | |
| 202 status.called_after_completed = true; | |
| 203 status.result = result; | |
| 204 status.completed = completed; | |
| 205 ++status.called; | |
| 206 status.entries.insert(status.entries.begin(), entries.begin(), | |
| 207 entries.end()); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 void WriteToOpenedFile(int request_id, | |
| 212 const char* data, | |
| 213 int length, | |
| 214 PlatformFileError result, | |
| 215 AsyncFileStream* stream) { | |
| 216 CallbackStatus status; | |
| 217 status.type = CALLBACK_TYPE_OPEN; | |
| 218 status.result = result; | |
| 219 status_map_[request_id] = status; | |
| 220 stream->Write(data, length, GetReadWriteCallback(GetNextRequestId())); | |
| 221 } | |
| 222 | |
| 223 scoped_ptr<MemoryFileUtil> file_util_; | |
| 224 std::map<int, CallbackStatus> status_map_; | |
| 225 int max_request_id_; | |
| 226 | |
| 227 DISALLOW_COPY_AND_ASSIGN(MemoryFileUtilTest); | |
| 228 }; | |
| 229 | |
| 230 TEST_F(MemoryFileUtilTest, TestCreateGetFileInfo) { | |
| 231 file_util()->GetFileInfo(FilePath("/mnt/memory/test.txt"), | |
| 232 GetGetFileInfoCallback(1)); | |
| 233 | |
| 234 // TODO(olege): In case the file system is truely asynchronous, RunAllPending | |
| 235 // is not enough to wait for answer. In that case the thread should be blocked | |
| 236 // until the callback is called. | |
|
kinuko
2011/12/14 03:39:22
Is this really a TODO comment or just a note for o
satorux1
2011/12/14 04:43:26
Looks like a note. Removed TODO, and added some mo
| |
| 237 MessageLoop::current()->RunAllPending(); | |
| 238 | |
| 239 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(1)); | |
| 240 CallbackStatus status = GetStatus(1); | |
| 241 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status.result); | |
| 242 | |
| 243 base::Time start_create = base::Time::Now(); | |
| 244 | |
| 245 file_util()->Create(FilePath("/mnt/memory/test.txt"), | |
|
kinuko
2011/12/14 03:39:22
I prefer using (local) const variables if we refer
satorux1
2011/12/14 04:43:26
Let's keep it as is. file_util() is inlined anyway
| |
| 246 GetStatusCallback(2)); | |
| 247 MessageLoop::current()->RunAllPending(); | |
| 248 ASSERT_EQ(CALLBACK_TYPE_STATUS, GetStatusType(2)); | |
| 249 status = GetStatus(2); | |
| 250 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 251 | |
| 252 file_util()->GetFileInfo(FilePath("/mnt/memory/test.txt"), | |
| 253 GetGetFileInfoCallback(3)); | |
| 254 MessageLoop::current()->RunAllPending(); | |
| 255 | |
| 256 base::Time end_create = base::Time::Now(); | |
| 257 | |
| 258 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(3)); | |
| 259 status = GetStatus(3); | |
| 260 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 261 ASSERT_EQ(0, status.file_info.size); | |
| 262 ASSERT_FALSE(status.file_info.is_directory); | |
| 263 ASSERT_FALSE(status.file_info.is_symbolic_link); | |
| 264 ASSERT_GE(status.file_info.last_modified, start_create); | |
| 265 ASSERT_LE(status.file_info.last_modified, end_create); | |
| 266 ASSERT_GE(status.file_info.creation_time, start_create); | |
| 267 ASSERT_LE(status.file_info.creation_time, end_create); | |
| 268 } | |
| 269 | |
| 270 TEST_F(MemoryFileUtilTest, TestReadWrite) { | |
| 271 // Check that the file does not exist. | |
| 272 | |
| 273 file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), | |
| 274 GetGetFileInfoCallback(1)); | |
| 275 | |
| 276 MessageLoop::current()->RunAllPending(); | |
| 277 | |
| 278 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(1)); | |
| 279 CallbackStatus status = GetStatus(1); | |
| 280 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status.result); | |
| 281 | |
| 282 // Create & open file for writing. | |
| 283 | |
| 284 base::Time start_create = base::Time::Now(); | |
| 285 file_util()->Open(FilePath("/mnt/memory/test1.txt"), | |
| 286 base::PLATFORM_FILE_CREATE_ALWAYS | | |
| 287 base::PLATFORM_FILE_WRITE, | |
| 288 GetOpenCallback(2)); | |
| 289 MessageLoop::current()->RunAllPending(); | |
| 290 | |
| 291 base::Time end_create = base::Time::Now(); | |
| 292 | |
| 293 ASSERT_EQ(CALLBACK_TYPE_OPEN, GetStatusType(2)); | |
| 294 status = GetStatus(2); | |
| 295 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 296 | |
| 297 AsyncFileStream* write_file_stream = status.file_stream; | |
| 298 | |
| 299 // Check that file was created and has 0 size. | |
| 300 | |
| 301 file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), | |
| 302 GetGetFileInfoCallback(3)); | |
| 303 MessageLoop::current()->RunAllPending(); | |
| 304 | |
| 305 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(3)); | |
| 306 status = GetStatus(3); | |
| 307 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 308 ASSERT_EQ(0, status.file_info.size); | |
| 309 ASSERT_FALSE(status.file_info.is_directory); | |
| 310 ASSERT_FALSE(status.file_info.is_symbolic_link); | |
| 311 ASSERT_GE(status.file_info.last_modified, start_create); | |
| 312 ASSERT_LE(status.file_info.last_modified, end_create); | |
| 313 | |
| 314 // Write 10 bytes to file. | |
| 315 | |
| 316 base::Time start_write = base::Time::Now(); | |
| 317 write_file_stream->Write(kTestString, 10, | |
| 318 GetReadWriteCallback(4)); | |
| 319 MessageLoop::current()->RunAllPending(); | |
| 320 base::Time end_write = base::Time::Now(); | |
| 321 | |
| 322 ASSERT_EQ(CALLBACK_TYPE_READ_WRITE, GetStatusType(4)); | |
| 323 status = GetStatus(4); | |
| 324 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 325 ASSERT_EQ(10, status.length); | |
| 326 | |
| 327 // Check that the file has now size 10 and correct modification time. | |
| 328 | |
| 329 file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), | |
| 330 GetGetFileInfoCallback(5)); | |
| 331 MessageLoop::current()->RunAllPending(); | |
| 332 | |
| 333 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(5)); | |
| 334 status = GetStatus(5); | |
| 335 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 336 ASSERT_EQ(10, status.file_info.size); | |
| 337 ASSERT_GE(status.file_info.last_modified, start_write); | |
| 338 ASSERT_LE(status.file_info.last_modified, end_write); | |
| 339 | |
| 340 // Write the rest of the string to file. | |
| 341 | |
| 342 start_write = base::Time::Now(); | |
| 343 write_file_stream->Write(kTestString + 10, | |
| 344 kTestStringLength - 10, | |
| 345 GetReadWriteCallback(6)); | |
| 346 MessageLoop::current()->RunAllPending(); | |
| 347 end_write = base::Time::Now(); | |
| 348 | |
| 349 ASSERT_EQ(CALLBACK_TYPE_READ_WRITE, GetStatusType(6)); | |
| 350 status = GetStatus(6); | |
| 351 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 352 ASSERT_EQ(static_cast<int64>(kTestStringLength) - 10, status.length); | |
| 353 | |
| 354 // Check the file size & modification time. | |
| 355 | |
| 356 file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), | |
| 357 GetGetFileInfoCallback(7)); | |
| 358 MessageLoop::current()->RunAllPending(); | |
| 359 | |
| 360 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(7)); | |
| 361 status = GetStatus(7); | |
| 362 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 363 ASSERT_EQ(static_cast<int64>(kTestStringLength), status.file_info.size); | |
| 364 ASSERT_GE(status.file_info.last_modified, start_write); | |
| 365 ASSERT_LE(status.file_info.last_modified, end_write); | |
| 366 | |
| 367 // Open file for reading. | |
| 368 | |
| 369 file_util()->Open(FilePath("/mnt/memory/test1.txt"), | |
| 370 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
| 371 GetOpenCallback(8)); | |
|
kinuko
2011/12/14 03:39:22
This code would make it hard to add other test sni
satorux1
2011/12/14 04:43:26
Done.
| |
| 372 MessageLoop::current()->RunAllPending(); | |
| 373 | |
| 374 ASSERT_EQ(CALLBACK_TYPE_OPEN, GetStatusType(8)); | |
| 375 status = GetStatus(8); | |
| 376 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 377 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 378 | |
| 379 scoped_ptr<AsyncFileStream> read_file_stream(status.file_stream); | |
| 380 | |
| 381 // Read the whole file | |
| 382 char buffer[1024]; | |
| 383 read_file_stream->Read(buffer, 1023, GetReadWriteCallback(9)); | |
| 384 MessageLoop::current()->RunAllPending(); | |
| 385 | |
| 386 ASSERT_EQ(CALLBACK_TYPE_READ_WRITE, GetStatusType(9)); | |
| 387 status = GetStatus(9); | |
| 388 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 389 ASSERT_EQ(static_cast<int64>(kTestStringLength), status.length); | |
| 390 | |
| 391 buffer[status.length] = '\0'; | |
| 392 std::string result_string(buffer); | |
| 393 ASSERT_EQ(kTestString, result_string); | |
| 394 | |
| 395 // Check that size & modification time have not changed. | |
| 396 | |
| 397 file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), | |
| 398 GetGetFileInfoCallback(10)); | |
| 399 MessageLoop::current()->RunAllPending(); | |
| 400 | |
| 401 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(10)); | |
| 402 status = GetStatus(10); | |
| 403 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 404 ASSERT_EQ(static_cast<int64>(kTestStringLength), status.file_info.size); | |
| 405 ASSERT_GE(status.file_info.last_modified, start_write); | |
| 406 ASSERT_LE(status.file_info.last_modified, end_write); | |
| 407 | |
| 408 // Open once more for writing. | |
| 409 | |
| 410 file_util()->Open(FilePath("/mnt/memory/test1.txt"), | |
| 411 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | |
| 412 GetOpenCallback(11)); | |
| 413 MessageLoop::current()->RunAllPending(); | |
| 414 | |
| 415 ASSERT_EQ(CALLBACK_TYPE_OPEN, GetStatusType(11)); | |
| 416 status = GetStatus(11); | |
| 417 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 418 | |
| 419 scoped_ptr<AsyncFileStream> write_file_stream2(status.file_stream); | |
| 420 | |
| 421 // Check that the size has not changed. | |
| 422 | |
| 423 file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), | |
| 424 GetGetFileInfoCallback(12)); | |
| 425 MessageLoop::current()->RunAllPending(); | |
| 426 | |
| 427 ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(12)); | |
| 428 status = GetStatus(12); | |
| 429 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 430 ASSERT_EQ(static_cast<int64>(kTestStringLength), status.file_info.size); | |
| 431 | |
| 432 // Seek beyond the end of file. Should return error. | |
| 433 | |
| 434 write_file_stream2->Seek(1000, GetStatusCallback(13)); | |
| 435 MessageLoop::current()->RunAllPending(); | |
| 436 ASSERT_EQ(CALLBACK_TYPE_STATUS, GetStatusType(13)); | |
| 437 status = GetStatus(13); | |
| 438 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status.result); | |
| 439 | |
| 440 // Try to write to read-only stream. | |
| 441 | |
| 442 read_file_stream->Write(kTestString, | |
| 443 kTestStringLength, | |
| 444 GetReadWriteCallback(14)); | |
| 445 MessageLoop::current()->RunAllPending(); | |
| 446 status = GetStatus(14); | |
| 447 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status.result); | |
| 448 | |
| 449 // Write data overlapping with already written. | |
| 450 | |
| 451 | |
| 452 write_file_stream2->Seek(10, GetStatusCallback(15)); | |
| 453 MessageLoop::current()->RunAllPending(); | |
| 454 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(15).result); | |
| 455 | |
| 456 write_file_stream2->Write(kTestString, | |
| 457 kTestStringLength, | |
| 458 GetReadWriteCallback(16)); | |
| 459 MessageLoop::current()->RunAllPending(); | |
| 460 status = GetStatus(16); | |
| 461 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 462 ASSERT_EQ(static_cast<int64>(kTestStringLength), status.length); | |
| 463 | |
| 464 // Check size. | |
| 465 | |
| 466 file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), | |
| 467 GetGetFileInfoCallback(17)); | |
| 468 MessageLoop::current()->RunAllPending(); | |
| 469 status = GetStatus(17); | |
| 470 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 471 ASSERT_EQ(static_cast<int64>(kTestStringLength) + 10, | |
| 472 status.file_info.size); | |
| 473 | |
| 474 // Read from 10th byte. | |
| 475 read_file_stream->Seek(10, GetStatusCallback(18)); | |
| 476 MessageLoop::current()->RunAllPending(); | |
| 477 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(18).result); | |
| 478 | |
| 479 read_file_stream->Read(buffer, 1023, GetReadWriteCallback(19)); | |
| 480 MessageLoop::current()->RunAllPending(); | |
| 481 status = GetStatus(19); | |
| 482 ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); | |
| 483 ASSERT_EQ(static_cast<int64>(kTestStringLength), status.length); | |
| 484 buffer[status.length] = '\0'; | |
| 485 std::string result_string2(buffer); | |
| 486 ASSERT_EQ(kTestString, result_string2); | |
| 487 } | |
| 488 | |
| 489 // The directory structure we'll be testing on: | |
| 490 // path size | |
| 491 // | |
| 492 // /mnt/memory/a 0 | |
| 493 // /mnt/memory/b/ | |
| 494 // /mnt/memory/b/c kTestStringLength | |
| 495 // /mnt/memory/b/d/ | |
| 496 // /mnt/memory/b/e 0 | |
| 497 // /mnt/memory/b/f kTestStringLength | |
| 498 // /mnt/memory/b/g/ | |
| 499 // /mnt/memory/b/g/h 0 | |
| 500 // /mnt/memory/b/i kTestStringLength | |
| 501 // /mnt/memory/c/ | |
| 502 // /mnt/memory/longer_file_name.txt kTestStringLength | |
|
kinuko
2011/12/14 03:39:22
Can we define these test constants somewhere and m
satorux1
2011/12/14 04:43:26
I thought about it but constants may make the code
satorux1
2011/12/14 04:44:40
I meant variable names.
| |
| 503 TEST_F(MemoryFileUtilTest, TestDirectoryOperations) { | |
| 504 // Check the directory is empty. | |
| 505 file_util()->ReadDirectory(FilePath("/mnt/memory/"), | |
| 506 GetReadDirectoryCallback(1)); | |
| 507 | |
| 508 MessageLoop::current()->RunAllPending(); | |
| 509 | |
| 510 ASSERT_EQ(CALLBACK_TYPE_READ_DIRECTORY, GetStatusType(1)); | |
| 511 CallbackStatus& status = GetStatus(1); | |
| 512 ASSERT_TRUE(status.completed); | |
| 513 ASSERT_FALSE(status.called_after_completed); | |
| 514 ASSERT_EQ(1, status.called); | |
| 515 ASSERT_EQ(0u, status.entries.size()); | |
| 516 | |
| 517 // Create /mnt/memory/a, /mnt/memory/b/, /mnt/memory/longer_file_name.txt, | |
| 518 // /mnt/memory/c/ asyncronously (i.e. we do not wait for each operation to | |
| 519 // complete before starting the next one. | |
| 520 | |
| 521 base::Time start_create = base::Time::Now(); | |
| 522 CreateEmptyFile(FilePath("/mnt/memory/a")); | |
| 523 | |
| 524 int request_id1 = GetNextRequestId(); | |
| 525 file_util()->CreateDirectory(FilePath("/mnt/memory/b"), | |
| 526 GetStatusCallback(request_id1)); | |
| 527 | |
| 528 CreateNonEmptyFile(FilePath("/mnt/memory/longer_file_name.txt"), | |
| 529 kTestString, | |
| 530 kTestStringLength); | |
| 531 | |
| 532 int request_id2 = GetNextRequestId(); | |
| 533 file_util()->CreateDirectory(FilePath("/mnt/memory/c"), | |
| 534 GetStatusCallback(request_id2)); | |
| 535 | |
| 536 MessageLoop::current()->RunAllPending(); | |
| 537 base::Time end_create = base::Time::Now(); | |
| 538 | |
| 539 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 540 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id2).result); | |
| 541 | |
| 542 // ReadDirectory /mnt/memory, /mnt/memory/a (not a dir), /mnt/memory/b/, | |
| 543 // /mnt/memory/d (not found) | |
| 544 | |
| 545 set_read_directory_buffer_size(5); // Should complete in one go. | |
| 546 | |
| 547 request_id1 = GetNextRequestId(); | |
| 548 file_util()->ReadDirectory(FilePath("/mnt/memory"), | |
| 549 GetReadDirectoryCallback(request_id1)); | |
| 550 request_id2 = GetNextRequestId(); | |
| 551 file_util()->ReadDirectory(FilePath("/mnt/memory/a"), | |
| 552 GetReadDirectoryCallback(request_id2)); | |
| 553 int request_id3 = GetNextRequestId(); | |
| 554 file_util()->ReadDirectory(FilePath("/mnt/memory/b/"), | |
| 555 GetReadDirectoryCallback(request_id3)); | |
| 556 int request_id4 = GetNextRequestId(); | |
| 557 file_util()->ReadDirectory(FilePath("/mnt/memory/d/"), | |
| 558 GetReadDirectoryCallback(request_id4)); | |
| 559 | |
| 560 MessageLoop::current()->RunAllPending(); | |
| 561 | |
| 562 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 563 status = GetStatus(request_id1); | |
| 564 ASSERT_TRUE(status.completed); | |
| 565 ASSERT_FALSE(status.called_after_completed); | |
| 566 ASSERT_EQ(1, status.called); // Because the number of entries < 5. | |
| 567 ASSERT_EQ(4u, status.entries.size()); | |
| 568 | |
| 569 std::set<FilePath::StringType> seen; | |
| 570 | |
| 571 for (FileUtilAsync::FileList::const_iterator it = status.entries.begin(); | |
| 572 it != status.entries.end(); | |
| 573 ++it) { | |
| 574 ASSERT_LE(start_create, it->last_modified_time); | |
| 575 ASSERT_GE(end_create, it->last_modified_time); | |
| 576 | |
| 577 ASSERT_EQ(seen.end(), seen.find(it->name)); | |
| 578 seen.insert(it->name); | |
| 579 | |
| 580 if (it->name == "a") { | |
| 581 ASSERT_FALSE(it->is_directory); | |
| 582 ASSERT_EQ(0, it->size); | |
| 583 } else if (it->name == "b") { | |
| 584 ASSERT_TRUE(it->is_directory); | |
| 585 } else if (it->name == "c") { | |
| 586 ASSERT_TRUE(it->is_directory); | |
| 587 } else if (it->name == "longer_file_name.txt") { | |
| 588 ASSERT_FALSE(it->is_directory); | |
| 589 ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); | |
| 590 } else { | |
| 591 LOG(ERROR) << "Unexpected file: " << it->name; | |
| 592 ASSERT_TRUE(false); | |
| 593 } | |
| 594 } | |
| 595 | |
| 596 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, | |
| 597 GetStatus(request_id2).result); | |
| 598 | |
| 599 status = GetStatus(request_id3); | |
| 600 ASSERT_TRUE(status.completed); | |
| 601 ASSERT_FALSE(status.called_after_completed); | |
| 602 ASSERT_EQ(1, status.called); | |
| 603 ASSERT_EQ(0u, status.entries.size()); | |
| 604 | |
| 605 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, GetStatus(request_id4).result); | |
| 606 | |
| 607 // Create files & dirs inside b/: | |
| 608 // /mnt/memory/b/c kTestStringLength | |
| 609 // /mnt/memory/b/d/ | |
| 610 // /mnt/memory/b/e 0 | |
| 611 // /mnt/memory/b/f kTestStringLength | |
| 612 // /mnt/memory/b/g/ | |
| 613 // /mnt/memory/b/i kTestStringLength | |
| 614 // | |
| 615 // /mnt/memory/b/g/h 0 | |
| 616 | |
| 617 start_create = base::Time::Now(); | |
| 618 CreateNonEmptyFile(FilePath("/mnt/memory/b/c"), | |
| 619 kTestString, | |
| 620 kTestStringLength); | |
| 621 request_id1 = GetNextRequestId(); | |
| 622 file_util()->CreateDirectory(FilePath("/mnt/memory/b/d"), | |
| 623 GetStatusCallback(request_id1)); | |
| 624 CreateEmptyFile(FilePath("/mnt/memory/b/e")); | |
| 625 CreateNonEmptyFile(FilePath("/mnt/memory/b/f"), | |
| 626 kTestString, | |
| 627 kTestStringLength); | |
| 628 request_id2 = GetNextRequestId(); | |
| 629 file_util()->CreateDirectory(FilePath("/mnt/memory/b/g"), | |
| 630 GetStatusCallback(request_id1)); | |
| 631 CreateNonEmptyFile(FilePath("/mnt/memory/b/i"), | |
| 632 kTestString, | |
| 633 kTestStringLength); | |
| 634 | |
| 635 MessageLoop::current()->RunAllPending(); | |
| 636 | |
| 637 CreateEmptyFile(FilePath("/mnt/memory/b/g/h")); | |
| 638 | |
| 639 MessageLoop::current()->RunAllPending(); | |
| 640 end_create = base::Time::Now(); | |
| 641 | |
| 642 // Read /mnt/memory and check that the number of entries is unchanged. | |
| 643 | |
| 644 request_id1 = GetNextRequestId(); | |
| 645 file_util()->ReadDirectory(FilePath("/mnt/memory"), | |
| 646 GetReadDirectoryCallback(request_id1)); | |
| 647 | |
| 648 MessageLoop::current()->RunAllPending(); | |
| 649 | |
| 650 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 651 status = GetStatus(request_id1); | |
| 652 ASSERT_TRUE(status.completed); | |
| 653 ASSERT_FALSE(status.called_after_completed); | |
| 654 ASSERT_EQ(1, status.called); // Because the number of entries < 5. | |
| 655 ASSERT_EQ(4u, status.entries.size()); | |
| 656 | |
| 657 // Read /mnt/memory/b | |
| 658 | |
| 659 request_id1 = GetNextRequestId(); | |
| 660 file_util()->ReadDirectory(FilePath("/mnt/memory/b"), | |
| 661 GetReadDirectoryCallback(request_id1)); | |
| 662 | |
| 663 MessageLoop::current()->RunAllPending(); | |
| 664 | |
| 665 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 666 status = GetStatus(request_id1); | |
| 667 ASSERT_TRUE(status.completed); | |
| 668 ASSERT_FALSE(status.called_after_completed); | |
| 669 ASSERT_EQ(2, status.called); // Because the number of entries > 5. | |
| 670 ASSERT_EQ(6u, status.entries.size()); | |
| 671 | |
| 672 seen.clear(); | |
| 673 | |
| 674 for (FileUtilAsync::FileList::const_iterator it = status.entries.begin(); | |
| 675 it != status.entries.end(); | |
| 676 ++it) { | |
| 677 ASSERT_LE(start_create, it->last_modified_time); | |
| 678 ASSERT_GE(end_create, it->last_modified_time); | |
| 679 | |
| 680 ASSERT_EQ(seen.end(), seen.find(it->name)); | |
| 681 seen.insert(it->name); | |
| 682 | |
| 683 // /mnt/memory/b/c kTestStringLength | |
| 684 // /mnt/memory/b/d/ | |
| 685 // /mnt/memory/b/e 0 | |
| 686 // /mnt/memory/b/f kTestStringLength | |
| 687 // /mnt/memory/b/g/ | |
| 688 // /mnt/memory/b/i kTestStringLength | |
| 689 | |
| 690 if (it->name == "c") { | |
| 691 ASSERT_FALSE(it->is_directory); | |
| 692 ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); | |
| 693 } else if (it->name == "d") { | |
| 694 ASSERT_TRUE(it->is_directory); | |
| 695 } else if (it->name == "e") { | |
| 696 ASSERT_FALSE(it->is_directory); | |
| 697 ASSERT_EQ(0, it->size); | |
| 698 } else if (it->name == "f") { | |
| 699 ASSERT_FALSE(it->is_directory); | |
| 700 ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); | |
| 701 } else if (it->name == "g") { | |
| 702 ASSERT_TRUE(it->is_directory); | |
| 703 } else if (it->name == "i") { | |
| 704 ASSERT_FALSE(it->is_directory); | |
| 705 ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); | |
| 706 } else { | |
| 707 LOG(ERROR) << "Unexpected file: " << it->name; | |
| 708 ASSERT_TRUE(false); | |
| 709 } | |
| 710 } | |
| 711 | |
| 712 // Remove single file: /mnt/memory/b/f | |
| 713 | |
| 714 request_id1 = GetNextRequestId(); | |
| 715 file_util()->Remove(FilePath("/mnt/memory/b/f"), false /* recursive */, | |
| 716 GetStatusCallback(request_id1)); | |
| 717 MessageLoop::current()->RunAllPending(); | |
| 718 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 719 | |
| 720 // Check the number of files in b/ | |
| 721 | |
| 722 request_id1 = GetNextRequestId(); | |
| 723 file_util()->ReadDirectory(FilePath("/mnt/memory/b"), | |
| 724 GetReadDirectoryCallback(request_id1)); | |
| 725 | |
| 726 MessageLoop::current()->RunAllPending(); | |
| 727 | |
| 728 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 729 status = GetStatus(request_id1); | |
| 730 ASSERT_TRUE(status.completed); | |
| 731 ASSERT_FALSE(status.called_after_completed); | |
| 732 ASSERT_EQ(5u, status.entries.size()); | |
| 733 | |
| 734 // Try remove /mnt/memory/b non-recursively (error) | |
| 735 | |
| 736 request_id1 = GetNextRequestId(); | |
| 737 file_util()->Remove(FilePath("/mnt/memory/b"), false /* recursive */, | |
| 738 GetStatusCallback(request_id1)); | |
| 739 MessageLoop::current()->RunAllPending(); | |
| 740 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, | |
| 741 GetStatus(request_id1).result); | |
| 742 | |
| 743 // Non-recursively remove empty directory. | |
| 744 | |
| 745 request_id1 = GetNextRequestId(); | |
| 746 file_util()->Remove(FilePath("/mnt/memory/b/d"), false, | |
| 747 GetStatusCallback(request_id1)); | |
| 748 MessageLoop::current()->RunAllPending(); | |
| 749 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 750 | |
| 751 request_id1 = GetNextRequestId(); | |
| 752 file_util()->GetFileInfo(FilePath("/mnt/memory/b/d"), | |
| 753 GetGetFileInfoCallback(request_id1)); | |
| 754 MessageLoop::current()->RunAllPending(); | |
| 755 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, GetStatus(request_id1).result); | |
| 756 | |
| 757 // Remove /mnt/memory/b recursively. | |
| 758 | |
| 759 request_id1 = GetNextRequestId(); | |
| 760 file_util()->Remove(FilePath("/mnt/memory/b"), true /* recursive */, | |
| 761 GetStatusCallback(request_id1)); | |
| 762 MessageLoop::current()->RunAllPending(); | |
| 763 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 764 | |
| 765 // ReadDirectory /mnt/memory/b -> not found | |
| 766 | |
| 767 request_id1 = GetNextRequestId(); | |
| 768 file_util()->ReadDirectory(FilePath("/mnt/memory/b"), | |
| 769 GetReadDirectoryCallback(request_id1)); | |
| 770 MessageLoop::current()->RunAllPending(); | |
| 771 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, GetStatus(request_id1).result); | |
| 772 | |
| 773 // ReadDirectory /mnt/memory | |
| 774 | |
| 775 request_id1 = GetNextRequestId(); | |
| 776 file_util()->ReadDirectory(FilePath("/mnt/memory"), | |
| 777 GetReadDirectoryCallback(request_id1)); | |
| 778 | |
| 779 MessageLoop::current()->RunAllPending(); | |
| 780 | |
| 781 ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); | |
| 782 status = GetStatus(request_id1); | |
| 783 ASSERT_TRUE(status.completed); | |
| 784 ASSERT_FALSE(status.called_after_completed); | |
| 785 ASSERT_EQ(3u, status.entries.size()); | |
| 786 | |
| 787 seen.clear(); | |
| 788 | |
| 789 for (FileUtilAsync::FileList::const_iterator it = status.entries.begin(); | |
| 790 it != status.entries.end(); | |
| 791 ++it) { | |
| 792 ASSERT_EQ(seen.end(), seen.find(it->name)); | |
| 793 seen.insert(it->name); | |
| 794 | |
| 795 if (it->name == "a") { | |
| 796 ASSERT_FALSE(it->is_directory); | |
| 797 ASSERT_EQ(0, it->size); | |
| 798 } else if (it->name == "c") { | |
| 799 ASSERT_TRUE(it->is_directory); | |
| 800 } else if (it->name == "longer_file_name.txt") { | |
| 801 ASSERT_FALSE(it->is_directory); | |
| 802 ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); | |
| 803 } else { | |
| 804 LOG(ERROR) << "Unexpected file: " << it->name; | |
| 805 ASSERT_TRUE(false); | |
| 806 } | |
| 807 } | |
| 808 } | |
| 809 | |
| 810 } // namespace fileapi | |
| OLD | NEW |