| 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/drive/drive_resource_metadata.h" | 5 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "chrome/browser/chromeos/drive/drive.pb.h" | 16 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 17 #include "chrome/browser/chromeos/drive/drive_cache.h" | 17 #include "chrome/browser/chromeos/drive/drive_cache.h" |
| 18 #include "chrome/browser/chromeos/drive/drive_files.h" | 18 #include "chrome/browser/chromeos/drive/drive_files.h" |
| 19 #include "chrome/browser/chromeos/drive/drive_test_util.h" | 19 #include "chrome/browser/chromeos/drive/drive_test_util.h" |
| 20 #include "chrome/browser/google_apis/gdata_util.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 21 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace drive { | 25 namespace drive { |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 28 // The root directory resource ID for WAPI. |
| 29 // TODO(haruki): Make Drive API equivalent work. http://crbug.com/157114 |
| 30 const char kTestRootDirectoryResourceId[] = "folder:testroot"; |
| 31 |
| 27 // See drive.proto for the difference between the two URLs. | 32 // See drive.proto for the difference between the two URLs. |
| 28 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; | 33 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; |
| 29 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; | 34 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; |
| 30 | 35 |
| 31 // Callback for DriveResourceMetadata::InitFromDB. | 36 // Callback for DriveResourceMetadata::InitFromDB. |
| 32 void InitFromDBCallback(DriveFileError expected_error, | 37 void InitFromDBCallback(DriveFileError expected_error, |
| 33 DriveFileError actual_error) { | 38 DriveFileError actual_error) { |
| 34 EXPECT_EQ(expected_error, actual_error); | 39 EXPECT_EQ(expected_error, actual_error); |
| 35 } | 40 } |
| 36 | 41 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 MessageLoopForUI message_loop_; | 82 MessageLoopForUI message_loop_; |
| 78 content::TestBrowserThread ui_thread_; | 83 content::TestBrowserThread ui_thread_; |
| 79 }; | 84 }; |
| 80 | 85 |
| 81 DriveResourceMetadataTest::DriveResourceMetadataTest() | 86 DriveResourceMetadataTest::DriveResourceMetadataTest() |
| 82 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 87 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 83 Init(); | 88 Init(); |
| 84 } | 89 } |
| 85 | 90 |
| 86 void DriveResourceMetadataTest::Init() { | 91 void DriveResourceMetadataTest::Init() { |
| 92 resource_metadata_.InitializeRootEntry(kTestRootDirectoryResourceId); |
| 93 |
| 87 int sequence_id = 1; | 94 int sequence_id = 1; |
| 88 DriveDirectory* dir1 = AddDirectory(resource_metadata_.root(), sequence_id++); | 95 DriveDirectory* dir1 = AddDirectory(resource_metadata_.root(), sequence_id++); |
| 89 DriveDirectory* dir2 = AddDirectory(resource_metadata_.root(), sequence_id++); | 96 DriveDirectory* dir2 = AddDirectory(resource_metadata_.root(), sequence_id++); |
| 90 DriveDirectory* dir3 = AddDirectory(dir1, sequence_id++); | 97 DriveDirectory* dir3 = AddDirectory(dir1, sequence_id++); |
| 91 | 98 |
| 92 AddFile(dir1, sequence_id++); | 99 AddFile(dir1, sequence_id++); |
| 93 AddFile(dir1, sequence_id++); | 100 AddFile(dir1, sequence_id++); |
| 94 | 101 |
| 95 AddFile(dir2, sequence_id++); | 102 AddFile(dir2, sequence_id++); |
| 96 AddFile(dir2, sequence_id++); | 103 AddFile(dir2, sequence_id++); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 126 | 133 |
| 127 return file.release(); | 134 return file.release(); |
| 128 } | 135 } |
| 129 | 136 |
| 130 TEST_F(DriveResourceMetadataTest, VersionCheck) { | 137 TEST_F(DriveResourceMetadataTest, VersionCheck) { |
| 131 // Set up the root directory. | 138 // Set up the root directory. |
| 132 DriveRootDirectoryProto proto; | 139 DriveRootDirectoryProto proto; |
| 133 DriveEntryProto* mutable_entry = | 140 DriveEntryProto* mutable_entry = |
| 134 proto.mutable_drive_directory()->mutable_drive_entry(); | 141 proto.mutable_drive_directory()->mutable_drive_entry(); |
| 135 mutable_entry->mutable_file_info()->set_is_directory(true); | 142 mutable_entry->mutable_file_info()->set_is_directory(true); |
| 136 mutable_entry->set_resource_id(kDriveRootDirectoryResourceId); | 143 mutable_entry->set_resource_id(kTestRootDirectoryResourceId); |
| 137 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | 144 mutable_entry->set_upload_url(kResumableCreateMediaUrl); |
| 138 mutable_entry->set_title("drive"); | 145 mutable_entry->set_title("drive"); |
| 139 | 146 |
| 140 DriveResourceMetadata resource_metadata; | 147 DriveResourceMetadata resource_metadata; |
| 141 | 148 |
| 142 std::string serialized_proto; | 149 std::string serialized_proto; |
| 143 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | 150 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); |
| 144 // This should fail as the version is emtpy. | 151 // This should fail as the version is emtpy. |
| 145 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); | 152 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); |
| 146 | 153 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 158 | 165 |
| 159 // Set a newer version, and serialize. | 166 // Set a newer version, and serialize. |
| 160 proto.set_version(kProtoVersion + 1); | 167 proto.set_version(kProtoVersion + 1); |
| 161 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | 168 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); |
| 162 // This should fail as the version is newer. | 169 // This should fail as the version is newer. |
| 163 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); | 170 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); |
| 164 } | 171 } |
| 165 | 172 |
| 166 TEST_F(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) { | 173 TEST_F(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) { |
| 167 DriveResourceMetadata resource_metadata; | 174 DriveResourceMetadata resource_metadata; |
| 175 resource_metadata.InitializeRootEntry(kTestRootDirectoryResourceId); |
| 176 EXPECT_EQ(kTestRootDirectoryResourceId, |
| 177 resource_metadata.root()->resource_id()); |
| 168 // Look up the root directory by its resource ID. | 178 // Look up the root directory by its resource ID. |
| 169 DriveEntry* entry = resource_metadata.GetEntryByResourceId( | 179 DriveEntry* entry = resource_metadata.GetEntryByResourceId( |
| 170 kDriveRootDirectoryResourceId); | 180 resource_metadata.root()->resource_id()); |
| 171 ASSERT_TRUE(entry); | 181 ASSERT_TRUE(entry); |
| 172 EXPECT_EQ(kDriveRootDirectoryResourceId, entry->resource_id()); | 182 EXPECT_EQ(kTestRootDirectoryResourceId, entry->resource_id()); |
| 173 } | 183 } |
| 174 | 184 |
| 175 TEST_F(DriveResourceMetadataTest, GetEntryInfoByResourceId) { | 185 TEST_F(DriveResourceMetadataTest, GetEntryInfoByResourceId) { |
| 176 // Confirm that an existing file is found. | 186 // Confirm that an existing file is found. |
| 177 DriveFileError error = DRIVE_FILE_ERROR_FAILED; | 187 DriveFileError error = DRIVE_FILE_ERROR_FAILED; |
| 178 FilePath drive_file_path; | 188 FilePath drive_file_path; |
| 179 scoped_ptr<DriveEntryProto> entry_proto; | 189 scoped_ptr<DriveEntryProto> entry_proto; |
| 180 resource_metadata_.GetEntryInfoByResourceId( | 190 resource_metadata_.GetEntryInfoByResourceId( |
| 181 "file_resource_id:file4", | 191 "file_resource_id:file4", |
| 182 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, | 192 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 resource_metadata_.SaveToDB(); | 363 resource_metadata_.SaveToDB(); |
| 354 google_apis::test_util::RunBlockingPoolTask(); | 364 google_apis::test_util::RunBlockingPoolTask(); |
| 355 | 365 |
| 356 // InitFromDB should fail with DRIVE_FILE_ERROR_IN_USE. | 366 // InitFromDB should fail with DRIVE_FILE_ERROR_IN_USE. |
| 357 resource_metadata_.InitFromDB(db_path, blocking_task_runner, | 367 resource_metadata_.InitFromDB(db_path, blocking_task_runner, |
| 358 base::Bind(&InitFromDBCallback, DRIVE_FILE_ERROR_IN_USE)); | 368 base::Bind(&InitFromDBCallback, DRIVE_FILE_ERROR_IN_USE)); |
| 359 google_apis::test_util::RunBlockingPoolTask(); | 369 google_apis::test_util::RunBlockingPoolTask(); |
| 360 | 370 |
| 361 // InitFromDB should succeed. | 371 // InitFromDB should succeed. |
| 362 DriveResourceMetadata test_resource_metadata; | 372 DriveResourceMetadata test_resource_metadata; |
| 373 test_resource_metadata.InitializeRootEntry(kTestRootDirectoryResourceId); |
| 363 test_resource_metadata.InitFromDB(db_path, blocking_task_runner, | 374 test_resource_metadata.InitFromDB(db_path, blocking_task_runner, |
| 364 base::Bind(&InitFromDBCallback, DRIVE_FILE_OK)); | 375 base::Bind(&InitFromDBCallback, DRIVE_FILE_OK)); |
| 365 google_apis::test_util::RunBlockingPoolTask(); | 376 google_apis::test_util::RunBlockingPoolTask(); |
| 366 | 377 |
| 367 // Verify by checking for drive/dir2, which should have 3 children. | 378 // Verify by checking for drive/dir2, which should have 3 children. |
| 368 DriveFileError error = DRIVE_FILE_ERROR_FAILED; | 379 DriveFileError error = DRIVE_FILE_ERROR_FAILED; |
| 369 scoped_ptr<DriveEntryProtoVector> entries; | 380 scoped_ptr<DriveEntryProtoVector> entries; |
| 370 test_resource_metadata.ReadDirectoryByPath( | 381 test_resource_metadata.ReadDirectoryByPath( |
| 371 FilePath::FromUTF8Unsafe("drive/dir2"), | 382 FilePath::FromUTF8Unsafe("drive/dir2"), |
| 372 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, | 383 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 // Remove unknown resource_id using RemoveEntryFromParent. | 455 // Remove unknown resource_id using RemoveEntryFromParent. |
| 445 resource_metadata_.RemoveEntryFromParent( | 456 resource_metadata_.RemoveEntryFromParent( |
| 446 "foo", | 457 "foo", |
| 447 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | 458 base::Bind(&test_util::CopyResultsFromFileMoveCallback, |
| 448 &error, &drive_file_path)); | 459 &error, &drive_file_path)); |
| 449 google_apis::test_util::RunBlockingPoolTask(); | 460 google_apis::test_util::RunBlockingPoolTask(); |
| 450 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); | 461 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); |
| 451 | 462 |
| 452 // Try removing root. This should fail. | 463 // Try removing root. This should fail. |
| 453 resource_metadata_.RemoveEntryFromParent( | 464 resource_metadata_.RemoveEntryFromParent( |
| 454 kDriveRootDirectoryResourceId, | 465 resource_metadata_.root()->resource_id(), |
| 455 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | 466 base::Bind(&test_util::CopyResultsFromFileMoveCallback, |
| 456 &error, &drive_file_path)); | 467 &error, &drive_file_path)); |
| 457 google_apis::test_util::RunBlockingPoolTask(); | 468 google_apis::test_util::RunBlockingPoolTask(); |
| 458 EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED, error); | 469 EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED, error); |
| 459 } | 470 } |
| 460 | 471 |
| 461 TEST_F(DriveResourceMetadataTest, MoveEntryToDirectory) { | 472 TEST_F(DriveResourceMetadataTest, MoveEntryToDirectory) { |
| 462 DriveFileError error = DRIVE_FILE_ERROR_FAILED; | 473 DriveFileError error = DRIVE_FILE_ERROR_FAILED; |
| 463 FilePath drive_file_path; | 474 FilePath drive_file_path; |
| 464 scoped_ptr<DriveEntryProto> entry_proto; | 475 scoped_ptr<DriveEntryProto> entry_proto; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 FilePath::FromUTF8Unsafe("drive/dir2/file11"), | 602 FilePath::FromUTF8Unsafe("drive/dir2/file11"), |
| 592 "file11", | 603 "file11", |
| 593 base::Bind(&test_util::CopyResultsFromFileMoveCallback, | 604 base::Bind(&test_util::CopyResultsFromFileMoveCallback, |
| 594 &error, &drive_file_path)); | 605 &error, &drive_file_path)); |
| 595 google_apis::test_util::RunBlockingPoolTask(); | 606 google_apis::test_util::RunBlockingPoolTask(); |
| 596 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); | 607 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); |
| 597 EXPECT_EQ(FilePath(), drive_file_path); | 608 EXPECT_EQ(FilePath(), drive_file_path); |
| 598 } | 609 } |
| 599 | 610 |
| 600 } // namespace drive | 611 } // namespace drive |
| OLD | NEW |