Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / sync_client_unittest.cc
1 // Copyright (c) 2012 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.
4
5 #include "chrome/browser/chromeos/drive/sync_client.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/run_loop.h"
13 #include "base/test/test_timeouts.h"
14 #include "chrome/browser/chromeos/drive/change_list_loader.h"
15 #include "chrome/browser/chromeos/drive/drive.pb.h"
16 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
17 #include "chrome/browser/chromeos/drive/file_cache.h"
18 #include "chrome/browser/chromeos/drive/file_system/move_operation.h"
19 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
20 #include "chrome/browser/chromeos/drive/file_system/remove_operation.h"
21 #include "chrome/browser/chromeos/drive/file_system_util.h"
22 #include "chrome/browser/chromeos/drive/job_scheduler.h"
23 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
24 #include "chrome/browser/chromeos/drive/resource_metadata.h"
25 #include "chrome/browser/chromeos/drive/test_util.h"
26 #include "chrome/browser/drive/event_logger.h"
27 #include "chrome/browser/drive/fake_drive_service.h"
28 #include "content/public/test/test_browser_thread_bundle.h"
29 #include "google_apis/drive/test_util.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 namespace drive {
33 namespace internal {
34
35 namespace {
36
37 // The content of files initially stored in the cache.
38 const char kLocalContent[] = "Hello!";
39
40 // The content of files stored in the service.
41 const char kRemoteContent[] = "World!";
42
43 // SyncClientTestDriveService will return GDATA_CANCELLED when a request is
44 // made with the specified resource ID.
45 class SyncClientTestDriveService : public ::drive::FakeDriveService {
46  public:
47   SyncClientTestDriveService() : download_file_count_(0) {}
48
49   // FakeDriveService override:
50   virtual google_apis::CancelCallback DownloadFile(
51       const base::FilePath& local_cache_path,
52       const std::string& resource_id,
53       const google_apis::DownloadActionCallback& download_action_callback,
54       const google_apis::GetContentCallback& get_content_callback,
55       const google_apis::ProgressCallback& progress_callback) OVERRIDE {
56     ++download_file_count_;
57     if (resource_id == resource_id_to_be_cancelled_) {
58       base::MessageLoopProxy::current()->PostTask(
59           FROM_HERE,
60           base::Bind(download_action_callback,
61                      google_apis::GDATA_CANCELLED,
62                      base::FilePath()));
63       return google_apis::CancelCallback();
64     }
65     if (resource_id == resource_id_to_be_paused_) {
66       paused_action_ = base::Bind(download_action_callback,
67                                   google_apis::GDATA_OTHER_ERROR,
68                                   base::FilePath());
69       return google_apis::CancelCallback();
70     }
71     return FakeDriveService::DownloadFile(local_cache_path,
72                                           resource_id,
73                                           download_action_callback,
74                                           get_content_callback,
75                                           progress_callback);
76   }
77
78   int download_file_count() const { return download_file_count_; }
79
80   void set_resource_id_to_be_cancelled(const std::string& resource_id) {
81     resource_id_to_be_cancelled_ = resource_id;
82   }
83
84   void set_resource_id_to_be_paused(const std::string& resource_id) {
85     resource_id_to_be_paused_ = resource_id;
86   }
87
88   const base::Closure& paused_action() const { return paused_action_; }
89
90  private:
91   int download_file_count_;
92   std::string resource_id_to_be_cancelled_;
93   std::string resource_id_to_be_paused_;
94   base::Closure paused_action_;
95 };
96
97 class DummyOperationObserver : public file_system::OperationObserver {
98   // OperationObserver override:
99   virtual void OnDirectoryChangedByOperation(
100       const base::FilePath& path) OVERRIDE {}
101 };
102
103 }  // namespace
104
105 class SyncClientTest : public testing::Test {
106  public:
107   virtual void SetUp() OVERRIDE {
108     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
109
110     pref_service_.reset(new TestingPrefServiceSimple);
111     test_util::RegisterDrivePrefs(pref_service_->registry());
112
113     fake_network_change_notifier_.reset(
114         new test_util::FakeNetworkChangeNotifier);
115
116     logger_.reset(new EventLogger);
117
118     drive_service_.reset(new SyncClientTestDriveService);
119     drive_service_->LoadResourceListForWapi("gdata/empty_feed.json");
120     drive_service_->LoadAccountMetadataForWapi(
121         "gdata/account_metadata.json");
122
123     scheduler_.reset(new JobScheduler(pref_service_.get(),
124                                       logger_.get(),
125                                       drive_service_.get(),
126                                       base::MessageLoopProxy::current().get()));
127
128     metadata_storage_.reset(new ResourceMetadataStorage(
129         temp_dir_.path(), base::MessageLoopProxy::current().get()));
130     ASSERT_TRUE(metadata_storage_->Initialize());
131
132     metadata_.reset(new internal::ResourceMetadata(
133         metadata_storage_.get(), base::MessageLoopProxy::current()));
134     ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
135
136     cache_.reset(new FileCache(metadata_storage_.get(),
137                                temp_dir_.path(),
138                                base::MessageLoopProxy::current().get(),
139                                NULL /* free_disk_space_getter */));
140     ASSERT_TRUE(cache_->Initialize());
141
142     about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
143     loader_controller_.reset(new LoaderController);
144     change_list_loader_.reset(new ChangeListLoader(
145         logger_.get(),
146         base::MessageLoopProxy::current().get(),
147         metadata_.get(),
148         scheduler_.get(),
149         about_resource_loader_.get(),
150         loader_controller_.get()));
151     ASSERT_NO_FATAL_FAILURE(SetUpTestData());
152
153     sync_client_.reset(new SyncClient(base::MessageLoopProxy::current().get(),
154                                       &observer_,
155                                       scheduler_.get(),
156                                       metadata_.get(),
157                                       cache_.get(),
158                                       loader_controller_.get(),
159                                       temp_dir_.path()));
160
161     // Disable delaying so that DoSyncLoop() starts immediately.
162     sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0));
163   }
164
165   // Adds a file to the service root and |resource_ids_|.
166   void AddFileEntry(const std::string& title) {
167     google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
168     scoped_ptr<google_apis::ResourceEntry> entry;
169     drive_service_->AddNewFile(
170         "text/plain",
171         kRemoteContent,
172         drive_service_->GetRootResourceId(),
173         title,
174         false,  // shared_with_me
175         google_apis::test_util::CreateCopyResultCallback(&error, &entry));
176     base::RunLoop().RunUntilIdle();
177     ASSERT_EQ(google_apis::HTTP_CREATED, error);
178     ASSERT_TRUE(entry);
179     resource_ids_[title] = entry->resource_id();
180   }
181
182   // Sets up data for tests.
183   void SetUpTestData() {
184     // Prepare a temp file.
185     base::FilePath temp_file;
186     EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file));
187     ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file,
188                                                           kLocalContent));
189
190     // Add file entries to the service.
191     ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo"));
192     ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar"));
193     ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz"));
194     ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched"));
195     ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty"));
196     ASSERT_NO_FATAL_FAILURE(AddFileEntry("removed"));
197     ASSERT_NO_FATAL_FAILURE(AddFileEntry("moved"));
198
199     // Load data from the service to the metadata.
200     FileError error = FILE_ERROR_FAILED;
201     change_list_loader_->LoadIfNeeded(
202         google_apis::test_util::CreateCopyResultCallback(&error));
203     base::RunLoop().RunUntilIdle();
204     EXPECT_EQ(FILE_ERROR_OK, error);
205
206     // Prepare 3 pinned-but-not-present files.
207     EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("foo")));
208     EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("bar")));
209     EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("baz")));
210
211     // Prepare a pinned-and-fetched file.
212     const std::string md5_fetched = "md5";
213     EXPECT_EQ(FILE_ERROR_OK,
214               cache_->Store(GetLocalId("fetched"), md5_fetched,
215                             temp_file, FileCache::FILE_OPERATION_COPY));
216     EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("fetched")));
217
218     // Prepare a pinned-and-fetched-and-dirty file.
219     EXPECT_EQ(FILE_ERROR_OK,
220               cache_->Store(GetLocalId("dirty"), std::string(),
221                             temp_file, FileCache::FILE_OPERATION_COPY));
222     EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("dirty")));
223
224     // Prepare a removed file.
225     file_system::RemoveOperation remove_operation(
226         base::MessageLoopProxy::current().get(), &observer_, metadata_.get(),
227         cache_.get());
228     remove_operation.Remove(
229         metadata_->GetFilePath(GetLocalId("removed")),
230         false,  // is_recursive
231         google_apis::test_util::CreateCopyResultCallback(&error));
232     base::RunLoop().RunUntilIdle();
233     EXPECT_EQ(FILE_ERROR_OK, error);
234
235     // Prepare a moved file.
236     file_system::MoveOperation move_operation(
237         base::MessageLoopProxy::current().get(), &observer_, metadata_.get());
238     move_operation.Move(
239         metadata_->GetFilePath(GetLocalId("moved")),
240         util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"),
241         google_apis::test_util::CreateCopyResultCallback(&error));
242     base::RunLoop().RunUntilIdle();
243     EXPECT_EQ(FILE_ERROR_OK, error);
244   }
245
246  protected:
247   std::string GetLocalId(const std::string& title) {
248     EXPECT_EQ(1U, resource_ids_.count(title));
249     std::string local_id;
250     EXPECT_EQ(FILE_ERROR_OK,
251               metadata_->GetIdByResourceId(resource_ids_[title], &local_id));
252     return local_id;
253   }
254
255   content::TestBrowserThreadBundle thread_bundle_;
256   base::ScopedTempDir temp_dir_;
257   scoped_ptr<TestingPrefServiceSimple> pref_service_;
258   scoped_ptr<test_util::FakeNetworkChangeNotifier>
259       fake_network_change_notifier_;
260   scoped_ptr<EventLogger> logger_;
261   scoped_ptr<SyncClientTestDriveService> drive_service_;
262   DummyOperationObserver observer_;
263   scoped_ptr<JobScheduler> scheduler_;
264   scoped_ptr<ResourceMetadataStorage,
265              test_util::DestroyHelperForTests> metadata_storage_;
266   scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
267   scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
268   scoped_ptr<AboutResourceLoader> about_resource_loader_;
269   scoped_ptr<LoaderController> loader_controller_;
270   scoped_ptr<ChangeListLoader> change_list_loader_;
271   scoped_ptr<SyncClient> sync_client_;
272
273   std::map<std::string, std::string> resource_ids_;  // Name-to-id map.
274 };
275
276 TEST_F(SyncClientTest, StartProcessingBacklog) {
277   sync_client_->StartProcessingBacklog();
278   base::RunLoop().RunUntilIdle();
279
280   FileCacheEntry cache_entry;
281   // Pinned files get downloaded.
282   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
283   EXPECT_TRUE(cache_entry.is_present());
284
285   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry));
286   EXPECT_TRUE(cache_entry.is_present());
287
288   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry));
289   EXPECT_TRUE(cache_entry.is_present());
290
291   // Dirty file gets uploaded.
292   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
293   EXPECT_FALSE(cache_entry.is_dirty());
294
295   // Removed entry is not found.
296   google_apis::GDataErrorCode status = google_apis::GDATA_OTHER_ERROR;
297   scoped_ptr<google_apis::ResourceEntry> resource_entry;
298   drive_service_->GetResourceEntry(
299       resource_ids_["removed"],
300       google_apis::test_util::CreateCopyResultCallback(&status,
301                                                        &resource_entry));
302   base::RunLoop().RunUntilIdle();
303   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
304   ASSERT_TRUE(resource_entry);
305   EXPECT_TRUE(resource_entry->deleted());
306
307   // Moved entry was moved.
308   status = google_apis::GDATA_OTHER_ERROR;
309   drive_service_->GetResourceEntry(
310       resource_ids_["moved"],
311       google_apis::test_util::CreateCopyResultCallback(&status,
312                                                        &resource_entry));
313   base::RunLoop().RunUntilIdle();
314   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
315   ASSERT_TRUE(resource_entry);
316   EXPECT_EQ("moved_new_title", resource_entry->title());
317 }
318
319 TEST_F(SyncClientTest, AddFetchTask) {
320   sync_client_->AddFetchTask(GetLocalId("foo"));
321   base::RunLoop().RunUntilIdle();
322
323   FileCacheEntry cache_entry;
324   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
325   EXPECT_TRUE(cache_entry.is_present());
326 }
327
328 TEST_F(SyncClientTest, AddFetchTaskAndCancelled) {
329   // Trigger fetching of a file which results in cancellation.
330   drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]);
331   sync_client_->AddFetchTask(GetLocalId("foo"));
332   base::RunLoop().RunUntilIdle();
333
334   // The file should be unpinned if the user wants the download to be cancelled.
335   FileCacheEntry cache_entry;
336   EXPECT_FALSE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
337 }
338
339 TEST_F(SyncClientTest, RemoveFetchTask) {
340   sync_client_->AddFetchTask(GetLocalId("foo"));
341   sync_client_->AddFetchTask(GetLocalId("bar"));
342   sync_client_->AddFetchTask(GetLocalId("baz"));
343
344   sync_client_->RemoveFetchTask(GetLocalId("foo"));
345   sync_client_->RemoveFetchTask(GetLocalId("baz"));
346   base::RunLoop().RunUntilIdle();
347
348   // Only "bar" should be fetched.
349   FileCacheEntry cache_entry;
350   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
351   EXPECT_FALSE(cache_entry.is_present());
352
353   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry));
354   EXPECT_TRUE(cache_entry.is_present());
355
356   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry));
357   EXPECT_FALSE(cache_entry.is_present());
358
359 }
360
361 TEST_F(SyncClientTest, ExistingPinnedFiles) {
362   // Start checking the existing pinned files. This will collect the resource
363   // IDs of pinned files, with stale local cache files.
364   sync_client_->StartCheckingExistingPinnedFiles();
365   base::RunLoop().RunUntilIdle();
366
367   // "fetched" and "dirty" are the existing pinned files.
368   // The non-dirty one should be synced, but the dirty one should not.
369   base::FilePath cache_file;
370   std::string content;
371   EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("fetched"), &cache_file));
372   EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
373   EXPECT_EQ(kRemoteContent, content);
374   content.clear();
375
376   EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("dirty"), &cache_file));
377   EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
378   EXPECT_EQ(kLocalContent, content);
379 }
380
381 TEST_F(SyncClientTest, RetryOnDisconnection) {
382   // Let the service go down.
383   drive_service_->set_offline(true);
384   // Change the network connection state after some delay, to test that
385   // FILE_ERROR_NO_CONNECTION is handled by SyncClient correctly.
386   // Without this delay, JobScheduler will keep the jobs unrun and SyncClient
387   // will receive no error.
388   base::MessageLoopProxy::current()->PostDelayedTask(
389       FROM_HERE,
390       base::Bind(&test_util::FakeNetworkChangeNotifier::SetConnectionType,
391                  base::Unretained(fake_network_change_notifier_.get()),
392                  net::NetworkChangeNotifier::CONNECTION_NONE),
393       TestTimeouts::tiny_timeout());
394
395   // Try fetch and upload.
396   sync_client_->AddFetchTask(GetLocalId("foo"));
397   sync_client_->AddUpdateTask(ClientContext(USER_INITIATED),
398                               GetLocalId("dirty"));
399   base::RunLoop().RunUntilIdle();
400
401   // Not yet fetched nor uploaded.
402   FileCacheEntry cache_entry;
403   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
404   EXPECT_FALSE(cache_entry.is_present());
405   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
406   EXPECT_TRUE(cache_entry.is_dirty());
407
408   // Switch to online.
409   fake_network_change_notifier_->SetConnectionType(
410       net::NetworkChangeNotifier::CONNECTION_WIFI);
411   drive_service_->set_offline(false);
412   base::RunLoop().RunUntilIdle();
413
414   // Fetched and uploaded.
415   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
416   EXPECT_TRUE(cache_entry.is_present());
417   EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
418   EXPECT_FALSE(cache_entry.is_dirty());
419 }
420
421 TEST_F(SyncClientTest, ScheduleRerun) {
422   // Add a fetch task for "foo", this should result in being paused.
423   drive_service_->set_resource_id_to_be_paused(resource_ids_["foo"]);
424   sync_client_->AddFetchTask(GetLocalId("foo"));
425   base::RunLoop().RunUntilIdle();
426
427   // While the first task is paused, add a task again.
428   // This results in scheduling rerun of the task.
429   sync_client_->AddFetchTask(GetLocalId("foo"));
430   base::RunLoop().RunUntilIdle();
431
432   // Resume the paused task.
433   drive_service_->set_resource_id_to_be_paused(std::string());
434   ASSERT_FALSE(drive_service_->paused_action().is_null());
435   drive_service_->paused_action().Run();
436   base::RunLoop().RunUntilIdle();
437
438   // Task should be run twice.
439   EXPECT_EQ(2, drive_service_->download_file_count());
440 }
441
442 TEST_F(SyncClientTest, Dependencies) {
443   // Create directories locally.
444   const base::FilePath kPath1(FILE_PATH_LITERAL("drive/root/dir1"));
445   const base::FilePath kPath2 = kPath1.AppendASCII("dir2");
446
447   ResourceEntry parent;
448   EXPECT_EQ(FILE_ERROR_OK,
449             metadata_->GetResourceEntryByPath(kPath1.DirName(), &parent));
450
451   ResourceEntry entry1;
452   entry1.set_parent_local_id(parent.local_id());
453   entry1.set_title(kPath1.BaseName().AsUTF8Unsafe());
454   entry1.mutable_file_info()->set_is_directory(true);
455   entry1.set_metadata_edit_state(ResourceEntry::DIRTY);
456   std::string local_id1;
457   EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry1, &local_id1));
458
459   ResourceEntry entry2;
460   entry2.set_parent_local_id(local_id1);
461   entry2.set_title(kPath2.BaseName().AsUTF8Unsafe());
462   entry2.mutable_file_info()->set_is_directory(true);
463   entry2.set_metadata_edit_state(ResourceEntry::DIRTY);
464   std::string local_id2;
465   EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry2, &local_id2));
466
467   // Start syncing the child first.
468   sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id2);
469   base::RunLoop().RunUntilIdle();
470   // Start syncing the parent later.
471   sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id1);
472   base::RunLoop().RunUntilIdle();
473
474   // Both entries are synced.
475   EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id1, &entry1));
476   EXPECT_EQ(ResourceEntry::CLEAN, entry1.metadata_edit_state());
477   EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id2, &entry2));
478   EXPECT_EQ(ResourceEntry::CLEAN, entry2.metadata_edit_state());
479 }
480
481 }  // namespace internal
482 }  // namespace drive