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