Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / change_list_loader_unittest.cc
blobb837d25033f09096d321935037bda3738be0489d
1 // Copyright 2013 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/change_list_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_observer.h"
13 #include "chrome/browser/chromeos/drive/file_cache.h"
14 #include "chrome/browser/chromeos/drive/file_change.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"
27 namespace drive {
28 namespace internal {
30 class TestChangeListLoaderObserver : public ChangeListLoaderObserver {
31 public:
32 explicit TestChangeListLoaderObserver(ChangeListLoader* loader)
33 : loader_(loader),
34 load_from_server_complete_count_(0),
35 initial_load_complete_count_(0) {
36 loader_->AddObserver(this);
39 ~TestChangeListLoaderObserver() override { loader_->RemoveObserver(this); }
41 const FileChange& changed_files() const { return changed_files_; }
42 void clear_changed_files() { changed_files_.ClearForTest(); }
44 int load_from_server_complete_count() const {
45 return load_from_server_complete_count_;
47 int initial_load_complete_count() const {
48 return initial_load_complete_count_;
51 // ChageListObserver overrides:
52 void OnFileChanged(const FileChange& changed_files) override {
53 changed_files_.Apply(changed_files);
55 void OnLoadFromServerComplete() override {
56 ++load_from_server_complete_count_;
58 void OnInitialLoadComplete() override { ++initial_load_complete_count_; }
60 private:
61 ChangeListLoader* loader_;
62 FileChange changed_files_;
63 int load_from_server_complete_count_;
64 int initial_load_complete_count_;
66 DISALLOW_COPY_AND_ASSIGN(TestChangeListLoaderObserver);
69 class ChangeListLoaderTest : public testing::Test {
70 protected:
71 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(),
82 logger_.get(),
83 drive_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 cache_.reset(new FileCache(metadata_storage_.get(),
90 temp_dir_.path(),
91 base::MessageLoopProxy::current().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::MessageLoopProxy::current().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 change_list_loader_.reset(
103 new ChangeListLoader(logger_.get(),
104 base::MessageLoopProxy::current().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<ChangeListLoader> change_list_loader_;
142 TEST_F(ChangeListLoaderTest, AboutResourceLoader) {
143 google_apis::DriveApiErrorCode error[6] = {};
144 scoped_ptr<google_apis::AboutResource> about[6];
146 // No resource is cached at the beginning.
147 ASSERT_FALSE(about_resource_loader_->cached_about_resource());
149 // Since no resource is cached, this "Get" should trigger the update.
150 about_resource_loader_->GetAboutResource(
151 google_apis::test_util::CreateCopyResultCallback(error + 0, about + 0));
152 // Since there is one in-flight update, the next "Get" just wait for it.
153 about_resource_loader_->GetAboutResource(
154 google_apis::test_util::CreateCopyResultCallback(error + 1, about + 1));
156 base::RunLoop().RunUntilIdle();
157 EXPECT_EQ(google_apis::HTTP_SUCCESS, error[0]);
158 EXPECT_EQ(google_apis::HTTP_SUCCESS, error[1]);
159 const int64 first_changestamp = about[0]->largest_change_id();
160 EXPECT_EQ(first_changestamp, about[1]->largest_change_id());
161 ASSERT_TRUE(about_resource_loader_->cached_about_resource());
162 EXPECT_EQ(
163 first_changestamp,
164 about_resource_loader_->cached_about_resource()->largest_change_id());
166 // Increment changestamp by 1.
167 AddNewFile("temp");
168 // Explicitly calling UpdateAboutResource will start another API call.
169 about_resource_loader_->UpdateAboutResource(
170 google_apis::test_util::CreateCopyResultCallback(error + 2, about + 2));
171 // It again waits for the in-flight UpdateAboutResoure call, even though this
172 // time there is a cached result.
173 about_resource_loader_->GetAboutResource(
174 google_apis::test_util::CreateCopyResultCallback(error + 3, about + 3));
176 base::RunLoop().RunUntilIdle();
177 EXPECT_EQ(google_apis::HTTP_SUCCESS, error[2]);
178 EXPECT_EQ(google_apis::HTTP_SUCCESS, error[3]);
179 EXPECT_EQ(first_changestamp + 1, about[2]->largest_change_id());
180 EXPECT_EQ(first_changestamp + 1, about[3]->largest_change_id());
181 EXPECT_EQ(
182 first_changestamp + 1,
183 about_resource_loader_->cached_about_resource()->largest_change_id());
185 // Increment changestamp by 1.
186 AddNewFile("temp2");
187 // Now no UpdateAboutResource task is running. Returns the cached result.
188 about_resource_loader_->GetAboutResource(
189 google_apis::test_util::CreateCopyResultCallback(error + 4, about + 4));
190 // Explicitly calling UpdateAboutResource will start another API call.
191 about_resource_loader_->UpdateAboutResource(
192 google_apis::test_util::CreateCopyResultCallback(error + 5, about + 5));
194 base::RunLoop().RunUntilIdle();
195 EXPECT_EQ(google_apis::HTTP_NO_CONTENT, error[4]);
196 EXPECT_EQ(google_apis::HTTP_SUCCESS, error[5]);
197 EXPECT_EQ(first_changestamp + 1, about[4]->largest_change_id());
198 EXPECT_EQ(first_changestamp + 2, about[5]->largest_change_id());
199 EXPECT_EQ(
200 first_changestamp + 2,
201 about_resource_loader_->cached_about_resource()->largest_change_id());
203 EXPECT_EQ(3, drive_service_->about_resource_load_count());
206 TEST_F(ChangeListLoaderTest, Load) {
207 EXPECT_FALSE(change_list_loader_->IsRefreshing());
209 // Start initial load.
210 TestChangeListLoaderObserver observer(change_list_loader_.get());
212 EXPECT_EQ(0, drive_service_->about_resource_load_count());
214 FileError error = FILE_ERROR_FAILED;
215 change_list_loader_->LoadIfNeeded(
216 google_apis::test_util::CreateCopyResultCallback(&error));
217 EXPECT_TRUE(change_list_loader_->IsRefreshing());
218 base::RunLoop().RunUntilIdle();
219 EXPECT_EQ(FILE_ERROR_OK, error);
221 EXPECT_FALSE(change_list_loader_->IsRefreshing());
222 int64 changestamp = 0;
223 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
224 EXPECT_LT(0, changestamp);
225 EXPECT_EQ(1, drive_service_->file_list_load_count());
226 EXPECT_EQ(1, drive_service_->about_resource_load_count());
227 EXPECT_EQ(1, observer.initial_load_complete_count());
228 EXPECT_EQ(1, observer.load_from_server_complete_count());
229 EXPECT_TRUE(observer.changed_files().empty());
231 base::FilePath file_path =
232 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
233 ResourceEntry entry;
234 EXPECT_EQ(FILE_ERROR_OK,
235 metadata_->GetResourceEntryByPath(file_path, &entry));
238 TEST_F(ChangeListLoaderTest, Load_LocalMetadataAvailable) {
239 // Prepare metadata.
240 FileError error = FILE_ERROR_FAILED;
241 change_list_loader_->LoadIfNeeded(
242 google_apis::test_util::CreateCopyResultCallback(&error));
243 base::RunLoop().RunUntilIdle();
244 EXPECT_EQ(FILE_ERROR_OK, error);
246 // Reset loader.
247 about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
248 change_list_loader_.reset(
249 new ChangeListLoader(logger_.get(),
250 base::MessageLoopProxy::current().get(),
251 metadata_.get(),
252 scheduler_.get(),
253 about_resource_loader_.get(),
254 loader_controller_.get()));
256 // Add a file to the service.
257 scoped_ptr<google_apis::FileResource> gdata_entry = AddNewFile("New File");
258 ASSERT_TRUE(gdata_entry);
260 // Start loading. Because local metadata is available, the load results in
261 // returning FILE_ERROR_OK without fetching full list of resources.
262 const int previous_file_list_load_count =
263 drive_service_->file_list_load_count();
264 TestChangeListLoaderObserver observer(change_list_loader_.get());
266 change_list_loader_->LoadIfNeeded(
267 google_apis::test_util::CreateCopyResultCallback(&error));
268 EXPECT_TRUE(change_list_loader_->IsRefreshing());
269 base::RunLoop().RunUntilIdle();
270 EXPECT_EQ(FILE_ERROR_OK, error);
271 EXPECT_EQ(previous_file_list_load_count,
272 drive_service_->file_list_load_count());
273 EXPECT_EQ(1, observer.initial_load_complete_count());
275 // Update should be checked by Load().
276 int64 changestamp = 0;
277 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
278 EXPECT_EQ(drive_service_->about_resource().largest_change_id(), changestamp);
279 EXPECT_EQ(1, drive_service_->change_list_load_count());
280 EXPECT_EQ(1, observer.load_from_server_complete_count());
281 EXPECT_TRUE(
282 observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath()));
284 base::FilePath file_path =
285 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title());
286 ResourceEntry entry;
287 EXPECT_EQ(FILE_ERROR_OK,
288 metadata_->GetResourceEntryByPath(file_path, &entry));
291 TEST_F(ChangeListLoaderTest, CheckForUpdates) {
292 // CheckForUpdates() results in no-op before load.
293 FileError check_for_updates_error = FILE_ERROR_FAILED;
294 change_list_loader_->CheckForUpdates(
295 google_apis::test_util::CreateCopyResultCallback(
296 &check_for_updates_error));
297 EXPECT_FALSE(change_list_loader_->IsRefreshing());
298 base::RunLoop().RunUntilIdle();
299 EXPECT_EQ(FILE_ERROR_FAILED,
300 check_for_updates_error); // Callback was not run.
301 int64 changestamp = 0;
302 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
303 EXPECT_EQ(0, changestamp);
304 EXPECT_EQ(0, drive_service_->file_list_load_count());
305 EXPECT_EQ(0, drive_service_->about_resource_load_count());
307 // Start initial load.
308 FileError load_error = FILE_ERROR_FAILED;
309 change_list_loader_->LoadIfNeeded(
310 google_apis::test_util::CreateCopyResultCallback(&load_error));
311 EXPECT_TRUE(change_list_loader_->IsRefreshing());
313 // CheckForUpdates() while loading.
314 change_list_loader_->CheckForUpdates(
315 google_apis::test_util::CreateCopyResultCallback(
316 &check_for_updates_error));
318 base::RunLoop().RunUntilIdle();
319 EXPECT_FALSE(change_list_loader_->IsRefreshing());
320 EXPECT_EQ(FILE_ERROR_OK, load_error);
321 EXPECT_EQ(FILE_ERROR_OK, check_for_updates_error);
322 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
323 EXPECT_LT(0, changestamp);
324 EXPECT_EQ(1, drive_service_->file_list_load_count());
326 int64 previous_changestamp = 0;
327 EXPECT_EQ(FILE_ERROR_OK,
328 metadata_->GetLargestChangestamp(&previous_changestamp));
329 // CheckForUpdates() results in no update.
330 change_list_loader_->CheckForUpdates(
331 google_apis::test_util::CreateCopyResultCallback(
332 &check_for_updates_error));
333 EXPECT_TRUE(change_list_loader_->IsRefreshing());
334 base::RunLoop().RunUntilIdle();
335 EXPECT_FALSE(change_list_loader_->IsRefreshing());
336 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
337 EXPECT_EQ(previous_changestamp, changestamp);
339 // Add a file to the service.
340 scoped_ptr<google_apis::FileResource> gdata_entry = AddNewFile("New File");
341 ASSERT_TRUE(gdata_entry);
343 // CheckForUpdates() results in update.
344 TestChangeListLoaderObserver observer(change_list_loader_.get());
345 change_list_loader_->CheckForUpdates(
346 google_apis::test_util::CreateCopyResultCallback(
347 &check_for_updates_error));
348 EXPECT_TRUE(change_list_loader_->IsRefreshing());
349 base::RunLoop().RunUntilIdle();
350 EXPECT_FALSE(change_list_loader_->IsRefreshing());
351 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
352 EXPECT_LT(previous_changestamp, changestamp);
353 EXPECT_EQ(1, observer.load_from_server_complete_count());
354 EXPECT_TRUE(
355 observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath()));
357 // The new file is found in the local metadata.
358 base::FilePath new_file_path =
359 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title());
360 ResourceEntry entry;
361 EXPECT_EQ(FILE_ERROR_OK,
362 metadata_->GetResourceEntryByPath(new_file_path, &entry));
365 TEST_F(ChangeListLoaderTest, Lock) {
366 FileError error = FILE_ERROR_FAILED;
367 change_list_loader_->LoadIfNeeded(
368 google_apis::test_util::CreateCopyResultCallback(&error));
369 base::RunLoop().RunUntilIdle();
370 EXPECT_EQ(FILE_ERROR_OK, error);
372 // Add a new file.
373 scoped_ptr<google_apis::FileResource> file = AddNewFile("New File");
374 ASSERT_TRUE(file);
376 // Lock the loader.
377 scoped_ptr<base::ScopedClosureRunner> lock = loader_controller_->GetLock();
379 // Start update.
380 TestChangeListLoaderObserver observer(change_list_loader_.get());
381 FileError check_for_updates_error = FILE_ERROR_FAILED;
382 change_list_loader_->CheckForUpdates(
383 google_apis::test_util::CreateCopyResultCallback(
384 &check_for_updates_error));
385 base::RunLoop().RunUntilIdle();
387 // Update is pending due to the lock.
388 EXPECT_TRUE(observer.changed_files().empty());
390 // Unlock the loader, this should resume the pending update.
391 lock.reset();
392 base::RunLoop().RunUntilIdle();
393 EXPECT_TRUE(
394 observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath()));
397 } // namespace internal
398 } // namespace drive