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 ~TestDirectoryLoaderObserver() override
{ loader_
->RemoveObserver(this); }
41 const std::set
<base::FilePath
>& changed_directories() const {
42 return changed_directories_
;
44 void clear_changed_directories() { changed_directories_
.clear(); }
46 // ChageListObserver overrides:
47 void OnDirectoryReloaded(const base::FilePath
& directory_path
) override
{
48 changed_directories_
.insert(directory_path
);
52 DirectoryLoader
* loader_
;
53 std::set
<base::FilePath
> changed_directories_
;
55 DISALLOW_COPY_AND_ASSIGN(TestDirectoryLoaderObserver
);
58 void AccumulateReadDirectoryResult(ResourceEntryVector
* out_entries
,
59 scoped_ptr
<ResourceEntryVector
> entries
) {
61 out_entries
->insert(out_entries
->end(), entries
->begin(), entries
->end());
66 class DirectoryLoaderTest
: public testing::Test
{
68 void SetUp() override
{
69 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
70 pref_service_
.reset(new TestingPrefServiceSimple
);
71 test_util::RegisterDrivePrefs(pref_service_
->registry());
73 logger_
.reset(new EventLogger
);
75 drive_service_
.reset(new FakeDriveService
);
76 ASSERT_TRUE(test_util::SetUpTestEntries(drive_service_
.get()));
78 scheduler_
.reset(new JobScheduler(pref_service_
.get(),
81 base::MessageLoopProxy::current().get()));
82 metadata_storage_
.reset(new ResourceMetadataStorage(
83 temp_dir_
.path(), base::MessageLoopProxy::current().get()));
84 ASSERT_TRUE(metadata_storage_
->Initialize());
86 cache_
.reset(new FileCache(metadata_storage_
.get(),
88 base::MessageLoopProxy::current().get(),
89 NULL
/* free_disk_space_getter */));
90 ASSERT_TRUE(cache_
->Initialize());
92 metadata_
.reset(new ResourceMetadata(
93 metadata_storage_
.get(), cache_
.get(),
94 base::MessageLoopProxy::current().get()));
95 ASSERT_EQ(FILE_ERROR_OK
, metadata_
->Initialize());
97 about_resource_loader_
.reset(new AboutResourceLoader(scheduler_
.get()));
98 loader_controller_
.reset(new LoaderController
);
99 directory_loader_
.reset(
100 new DirectoryLoader(logger_
.get(),
101 base::MessageLoopProxy::current().get(),
104 about_resource_loader_
.get(),
105 loader_controller_
.get()));
108 // Adds a new file to the root directory of the service.
109 scoped_ptr
<google_apis::FileResource
> AddNewFile(const std::string
& title
) {
110 google_apis::DriveApiErrorCode error
= google_apis::DRIVE_FILE_ERROR
;
111 scoped_ptr
<google_apis::FileResource
> entry
;
112 drive_service_
->AddNewFile(
115 drive_service_
->GetRootResourceId(),
117 false, // shared_with_me
118 google_apis::test_util::CreateCopyResultCallback(&error
, &entry
));
119 base::RunLoop().RunUntilIdle();
120 EXPECT_EQ(google_apis::HTTP_CREATED
, error
);
124 content::TestBrowserThreadBundle thread_bundle_
;
125 base::ScopedTempDir temp_dir_
;
126 scoped_ptr
<TestingPrefServiceSimple
> pref_service_
;
127 scoped_ptr
<EventLogger
> logger_
;
128 scoped_ptr
<FakeDriveService
> drive_service_
;
129 scoped_ptr
<JobScheduler
> scheduler_
;
130 scoped_ptr
<ResourceMetadataStorage
,
131 test_util::DestroyHelperForTests
> metadata_storage_
;
132 scoped_ptr
<FileCache
, test_util::DestroyHelperForTests
> cache_
;
133 scoped_ptr
<ResourceMetadata
, test_util::DestroyHelperForTests
> metadata_
;
134 scoped_ptr
<AboutResourceLoader
> about_resource_loader_
;
135 scoped_ptr
<LoaderController
> loader_controller_
;
136 scoped_ptr
<DirectoryLoader
> directory_loader_
;
139 TEST_F(DirectoryLoaderTest
, ReadDirectory_GrandRoot
) {
140 TestDirectoryLoaderObserver
observer(directory_loader_
.get());
143 FileError error
= FILE_ERROR_FAILED
;
144 ResourceEntryVector entries
;
145 directory_loader_
->ReadDirectory(
146 util::GetDriveGrandRootPath(),
147 base::Bind(&AccumulateReadDirectoryResult
, &entries
),
148 google_apis::test_util::CreateCopyResultCallback(&error
));
149 base::RunLoop().RunUntilIdle();
150 EXPECT_EQ(FILE_ERROR_OK
, error
);
151 EXPECT_EQ(0U, observer
.changed_directories().size());
152 observer
.clear_changed_directories();
154 // My Drive has resource ID.
156 EXPECT_EQ(FILE_ERROR_OK
,
157 metadata_
->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
159 EXPECT_EQ(drive_service_
->GetRootResourceId(), entry
.resource_id());
162 TEST_F(DirectoryLoaderTest
, ReadDirectory_MyDrive
) {
163 TestDirectoryLoaderObserver
observer(directory_loader_
.get());
165 // My Drive does not have resource ID yet.
167 EXPECT_EQ(FILE_ERROR_OK
,
168 metadata_
->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
170 EXPECT_TRUE(entry
.resource_id().empty());
173 FileError error
= FILE_ERROR_FAILED
;
174 ResourceEntryVector entries
;
175 directory_loader_
->ReadDirectory(
176 util::GetDriveMyDriveRootPath(),
177 base::Bind(&AccumulateReadDirectoryResult
, &entries
),
178 google_apis::test_util::CreateCopyResultCallback(&error
));
179 base::RunLoop().RunUntilIdle();
180 EXPECT_EQ(FILE_ERROR_OK
, error
);
181 EXPECT_EQ(1U, observer
.changed_directories().count(
182 util::GetDriveMyDriveRootPath()));
184 // My Drive has resource ID.
185 EXPECT_EQ(FILE_ERROR_OK
,
186 metadata_
->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
188 EXPECT_EQ(drive_service_
->GetRootResourceId(), entry
.resource_id());
189 EXPECT_EQ(drive_service_
->about_resource().largest_change_id(),
190 entry
.directory_specific_info().changestamp());
192 // My Drive's child is present.
193 base::FilePath file_path
=
194 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
195 EXPECT_EQ(FILE_ERROR_OK
,
196 metadata_
->GetResourceEntryByPath(file_path
, &entry
));
199 TEST_F(DirectoryLoaderTest
, ReadDirectory_MultipleCalls
) {
200 TestDirectoryLoaderObserver
observer(directory_loader_
.get());
203 FileError error
= FILE_ERROR_FAILED
;
204 ResourceEntryVector entries
;
205 directory_loader_
->ReadDirectory(
206 util::GetDriveGrandRootPath(),
207 base::Bind(&AccumulateReadDirectoryResult
, &entries
),
208 google_apis::test_util::CreateCopyResultCallback(&error
));
210 // Load grand root again without waiting for the result.
211 FileError error2
= FILE_ERROR_FAILED
;
212 ResourceEntryVector entries2
;
213 directory_loader_
->ReadDirectory(
214 util::GetDriveGrandRootPath(),
215 base::Bind(&AccumulateReadDirectoryResult
, &entries2
),
216 google_apis::test_util::CreateCopyResultCallback(&error2
));
217 base::RunLoop().RunUntilIdle();
219 // Callback is called for each method call.
220 EXPECT_EQ(FILE_ERROR_OK
, error
);
221 EXPECT_EQ(FILE_ERROR_OK
, error2
);
224 TEST_F(DirectoryLoaderTest
, Lock
) {
226 scoped_ptr
<base::ScopedClosureRunner
> lock
= loader_controller_
->GetLock();
229 TestDirectoryLoaderObserver
observer(directory_loader_
.get());
230 FileError error
= FILE_ERROR_FAILED
;
231 ResourceEntryVector entries
;
232 directory_loader_
->ReadDirectory(
233 util::GetDriveMyDriveRootPath(),
234 base::Bind(&AccumulateReadDirectoryResult
, &entries
),
235 google_apis::test_util::CreateCopyResultCallback(&error
));
236 base::RunLoop().RunUntilIdle();
238 // Update is pending due to the lock.
239 EXPECT_TRUE(observer
.changed_directories().empty());
241 // Unlock the loader, this should resume the pending udpate.
243 base::RunLoop().RunUntilIdle();
244 EXPECT_EQ(1U, observer
.changed_directories().count(
245 util::GetDriveMyDriveRootPath()));
248 } // namespace internal