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 "chrome/browser/chromeos/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 "chrome/browser/chromeos/drive/change_list_loader.h"
13 #include "chrome/browser/chromeos/drive/change_list_loader_observer.h"
14 #include "chrome/browser/chromeos/drive/file_cache.h"
15 #include "chrome/browser/chromeos/drive/file_system_util.h"
16 #include "chrome/browser/chromeos/drive/job_scheduler.h"
17 #include "chrome/browser/chromeos/drive/resource_metadata.h"
18 #include "chrome/browser/chromeos/drive/test_util.h"
19 #include "chrome/browser/drive/event_logger.h"
20 #include "chrome/browser/drive/fake_drive_service.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "google_apis/drive/drive_api_parser.h"
23 #include "google_apis/drive/test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
31 class TestDirectoryLoaderObserver : public ChangeListLoaderObserver {
33 explicit TestDirectoryLoaderObserver(DirectoryLoader* loader)
35 loader_->AddObserver(this);
38 virtual ~TestDirectoryLoaderObserver() {
39 loader_->RemoveObserver(this);
42 const std::set<base::FilePath>& changed_directories() const {
43 return changed_directories_;
45 void clear_changed_directories() { changed_directories_.clear(); }
47 // ChageListObserver overrides:
48 virtual void OnDirectoryChanged(
49 const base::FilePath& directory_path) OVERRIDE {
50 changed_directories_.insert(directory_path);
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) {
63 out_entries->insert(out_entries->end(), entries->begin(), entries->end());
68 class DirectoryLoaderTest : public testing::Test {
70 virtual 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(drive_service_->LoadResourceListForWapi(
79 "gdata/root_feed.json"));
80 ASSERT_TRUE(drive_service_->LoadAccountMetadataForWapi(
81 "gdata/account_metadata.json"));
83 scheduler_.reset(new JobScheduler(pref_service_.get(),
86 base::MessageLoopProxy::current().get()));
87 metadata_storage_.reset(new ResourceMetadataStorage(
88 temp_dir_.path(), base::MessageLoopProxy::current().get()));
89 ASSERT_TRUE(metadata_storage_->Initialize());
91 metadata_.reset(new ResourceMetadata(
92 metadata_storage_.get(), base::MessageLoopProxy::current().get()));
93 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
95 cache_.reset(new FileCache(metadata_storage_.get(),
97 base::MessageLoopProxy::current().get(),
98 NULL /* free_disk_space_getter */));
99 ASSERT_TRUE(cache_->Initialize());
101 about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
102 loader_controller_.reset(new LoaderController);
103 directory_loader_.reset(
104 new DirectoryLoader(logger_.get(),
105 base::MessageLoopProxy::current().get(),
108 drive_service_.get(),
109 about_resource_loader_.get(),
110 loader_controller_.get()));
113 // Adds a new file to the root directory of the service.
114 scoped_ptr<google_apis::ResourceEntry> AddNewFile(const std::string& title) {
115 google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
116 scoped_ptr<google_apis::ResourceEntry> entry;
117 drive_service_->AddNewFile(
120 drive_service_->GetRootResourceId(),
122 false, // shared_with_me
123 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
124 base::RunLoop().RunUntilIdle();
125 EXPECT_EQ(google_apis::HTTP_CREATED, error);
129 content::TestBrowserThreadBundle thread_bundle_;
130 base::ScopedTempDir temp_dir_;
131 scoped_ptr<TestingPrefServiceSimple> pref_service_;
132 scoped_ptr<EventLogger> logger_;
133 scoped_ptr<FakeDriveService> drive_service_;
134 scoped_ptr<JobScheduler> scheduler_;
135 scoped_ptr<ResourceMetadataStorage,
136 test_util::DestroyHelperForTests> metadata_storage_;
137 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
138 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
139 scoped_ptr<AboutResourceLoader> about_resource_loader_;
140 scoped_ptr<LoaderController> loader_controller_;
141 scoped_ptr<DirectoryLoader> directory_loader_;
144 TEST_F(DirectoryLoaderTest, ReadDirectory_GrandRoot) {
145 TestDirectoryLoaderObserver observer(directory_loader_.get());
148 FileError error = FILE_ERROR_FAILED;
149 ResourceEntryVector entries;
150 directory_loader_->ReadDirectory(
151 util::GetDriveGrandRootPath(),
152 base::Bind(&AccumulateReadDirectoryResult, &entries),
153 google_apis::test_util::CreateCopyResultCallback(&error));
154 base::RunLoop().RunUntilIdle();
155 EXPECT_EQ(FILE_ERROR_OK, error);
156 EXPECT_EQ(0U, observer.changed_directories().size());
157 observer.clear_changed_directories();
159 // My Drive has resource ID.
161 EXPECT_EQ(FILE_ERROR_OK,
162 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
164 EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id());
167 TEST_F(DirectoryLoaderTest, ReadDirectory_MyDrive) {
168 TestDirectoryLoaderObserver observer(directory_loader_.get());
170 // My Drive does not have resource ID yet.
172 EXPECT_EQ(FILE_ERROR_OK,
173 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
175 EXPECT_TRUE(entry.resource_id().empty());
178 FileError error = FILE_ERROR_FAILED;
179 ResourceEntryVector entries;
180 directory_loader_->ReadDirectory(
181 util::GetDriveMyDriveRootPath(),
182 base::Bind(&AccumulateReadDirectoryResult, &entries),
183 google_apis::test_util::CreateCopyResultCallback(&error));
184 base::RunLoop().RunUntilIdle();
185 EXPECT_EQ(FILE_ERROR_OK, error);
186 EXPECT_EQ(1U, observer.changed_directories().count(
187 util::GetDriveMyDriveRootPath()));
189 // My Drive has resource ID.
190 EXPECT_EQ(FILE_ERROR_OK,
191 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
193 EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id());
194 EXPECT_EQ(drive_service_->about_resource().largest_change_id(),
195 entry.directory_specific_info().changestamp());
197 // My Drive's child is present.
198 base::FilePath file_path =
199 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
200 EXPECT_EQ(FILE_ERROR_OK,
201 metadata_->GetResourceEntryByPath(file_path, &entry));
204 TEST_F(DirectoryLoaderTest, ReadDirectory_MultipleCalls) {
205 TestDirectoryLoaderObserver observer(directory_loader_.get());
208 FileError error = FILE_ERROR_FAILED;
209 ResourceEntryVector entries;
210 directory_loader_->ReadDirectory(
211 util::GetDriveGrandRootPath(),
212 base::Bind(&AccumulateReadDirectoryResult, &entries),
213 google_apis::test_util::CreateCopyResultCallback(&error));
215 // Load grand root again without waiting for the result.
216 FileError error2 = FILE_ERROR_FAILED;
217 ResourceEntryVector entries2;
218 directory_loader_->ReadDirectory(
219 util::GetDriveGrandRootPath(),
220 base::Bind(&AccumulateReadDirectoryResult, &entries2),
221 google_apis::test_util::CreateCopyResultCallback(&error2));
222 base::RunLoop().RunUntilIdle();
224 // Callback is called for each method call.
225 EXPECT_EQ(FILE_ERROR_OK, error);
226 EXPECT_EQ(FILE_ERROR_OK, error2);
229 TEST_F(DirectoryLoaderTest, Lock) {
231 scoped_ptr<base::ScopedClosureRunner> lock = loader_controller_->GetLock();
234 TestDirectoryLoaderObserver observer(directory_loader_.get());
235 FileError error = FILE_ERROR_FAILED;
236 ResourceEntryVector entries;
237 directory_loader_->ReadDirectory(
238 util::GetDriveMyDriveRootPath(),
239 base::Bind(&AccumulateReadDirectoryResult, &entries),
240 google_apis::test_util::CreateCopyResultCallback(&error));
241 base::RunLoop().RunUntilIdle();
243 // Update is pending due to the lock.
244 EXPECT_TRUE(observer.changed_directories().empty());
246 // Unlock the loader, this should resume the pending udpate.
248 base::RunLoop().RunUntilIdle();
249 EXPECT_EQ(1U, observer.changed_directories().count(
250 util::GetDriveMyDriveRootPath()));
253 } // namespace internal