blob: ac74b81ed3d1899dad047fddce906183df76b898 [file] [log] [blame]
[email protected]92b84f332012-03-21 20:45:211// 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
msramek4cfdf21b2016-01-06 11:59:025#include "components/drive/sync_client.h"
[email protected]92b84f332012-03-21 20:45:216
dchengf42750232016-04-12 04:12:277#include <memory>
8
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0410#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2311#include "base/files/scoped_temp_dir.h"
[email protected]b568b882013-06-10 04:38:0712#include "base/run_loop.h"
pranay.kumar229290b52015-05-14 05:21:0413#include "base/single_thread_task_runner.h"
[email protected]b83e5202012-06-27 07:50:2414#include "base/test/test_timeouts.h"
pranay.kumar229290b52015-05-14 05:21:0415#include "base/thread_task_runner_handle.h"
msramek4cfdf21b2016-01-06 11:59:0216#include "components/drive/change_list_loader.h"
lukasza01b9d55a2015-07-21 15:19:2517#include "components/drive/drive.pb.h"
msramek4cfdf21b2016-01-06 11:59:0218#include "components/drive/drive_test_util.h"
lukasza8acc4eb2015-07-20 20:57:2019#include "components/drive/event_logger.h"
msramek4cfdf21b2016-01-06 11:59:0220#include "components/drive/fake_free_disk_space_getter.h"
21#include "components/drive/file_cache.h"
lukasza76b4a982015-08-08 00:36:3922#include "components/drive/file_change.h"
msramek4cfdf21b2016-01-06 11:59:0223#include "components/drive/file_system/move_operation.h"
24#include "components/drive/file_system/operation_delegate.h"
25#include "components/drive/file_system/remove_operation.h"
lukasza6364a022015-08-21 01:13:2426#include "components/drive/file_system_core_util.h"
lukaszab371ff4f2015-08-13 18:23:4727#include "components/drive/job_scheduler.h"
lukasza6364a022015-08-21 01:13:2428#include "components/drive/resource_entry_conversion.h"
msramek4cfdf21b2016-01-06 11:59:0229#include "components/drive/resource_metadata.h"
lukasza8acc4eb2015-07-20 20:57:2030#include "components/drive/service/fake_drive_service.h"
brettw066508682016-02-03 08:22:0231#include "components/prefs/testing_pref_service.h"
[email protected]b568b882013-06-10 04:38:0732#include "content/public/test/test_browser_thread_bundle.h"
[email protected]31258442014-06-05 03:37:4133#include "google_apis/drive/drive_api_parser.h"
[email protected]e196bef32013-12-03 05:33:1334#include "google_apis/drive/test_util.h"
[email protected]92b84f332012-03-21 20:45:2135#include "testing/gtest/include/gtest/gtest.h"
36
[email protected]d9d04df2012-10-12 07:06:3537namespace drive {
[email protected]4e87c4862013-05-20 04:06:3238namespace internal {
[email protected]92b84f332012-03-21 20:45:2139
[email protected]80199362012-09-09 23:24:2940namespace {
41
[email protected]7a6860162013-07-03 12:31:4642// The content of files initially stored in the cache.
[email protected]b568b882013-06-10 04:38:0743const char kLocalContent[] = "Hello!";
[email protected]61a41c12013-06-06 06:23:3644
[email protected]b568b882013-06-10 04:38:0745// The content of files stored in the service.
46const char kRemoteContent[] = "World!";
47
satorux1e04b422015-01-29 07:50:5348// SyncClientTestDriveService will return DRIVE_CANCELLED when a request is
[email protected]b568b882013-06-10 04:38:0749// made with the specified resource ID.
[email protected]e50af7652013-06-20 06:39:3150class SyncClientTestDriveService : public ::drive::FakeDriveService {
[email protected]67e264a2013-06-03 05:39:0151 public:
[email protected]799d4ec2013-12-06 07:39:2352 SyncClientTestDriveService() : download_file_count_(0) {}
53
[email protected]b568b882013-06-10 04:38:0754 // FakeDriveService override:
dchengfe773302015-01-22 19:45:5255 google_apis::CancelCallback DownloadFile(
[email protected]7a6860162013-07-03 12:31:4656 const base::FilePath& local_cache_path,
[email protected]67e264a2013-06-03 05:39:0157 const std::string& resource_id,
[email protected]7a6860162013-07-03 12:31:4658 const google_apis::DownloadActionCallback& download_action_callback,
59 const google_apis::GetContentCallback& get_content_callback,
mostynb9108efb92014-10-03 23:46:1560 const google_apis::ProgressCallback& progress_callback) override {
[email protected]799d4ec2013-12-06 07:39:2361 ++download_file_count_;
[email protected]b568b882013-06-10 04:38:0762 if (resource_id == resource_id_to_be_cancelled_) {
pranay.kumar229290b52015-05-14 05:21:0463 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]b568b882013-06-10 04:38:0764 FROM_HERE,
[email protected]7a6860162013-07-03 12:31:4665 base::Bind(download_action_callback,
satorux1e04b422015-01-29 07:50:5366 google_apis::DRIVE_CANCELLED,
[email protected]7a6860162013-07-03 12:31:4667 base::FilePath()));
[email protected]cef0f062013-06-14 16:13:0768 return google_apis::CancelCallback();
[email protected]61a41c12013-06-06 06:23:3669 }
[email protected]799d4ec2013-12-06 07:39:2370 if (resource_id == resource_id_to_be_paused_) {
71 paused_action_ = base::Bind(download_action_callback,
satorux1e04b422015-01-29 07:50:5372 google_apis::DRIVE_OTHER_ERROR,
[email protected]799d4ec2013-12-06 07:39:2373 base::FilePath());
74 return google_apis::CancelCallback();
75 }
[email protected]7a6860162013-07-03 12:31:4676 return FakeDriveService::DownloadFile(local_cache_path,
77 resource_id,
78 download_action_callback,
79 get_content_callback,
80 progress_callback);
[email protected]67e264a2013-06-03 05:39:0181 }
[email protected]aaa70a42012-06-05 22:22:5182
[email protected]799d4ec2013-12-06 07:39:2383 int download_file_count() const { return download_file_count_; }
84
[email protected]b568b882013-06-10 04:38:0785 void set_resource_id_to_be_cancelled(const std::string& resource_id) {
86 resource_id_to_be_cancelled_ = resource_id;
[email protected]67e264a2013-06-03 05:39:0187 }
[email protected]b83e5202012-06-27 07:50:2488
[email protected]799d4ec2013-12-06 07:39:2389 void set_resource_id_to_be_paused(const std::string& resource_id) {
90 resource_id_to_be_paused_ = resource_id;
91 }
92
93 const base::Closure& paused_action() const { return paused_action_; }
94
[email protected]67e264a2013-06-03 05:39:0195 private:
[email protected]799d4ec2013-12-06 07:39:2396 int download_file_count_;
[email protected]b568b882013-06-10 04:38:0797 std::string resource_id_to_be_cancelled_;
[email protected]799d4ec2013-12-06 07:39:2398 std::string resource_id_to_be_paused_;
99 base::Closure paused_action_;
[email protected]b568b882013-06-10 04:38:07100};
101
[email protected]80199362012-09-09 23:24:29102} // namespace
103
[email protected]d876d70b2013-04-23 20:06:15104class SyncClientTest : public testing::Test {
[email protected]ffd60e432012-03-24 20:36:00105 public:
dchengfe773302015-01-22 19:45:52106 void SetUp() override {
[email protected]92b84f332012-03-21 20:45:21107 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
108
[email protected]db20d212013-07-08 07:18:16109 pref_service_.reset(new TestingPrefServiceSimple);
110 test_util::RegisterDrivePrefs(pref_service_->registry());
[email protected]b568b882013-06-10 04:38:07111
[email protected]d2c08b72013-07-02 08:23:48112 fake_network_change_notifier_.reset(
113 new test_util::FakeNetworkChangeNotifier);
114
[email protected]9475cc72014-02-05 01:50:57115 logger_.reset(new EventLogger);
116
[email protected]b568b882013-06-10 04:38:07117 drive_service_.reset(new SyncClientTestDriveService);
[email protected]b568b882013-06-10 04:38:07118
pranay.kumar229290b52015-05-14 05:21:04119 scheduler_.reset(new JobScheduler(
120 pref_service_.get(),
121 logger_.get(),
122 drive_service_.get(),
123 base::ThreadTaskRunnerHandle::Get().get()));
[email protected]be427d72013-06-21 07:09:27124
125 metadata_storage_.reset(new ResourceMetadataStorage(
pranay.kumar229290b52015-05-14 05:21:04126 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get().get()));
[email protected]be427d72013-06-21 07:09:27127 ASSERT_TRUE(metadata_storage_->Initialize());
128
[email protected]2df61e12013-06-21 16:00:09129 cache_.reset(new FileCache(metadata_storage_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12130 temp_dir_.path(),
pranay.kumar229290b52015-05-14 05:21:04131 base::ThreadTaskRunnerHandle::Get().get(),
[email protected]b568b882013-06-10 04:38:07132 NULL /* free_disk_space_getter */));
[email protected]34a1bbf32013-06-17 07:24:02133 ASSERT_TRUE(cache_->Initialize());
[email protected]e63bdc972012-11-22 12:10:48134
[email protected]1c98a9ee2014-05-14 04:01:55135 metadata_.reset(new internal::ResourceMetadata(
136 metadata_storage_.get(), cache_.get(),
pranay.kumar229290b52015-05-14 05:21:04137 base::ThreadTaskRunnerHandle::Get()));
[email protected]1c98a9ee2014-05-14 04:01:55138 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
139
[email protected]5a06ba82014-02-10 06:00:06140 about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
[email protected]20f33e62014-01-30 10:50:39141 loader_controller_.reset(new LoaderController);
[email protected]069011f2014-01-29 07:56:59142 change_list_loader_.reset(new ChangeListLoader(
[email protected]9475cc72014-02-05 01:50:57143 logger_.get(),
pranay.kumar229290b52015-05-14 05:21:04144 base::ThreadTaskRunnerHandle::Get().get(),
[email protected]069011f2014-01-29 07:56:59145 metadata_.get(),
146 scheduler_.get(),
[email protected]5a06ba82014-02-10 06:00:06147 about_resource_loader_.get(),
[email protected]20f33e62014-01-30 10:50:39148 loader_controller_.get()));
[email protected]b568b882013-06-10 04:38:07149 ASSERT_NO_FATAL_FAILURE(SetUpTestData());
150
pranay.kumar229290b52015-05-14 05:21:04151 sync_client_.reset(new SyncClient(base::ThreadTaskRunnerHandle::Get().get(),
[email protected]4ffef212014-07-24 02:33:18152 &delegate_,
[email protected]b568b882013-06-10 04:38:07153 scheduler_.get(),
154 metadata_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12155 cache_.get(),
[email protected]20f33e62014-01-30 10:50:39156 loader_controller_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12157 temp_dir_.path()));
[email protected]e0529fc2012-06-12 11:07:58158
[email protected]b83e5202012-06-27 07:50:24159 // Disable delaying so that DoSyncLoop() starts immediately.
160 sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0));
[email protected]92b84f332012-03-21 20:45:21161 }
162
[email protected]b568b882013-06-10 04:38:07163 // Adds a file to the service root and |resource_ids_|.
164 void AddFileEntry(const std::string& title) {
satorux1e04b422015-01-29 07:50:53165 google_apis::DriveApiErrorCode error = google_apis::DRIVE_FILE_ERROR;
dchengf42750232016-04-12 04:12:27166 std::unique_ptr<google_apis::FileResource> entry;
[email protected]b568b882013-06-10 04:38:07167 drive_service_->AddNewFile(
168 "text/plain",
169 kRemoteContent,
170 drive_service_->GetRootResourceId(),
171 title,
172 false, // shared_with_me
173 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
174 base::RunLoop().RunUntilIdle();
175 ASSERT_EQ(google_apis::HTTP_CREATED, error);
176 ASSERT_TRUE(entry);
[email protected]31258442014-06-05 03:37:41177 resource_ids_[title] = entry->file_id();
[email protected]b568b882013-06-10 04:38:07178 }
179
180 // Sets up data for tests.
181 void SetUpTestData() {
[email protected]e63bdc972012-11-22 12:10:48182 // Prepare a temp file.
[email protected]650b2d52013-02-10 03:41:45183 base::FilePath temp_file;
[email protected]03d9afc02013-12-03 17:55:52184 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file));
[email protected]b568b882013-06-10 04:38:07185 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file,
186 kLocalContent));
[email protected]b83e5202012-06-27 07:50:24187
[email protected]efd37ff2013-10-11 03:27:57188 // Add file entries to the service.
[email protected]b568b882013-06-10 04:38:07189 ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo"));
[email protected]b568b882013-06-10 04:38:07190 ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar"));
[email protected]b568b882013-06-10 04:38:07191 ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz"));
[email protected]b568b882013-06-10 04:38:07192 ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched"));
[email protected]b568b882013-06-10 04:38:07193 ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty"));
[email protected]562af1c12013-11-26 10:27:53194 ASSERT_NO_FATAL_FAILURE(AddFileEntry("removed"));
[email protected]5170a132013-12-02 10:35:13195 ASSERT_NO_FATAL_FAILURE(AddFileEntry("moved"));
[email protected]adf84402012-03-25 21:56:38196
[email protected]b568b882013-06-10 04:38:07197 // Load data from the service to the metadata.
198 FileError error = FILE_ERROR_FAILED;
[email protected]53f57102014-02-26 03:39:03199 change_list_loader_->LoadIfNeeded(
[email protected]b568b882013-06-10 04:38:07200 google_apis::test_util::CreateCopyResultCallback(&error));
201 base::RunLoop().RunUntilIdle();
202 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]efd37ff2013-10-11 03:27:57203
204 // Prepare 3 pinned-but-not-present files.
205 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("foo")));
206 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("bar")));
207 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("baz")));
208
209 // Prepare a pinned-and-fetched file.
210 const std::string md5_fetched = "md5";
211 EXPECT_EQ(FILE_ERROR_OK,
212 cache_->Store(GetLocalId("fetched"), md5_fetched,
213 temp_file, FileCache::FILE_OPERATION_COPY));
214 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("fetched")));
215
216 // Prepare a pinned-and-fetched-and-dirty file.
[email protected]efd37ff2013-10-11 03:27:57217 EXPECT_EQ(FILE_ERROR_OK,
[email protected]bae99ae52014-01-29 01:13:14218 cache_->Store(GetLocalId("dirty"), std::string(),
[email protected]efd37ff2013-10-11 03:27:57219 temp_file, FileCache::FILE_OPERATION_COPY));
220 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("dirty")));
[email protected]efd37ff2013-10-11 03:27:57221
[email protected]562af1c12013-11-26 10:27:53222 // Prepare a removed file.
223 file_system::RemoveOperation remove_operation(
pranay.kumar229290b52015-05-14 05:21:04224 base::ThreadTaskRunnerHandle::Get().get(), &delegate_, metadata_.get(),
[email protected]562af1c12013-11-26 10:27:53225 cache_.get());
226 remove_operation.Remove(
[email protected]86097a0a2014-05-12 09:25:19227 util::GetDriveMyDriveRootPath().AppendASCII("removed"),
[email protected]562af1c12013-11-26 10:27:53228 false, // is_recursive
229 google_apis::test_util::CreateCopyResultCallback(&error));
230 base::RunLoop().RunUntilIdle();
231 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]5170a132013-12-02 10:35:13232
233 // Prepare a moved file.
234 file_system::MoveOperation move_operation(
pranay.kumar229290b52015-05-14 05:21:04235 base::ThreadTaskRunnerHandle::Get().get(), &delegate_, metadata_.get());
[email protected]5170a132013-12-02 10:35:13236 move_operation.Move(
[email protected]86097a0a2014-05-12 09:25:19237 util::GetDriveMyDriveRootPath().AppendASCII("moved"),
[email protected]5170a132013-12-02 10:35:13238 util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"),
[email protected]5170a132013-12-02 10:35:13239 google_apis::test_util::CreateCopyResultCallback(&error));
240 base::RunLoop().RunUntilIdle();
241 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b83e5202012-06-27 07:50:24242 }
243
[email protected]ffd60e432012-03-24 20:36:00244 protected:
[email protected]efd37ff2013-10-11 03:27:57245 std::string GetLocalId(const std::string& title) {
246 EXPECT_EQ(1U, resource_ids_.count(title));
247 std::string local_id;
248 EXPECT_EQ(FILE_ERROR_OK,
249 metadata_->GetIdByResourceId(resource_ids_[title], &local_id));
250 return local_id;
251 }
252
[email protected]b568b882013-06-10 04:38:07253 content::TestBrowserThreadBundle thread_bundle_;
[email protected]ea1a3f62012-11-16 20:34:23254 base::ScopedTempDir temp_dir_;
dchengf42750232016-04-12 04:12:27255 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
256 std::unique_ptr<test_util::FakeNetworkChangeNotifier>
[email protected]d2c08b72013-07-02 08:23:48257 fake_network_change_notifier_;
dchengf42750232016-04-12 04:12:27258 std::unique_ptr<EventLogger> logger_;
259 std::unique_ptr<SyncClientTestDriveService> drive_service_;
[email protected]4ffef212014-07-24 02:33:18260 file_system::OperationDelegate delegate_;
dchengf42750232016-04-12 04:12:27261 std::unique_ptr<JobScheduler> scheduler_;
262 std::unique_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests>
263 metadata_storage_;
264 std::unique_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
265 std::unique_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
266 std::unique_ptr<AboutResourceLoader> about_resource_loader_;
267 std::unique_ptr<LoaderController> loader_controller_;
268 std::unique_ptr<ChangeListLoader> change_list_loader_;
269 std::unique_ptr<SyncClient> sync_client_;
[email protected]b568b882013-06-10 04:38:07270
271 std::map<std::string, std::string> resource_ids_; // Name-to-id map.
[email protected]92b84f332012-03-21 20:45:21272};
273
[email protected]b568b882013-06-10 04:38:07274TEST_F(SyncClientTest, StartProcessingBacklog) {
[email protected]b83e5202012-06-27 07:50:24275 sync_client_->StartProcessingBacklog();
[email protected]b568b882013-06-10 04:38:07276 base::RunLoop().RunUntilIdle();
[email protected]92b84f332012-03-21 20:45:21277
[email protected]9971db12014-05-16 06:57:12278 ResourceEntry entry;
[email protected]b568b882013-06-10 04:38:07279 // Pinned files get downloaded.
[email protected]bbd72ec2014-05-12 09:27:04280 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12281 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
282 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b83e5202012-06-27 07:50:24283
[email protected]bbd72ec2014-05-12 09:27:04284 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12285 metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
286 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07287
[email protected]bbd72ec2014-05-12 09:27:04288 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12289 metadata_->GetResourceEntryById(GetLocalId("baz"), &entry));
290 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07291
292 // Dirty file gets uploaded.
[email protected]bbd72ec2014-05-12 09:27:04293 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12294 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
295 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
[email protected]562af1c12013-11-26 10:27:53296
297 // Removed entry is not found.
satorux1e04b422015-01-29 07:50:53298 google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR;
dchengf42750232016-04-12 04:12:27299 std::unique_ptr<google_apis::FileResource> server_entry;
[email protected]31258442014-06-05 03:37:41300 drive_service_->GetFileResource(
[email protected]562af1c12013-11-26 10:27:53301 resource_ids_["removed"],
[email protected]31258442014-06-05 03:37:41302 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]562af1c12013-11-26 10:27:53303 base::RunLoop().RunUntilIdle();
304 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
[email protected]31258442014-06-05 03:37:41305 ASSERT_TRUE(server_entry);
306 EXPECT_TRUE(server_entry->labels().is_trashed());
[email protected]5170a132013-12-02 10:35:13307
308 // Moved entry was moved.
satorux1e04b422015-01-29 07:50:53309 status = google_apis::DRIVE_OTHER_ERROR;
[email protected]31258442014-06-05 03:37:41310 drive_service_->GetFileResource(
[email protected]5170a132013-12-02 10:35:13311 resource_ids_["moved"],
[email protected]31258442014-06-05 03:37:41312 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]5170a132013-12-02 10:35:13313 base::RunLoop().RunUntilIdle();
314 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
[email protected]31258442014-06-05 03:37:41315 ASSERT_TRUE(server_entry);
316 EXPECT_EQ("moved_new_title", server_entry->title());
[email protected]a5381772012-04-05 02:20:04317}
318
[email protected]bd2254d2013-06-12 16:00:47319TEST_F(SyncClientTest, AddFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57320 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07321 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01322
[email protected]9971db12014-05-16 06:57:12323 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04324 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12325 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
326 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]adf84402012-03-25 21:56:38327}
328
[email protected]bd2254d2013-06-12 16:00:47329TEST_F(SyncClientTest, AddFetchTaskAndCancelled) {
[email protected]61a41c12013-06-06 06:23:36330 // Trigger fetching of a file which results in cancellation.
[email protected]bd2254d2013-06-12 16:00:47331 drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]);
[email protected]efd37ff2013-10-11 03:27:57332 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07333 base::RunLoop().RunUntilIdle();
[email protected]61a41c12013-06-06 06:23:36334
335 // The file should be unpinned if the user wants the download to be cancelled.
[email protected]9971db12014-05-16 06:57:12336 ResourceEntry entry;
337 EXPECT_EQ(FILE_ERROR_OK,
338 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
339 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
[email protected]61a41c12013-06-06 06:23:36340}
341
[email protected]bd2254d2013-06-12 16:00:47342TEST_F(SyncClientTest, RemoveFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57343 sync_client_->AddFetchTask(GetLocalId("foo"));
344 sync_client_->AddFetchTask(GetLocalId("bar"));
345 sync_client_->AddFetchTask(GetLocalId("baz"));
[email protected]adf84402012-03-25 21:56:38346
[email protected]efd37ff2013-10-11 03:27:57347 sync_client_->RemoveFetchTask(GetLocalId("foo"));
348 sync_client_->RemoveFetchTask(GetLocalId("baz"));
[email protected]b568b882013-06-10 04:38:07349 base::RunLoop().RunUntilIdle();
[email protected]e07bbd972013-01-23 17:38:42350
[email protected]b568b882013-06-10 04:38:07351 // Only "bar" should be fetched.
[email protected]9971db12014-05-16 06:57:12352 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04353 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12354 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
355 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07356
[email protected]bbd72ec2014-05-12 09:27:04357 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12358 metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
359 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07360
[email protected]bbd72ec2014-05-12 09:27:04361 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12362 metadata_->GetResourceEntryById(GetLocalId("baz"), &entry));
363 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07364
[email protected]adf84402012-03-25 21:56:38365}
366
[email protected]d876d70b2013-04-23 20:06:15367TEST_F(SyncClientTest, ExistingPinnedFiles) {
[email protected]85b62192012-06-29 19:56:38368 // Start checking the existing pinned files. This will collect the resource
369 // IDs of pinned files, with stale local cache files.
370 sync_client_->StartCheckingExistingPinnedFiles();
[email protected]b568b882013-06-10 04:38:07371 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01372
[email protected]b568b882013-06-10 04:38:07373 // "fetched" and "dirty" are the existing pinned files.
374 // The non-dirty one should be synced, but the dirty one should not.
375 base::FilePath cache_file;
376 std::string content;
[email protected]efd37ff2013-10-11 03:27:57377 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("fetched"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50378 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07379 EXPECT_EQ(kRemoteContent, content);
380 content.clear();
381
[email protected]efd37ff2013-10-11 03:27:57382 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("dirty"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50383 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07384 EXPECT_EQ(kLocalContent, content);
[email protected]189541ba2012-10-24 11:18:15385}
386
[email protected]d2c08b72013-07-02 08:23:48387TEST_F(SyncClientTest, RetryOnDisconnection) {
388 // Let the service go down.
389 drive_service_->set_offline(true);
390 // Change the network connection state after some delay, to test that
391 // FILE_ERROR_NO_CONNECTION is handled by SyncClient correctly.
392 // Without this delay, JobScheduler will keep the jobs unrun and SyncClient
393 // will receive no error.
pranay.kumar229290b52015-05-14 05:21:04394 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
[email protected]d2c08b72013-07-02 08:23:48395 FROM_HERE,
396 base::Bind(&test_util::FakeNetworkChangeNotifier::SetConnectionType,
397 base::Unretained(fake_network_change_notifier_.get()),
398 net::NetworkChangeNotifier::CONNECTION_NONE),
399 TestTimeouts::tiny_timeout());
400
401 // Try fetch and upload.
[email protected]efd37ff2013-10-11 03:27:57402 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]bfcaaf332014-01-27 06:34:59403 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED),
[email protected]882e3202013-11-25 08:33:50404 GetLocalId("dirty"));
[email protected]d2c08b72013-07-02 08:23:48405 base::RunLoop().RunUntilIdle();
406
407 // Not yet fetched nor uploaded.
[email protected]9971db12014-05-16 06:57:12408 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04409 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12410 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
411 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]bbd72ec2014-05-12 09:27:04412 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12413 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
414 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
[email protected]d2c08b72013-07-02 08:23:48415
416 // Switch to online.
417 fake_network_change_notifier_->SetConnectionType(
418 net::NetworkChangeNotifier::CONNECTION_WIFI);
419 drive_service_->set_offline(false);
420 base::RunLoop().RunUntilIdle();
421
422 // Fetched and uploaded.
[email protected]bbd72ec2014-05-12 09:27:04423 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12424 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
425 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]bbd72ec2014-05-12 09:27:04426 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12427 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
428 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
[email protected]d2c08b72013-07-02 08:23:48429}
430
[email protected]799d4ec2013-12-06 07:39:23431TEST_F(SyncClientTest, ScheduleRerun) {
432 // Add a fetch task for "foo", this should result in being paused.
433 drive_service_->set_resource_id_to_be_paused(resource_ids_["foo"]);
434 sync_client_->AddFetchTask(GetLocalId("foo"));
435 base::RunLoop().RunUntilIdle();
436
437 // While the first task is paused, add a task again.
438 // This results in scheduling rerun of the task.
439 sync_client_->AddFetchTask(GetLocalId("foo"));
440 base::RunLoop().RunUntilIdle();
441
442 // Resume the paused task.
443 drive_service_->set_resource_id_to_be_paused(std::string());
444 ASSERT_FALSE(drive_service_->paused_action().is_null());
445 drive_service_->paused_action().Run();
446 base::RunLoop().RunUntilIdle();
447
448 // Task should be run twice.
449 EXPECT_EQ(2, drive_service_->download_file_count());
450}
451
[email protected]99bd5432014-02-03 06:53:38452TEST_F(SyncClientTest, Dependencies) {
453 // Create directories locally.
454 const base::FilePath kPath1(FILE_PATH_LITERAL("drive/root/dir1"));
455 const base::FilePath kPath2 = kPath1.AppendASCII("dir2");
456
457 ResourceEntry parent;
458 EXPECT_EQ(FILE_ERROR_OK,
459 metadata_->GetResourceEntryByPath(kPath1.DirName(), &parent));
460
461 ResourceEntry entry1;
462 entry1.set_parent_local_id(parent.local_id());
463 entry1.set_title(kPath1.BaseName().AsUTF8Unsafe());
464 entry1.mutable_file_info()->set_is_directory(true);
465 entry1.set_metadata_edit_state(ResourceEntry::DIRTY);
466 std::string local_id1;
467 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry1, &local_id1));
468
469 ResourceEntry entry2;
470 entry2.set_parent_local_id(local_id1);
471 entry2.set_title(kPath2.BaseName().AsUTF8Unsafe());
472 entry2.mutable_file_info()->set_is_directory(true);
473 entry2.set_metadata_edit_state(ResourceEntry::DIRTY);
474 std::string local_id2;
475 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry2, &local_id2));
476
477 // Start syncing the child first.
478 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id2);
[email protected]99bd5432014-02-03 06:53:38479 // Start syncing the parent later.
480 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id1);
481 base::RunLoop().RunUntilIdle();
482
483 // Both entries are synced.
484 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id1, &entry1));
485 EXPECT_EQ(ResourceEntry::CLEAN, entry1.metadata_edit_state());
486 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id2, &entry2));
487 EXPECT_EQ(ResourceEntry::CLEAN, entry2.metadata_edit_state());
488}
489
[email protected]e849db52014-07-23 07:02:09490TEST_F(SyncClientTest, WaitForUpdateTaskToComplete) {
491 // Create a directory locally.
492 const base::FilePath kPath(FILE_PATH_LITERAL("drive/root/dir1"));
493
494 ResourceEntry parent;
495 EXPECT_EQ(FILE_ERROR_OK,
496 metadata_->GetResourceEntryByPath(kPath.DirName(), &parent));
497
498 ResourceEntry entry;
499 entry.set_parent_local_id(parent.local_id());
500 entry.set_title(kPath.BaseName().AsUTF8Unsafe());
501 entry.mutable_file_info()->set_is_directory(true);
502 entry.set_metadata_edit_state(ResourceEntry::DIRTY);
503 std::string local_id;
504 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry, &local_id));
505
506 // Sync task is not yet avialable.
507 FileError error = FILE_ERROR_FAILED;
508 EXPECT_FALSE(sync_client_->WaitForUpdateTaskToComplete(
509 local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
510
511 // Start syncing the directory and wait for it to complete.
512 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id);
513
514 EXPECT_TRUE(sync_client_->WaitForUpdateTaskToComplete(
515 local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
516
517 base::RunLoop().RunUntilIdle();
518
519 // The callback is called.
520 EXPECT_EQ(FILE_ERROR_OK, error);
521}
522
[email protected]4e87c4862013-05-20 04:06:32523} // namespace internal
[email protected]d9d04df2012-10-12 07:06:35524} // namespace drive