Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / drive / directory_loader_unittest.cc
blob20e4ead480a1536dda72be398f07c1d7e93aee89
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.
5 #include "components/drive/directory_loader.h"
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"
29 namespace drive {
30 namespace internal {
32 namespace {
34 class TestDirectoryLoaderObserver : public ChangeListLoaderObserver {
35 public:
36 explicit TestDirectoryLoaderObserver(DirectoryLoader* loader)
37 : loader_(loader) {
38 loader_->AddObserver(this);
41 ~TestDirectoryLoaderObserver() override { loader_->RemoveObserver(this); }
43 const std::set<base::FilePath>& changed_directories() const {
44 return changed_directories_;
46 void clear_changed_directories() { changed_directories_.clear(); }
48 // ChageListObserver overrides:
49 void OnDirectoryReloaded(const base::FilePath& directory_path) override {
50 changed_directories_.insert(directory_path);
53 private:
54 DirectoryLoader* loader_;
55 std::set<base::FilePath> changed_directories_;
57 DISALLOW_COPY_AND_ASSIGN(TestDirectoryLoaderObserver);
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());
66 } // namespace
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());
75 logger_.reset(new EventLogger);
77 drive_service_.reset(new FakeDriveService);
78 ASSERT_TRUE(test_util::SetUpTestEntries(drive_service_.get()));
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());
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());
95 metadata_.reset(new ResourceMetadata(
96 metadata_storage_.get(), cache_.get(),
97 base::ThreadTaskRunnerHandle::Get().get()));
98 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
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()));
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();
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_;
142 TEST_F(DirectoryLoaderTest, ReadDirectory_GrandRoot) {
143 TestDirectoryLoaderObserver observer(directory_loader_.get());
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();
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());
165 TEST_F(DirectoryLoaderTest, ReadDirectory_MyDrive) {
166 TestDirectoryLoaderObserver observer(directory_loader_.get());
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());
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()));
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());
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));
202 TEST_F(DirectoryLoaderTest, ReadDirectory_MultipleCalls) {
203 TestDirectoryLoaderObserver observer(directory_loader_.get());
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));
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();
222 // Callback is called for each method call.
223 EXPECT_EQ(FILE_ERROR_OK, error);
224 EXPECT_EQ(FILE_ERROR_OK, error2);
227 TEST_F(DirectoryLoaderTest, Lock) {
228 // Lock the loader.
229 scoped_ptr<base::ScopedClosureRunner> lock = loader_controller_->GetLock();
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();
241 // Update is pending due to the lock.
242 EXPECT_TRUE(observer.changed_directories().empty());
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()));
251 } // namespace internal
252 } // namespace drive