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 "chrome/browser/drive/test_util.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "google_apis/drive/drive_api_parser.h"
24 #include "google_apis/drive/test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h"
32 class TestDirectoryLoaderObserver : public ChangeListLoaderObserver {
34 explicit TestDirectoryLoaderObserver(DirectoryLoader* loader)
36 loader_->AddObserver(this);
39 virtual ~TestDirectoryLoaderObserver() {
40 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 virtual void OnDirectoryChanged(
50 const base::FilePath& directory_path) OVERRIDE {
51 changed_directories_.insert(directory_path);
55 DirectoryLoader* loader_;
56 std::set<base::FilePath> changed_directories_;
58 DISALLOW_COPY_AND_ASSIGN(TestDirectoryLoaderObserver);
61 void AccumulateReadDirectoryResult(ResourceEntryVector* out_entries,
62 scoped_ptr<ResourceEntryVector> entries) {
64 out_entries->insert(out_entries->end(), entries->begin(), entries->end());
69 class DirectoryLoaderTest : public testing::Test {
71 virtual void SetUp() OVERRIDE {
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
73 pref_service_.reset(new TestingPrefServiceSimple);
74 test_util::RegisterDrivePrefs(pref_service_->registry());
76 logger_.reset(new EventLogger);
78 drive_service_.reset(new FakeDriveService);
79 ASSERT_TRUE(test_util::SetUpTestEntries(drive_service_.get()));
81 scheduler_.reset(new JobScheduler(pref_service_.get(),
84 base::MessageLoopProxy::current().get()));
85 metadata_storage_.reset(new ResourceMetadataStorage(
86 temp_dir_.path(), base::MessageLoopProxy::current().get()));
87 ASSERT_TRUE(metadata_storage_->Initialize());
89 metadata_.reset(new ResourceMetadata(
90 metadata_storage_.get(), base::MessageLoopProxy::current().get()));
91 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
93 cache_.reset(new FileCache(metadata_storage_.get(),
95 base::MessageLoopProxy::current().get(),
96 NULL /* free_disk_space_getter */));
97 ASSERT_TRUE(cache_->Initialize());
99 about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
100 loader_controller_.reset(new LoaderController);
101 directory_loader_.reset(
102 new DirectoryLoader(logger_.get(),
103 base::MessageLoopProxy::current().get(),
106 about_resource_loader_.get(),
107 loader_controller_.get()));
110 // Adds a new file to the root directory of the service.
111 scoped_ptr<google_apis::ResourceEntry> AddNewFile(const std::string& title) {
112 google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
113 scoped_ptr<google_apis::ResourceEntry> entry;
114 drive_service_->AddNewFile(
117 drive_service_->GetRootResourceId(),
119 false, // shared_with_me
120 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
121 base::RunLoop().RunUntilIdle();
122 EXPECT_EQ(google_apis::HTTP_CREATED, error);
126 content::TestBrowserThreadBundle thread_bundle_;
127 base::ScopedTempDir temp_dir_;
128 scoped_ptr<TestingPrefServiceSimple> pref_service_;
129 scoped_ptr<EventLogger> logger_;
130 scoped_ptr<FakeDriveService> drive_service_;
131 scoped_ptr<JobScheduler> scheduler_;
132 scoped_ptr<ResourceMetadataStorage,
133 test_util::DestroyHelperForTests> metadata_storage_;
134 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
135 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
136 scoped_ptr<AboutResourceLoader> about_resource_loader_;
137 scoped_ptr<LoaderController> loader_controller_;
138 scoped_ptr<DirectoryLoader> directory_loader_;
141 TEST_F(DirectoryLoaderTest, ReadDirectory_GrandRoot) {
142 TestDirectoryLoaderObserver observer(directory_loader_.get());
145 FileError error = FILE_ERROR_FAILED;
146 ResourceEntryVector entries;
147 directory_loader_->ReadDirectory(
148 util::GetDriveGrandRootPath(),
149 base::Bind(&AccumulateReadDirectoryResult, &entries),
150 google_apis::test_util::CreateCopyResultCallback(&error));
151 base::RunLoop().RunUntilIdle();
152 EXPECT_EQ(FILE_ERROR_OK, error);
153 EXPECT_EQ(0U, observer.changed_directories().size());
154 observer.clear_changed_directories();
156 // My Drive has resource ID.
158 EXPECT_EQ(FILE_ERROR_OK,
159 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
161 EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id());
164 TEST_F(DirectoryLoaderTest, ReadDirectory_MyDrive) {
165 TestDirectoryLoaderObserver observer(directory_loader_.get());
167 // My Drive does not have resource ID yet.
169 EXPECT_EQ(FILE_ERROR_OK,
170 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
172 EXPECT_TRUE(entry.resource_id().empty());
175 FileError error = FILE_ERROR_FAILED;
176 ResourceEntryVector entries;
177 directory_loader_->ReadDirectory(
178 util::GetDriveMyDriveRootPath(),
179 base::Bind(&AccumulateReadDirectoryResult, &entries),
180 google_apis::test_util::CreateCopyResultCallback(&error));
181 base::RunLoop().RunUntilIdle();
182 EXPECT_EQ(FILE_ERROR_OK, error);
183 EXPECT_EQ(1U, observer.changed_directories().count(
184 util::GetDriveMyDriveRootPath()));
186 // My Drive has resource ID.
187 EXPECT_EQ(FILE_ERROR_OK,
188 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
190 EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id());
191 EXPECT_EQ(drive_service_->about_resource().largest_change_id(),
192 entry.directory_specific_info().changestamp());
194 // My Drive's child is present.
195 base::FilePath file_path =
196 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
197 EXPECT_EQ(FILE_ERROR_OK,
198 metadata_->GetResourceEntryByPath(file_path, &entry));
201 TEST_F(DirectoryLoaderTest, ReadDirectory_MultipleCalls) {
202 TestDirectoryLoaderObserver observer(directory_loader_.get());
205 FileError error = FILE_ERROR_FAILED;
206 ResourceEntryVector entries;
207 directory_loader_->ReadDirectory(
208 util::GetDriveGrandRootPath(),
209 base::Bind(&AccumulateReadDirectoryResult, &entries),
210 google_apis::test_util::CreateCopyResultCallback(&error));
212 // Load grand root again without waiting for the result.
213 FileError error2 = FILE_ERROR_FAILED;
214 ResourceEntryVector entries2;
215 directory_loader_->ReadDirectory(
216 util::GetDriveGrandRootPath(),
217 base::Bind(&AccumulateReadDirectoryResult, &entries2),
218 google_apis::test_util::CreateCopyResultCallback(&error2));
219 base::RunLoop().RunUntilIdle();
221 // Callback is called for each method call.
222 EXPECT_EQ(FILE_ERROR_OK, error);
223 EXPECT_EQ(FILE_ERROR_OK, error2);
226 TEST_F(DirectoryLoaderTest, Lock) {
228 scoped_ptr<base::ScopedClosureRunner> lock = loader_controller_->GetLock();
231 TestDirectoryLoaderObserver observer(directory_loader_.get());
232 FileError error = FILE_ERROR_FAILED;
233 ResourceEntryVector entries;
234 directory_loader_->ReadDirectory(
235 util::GetDriveMyDriveRootPath(),
236 base::Bind(&AccumulateReadDirectoryResult, &entries),
237 google_apis::test_util::CreateCopyResultCallback(&error));
238 base::RunLoop().RunUntilIdle();
240 // Update is pending due to the lock.
241 EXPECT_TRUE(observer.changed_directories().empty());
243 // Unlock the loader, this should resume the pending udpate.
245 base::RunLoop().RunUntilIdle();
246 EXPECT_EQ(1U, observer.changed_directories().count(
247 util::GetDriveMyDriveRootPath()));
250 } // namespace internal