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