| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/gdata/gdata_directory_service.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/sequenced_task_runner.h" | |
| 13 #include "base/string_number_conversions.h" | |
| 14 #include "base/threading/sequenced_worker_pool.h" | |
| 15 #include "base/message_loop.h" | |
| 16 #include "chrome/browser/chromeos/gdata/drive.pb.h" | |
| 17 #include "chrome/browser/chromeos/gdata/drive_files.h" | |
| 18 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | |
| 19 #include "chrome/browser/chromeos/gdata/gdata_test_util.h" | |
| 20 #include "chrome/test/base/testing_profile.h" | |
| 21 #include "content/public/test/test_browser_thread.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 namespace gdata { | |
| 25 namespace { | |
| 26 | |
| 27 // See drive.proto for the difference between the two URLs. | |
| 28 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; | |
| 29 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; | |
| 30 | |
| 31 // Add a directory to |parent| and return that directory. The name and | |
| 32 // resource_id are determined by the incrementing counter |sequence_id|. | |
| 33 DriveDirectory* AddDirectory(DriveDirectory* parent, | |
| 34 GDataDirectoryService* directory_service, | |
| 35 int sequence_id) { | |
| 36 DriveDirectory* dir = directory_service->CreateDriveDirectory(); | |
| 37 const std::string dir_name = "dir" + base::IntToString(sequence_id); | |
| 38 const std::string resource_id = std::string("dir_resource_id:") + | |
| 39 dir_name; | |
| 40 dir->set_title(dir_name); | |
| 41 dir->set_resource_id(resource_id); | |
| 42 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 43 FilePath moved_file_path; | |
| 44 directory_service->MoveEntryToDirectory( | |
| 45 parent->GetFilePath(), | |
| 46 dir, | |
| 47 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | |
| 48 &error, | |
| 49 &moved_file_path)); | |
| 50 test_util::RunBlockingPoolTask(); | |
| 51 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 52 EXPECT_EQ(parent->GetFilePath().AppendASCII(dir_name), moved_file_path); | |
| 53 return dir; | |
| 54 } | |
| 55 | |
| 56 // Add a file to |parent| and return that file. The name and | |
| 57 // resource_id are determined by the incrementing counter |sequence_id|. | |
| 58 DriveFile* AddFile(DriveDirectory* parent, | |
| 59 GDataDirectoryService* directory_service, | |
| 60 int sequence_id) { | |
| 61 DriveFile* file = directory_service->CreateDriveFile(); | |
| 62 const std::string title = "file" + base::IntToString(sequence_id); | |
| 63 const std::string resource_id = std::string("file_resource_id:") + | |
| 64 title; | |
| 65 file->set_title(title); | |
| 66 file->set_resource_id(resource_id); | |
| 67 file->set_file_md5(std::string("file_md5:") + title); | |
| 68 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 69 FilePath moved_file_path; | |
| 70 directory_service->MoveEntryToDirectory( | |
| 71 parent->GetFilePath(), | |
| 72 file, | |
| 73 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | |
| 74 &error, | |
| 75 &moved_file_path)); | |
| 76 test_util::RunBlockingPoolTask(); | |
| 77 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 78 EXPECT_EQ(parent->GetFilePath().AppendASCII(title), moved_file_path); | |
| 79 return file; | |
| 80 } | |
| 81 | |
| 82 // Creates the following files/directories | |
| 83 // drive/dir1/ | |
| 84 // drive/dir2/ | |
| 85 // drive/dir1/dir3/ | |
| 86 // drive/dir1/file4 | |
| 87 // drive/dir1/file5 | |
| 88 // drive/dir2/file6 | |
| 89 // drive/dir2/file7 | |
| 90 // drive/dir2/file8 | |
| 91 // drive/dir1/dir3/file9 | |
| 92 // drive/dir1/dir3/file10 | |
| 93 void InitDirectoryService(GDataDirectoryService* directory_service) { | |
| 94 int sequence_id = 1; | |
| 95 DriveDirectory* dir1 = AddDirectory(directory_service->root(), | |
| 96 directory_service, sequence_id++); | |
| 97 DriveDirectory* dir2 = AddDirectory(directory_service->root(), | |
| 98 directory_service, sequence_id++); | |
| 99 DriveDirectory* dir3 = AddDirectory(dir1, directory_service, sequence_id++); | |
| 100 | |
| 101 AddFile(dir1, directory_service, sequence_id++); | |
| 102 AddFile(dir1, directory_service, sequence_id++); | |
| 103 | |
| 104 AddFile(dir2, directory_service, sequence_id++); | |
| 105 AddFile(dir2, directory_service, sequence_id++); | |
| 106 AddFile(dir2, directory_service, sequence_id++); | |
| 107 | |
| 108 AddFile(dir3, directory_service, sequence_id++); | |
| 109 AddFile(dir3, directory_service, sequence_id++); | |
| 110 } | |
| 111 | |
| 112 // Find directory by path. | |
| 113 DriveDirectory* FindDirectory(GDataDirectoryService* directory_service, | |
| 114 const char* path) { | |
| 115 return directory_service->FindEntryByPathSync( | |
| 116 FilePath(path))->AsDriveDirectory(); | |
| 117 } | |
| 118 | |
| 119 // Find file by path. | |
| 120 DriveFile* FindFile(GDataDirectoryService* directory_service, | |
| 121 const char* path) { | |
| 122 return directory_service->FindEntryByPathSync(FilePath(path))->AsDriveFile(); | |
| 123 } | |
| 124 | |
| 125 // Verify that the recreated directory service matches what we created in | |
| 126 // InitDirectoryService. | |
| 127 void VerifyDirectoryService(GDataDirectoryService* directory_service) { | |
| 128 ASSERT_TRUE(directory_service->root()); | |
| 129 | |
| 130 DriveDirectory* dir1 = FindDirectory(directory_service, "drive/dir1"); | |
| 131 ASSERT_TRUE(dir1); | |
| 132 DriveDirectory* dir2 = FindDirectory(directory_service, "drive/dir2"); | |
| 133 ASSERT_TRUE(dir2); | |
| 134 DriveDirectory* dir3 = FindDirectory(directory_service, "drive/dir1/dir3"); | |
| 135 ASSERT_TRUE(dir3); | |
| 136 | |
| 137 DriveFile* file4 = FindFile(directory_service, "drive/dir1/file4"); | |
| 138 ASSERT_TRUE(file4); | |
| 139 EXPECT_EQ(file4->parent(), dir1); | |
| 140 | |
| 141 DriveFile* file5 = FindFile(directory_service, "drive/dir1/file5"); | |
| 142 ASSERT_TRUE(file5); | |
| 143 EXPECT_EQ(file5->parent(), dir1); | |
| 144 | |
| 145 DriveFile* file6 = FindFile(directory_service, "drive/dir2/file6"); | |
| 146 ASSERT_TRUE(file6); | |
| 147 EXPECT_EQ(file6->parent(), dir2); | |
| 148 | |
| 149 DriveFile* file7 = FindFile(directory_service, "drive/dir2/file7"); | |
| 150 ASSERT_TRUE(file7); | |
| 151 EXPECT_EQ(file7->parent(), dir2); | |
| 152 | |
| 153 DriveFile* file8 = FindFile(directory_service, "drive/dir2/file8"); | |
| 154 ASSERT_TRUE(file8); | |
| 155 EXPECT_EQ(file8->parent(), dir2); | |
| 156 | |
| 157 DriveFile* file9 = FindFile(directory_service, "drive/dir1/dir3/file9"); | |
| 158 ASSERT_TRUE(file9); | |
| 159 EXPECT_EQ(file9->parent(), dir3); | |
| 160 | |
| 161 DriveFile* file10 = FindFile(directory_service, "drive/dir1/dir3/file10"); | |
| 162 ASSERT_TRUE(file10); | |
| 163 EXPECT_EQ(file10->parent(), dir3); | |
| 164 | |
| 165 EXPECT_EQ(dir1, directory_service->GetEntryByResourceId( | |
| 166 "dir_resource_id:dir1")); | |
| 167 EXPECT_EQ(dir2, directory_service->GetEntryByResourceId( | |
| 168 "dir_resource_id:dir2")); | |
| 169 EXPECT_EQ(dir3, directory_service->GetEntryByResourceId( | |
| 170 "dir_resource_id:dir3")); | |
| 171 EXPECT_EQ(file4, directory_service->GetEntryByResourceId( | |
| 172 "file_resource_id:file4")); | |
| 173 EXPECT_EQ(file5, directory_service->GetEntryByResourceId( | |
| 174 "file_resource_id:file5")); | |
| 175 EXPECT_EQ(file6, directory_service->GetEntryByResourceId( | |
| 176 "file_resource_id:file6")); | |
| 177 EXPECT_EQ(file7, directory_service->GetEntryByResourceId( | |
| 178 "file_resource_id:file7")); | |
| 179 EXPECT_EQ(file8, directory_service->GetEntryByResourceId( | |
| 180 "file_resource_id:file8")); | |
| 181 EXPECT_EQ(file9, directory_service->GetEntryByResourceId( | |
| 182 "file_resource_id:file9")); | |
| 183 EXPECT_EQ(file10, directory_service->GetEntryByResourceId( | |
| 184 "file_resource_id:file10")); | |
| 185 } | |
| 186 | |
| 187 // Callback for GDataDirectoryService::InitFromDB. | |
| 188 void InitFromDBCallback(GDataFileError expected_error, | |
| 189 GDataFileError actual_error) { | |
| 190 EXPECT_EQ(expected_error, actual_error); | |
| 191 } | |
| 192 | |
| 193 // Callback for GDataDirectoryService::ReadDirectoryByPath. | |
| 194 void ReadDirectoryByPathCallback( | |
| 195 scoped_ptr<DriveEntryProtoVector>* result, | |
| 196 GDataFileError error, | |
| 197 scoped_ptr<DriveEntryProtoVector> entries) { | |
| 198 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 199 *result = entries.Pass(); | |
| 200 } | |
| 201 | |
| 202 } // namespace | |
| 203 | |
| 204 TEST(GDataDirectoryServiceTest, VersionCheck) { | |
| 205 // Set up the root directory. | |
| 206 DriveRootDirectoryProto proto; | |
| 207 DriveEntryProto* mutable_entry = | |
| 208 proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 209 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 210 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 211 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 212 mutable_entry->set_title("drive"); | |
| 213 | |
| 214 GDataDirectoryService directory_service; | |
| 215 | |
| 216 std::string serialized_proto; | |
| 217 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 218 // This should fail as the version is emtpy. | |
| 219 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 220 | |
| 221 // Set an older version, and serialize. | |
| 222 proto.set_version(kProtoVersion - 1); | |
| 223 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 224 // This should fail as the version is older. | |
| 225 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 226 | |
| 227 // Set the current version, and serialize. | |
| 228 proto.set_version(kProtoVersion); | |
| 229 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 230 // This should succeed as the version matches the current number. | |
| 231 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 232 | |
| 233 // Set a newer version, and serialize. | |
| 234 proto.set_version(kProtoVersion + 1); | |
| 235 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 236 // This should fail as the version is newer. | |
| 237 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 238 } | |
| 239 | |
| 240 TEST(GDataDirectoryServiceTest, RefreshFile) { | |
| 241 MessageLoopForUI message_loop; | |
| 242 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 243 &message_loop); | |
| 244 | |
| 245 GDataDirectoryService directory_service; | |
| 246 // Add a directory to the file system. | |
| 247 DriveDirectory* directory_entry = directory_service.CreateDriveDirectory(); | |
| 248 directory_entry->set_resource_id("folder:directory_resource_id"); | |
| 249 directory_entry->set_title("directory"); | |
| 250 directory_entry->SetBaseNameFromTitle(); | |
| 251 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 252 FilePath moved_file_path; | |
| 253 FilePath root_path(kGDataRootDirectory); | |
| 254 directory_service.MoveEntryToDirectory( | |
| 255 root_path, | |
| 256 directory_entry, | |
| 257 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | |
| 258 &error, | |
| 259 &moved_file_path)); | |
| 260 test_util::RunBlockingPoolTask(); | |
| 261 ASSERT_EQ(GDATA_FILE_OK, error); | |
| 262 EXPECT_EQ(root_path.AppendASCII(directory_entry->base_name()), | |
| 263 moved_file_path); | |
| 264 | |
| 265 // Add a new file to the directory. | |
| 266 DriveFile* initial_file_entry = directory_service.CreateDriveFile(); | |
| 267 initial_file_entry->set_resource_id("file:file_resource_id"); | |
| 268 initial_file_entry->set_title("file"); | |
| 269 initial_file_entry->SetBaseNameFromTitle(); | |
| 270 error = GDATA_FILE_ERROR_FAILED; | |
| 271 moved_file_path.clear(); | |
| 272 directory_service.MoveEntryToDirectory( | |
| 273 directory_entry->GetFilePath(), | |
| 274 initial_file_entry, | |
| 275 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | |
| 276 &error, | |
| 277 &moved_file_path)); | |
| 278 test_util::RunBlockingPoolTask(); | |
| 279 ASSERT_EQ(GDATA_FILE_OK, error); | |
| 280 EXPECT_EQ(directory_entry->GetFilePath().AppendASCII( | |
| 281 initial_file_entry->base_name()), moved_file_path); | |
| 282 | |
| 283 ASSERT_EQ(directory_entry, initial_file_entry->parent()); | |
| 284 | |
| 285 // Initial file system state set, let's try refreshing entries. | |
| 286 | |
| 287 // New value for the entry with resource id "file:file_resource_id". | |
| 288 DriveFile* new_file_entry = directory_service.CreateDriveFile(); | |
| 289 new_file_entry->set_resource_id("file:file_resource_id"); | |
| 290 directory_service.RefreshFile(scoped_ptr<DriveFile>(new_file_entry).Pass()); | |
| 291 // Root should have |new_file_entry|, not |initial_file_entry|. | |
| 292 // If this is not true, |new_file_entry| has probably been destroyed, hence | |
| 293 // ASSERT (we're trying to access |new_file_entry| later on). | |
| 294 ASSERT_EQ(new_file_entry, | |
| 295 directory_service.GetEntryByResourceId("file:file_resource_id")); | |
| 296 // We have just verified new_file_entry exists inside root, so accessing | |
| 297 // |new_file_entry->parent()| should be safe. | |
| 298 EXPECT_EQ(directory_entry, new_file_entry->parent()); | |
| 299 | |
| 300 // Let's try refreshing file that didn't prviously exist. | |
| 301 DriveFile* non_existent_entry = directory_service.CreateDriveFile(); | |
| 302 non_existent_entry->set_resource_id("file:does_not_exist"); | |
| 303 directory_service.RefreshFile( | |
| 304 scoped_ptr<DriveFile>(non_existent_entry).Pass()); | |
| 305 // File with non existent resource id should not be added. | |
| 306 EXPECT_FALSE(directory_service.GetEntryByResourceId("file:does_not_exist")); | |
| 307 } | |
| 308 | |
| 309 TEST(GDataDirectoryServiceTest, GetEntryByResourceId_RootDirectory) { | |
| 310 GDataDirectoryService directory_service; | |
| 311 // Look up the root directory by its resource ID. | |
| 312 DriveEntry* entry = directory_service.GetEntryByResourceId( | |
| 313 kGDataRootDirectoryResourceId); | |
| 314 ASSERT_TRUE(entry); | |
| 315 EXPECT_EQ(kGDataRootDirectoryResourceId, entry->resource_id()); | |
| 316 } | |
| 317 | |
| 318 TEST(GDataDirectoryServiceTest, GetEntryInfoByResourceId) { | |
| 319 MessageLoopForUI message_loop; | |
| 320 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 321 &message_loop); | |
| 322 GDataDirectoryService directory_service; | |
| 323 InitDirectoryService(&directory_service); | |
| 324 | |
| 325 // Confirm that an existing file is found. | |
| 326 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 327 FilePath drive_file_path; | |
| 328 scoped_ptr<DriveEntryProto> entry_proto; | |
| 329 directory_service.GetEntryInfoByResourceId( | |
| 330 "file_resource_id:file4", | |
| 331 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, | |
| 332 &error, &drive_file_path, &entry_proto)); | |
| 333 test_util::RunBlockingPoolTask(); | |
| 334 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 335 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"), drive_file_path); | |
| 336 ASSERT_TRUE(entry_proto.get()); | |
| 337 EXPECT_EQ("file4", entry_proto->base_name()); | |
| 338 | |
| 339 // Confirm that a non existing file is not found. | |
| 340 error = GDATA_FILE_ERROR_FAILED; | |
| 341 entry_proto.reset(); | |
| 342 directory_service.GetEntryInfoByResourceId( | |
| 343 "file:non_existing", | |
| 344 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, | |
| 345 &error, &drive_file_path, &entry_proto)); | |
| 346 test_util::RunBlockingPoolTask(); | |
| 347 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error); | |
| 348 EXPECT_FALSE(entry_proto.get()); | |
| 349 } | |
| 350 | |
| 351 TEST(GDataDirectoryServiceTest, GetEntryInfoByPath) { | |
| 352 MessageLoopForUI message_loop; | |
| 353 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 354 &message_loop); | |
| 355 GDataDirectoryService directory_service; | |
| 356 InitDirectoryService(&directory_service); | |
| 357 | |
| 358 // Confirm that an existing file is found. | |
| 359 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 360 scoped_ptr<DriveEntryProto> entry_proto; | |
| 361 directory_service.GetEntryInfoByPath( | |
| 362 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 363 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, | |
| 364 &error, &entry_proto)); | |
| 365 test_util::RunBlockingPoolTask(); | |
| 366 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 367 ASSERT_TRUE(entry_proto.get()); | |
| 368 EXPECT_EQ("file4", entry_proto->base_name()); | |
| 369 | |
| 370 // Confirm that a non existing file is not found. | |
| 371 error = GDATA_FILE_ERROR_FAILED; | |
| 372 entry_proto.reset(); | |
| 373 directory_service.GetEntryInfoByPath( | |
| 374 FilePath::FromUTF8Unsafe("drive/dir1/non_existing"), | |
| 375 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, | |
| 376 &error, &entry_proto)); | |
| 377 test_util::RunBlockingPoolTask(); | |
| 378 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error); | |
| 379 EXPECT_FALSE(entry_proto.get()); | |
| 380 } | |
| 381 | |
| 382 TEST(GDataDirectoryServiceTest, ReadDirectoryByPath) { | |
| 383 MessageLoopForUI message_loop; | |
| 384 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 385 &message_loop); | |
| 386 GDataDirectoryService directory_service; | |
| 387 InitDirectoryService(&directory_service); | |
| 388 | |
| 389 // Confirm that an existing directory is found. | |
| 390 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 391 scoped_ptr<DriveEntryProtoVector> entries; | |
| 392 directory_service.ReadDirectoryByPath( | |
| 393 FilePath::FromUTF8Unsafe("drive/dir1"), | |
| 394 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, | |
| 395 &error, &entries)); | |
| 396 test_util::RunBlockingPoolTask(); | |
| 397 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 398 ASSERT_TRUE(entries.get()); | |
| 399 ASSERT_EQ(3U, entries->size()); | |
| 400 | |
| 401 // The order is not guaranteed so we should sort the base names. | |
| 402 std::vector<std::string> base_names; | |
| 403 for (size_t i = 0; i < 3; ++i) | |
| 404 base_names.push_back(entries->at(i).base_name()); | |
| 405 std::sort(base_names.begin(), base_names.end()); | |
| 406 | |
| 407 EXPECT_EQ("dir3", base_names[0]); | |
| 408 EXPECT_EQ("file4", base_names[1]); | |
| 409 EXPECT_EQ("file5", base_names[2]); | |
| 410 | |
| 411 // Confirm that a non existing directory is not found. | |
| 412 error = GDATA_FILE_ERROR_FAILED; | |
| 413 entries.reset(); | |
| 414 directory_service.ReadDirectoryByPath( | |
| 415 FilePath::FromUTF8Unsafe("drive/non_existing"), | |
| 416 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, | |
| 417 &error, &entries)); | |
| 418 test_util::RunBlockingPoolTask(); | |
| 419 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error); | |
| 420 EXPECT_FALSE(entries.get()); | |
| 421 | |
| 422 // Confirm that reading a file results in GDATA_FILE_ERROR_NOT_A_DIRECTORY. | |
| 423 error = GDATA_FILE_ERROR_FAILED; | |
| 424 entries.reset(); | |
| 425 directory_service.ReadDirectoryByPath( | |
| 426 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 427 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, | |
| 428 &error, &entries)); | |
| 429 test_util::RunBlockingPoolTask(); | |
| 430 EXPECT_EQ(GDATA_FILE_ERROR_NOT_A_DIRECTORY, error); | |
| 431 EXPECT_FALSE(entries.get()); | |
| 432 } | |
| 433 | |
| 434 TEST(GDataDirectoryServiceTest, GetEntryInfoPairByPaths) { | |
| 435 MessageLoopForUI message_loop; | |
| 436 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 437 &message_loop); | |
| 438 GDataDirectoryService directory_service; | |
| 439 InitDirectoryService(&directory_service); | |
| 440 | |
| 441 // Confirm that existing two files are found. | |
| 442 scoped_ptr<EntryInfoPairResult> pair_result; | |
| 443 directory_service.GetEntryInfoPairByPaths( | |
| 444 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 445 FilePath::FromUTF8Unsafe("drive/dir1/file5"), | |
| 446 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, | |
| 447 &pair_result)); | |
| 448 test_util::RunBlockingPoolTask(); | |
| 449 // The first entry should be found. | |
| 450 EXPECT_EQ(GDATA_FILE_OK, pair_result->first.error); | |
| 451 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 452 pair_result->first.path); | |
| 453 ASSERT_TRUE(pair_result->first.proto.get()); | |
| 454 EXPECT_EQ("file4", pair_result->first.proto->base_name()); | |
| 455 // The second entry should be found. | |
| 456 EXPECT_EQ(GDATA_FILE_OK, pair_result->second.error); | |
| 457 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file5"), | |
| 458 pair_result->second.path); | |
| 459 ASSERT_TRUE(pair_result->second.proto.get()); | |
| 460 EXPECT_EQ("file5", pair_result->second.proto->base_name()); | |
| 461 | |
| 462 // Confirm that the first non existent file is not found. | |
| 463 pair_result.reset(); | |
| 464 directory_service.GetEntryInfoPairByPaths( | |
| 465 FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 466 FilePath::FromUTF8Unsafe("drive/dir1/file5"), | |
| 467 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, | |
| 468 &pair_result)); | |
| 469 test_util::RunBlockingPoolTask(); | |
| 470 // The first entry should not be found. | |
| 471 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, pair_result->first.error); | |
| 472 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 473 pair_result->first.path); | |
| 474 ASSERT_FALSE(pair_result->first.proto.get()); | |
| 475 // The second entry should not be found, because the first one failed. | |
| 476 EXPECT_EQ(GDATA_FILE_ERROR_FAILED, pair_result->second.error); | |
| 477 EXPECT_EQ(FilePath(), pair_result->second.path); | |
| 478 ASSERT_FALSE(pair_result->second.proto.get()); | |
| 479 | |
| 480 // Confirm that the second non existent file is not found. | |
| 481 pair_result.reset(); | |
| 482 directory_service.GetEntryInfoPairByPaths( | |
| 483 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 484 FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 485 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, | |
| 486 &pair_result)); | |
| 487 test_util::RunBlockingPoolTask(); | |
| 488 // The first entry should be found. | |
| 489 EXPECT_EQ(GDATA_FILE_OK, pair_result->first.error); | |
| 490 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 491 pair_result->first.path); | |
| 492 ASSERT_TRUE(pair_result->first.proto.get()); | |
| 493 EXPECT_EQ("file4", pair_result->first.proto->base_name()); | |
| 494 // The second entry should not be found. | |
| 495 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, pair_result->second.error); | |
| 496 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 497 pair_result->second.path); | |
| 498 ASSERT_FALSE(pair_result->second.proto.get()); | |
| 499 } | |
| 500 | |
| 501 TEST(GDataDirectoryServiceTest, DBTest) { | |
| 502 MessageLoopForUI message_loop; | |
| 503 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 504 &message_loop); | |
| 505 | |
| 506 scoped_ptr<TestingProfile> profile(new TestingProfile); | |
| 507 scoped_refptr<base::SequencedWorkerPool> pool = | |
| 508 content::BrowserThread::GetBlockingPool(); | |
| 509 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = | |
| 510 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); | |
| 511 | |
| 512 GDataDirectoryService directory_service; | |
| 513 FilePath db_path(GDataCache::GetCacheRootPath(profile.get()). | |
| 514 AppendASCII("meta").AppendASCII("resource_metadata.db")); | |
| 515 // InitFromDB should fail with GDATA_FILE_ERROR_NOT_FOUND since the db | |
| 516 // doesn't exist. | |
| 517 directory_service.InitFromDB(db_path, blocking_task_runner, | |
| 518 base::Bind(&InitFromDBCallback, GDATA_FILE_ERROR_NOT_FOUND)); | |
| 519 test_util::RunBlockingPoolTask(); | |
| 520 InitDirectoryService(&directory_service); | |
| 521 | |
| 522 // Write the filesystem to db. | |
| 523 directory_service.SaveToDB(); | |
| 524 test_util::RunBlockingPoolTask(); | |
| 525 | |
| 526 GDataDirectoryService directory_service2; | |
| 527 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. | |
| 528 directory_service2.InitFromDB(db_path, blocking_task_runner, | |
| 529 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); | |
| 530 test_util::RunBlockingPoolTask(); | |
| 531 | |
| 532 VerifyDirectoryService(&directory_service2); | |
| 533 } | |
| 534 | |
| 535 } // namespace gdata | |
| OLD | NEW |