| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
| 6 | 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" | 7 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
| 17 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 8 #include "chrome/browser/chromeos/gdata/gdata_directory_service.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" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 10 |
| 23 namespace gdata { | 11 namespace gdata { |
| 24 namespace { | 12 namespace { |
| 25 | 13 |
| 26 // See gdata.proto for the difference between the two URLs. | |
| 27 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; | 14 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; |
| 28 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; | |
| 29 | 15 |
| 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 } | 16 } |
| 54 | 17 |
| 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(GDataEntryTest, FromProto_DetectBadUploadUrl) { | 18 TEST(GDataEntryTest, FromProto_DetectBadUploadUrl) { |
| 195 GDataEntryProto proto; | 19 GDataEntryProto proto; |
| 196 proto.set_title("test.txt"); | 20 proto.set_title("test.txt"); |
| 197 | 21 |
| 198 GDataDirectoryService directory_service; | 22 GDataDirectoryService directory_service; |
| 199 | 23 |
| 200 scoped_ptr<GDataEntry> entry(directory_service.CreateGDataFile()); | 24 scoped_ptr<GDataEntry> entry(directory_service.CreateGDataFile()); |
| 201 // This should fail as the upload URL is empty. | 25 // This should fail as the upload URL is empty. |
| 202 ASSERT_FALSE(entry->FromProto(proto)); | 26 ASSERT_FALSE(entry->FromProto(proto)); |
| 203 | 27 |
| 204 // Set a upload URL. | 28 // Set a upload URL. |
| 205 proto.set_upload_url(kResumableEditMediaUrl); | 29 proto.set_upload_url(kResumableEditMediaUrl); |
| 206 | 30 |
| 207 // This should succeed as the upload URL is set. | 31 // This should succeed as the upload URL is set. |
| 208 ASSERT_TRUE(entry->FromProto(proto)); | 32 ASSERT_TRUE(entry->FromProto(proto)); |
| 209 EXPECT_EQ(kResumableEditMediaUrl, entry->upload_url().spec()); | 33 EXPECT_EQ(kResumableEditMediaUrl, entry->upload_url().spec()); |
| 210 } | 34 } |
| 211 | 35 |
| 212 TEST(GDataDirectoryServiceTest, VersionCheck) { | |
| 213 // Set up the root directory. | |
| 214 GDataRootDirectoryProto proto; | |
| 215 GDataEntryProto* mutable_entry = | |
| 216 proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 217 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 218 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 219 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 220 mutable_entry->set_title("drive"); | |
| 221 | |
| 222 GDataDirectoryService directory_service; | |
| 223 | |
| 224 std::string serialized_proto; | |
| 225 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 226 // This should fail as the version is emtpy. | |
| 227 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 228 | |
| 229 // Set an older version, and serialize. | |
| 230 proto.set_version(kProtoVersion - 1); | |
| 231 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 232 // This should fail as the version is older. | |
| 233 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 234 | |
| 235 // Set the current version, and serialize. | |
| 236 proto.set_version(kProtoVersion); | |
| 237 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 238 // This should succeed as the version matches the current number. | |
| 239 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 240 | |
| 241 // Set a newer version, and serialize. | |
| 242 proto.set_version(kProtoVersion + 1); | |
| 243 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 244 // This should fail as the version is newer. | |
| 245 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 246 } | |
| 247 | |
| 248 TEST(GDataDirectoryServiceTest, ParseFromString_DetectBadTitle) { | |
| 249 GDataRootDirectoryProto proto; | |
| 250 proto.set_version(kProtoVersion); | |
| 251 | |
| 252 GDataEntryProto* mutable_entry = | |
| 253 proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 254 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 255 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 256 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 257 | |
| 258 std::string serialized_proto; | |
| 259 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 260 | |
| 261 GDataDirectoryService directory_service; | |
| 262 GDataDirectory* root(directory_service.root()); | |
| 263 // This should fail as the title is empty. | |
| 264 // root.title() should be unchanged. | |
| 265 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 266 ASSERT_EQ(kGDataRootDirectory, root->title()); | |
| 267 | |
| 268 // Setting the title to "gdata". | |
| 269 mutable_entry->set_title("gdata"); | |
| 270 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 271 | |
| 272 // This should fail as the title is not kGDataRootDirectory. | |
| 273 // root.title() should be unchanged. | |
| 274 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 275 ASSERT_EQ(kGDataRootDirectory, root->title()); | |
| 276 | |
| 277 // Setting the title to kGDataRootDirectory. | |
| 278 mutable_entry->set_title(kGDataRootDirectory); | |
| 279 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 280 | |
| 281 // This should succeed as the title is kGDataRootDirectory. | |
| 282 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 283 ASSERT_EQ(kGDataRootDirectory, root->title()); | |
| 284 } | |
| 285 | |
| 286 TEST(GDataDirectoryServiceTest, ParseFromString_DetectBadResourceID) { | |
| 287 GDataRootDirectoryProto proto; | |
| 288 proto.set_version(kProtoVersion); | |
| 289 | |
| 290 GDataEntryProto* mutable_entry = | |
| 291 proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 292 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 293 mutable_entry->set_title(kGDataRootDirectory); | |
| 294 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 295 | |
| 296 std::string serialized_proto; | |
| 297 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 298 | |
| 299 GDataDirectoryService directory_service; | |
| 300 GDataDirectory* root(directory_service.root()); | |
| 301 // This should fail as the resource ID is empty. | |
| 302 // root.resource_id() should be unchanged. | |
| 303 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 304 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); | |
| 305 | |
| 306 // Set the correct resource ID. | |
| 307 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 308 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 309 | |
| 310 // This should succeed as the resource ID is correct. | |
| 311 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 312 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); | |
| 313 } | |
| 314 | |
| 315 // We have a similar test in FromProto_DetectBadUploadUrl, but the test here | |
| 316 // is to ensure that an error in GDataFile::FromProto() is properly | |
| 317 // propagated to GDataRootDirectory::ParseFromString(). | |
| 318 TEST(GDataDirectoryServiceTest, ParseFromString_DetectNoUploadUrl) { | |
| 319 // Set up the root directory properly. | |
| 320 GDataRootDirectoryProto root_directory_proto; | |
| 321 root_directory_proto.set_version(kProtoVersion); | |
| 322 | |
| 323 GDataEntryProto* mutable_entry = | |
| 324 root_directory_proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 325 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 326 mutable_entry->set_title(kGDataRootDirectory); | |
| 327 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 328 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 329 | |
| 330 // Add an empty sub directory under the root directory. This directory is | |
| 331 // added to ensure that nothing is left when the parsing failed. | |
| 332 GDataDirectoryProto* sub_directory_proto = | |
| 333 root_directory_proto.mutable_gdata_directory()->add_child_directories(); | |
| 334 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> | |
| 335 set_is_directory(true); | |
| 336 sub_directory_proto->mutable_gdata_entry()->set_title("empty"); | |
| 337 sub_directory_proto->mutable_gdata_entry()->set_upload_url( | |
| 338 kResumableCreateMediaUrl); | |
| 339 | |
| 340 // Add a sub directory under the root directory. | |
| 341 sub_directory_proto = | |
| 342 root_directory_proto.mutable_gdata_directory()->add_child_directories(); | |
| 343 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> | |
| 344 set_is_directory(true); | |
| 345 sub_directory_proto->mutable_gdata_entry()->set_title("dir"); | |
| 346 sub_directory_proto->mutable_gdata_entry()->set_upload_url( | |
| 347 kResumableCreateMediaUrl); | |
| 348 | |
| 349 // Add a new file under the sub directory "dir". | |
| 350 GDataEntryProto* entry_proto = | |
| 351 sub_directory_proto->add_child_files(); | |
| 352 entry_proto->set_title("test.txt"); | |
| 353 entry_proto->mutable_file_specific_info()->set_file_md5("md5"); | |
| 354 | |
| 355 GDataDirectoryService directory_service; | |
| 356 GDataDirectory* root(directory_service.root()); | |
| 357 // The origin is set to UNINITIALIZED by default. | |
| 358 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); | |
| 359 std::string serialized_proto; | |
| 360 // Serialize the proto and check if it's loaded. | |
| 361 // This should fail as the upload URL is not set for |entry_proto|. | |
| 362 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); | |
| 363 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 364 // Nothing should be added to the root directory if the parse failed. | |
| 365 ASSERT_TRUE(root->child_files().empty()); | |
| 366 ASSERT_TRUE(root->child_directories().empty()); | |
| 367 // The origin should remain UNINITIALIZED because the loading failed. | |
| 368 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); | |
| 369 | |
| 370 // Set an upload URL. | |
| 371 entry_proto->set_upload_url(kResumableEditMediaUrl); | |
| 372 | |
| 373 // Serialize the proto and check if it's loaded. | |
| 374 // This should succeed as the upload URL is set for |entry_proto|. | |
| 375 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); | |
| 376 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 377 // No file should be added to the root directory. | |
| 378 ASSERT_TRUE(root->child_files().empty()); | |
| 379 // Two directories ("empty", "dir") should be added to the root directory. | |
| 380 ASSERT_EQ(2U, root->child_directories().size()); | |
| 381 // The origin should change to FROM_CACHE because we loaded from the cache. | |
| 382 ASSERT_EQ(FROM_CACHE, directory_service.origin()); | |
| 383 } | |
| 384 | |
| 385 TEST(GDataDirectoryServiceTest, RefreshFile) { | |
| 386 MessageLoopForUI message_loop; | |
| 387 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 388 &message_loop); | |
| 389 | |
| 390 GDataDirectoryService directory_service; | |
| 391 // Add a directory to the file system. | |
| 392 GDataDirectory* directory_entry = directory_service.CreateGDataDirectory(); | |
| 393 directory_entry->set_resource_id("folder:directory_resource_id"); | |
| 394 directory_entry->set_title("directory"); | |
| 395 directory_entry->SetBaseNameFromTitle(); | |
| 396 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 397 FilePath moved_file_path; | |
| 398 FilePath root_path(kGDataRootDirectory); | |
| 399 directory_service.MoveEntryToDirectory( | |
| 400 root_path, | |
| 401 directory_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(root_path.AppendASCII(directory_entry->base_name()), | |
| 408 moved_file_path); | |
| 409 | |
| 410 // Add a new file to the directory. | |
| 411 GDataFile* initial_file_entry = directory_service.CreateGDataFile(); | |
| 412 initial_file_entry->set_resource_id("file:file_resource_id"); | |
| 413 initial_file_entry->set_title("file"); | |
| 414 initial_file_entry->SetBaseNameFromTitle(); | |
| 415 error = GDATA_FILE_ERROR_FAILED; | |
| 416 moved_file_path.clear(); | |
| 417 directory_service.MoveEntryToDirectory( | |
| 418 directory_entry->GetFilePath(), | |
| 419 initial_file_entry, | |
| 420 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | |
| 421 &error, | |
| 422 &moved_file_path)); | |
| 423 test_util::RunBlockingPoolTask(); | |
| 424 ASSERT_EQ(GDATA_FILE_OK, error); | |
| 425 EXPECT_EQ(directory_entry->GetFilePath().AppendASCII( | |
| 426 initial_file_entry->base_name()), moved_file_path); | |
| 427 | |
| 428 ASSERT_EQ(directory_entry, initial_file_entry->parent()); | |
| 429 | |
| 430 // Initial file system state set, let's try refreshing entries. | |
| 431 | |
| 432 // New value for the entry with resource id "file:file_resource_id". | |
| 433 GDataFile* new_file_entry = directory_service.CreateGDataFile(); | |
| 434 new_file_entry->set_resource_id("file:file_resource_id"); | |
| 435 directory_service.RefreshFile(scoped_ptr<GDataFile>(new_file_entry).Pass()); | |
| 436 // Root should have |new_file_entry|, not |initial_file_entry|. | |
| 437 // If this is not true, |new_file_entry| has probably been destroyed, hence | |
| 438 // ASSERT (we're trying to access |new_file_entry| later on). | |
| 439 ASSERT_EQ(new_file_entry, | |
| 440 directory_service.GetEntryByResourceId("file:file_resource_id")); | |
| 441 // We have just verified new_file_entry exists inside root, so accessing | |
| 442 // |new_file_entry->parent()| should be safe. | |
| 443 EXPECT_EQ(directory_entry, new_file_entry->parent()); | |
| 444 | |
| 445 // Let's try refreshing file that didn't prviously exist. | |
| 446 GDataFile* non_existent_entry = directory_service.CreateGDataFile(); | |
| 447 non_existent_entry->set_resource_id("file:does_not_exist"); | |
| 448 directory_service.RefreshFile( | |
| 449 scoped_ptr<GDataFile>(non_existent_entry).Pass()); | |
| 450 // File with non existent resource id should not be added. | |
| 451 EXPECT_FALSE(directory_service.GetEntryByResourceId("file:does_not_exist")); | |
| 452 } | |
| 453 | |
| 454 TEST(GDataDirectoryServiceTest, GetEntryByResourceId_RootDirectory) { | |
| 455 GDataDirectoryService directory_service; | |
| 456 // Look up the root directory by its resource ID. | |
| 457 GDataEntry* entry = directory_service.GetEntryByResourceId( | |
| 458 kGDataRootDirectoryResourceId); | |
| 459 ASSERT_TRUE(entry); | |
| 460 EXPECT_EQ(kGDataRootDirectoryResourceId, entry->resource_id()); | |
| 461 } | |
| 462 | |
| 463 TEST(GDataDirectoryServiceTest, GetEntryInfoByPath) { | |
| 464 MessageLoopForUI message_loop; | |
| 465 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 466 &message_loop); | |
| 467 GDataDirectoryService directory_service; | |
| 468 InitDirectoryService(&directory_service); | |
| 469 | |
| 470 // Confirm that an existing file is found. | |
| 471 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 472 scoped_ptr<GDataEntryProto> entry_proto; | |
| 473 directory_service.GetEntryInfoByPath( | |
| 474 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 475 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, | |
| 476 &error, &entry_proto)); | |
| 477 test_util::RunBlockingPoolTask(); | |
| 478 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 479 ASSERT_TRUE(entry_proto.get()); | |
| 480 EXPECT_EQ("file4", entry_proto->base_name()); | |
| 481 | |
| 482 // Confirm that a non existing file is not found. | |
| 483 error = GDATA_FILE_ERROR_FAILED; | |
| 484 entry_proto.reset(); | |
| 485 directory_service.GetEntryInfoByPath( | |
| 486 FilePath::FromUTF8Unsafe("drive/dir1/non_existing"), | |
| 487 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, | |
| 488 &error, &entry_proto)); | |
| 489 test_util::RunBlockingPoolTask(); | |
| 490 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error); | |
| 491 EXPECT_FALSE(entry_proto.get()); | |
| 492 } | |
| 493 | |
| 494 TEST(GDataDirectoryServiceTest, ReadDirectoryByPath) { | |
| 495 MessageLoopForUI message_loop; | |
| 496 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 497 &message_loop); | |
| 498 GDataDirectoryService directory_service; | |
| 499 InitDirectoryService(&directory_service); | |
| 500 | |
| 501 // Confirm that an existing directory is found. | |
| 502 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
| 503 scoped_ptr<GDataEntryProtoVector> entries; | |
| 504 directory_service.ReadDirectoryByPath( | |
| 505 FilePath::FromUTF8Unsafe("drive/dir1"), | |
| 506 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, | |
| 507 &error, &entries)); | |
| 508 test_util::RunBlockingPoolTask(); | |
| 509 EXPECT_EQ(GDATA_FILE_OK, error); | |
| 510 ASSERT_TRUE(entries.get()); | |
| 511 ASSERT_EQ(3U, entries->size()); | |
| 512 | |
| 513 // The order is not guaranteed so we should sort the base names. | |
| 514 std::vector<std::string> base_names; | |
| 515 for (size_t i = 0; i < 3; ++i) | |
| 516 base_names.push_back(entries->at(i).base_name()); | |
| 517 std::sort(base_names.begin(), base_names.end()); | |
| 518 | |
| 519 EXPECT_EQ("dir3", base_names[0]); | |
| 520 EXPECT_EQ("file4", base_names[1]); | |
| 521 EXPECT_EQ("file5", base_names[2]); | |
| 522 | |
| 523 // Confirm that a non existing directory is not found. | |
| 524 error = GDATA_FILE_ERROR_FAILED; | |
| 525 entries.reset(); | |
| 526 directory_service.ReadDirectoryByPath( | |
| 527 FilePath::FromUTF8Unsafe("drive/non_existing"), | |
| 528 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, | |
| 529 &error, &entries)); | |
| 530 test_util::RunBlockingPoolTask(); | |
| 531 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error); | |
| 532 EXPECT_FALSE(entries.get()); | |
| 533 | |
| 534 // Confirm that reading a file results in GDATA_FILE_ERROR_NOT_A_DIRECTORY. | |
| 535 error = GDATA_FILE_ERROR_FAILED; | |
| 536 entries.reset(); | |
| 537 directory_service.ReadDirectoryByPath( | |
| 538 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 539 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, | |
| 540 &error, &entries)); | |
| 541 test_util::RunBlockingPoolTask(); | |
| 542 EXPECT_EQ(GDATA_FILE_ERROR_NOT_A_DIRECTORY, error); | |
| 543 EXPECT_FALSE(entries.get()); | |
| 544 } | |
| 545 | |
| 546 TEST(GDataDirectoryServiceTest, GetEntryInfoPairByPaths) { | |
| 547 MessageLoopForUI message_loop; | |
| 548 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 549 &message_loop); | |
| 550 GDataDirectoryService directory_service; | |
| 551 InitDirectoryService(&directory_service); | |
| 552 | |
| 553 // Confirm that existing two files are found. | |
| 554 scoped_ptr<EntryInfoPairResult> pair_result; | |
| 555 directory_service.GetEntryInfoPairByPaths( | |
| 556 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 557 FilePath::FromUTF8Unsafe("drive/dir1/file5"), | |
| 558 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, | |
| 559 &pair_result)); | |
| 560 test_util::RunBlockingPoolTask(); | |
| 561 // The first entry should be found. | |
| 562 EXPECT_EQ(GDATA_FILE_OK, pair_result->first.error); | |
| 563 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 564 pair_result->first.path); | |
| 565 ASSERT_TRUE(pair_result->first.proto.get()); | |
| 566 EXPECT_EQ("file4", pair_result->first.proto->base_name()); | |
| 567 // The second entry should be found. | |
| 568 EXPECT_EQ(GDATA_FILE_OK, pair_result->second.error); | |
| 569 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file5"), | |
| 570 pair_result->second.path); | |
| 571 ASSERT_TRUE(pair_result->second.proto.get()); | |
| 572 EXPECT_EQ("file5", pair_result->second.proto->base_name()); | |
| 573 | |
| 574 // Confirm that the first non existent file is not found. | |
| 575 pair_result.reset(); | |
| 576 directory_service.GetEntryInfoPairByPaths( | |
| 577 FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 578 FilePath::FromUTF8Unsafe("drive/dir1/file5"), | |
| 579 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, | |
| 580 &pair_result)); | |
| 581 test_util::RunBlockingPoolTask(); | |
| 582 // The first entry should not be found. | |
| 583 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, pair_result->first.error); | |
| 584 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 585 pair_result->first.path); | |
| 586 ASSERT_FALSE(pair_result->first.proto.get()); | |
| 587 // The second entry should not be found, because the first one failed. | |
| 588 EXPECT_EQ(GDATA_FILE_ERROR_FAILED, pair_result->second.error); | |
| 589 EXPECT_EQ(FilePath(), pair_result->second.path); | |
| 590 ASSERT_FALSE(pair_result->second.proto.get()); | |
| 591 | |
| 592 // Confirm that the second non existent file is not found. | |
| 593 pair_result.reset(); | |
| 594 directory_service.GetEntryInfoPairByPaths( | |
| 595 FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 596 FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 597 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, | |
| 598 &pair_result)); | |
| 599 test_util::RunBlockingPoolTask(); | |
| 600 // The first entry should be found. | |
| 601 EXPECT_EQ(GDATA_FILE_OK, pair_result->first.error); | |
| 602 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"), | |
| 603 pair_result->first.path); | |
| 604 ASSERT_TRUE(pair_result->first.proto.get()); | |
| 605 EXPECT_EQ("file4", pair_result->first.proto->base_name()); | |
| 606 // The second entry should not be found. | |
| 607 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, pair_result->second.error); | |
| 608 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), | |
| 609 pair_result->second.path); | |
| 610 ASSERT_FALSE(pair_result->second.proto.get()); | |
| 611 } | |
| 612 | |
| 613 TEST(GDataDirectoryServiceTest, DBTest) { | |
| 614 MessageLoopForUI message_loop; | |
| 615 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 616 &message_loop); | |
| 617 | |
| 618 scoped_ptr<TestingProfile> profile(new TestingProfile); | |
| 619 scoped_refptr<base::SequencedWorkerPool> pool = | |
| 620 content::BrowserThread::GetBlockingPool(); | |
| 621 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = | |
| 622 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); | |
| 623 | |
| 624 GDataDirectoryService directory_service; | |
| 625 FilePath db_path(GDataCache::GetCacheRootPath(profile.get()). | |
| 626 AppendASCII("meta").AppendASCII("resource_metadata.db")); | |
| 627 // InitFromDB should fail with GDATA_FILE_ERROR_NOT_FOUND since the db | |
| 628 // doesn't exist. | |
| 629 directory_service.InitFromDB(db_path, blocking_task_runner, | |
| 630 base::Bind(&InitFromDBCallback, GDATA_FILE_ERROR_NOT_FOUND)); | |
| 631 test_util::RunBlockingPoolTask(); | |
| 632 InitDirectoryService(&directory_service); | |
| 633 | |
| 634 // Write the filesystem to db. | |
| 635 directory_service.SaveToDB(); | |
| 636 test_util::RunBlockingPoolTask(); | |
| 637 | |
| 638 GDataDirectoryService directory_service2; | |
| 639 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. | |
| 640 directory_service2.InitFromDB(db_path, blocking_task_runner, | |
| 641 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); | |
| 642 test_util::RunBlockingPoolTask(); | |
| 643 | |
| 644 VerifyDirectoryService(&directory_service2); | |
| 645 } | |
| 646 | |
| 647 } // namespace gdata | 36 } // namespace gdata |
| OLD | NEW |