| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/drive/directory_loader.h" | |
| 6 | |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/prefs/testing_pref_service.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/thread_task_runner_handle.h" | |
| 14 #include "components/drive/change_list_loader.h" | |
| 15 #include "components/drive/change_list_loader_observer.h" | |
| 16 #include "components/drive/drive_test_util.h" | |
| 17 #include "components/drive/event_logger.h" | |
| 18 #include "components/drive/file_cache.h" | |
| 19 #include "components/drive/file_system_core_util.h" | |
| 20 #include "components/drive/job_scheduler.h" | |
| 21 #include "components/drive/resource_metadata.h" | |
| 22 #include "components/drive/service/fake_drive_service.h" | |
| 23 #include "components/drive/service/test_util.h" | |
| 24 #include "content/public/test/test_browser_thread_bundle.h" | |
| 25 #include "google_apis/drive/drive_api_parser.h" | |
| 26 #include "google_apis/drive/test_util.h" | |
| 27 #include "testing/gtest/include/gtest/gtest.h" | |
| 28 | |
| 29 namespace drive { | |
| 30 namespace internal { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 class TestDirectoryLoaderObserver : public ChangeListLoaderObserver { | |
| 35 public: | |
| 36 explicit TestDirectoryLoaderObserver(DirectoryLoader* loader) | |
| 37 : loader_(loader) { | |
| 38 loader_->AddObserver(this); | |
| 39 } | |
| 40 | |
| 41 ~TestDirectoryLoaderObserver() override { loader_->RemoveObserver(this); } | |
| 42 | |
| 43 const std::set<base::FilePath>& changed_directories() const { | |
| 44 return changed_directories_; | |
| 45 } | |
| 46 void clear_changed_directories() { changed_directories_.clear(); } | |
| 47 | |
| 48 // ChageListObserver overrides: | |
| 49 void OnDirectoryReloaded(const base::FilePath& directory_path) override { | |
| 50 changed_directories_.insert(directory_path); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 DirectoryLoader* loader_; | |
| 55 std::set<base::FilePath> changed_directories_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(TestDirectoryLoaderObserver); | |
| 58 }; | |
| 59 | |
| 60 void AccumulateReadDirectoryResult(ResourceEntryVector* out_entries, | |
| 61 scoped_ptr<ResourceEntryVector> entries) { | |
| 62 ASSERT_TRUE(entries); | |
| 63 out_entries->insert(out_entries->end(), entries->begin(), entries->end()); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 class DirectoryLoaderTest : public testing::Test { | |
| 69 protected: | |
| 70 void SetUp() override { | |
| 71 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 72 pref_service_.reset(new TestingPrefServiceSimple); | |
| 73 test_util::RegisterDrivePrefs(pref_service_->registry()); | |
| 74 | |
| 75 logger_.reset(new EventLogger); | |
| 76 | |
| 77 drive_service_.reset(new FakeDriveService); | |
| 78 ASSERT_TRUE(test_util::SetUpTestEntries(drive_service_.get())); | |
| 79 | |
| 80 scheduler_.reset(new JobScheduler( | |
| 81 pref_service_.get(), | |
| 82 logger_.get(), | |
| 83 drive_service_.get(), | |
| 84 base::ThreadTaskRunnerHandle::Get().get())); | |
| 85 metadata_storage_.reset(new ResourceMetadataStorage( | |
| 86 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get().get())); | |
| 87 ASSERT_TRUE(metadata_storage_->Initialize()); | |
| 88 | |
| 89 cache_.reset(new FileCache(metadata_storage_.get(), | |
| 90 temp_dir_.path(), | |
| 91 base::ThreadTaskRunnerHandle::Get().get(), | |
| 92 NULL /* free_disk_space_getter */)); | |
| 93 ASSERT_TRUE(cache_->Initialize()); | |
| 94 | |
| 95 metadata_.reset(new ResourceMetadata( | |
| 96 metadata_storage_.get(), cache_.get(), | |
| 97 base::ThreadTaskRunnerHandle::Get().get())); | |
| 98 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize()); | |
| 99 | |
| 100 about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get())); | |
| 101 loader_controller_.reset(new LoaderController); | |
| 102 directory_loader_.reset( | |
| 103 new DirectoryLoader(logger_.get(), | |
| 104 base::ThreadTaskRunnerHandle::Get().get(), | |
| 105 metadata_.get(), | |
| 106 scheduler_.get(), | |
| 107 about_resource_loader_.get(), | |
| 108 loader_controller_.get())); | |
| 109 } | |
| 110 | |
| 111 // Adds a new file to the root directory of the service. | |
| 112 scoped_ptr<google_apis::FileResource> AddNewFile(const std::string& title) { | |
| 113 google_apis::DriveApiErrorCode error = google_apis::DRIVE_FILE_ERROR; | |
| 114 scoped_ptr<google_apis::FileResource> entry; | |
| 115 drive_service_->AddNewFile( | |
| 116 "text/plain", | |
| 117 "content text", | |
| 118 drive_service_->GetRootResourceId(), | |
| 119 title, | |
| 120 false, // shared_with_me | |
| 121 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | |
| 122 base::RunLoop().RunUntilIdle(); | |
| 123 EXPECT_EQ(google_apis::HTTP_CREATED, error); | |
| 124 return entry.Pass(); | |
| 125 } | |
| 126 | |
| 127 content::TestBrowserThreadBundle thread_bundle_; | |
| 128 base::ScopedTempDir temp_dir_; | |
| 129 scoped_ptr<TestingPrefServiceSimple> pref_service_; | |
| 130 scoped_ptr<EventLogger> logger_; | |
| 131 scoped_ptr<FakeDriveService> drive_service_; | |
| 132 scoped_ptr<JobScheduler> scheduler_; | |
| 133 scoped_ptr<ResourceMetadataStorage, | |
| 134 test_util::DestroyHelperForTests> metadata_storage_; | |
| 135 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; | |
| 136 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_; | |
| 137 scoped_ptr<AboutResourceLoader> about_resource_loader_; | |
| 138 scoped_ptr<LoaderController> loader_controller_; | |
| 139 scoped_ptr<DirectoryLoader> directory_loader_; | |
| 140 }; | |
| 141 | |
| 142 TEST_F(DirectoryLoaderTest, ReadDirectory_GrandRoot) { | |
| 143 TestDirectoryLoaderObserver observer(directory_loader_.get()); | |
| 144 | |
| 145 // Load grand root. | |
| 146 FileError error = FILE_ERROR_FAILED; | |
| 147 ResourceEntryVector entries; | |
| 148 directory_loader_->ReadDirectory( | |
| 149 util::GetDriveGrandRootPath(), | |
| 150 base::Bind(&AccumulateReadDirectoryResult, &entries), | |
| 151 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 152 base::RunLoop().RunUntilIdle(); | |
| 153 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 154 EXPECT_EQ(0U, observer.changed_directories().size()); | |
| 155 observer.clear_changed_directories(); | |
| 156 | |
| 157 // My Drive has resource ID. | |
| 158 ResourceEntry entry; | |
| 159 EXPECT_EQ(FILE_ERROR_OK, | |
| 160 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(), | |
| 161 &entry)); | |
| 162 EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id()); | |
| 163 } | |
| 164 | |
| 165 TEST_F(DirectoryLoaderTest, ReadDirectory_MyDrive) { | |
| 166 TestDirectoryLoaderObserver observer(directory_loader_.get()); | |
| 167 | |
| 168 // My Drive does not have resource ID yet. | |
| 169 ResourceEntry entry; | |
| 170 EXPECT_EQ(FILE_ERROR_OK, | |
| 171 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(), | |
| 172 &entry)); | |
| 173 EXPECT_TRUE(entry.resource_id().empty()); | |
| 174 | |
| 175 // Load My Drive. | |
| 176 FileError error = FILE_ERROR_FAILED; | |
| 177 ResourceEntryVector entries; | |
| 178 directory_loader_->ReadDirectory( | |
| 179 util::GetDriveMyDriveRootPath(), | |
| 180 base::Bind(&AccumulateReadDirectoryResult, &entries), | |
| 181 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 182 base::RunLoop().RunUntilIdle(); | |
| 183 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 184 EXPECT_EQ(1U, observer.changed_directories().count( | |
| 185 util::GetDriveMyDriveRootPath())); | |
| 186 | |
| 187 // My Drive has resource ID. | |
| 188 EXPECT_EQ(FILE_ERROR_OK, | |
| 189 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(), | |
| 190 &entry)); | |
| 191 EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id()); | |
| 192 EXPECT_EQ(drive_service_->about_resource().largest_change_id(), | |
| 193 entry.directory_specific_info().changestamp()); | |
| 194 | |
| 195 // My Drive's child is present. | |
| 196 base::FilePath file_path = | |
| 197 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | |
| 198 EXPECT_EQ(FILE_ERROR_OK, | |
| 199 metadata_->GetResourceEntryByPath(file_path, &entry)); | |
| 200 } | |
| 201 | |
| 202 TEST_F(DirectoryLoaderTest, ReadDirectory_MultipleCalls) { | |
| 203 TestDirectoryLoaderObserver observer(directory_loader_.get()); | |
| 204 | |
| 205 // Load grand root. | |
| 206 FileError error = FILE_ERROR_FAILED; | |
| 207 ResourceEntryVector entries; | |
| 208 directory_loader_->ReadDirectory( | |
| 209 util::GetDriveGrandRootPath(), | |
| 210 base::Bind(&AccumulateReadDirectoryResult, &entries), | |
| 211 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 212 | |
| 213 // Load grand root again without waiting for the result. | |
| 214 FileError error2 = FILE_ERROR_FAILED; | |
| 215 ResourceEntryVector entries2; | |
| 216 directory_loader_->ReadDirectory( | |
| 217 util::GetDriveGrandRootPath(), | |
| 218 base::Bind(&AccumulateReadDirectoryResult, &entries2), | |
| 219 google_apis::test_util::CreateCopyResultCallback(&error2)); | |
| 220 base::RunLoop().RunUntilIdle(); | |
| 221 | |
| 222 // Callback is called for each method call. | |
| 223 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 224 EXPECT_EQ(FILE_ERROR_OK, error2); | |
| 225 } | |
| 226 | |
| 227 TEST_F(DirectoryLoaderTest, Lock) { | |
| 228 // Lock the loader. | |
| 229 scoped_ptr<base::ScopedClosureRunner> lock = loader_controller_->GetLock(); | |
| 230 | |
| 231 // Start loading. | |
| 232 TestDirectoryLoaderObserver observer(directory_loader_.get()); | |
| 233 FileError error = FILE_ERROR_FAILED; | |
| 234 ResourceEntryVector entries; | |
| 235 directory_loader_->ReadDirectory( | |
| 236 util::GetDriveMyDriveRootPath(), | |
| 237 base::Bind(&AccumulateReadDirectoryResult, &entries), | |
| 238 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 239 base::RunLoop().RunUntilIdle(); | |
| 240 | |
| 241 // Update is pending due to the lock. | |
| 242 EXPECT_TRUE(observer.changed_directories().empty()); | |
| 243 | |
| 244 // Unlock the loader, this should resume the pending udpate. | |
| 245 lock.reset(); | |
| 246 base::RunLoop().RunUntilIdle(); | |
| 247 EXPECT_EQ(1U, observer.changed_directories().count( | |
| 248 util::GetDriveMyDriveRootPath())); | |
| 249 } | |
| 250 | |
| 251 } // namespace internal | |
| 252 } // namespace drive | |
| OLD | NEW |