blob: a92999b7b9b3fc9b84dea555d51c8b2b4e8967a9 [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
[email protected]d876d70b2013-04-23 20:06:155#include "chrome/browser/chromeos/drive/sync_client.h"
[email protected]92b84f332012-03-21 20:45:216
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
[email protected]92b84f332012-03-21 20:45:2110#include "base/memory/scoped_ptr.h"
[email protected]db20d212013-07-08 07:18:1611#include "base/prefs/testing_pref_service.h"
[email protected]b568b882013-06-10 04:38:0712#include "base/run_loop.h"
[email protected]b83e5202012-06-27 07:50:2413#include "base/test/test_timeouts.h"
[email protected]b568b882013-06-10 04:38:0714#include "chrome/browser/chromeos/drive/change_list_loader.h"
[email protected]15de8142012-10-11 06:00:5415#include "chrome/browser/chromeos/drive/drive.pb.h"
[email protected]d2c08b72013-07-02 08:23:4816#include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
[email protected]eca3fc92013-05-01 03:53:4017#include "chrome/browser/chromeos/drive/file_cache.h"
[email protected]741a1772014-07-01 18:40:2918#include "chrome/browser/chromeos/drive/file_change.h"
[email protected]5170a132013-12-02 10:35:1319#include "chrome/browser/chromeos/drive/file_system/move_operation.h"
[email protected]4ffef212014-07-24 02:33:1820#include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
[email protected]562af1c12013-11-26 10:27:5321#include "chrome/browser/chromeos/drive/file_system/remove_operation.h"
[email protected]5170a132013-12-02 10:35:1322#include "chrome/browser/chromeos/drive/file_system_util.h"
[email protected]b568b882013-06-10 04:38:0723#include "chrome/browser/chromeos/drive/job_scheduler.h"
24#include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
25#include "chrome/browser/chromeos/drive/resource_metadata.h"
[email protected]92931d72013-04-24 05:16:1526#include "chrome/browser/chromeos/drive/test_util.h"
[email protected]9475cc72014-02-05 01:50:5727#include "chrome/browser/drive/event_logger.h"
[email protected]0390b812013-06-19 00:27:5028#include "chrome/browser/drive/fake_drive_service.h"
[email protected]b568b882013-06-10 04:38:0729#include "content/public/test/test_browser_thread_bundle.h"
[email protected]31258442014-06-05 03:37:4130#include "google_apis/drive/drive_api_parser.h"
[email protected]e196bef32013-12-03 05:33:1331#include "google_apis/drive/test_util.h"
[email protected]92b84f332012-03-21 20:45:2132#include "testing/gtest/include/gtest/gtest.h"
33
[email protected]d9d04df2012-10-12 07:06:3534namespace drive {
[email protected]4e87c4862013-05-20 04:06:3235namespace internal {
[email protected]92b84f332012-03-21 20:45:2136
[email protected]80199362012-09-09 23:24:2937namespace {
38
[email protected]7a6860162013-07-03 12:31:4639// The content of files initially stored in the cache.
[email protected]b568b882013-06-10 04:38:0740const char kLocalContent[] = "Hello!";
[email protected]61a41c12013-06-06 06:23:3641
[email protected]b568b882013-06-10 04:38:0742// The content of files stored in the service.
43const char kRemoteContent[] = "World!";
44
45// SyncClientTestDriveService will return GDATA_CANCELLED when a request is
46// made with the specified resource ID.
[email protected]e50af7652013-06-20 06:39:3147class SyncClientTestDriveService : public ::drive::FakeDriveService {
[email protected]67e264a2013-06-03 05:39:0148 public:
[email protected]799d4ec2013-12-06 07:39:2349 SyncClientTestDriveService() : download_file_count_(0) {}
50
[email protected]b568b882013-06-10 04:38:0751 // FakeDriveService override:
[email protected]7a6860162013-07-03 12:31:4652 virtual google_apis::CancelCallback DownloadFile(
53 const base::FilePath& local_cache_path,
[email protected]67e264a2013-06-03 05:39:0154 const std::string& resource_id,
[email protected]7a6860162013-07-03 12:31:4655 const google_apis::DownloadActionCallback& download_action_callback,
56 const google_apis::GetContentCallback& get_content_callback,
mostynb9108efb92014-10-03 23:46:1557 const google_apis::ProgressCallback& progress_callback) override {
[email protected]799d4ec2013-12-06 07:39:2358 ++download_file_count_;
[email protected]b568b882013-06-10 04:38:0759 if (resource_id == resource_id_to_be_cancelled_) {
[email protected]b568b882013-06-10 04:38:0760 base::MessageLoopProxy::current()->PostTask(
61 FROM_HERE,
[email protected]7a6860162013-07-03 12:31:4662 base::Bind(download_action_callback,
[email protected]b568b882013-06-10 04:38:0763 google_apis::GDATA_CANCELLED,
[email protected]7a6860162013-07-03 12:31:4664 base::FilePath()));
[email protected]cef0f062013-06-14 16:13:0765 return google_apis::CancelCallback();
[email protected]61a41c12013-06-06 06:23:3666 }
[email protected]799d4ec2013-12-06 07:39:2367 if (resource_id == resource_id_to_be_paused_) {
68 paused_action_ = base::Bind(download_action_callback,
69 google_apis::GDATA_OTHER_ERROR,
70 base::FilePath());
71 return google_apis::CancelCallback();
72 }
[email protected]7a6860162013-07-03 12:31:4673 return FakeDriveService::DownloadFile(local_cache_path,
74 resource_id,
75 download_action_callback,
76 get_content_callback,
77 progress_callback);
[email protected]67e264a2013-06-03 05:39:0178 }
[email protected]aaa70a42012-06-05 22:22:5179
[email protected]799d4ec2013-12-06 07:39:2380 int download_file_count() const { return download_file_count_; }
81
[email protected]b568b882013-06-10 04:38:0782 void set_resource_id_to_be_cancelled(const std::string& resource_id) {
83 resource_id_to_be_cancelled_ = resource_id;
[email protected]67e264a2013-06-03 05:39:0184 }
[email protected]b83e5202012-06-27 07:50:2485
[email protected]799d4ec2013-12-06 07:39:2386 void set_resource_id_to_be_paused(const std::string& resource_id) {
87 resource_id_to_be_paused_ = resource_id;
88 }
89
90 const base::Closure& paused_action() const { return paused_action_; }
91
[email protected]67e264a2013-06-03 05:39:0192 private:
[email protected]799d4ec2013-12-06 07:39:2393 int download_file_count_;
[email protected]b568b882013-06-10 04:38:0794 std::string resource_id_to_be_cancelled_;
[email protected]799d4ec2013-12-06 07:39:2395 std::string resource_id_to_be_paused_;
96 base::Closure paused_action_;
[email protected]b568b882013-06-10 04:38:0797};
98
[email protected]80199362012-09-09 23:24:2999} // namespace
100
[email protected]d876d70b2013-04-23 20:06:15101class SyncClientTest : public testing::Test {
[email protected]ffd60e432012-03-24 20:36:00102 public:
mostynb9108efb92014-10-03 23:46:15103 virtual void SetUp() override {
[email protected]92b84f332012-03-21 20:45:21104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
105
[email protected]db20d212013-07-08 07:18:16106 pref_service_.reset(new TestingPrefServiceSimple);
107 test_util::RegisterDrivePrefs(pref_service_->registry());
[email protected]b568b882013-06-10 04:38:07108
[email protected]d2c08b72013-07-02 08:23:48109 fake_network_change_notifier_.reset(
110 new test_util::FakeNetworkChangeNotifier);
111
[email protected]9475cc72014-02-05 01:50:57112 logger_.reset(new EventLogger);
113
[email protected]b568b882013-06-10 04:38:07114 drive_service_.reset(new SyncClientTestDriveService);
[email protected]b568b882013-06-10 04:38:07115
[email protected]db20d212013-07-08 07:18:16116 scheduler_.reset(new JobScheduler(pref_service_.get(),
[email protected]9475cc72014-02-05 01:50:57117 logger_.get(),
[email protected]3cd8a6a2013-07-03 22:01:55118 drive_service_.get(),
119 base::MessageLoopProxy::current().get()));
[email protected]be427d72013-06-21 07:09:27120
121 metadata_storage_.reset(new ResourceMetadataStorage(
[email protected]3cd8a6a2013-07-03 22:01:55122 temp_dir_.path(), base::MessageLoopProxy::current().get()));
[email protected]be427d72013-06-21 07:09:27123 ASSERT_TRUE(metadata_storage_->Initialize());
124
[email protected]2df61e12013-06-21 16:00:09125 cache_.reset(new FileCache(metadata_storage_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12126 temp_dir_.path(),
[email protected]3cd8a6a2013-07-03 22:01:55127 base::MessageLoopProxy::current().get(),
[email protected]b568b882013-06-10 04:38:07128 NULL /* free_disk_space_getter */));
[email protected]34a1bbf32013-06-17 07:24:02129 ASSERT_TRUE(cache_->Initialize());
[email protected]e63bdc972012-11-22 12:10:48130
[email protected]1c98a9ee2014-05-14 04:01:55131 metadata_.reset(new internal::ResourceMetadata(
132 metadata_storage_.get(), cache_.get(),
133 base::MessageLoopProxy::current()));
134 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
135
[email protected]5a06ba82014-02-10 06:00:06136 about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
[email protected]20f33e62014-01-30 10:50:39137 loader_controller_.reset(new LoaderController);
[email protected]069011f2014-01-29 07:56:59138 change_list_loader_.reset(new ChangeListLoader(
[email protected]9475cc72014-02-05 01:50:57139 logger_.get(),
[email protected]069011f2014-01-29 07:56:59140 base::MessageLoopProxy::current().get(),
141 metadata_.get(),
142 scheduler_.get(),
[email protected]5a06ba82014-02-10 06:00:06143 about_resource_loader_.get(),
[email protected]20f33e62014-01-30 10:50:39144 loader_controller_.get()));
[email protected]b568b882013-06-10 04:38:07145 ASSERT_NO_FATAL_FAILURE(SetUpTestData());
146
[email protected]3cd8a6a2013-07-03 22:01:55147 sync_client_.reset(new SyncClient(base::MessageLoopProxy::current().get(),
[email protected]4ffef212014-07-24 02:33:18148 &delegate_,
[email protected]b568b882013-06-10 04:38:07149 scheduler_.get(),
150 metadata_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12151 cache_.get(),
[email protected]20f33e62014-01-30 10:50:39152 loader_controller_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12153 temp_dir_.path()));
[email protected]e0529fc2012-06-12 11:07:58154
[email protected]b83e5202012-06-27 07:50:24155 // Disable delaying so that DoSyncLoop() starts immediately.
156 sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0));
[email protected]92b84f332012-03-21 20:45:21157 }
158
[email protected]b568b882013-06-10 04:38:07159 // Adds a file to the service root and |resource_ids_|.
160 void AddFileEntry(const std::string& title) {
161 google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
[email protected]31258442014-06-05 03:37:41162 scoped_ptr<google_apis::FileResource> entry;
[email protected]b568b882013-06-10 04:38:07163 drive_service_->AddNewFile(
164 "text/plain",
165 kRemoteContent,
166 drive_service_->GetRootResourceId(),
167 title,
168 false, // shared_with_me
169 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
170 base::RunLoop().RunUntilIdle();
171 ASSERT_EQ(google_apis::HTTP_CREATED, error);
172 ASSERT_TRUE(entry);
[email protected]31258442014-06-05 03:37:41173 resource_ids_[title] = entry->file_id();
[email protected]b568b882013-06-10 04:38:07174 }
175
176 // Sets up data for tests.
177 void SetUpTestData() {
[email protected]e63bdc972012-11-22 12:10:48178 // Prepare a temp file.
[email protected]650b2d52013-02-10 03:41:45179 base::FilePath temp_file;
[email protected]03d9afc02013-12-03 17:55:52180 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file));
[email protected]b568b882013-06-10 04:38:07181 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file,
182 kLocalContent));
[email protected]b83e5202012-06-27 07:50:24183
[email protected]efd37ff2013-10-11 03:27:57184 // Add file entries to the service.
[email protected]b568b882013-06-10 04:38:07185 ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo"));
[email protected]b568b882013-06-10 04:38:07186 ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar"));
[email protected]b568b882013-06-10 04:38:07187 ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz"));
[email protected]b568b882013-06-10 04:38:07188 ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched"));
[email protected]b568b882013-06-10 04:38:07189 ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty"));
[email protected]562af1c12013-11-26 10:27:53190 ASSERT_NO_FATAL_FAILURE(AddFileEntry("removed"));
[email protected]5170a132013-12-02 10:35:13191 ASSERT_NO_FATAL_FAILURE(AddFileEntry("moved"));
[email protected]adf84402012-03-25 21:56:38192
[email protected]b568b882013-06-10 04:38:07193 // Load data from the service to the metadata.
194 FileError error = FILE_ERROR_FAILED;
[email protected]53f57102014-02-26 03:39:03195 change_list_loader_->LoadIfNeeded(
[email protected]b568b882013-06-10 04:38:07196 google_apis::test_util::CreateCopyResultCallback(&error));
197 base::RunLoop().RunUntilIdle();
198 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]efd37ff2013-10-11 03:27:57199
200 // Prepare 3 pinned-but-not-present files.
201 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("foo")));
202 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("bar")));
203 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("baz")));
204
205 // Prepare a pinned-and-fetched file.
206 const std::string md5_fetched = "md5";
207 EXPECT_EQ(FILE_ERROR_OK,
208 cache_->Store(GetLocalId("fetched"), md5_fetched,
209 temp_file, FileCache::FILE_OPERATION_COPY));
210 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("fetched")));
211
212 // Prepare a pinned-and-fetched-and-dirty file.
[email protected]efd37ff2013-10-11 03:27:57213 EXPECT_EQ(FILE_ERROR_OK,
[email protected]bae99ae52014-01-29 01:13:14214 cache_->Store(GetLocalId("dirty"), std::string(),
[email protected]efd37ff2013-10-11 03:27:57215 temp_file, FileCache::FILE_OPERATION_COPY));
216 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("dirty")));
[email protected]efd37ff2013-10-11 03:27:57217
[email protected]562af1c12013-11-26 10:27:53218 // Prepare a removed file.
219 file_system::RemoveOperation remove_operation(
[email protected]4ffef212014-07-24 02:33:18220 base::MessageLoopProxy::current().get(), &delegate_, metadata_.get(),
[email protected]562af1c12013-11-26 10:27:53221 cache_.get());
222 remove_operation.Remove(
[email protected]86097a0a2014-05-12 09:25:19223 util::GetDriveMyDriveRootPath().AppendASCII("removed"),
[email protected]562af1c12013-11-26 10:27:53224 false, // is_recursive
225 google_apis::test_util::CreateCopyResultCallback(&error));
226 base::RunLoop().RunUntilIdle();
227 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]5170a132013-12-02 10:35:13228
229 // Prepare a moved file.
230 file_system::MoveOperation move_operation(
[email protected]4ffef212014-07-24 02:33:18231 base::MessageLoopProxy::current().get(), &delegate_, metadata_.get());
[email protected]5170a132013-12-02 10:35:13232 move_operation.Move(
[email protected]86097a0a2014-05-12 09:25:19233 util::GetDriveMyDriveRootPath().AppendASCII("moved"),
[email protected]5170a132013-12-02 10:35:13234 util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"),
[email protected]5170a132013-12-02 10:35:13235 google_apis::test_util::CreateCopyResultCallback(&error));
236 base::RunLoop().RunUntilIdle();
237 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b83e5202012-06-27 07:50:24238 }
239
[email protected]ffd60e432012-03-24 20:36:00240 protected:
[email protected]efd37ff2013-10-11 03:27:57241 std::string GetLocalId(const std::string& title) {
242 EXPECT_EQ(1U, resource_ids_.count(title));
243 std::string local_id;
244 EXPECT_EQ(FILE_ERROR_OK,
245 metadata_->GetIdByResourceId(resource_ids_[title], &local_id));
246 return local_id;
247 }
248
[email protected]b568b882013-06-10 04:38:07249 content::TestBrowserThreadBundle thread_bundle_;
[email protected]ea1a3f62012-11-16 20:34:23250 base::ScopedTempDir temp_dir_;
[email protected]db20d212013-07-08 07:18:16251 scoped_ptr<TestingPrefServiceSimple> pref_service_;
[email protected]d2c08b72013-07-02 08:23:48252 scoped_ptr<test_util::FakeNetworkChangeNotifier>
253 fake_network_change_notifier_;
[email protected]9475cc72014-02-05 01:50:57254 scoped_ptr<EventLogger> logger_;
[email protected]b568b882013-06-10 04:38:07255 scoped_ptr<SyncClientTestDriveService> drive_service_;
[email protected]4ffef212014-07-24 02:33:18256 file_system::OperationDelegate delegate_;
[email protected]b568b882013-06-10 04:38:07257 scoped_ptr<JobScheduler> scheduler_;
[email protected]be427d72013-06-21 07:09:27258 scoped_ptr<ResourceMetadataStorage,
259 test_util::DestroyHelperForTests> metadata_storage_;
[email protected]4e87c4862013-05-20 04:06:32260 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
[email protected]1c98a9ee2014-05-14 04:01:55261 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
[email protected]5a06ba82014-02-10 06:00:06262 scoped_ptr<AboutResourceLoader> about_resource_loader_;
[email protected]20f33e62014-01-30 10:50:39263 scoped_ptr<LoaderController> loader_controller_;
[email protected]069011f2014-01-29 07:56:59264 scoped_ptr<ChangeListLoader> change_list_loader_;
[email protected]d876d70b2013-04-23 20:06:15265 scoped_ptr<SyncClient> sync_client_;
[email protected]b568b882013-06-10 04:38:07266
267 std::map<std::string, std::string> resource_ids_; // Name-to-id map.
[email protected]92b84f332012-03-21 20:45:21268};
269
[email protected]b568b882013-06-10 04:38:07270TEST_F(SyncClientTest, StartProcessingBacklog) {
[email protected]b83e5202012-06-27 07:50:24271 sync_client_->StartProcessingBacklog();
[email protected]b568b882013-06-10 04:38:07272 base::RunLoop().RunUntilIdle();
[email protected]92b84f332012-03-21 20:45:21273
[email protected]9971db12014-05-16 06:57:12274 ResourceEntry entry;
[email protected]b568b882013-06-10 04:38:07275 // Pinned files get downloaded.
[email protected]bbd72ec2014-05-12 09:27:04276 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12277 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
278 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b83e5202012-06-27 07:50:24279
[email protected]bbd72ec2014-05-12 09:27:04280 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12281 metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
282 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07283
[email protected]bbd72ec2014-05-12 09:27:04284 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12285 metadata_->GetResourceEntryById(GetLocalId("baz"), &entry));
286 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07287
288 // Dirty file gets uploaded.
[email protected]bbd72ec2014-05-12 09:27:04289 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12290 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
291 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
[email protected]562af1c12013-11-26 10:27:53292
293 // Removed entry is not found.
294 google_apis::GDataErrorCode status = google_apis::GDATA_OTHER_ERROR;
[email protected]31258442014-06-05 03:37:41295 scoped_ptr<google_apis::FileResource> server_entry;
296 drive_service_->GetFileResource(
[email protected]562af1c12013-11-26 10:27:53297 resource_ids_["removed"],
[email protected]31258442014-06-05 03:37:41298 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]562af1c12013-11-26 10:27:53299 base::RunLoop().RunUntilIdle();
300 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
[email protected]31258442014-06-05 03:37:41301 ASSERT_TRUE(server_entry);
302 EXPECT_TRUE(server_entry->labels().is_trashed());
[email protected]5170a132013-12-02 10:35:13303
304 // Moved entry was moved.
305 status = google_apis::GDATA_OTHER_ERROR;
[email protected]31258442014-06-05 03:37:41306 drive_service_->GetFileResource(
[email protected]5170a132013-12-02 10:35:13307 resource_ids_["moved"],
[email protected]31258442014-06-05 03:37:41308 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]5170a132013-12-02 10:35:13309 base::RunLoop().RunUntilIdle();
310 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
[email protected]31258442014-06-05 03:37:41311 ASSERT_TRUE(server_entry);
312 EXPECT_EQ("moved_new_title", server_entry->title());
[email protected]a5381772012-04-05 02:20:04313}
314
[email protected]bd2254d2013-06-12 16:00:47315TEST_F(SyncClientTest, AddFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57316 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07317 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01318
[email protected]9971db12014-05-16 06:57:12319 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04320 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12321 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
322 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]adf84402012-03-25 21:56:38323}
324
[email protected]bd2254d2013-06-12 16:00:47325TEST_F(SyncClientTest, AddFetchTaskAndCancelled) {
[email protected]61a41c12013-06-06 06:23:36326 // Trigger fetching of a file which results in cancellation.
[email protected]bd2254d2013-06-12 16:00:47327 drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]);
[email protected]efd37ff2013-10-11 03:27:57328 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07329 base::RunLoop().RunUntilIdle();
[email protected]61a41c12013-06-06 06:23:36330
331 // The file should be unpinned if the user wants the download to be cancelled.
[email protected]9971db12014-05-16 06:57:12332 ResourceEntry entry;
333 EXPECT_EQ(FILE_ERROR_OK,
334 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
335 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
[email protected]61a41c12013-06-06 06:23:36336}
337
[email protected]bd2254d2013-06-12 16:00:47338TEST_F(SyncClientTest, RemoveFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57339 sync_client_->AddFetchTask(GetLocalId("foo"));
340 sync_client_->AddFetchTask(GetLocalId("bar"));
341 sync_client_->AddFetchTask(GetLocalId("baz"));
[email protected]adf84402012-03-25 21:56:38342
[email protected]efd37ff2013-10-11 03:27:57343 sync_client_->RemoveFetchTask(GetLocalId("foo"));
344 sync_client_->RemoveFetchTask(GetLocalId("baz"));
[email protected]b568b882013-06-10 04:38:07345 base::RunLoop().RunUntilIdle();
[email protected]e07bbd972013-01-23 17:38:42346
[email protected]b568b882013-06-10 04:38:07347 // Only "bar" should be fetched.
[email protected]9971db12014-05-16 06:57:12348 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04349 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12350 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
351 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07352
[email protected]bbd72ec2014-05-12 09:27:04353 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12354 metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
355 EXPECT_TRUE(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("baz"), &entry));
359 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07360
[email protected]adf84402012-03-25 21:56:38361}
362
[email protected]d876d70b2013-04-23 20:06:15363TEST_F(SyncClientTest, ExistingPinnedFiles) {
[email protected]85b62192012-06-29 19:56:38364 // Start checking the existing pinned files. This will collect the resource
365 // IDs of pinned files, with stale local cache files.
366 sync_client_->StartCheckingExistingPinnedFiles();
[email protected]b568b882013-06-10 04:38:07367 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01368
[email protected]b568b882013-06-10 04:38:07369 // "fetched" and "dirty" are the existing pinned files.
370 // The non-dirty one should be synced, but the dirty one should not.
371 base::FilePath cache_file;
372 std::string content;
[email protected]efd37ff2013-10-11 03:27:57373 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("fetched"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50374 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07375 EXPECT_EQ(kRemoteContent, content);
376 content.clear();
377
[email protected]efd37ff2013-10-11 03:27:57378 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("dirty"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50379 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07380 EXPECT_EQ(kLocalContent, content);
[email protected]189541ba2012-10-24 11:18:15381}
382
[email protected]d2c08b72013-07-02 08:23:48383TEST_F(SyncClientTest, RetryOnDisconnection) {
384 // Let the service go down.
385 drive_service_->set_offline(true);
386 // Change the network connection state after some delay, to test that
387 // FILE_ERROR_NO_CONNECTION is handled by SyncClient correctly.
388 // Without this delay, JobScheduler will keep the jobs unrun and SyncClient
389 // will receive no error.
390 base::MessageLoopProxy::current()->PostDelayedTask(
391 FROM_HERE,
392 base::Bind(&test_util::FakeNetworkChangeNotifier::SetConnectionType,
393 base::Unretained(fake_network_change_notifier_.get()),
394 net::NetworkChangeNotifier::CONNECTION_NONE),
395 TestTimeouts::tiny_timeout());
396
397 // Try fetch and upload.
[email protected]efd37ff2013-10-11 03:27:57398 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]bfcaaf332014-01-27 06:34:59399 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED),
[email protected]882e3202013-11-25 08:33:50400 GetLocalId("dirty"));
[email protected]d2c08b72013-07-02 08:23:48401 base::RunLoop().RunUntilIdle();
402
403 // Not yet fetched nor uploaded.
[email protected]9971db12014-05-16 06:57:12404 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04405 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12406 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
407 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]bbd72ec2014-05-12 09:27:04408 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12409 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
410 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
[email protected]d2c08b72013-07-02 08:23:48411
412 // Switch to online.
413 fake_network_change_notifier_->SetConnectionType(
414 net::NetworkChangeNotifier::CONNECTION_WIFI);
415 drive_service_->set_offline(false);
416 base::RunLoop().RunUntilIdle();
417
418 // Fetched and uploaded.
[email protected]bbd72ec2014-05-12 09:27:04419 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12420 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
421 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]bbd72ec2014-05-12 09:27:04422 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12423 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
424 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
[email protected]d2c08b72013-07-02 08:23:48425}
426
[email protected]799d4ec2013-12-06 07:39:23427TEST_F(SyncClientTest, ScheduleRerun) {
428 // Add a fetch task for "foo", this should result in being paused.
429 drive_service_->set_resource_id_to_be_paused(resource_ids_["foo"]);
430 sync_client_->AddFetchTask(GetLocalId("foo"));
431 base::RunLoop().RunUntilIdle();
432
433 // While the first task is paused, add a task again.
434 // This results in scheduling rerun of the task.
435 sync_client_->AddFetchTask(GetLocalId("foo"));
436 base::RunLoop().RunUntilIdle();
437
438 // Resume the paused task.
439 drive_service_->set_resource_id_to_be_paused(std::string());
440 ASSERT_FALSE(drive_service_->paused_action().is_null());
441 drive_service_->paused_action().Run();
442 base::RunLoop().RunUntilIdle();
443
444 // Task should be run twice.
445 EXPECT_EQ(2, drive_service_->download_file_count());
446}
447
[email protected]99bd5432014-02-03 06:53:38448TEST_F(SyncClientTest, Dependencies) {
449 // Create directories locally.
450 const base::FilePath kPath1(FILE_PATH_LITERAL("drive/root/dir1"));
451 const base::FilePath kPath2 = kPath1.AppendASCII("dir2");
452
453 ResourceEntry parent;
454 EXPECT_EQ(FILE_ERROR_OK,
455 metadata_->GetResourceEntryByPath(kPath1.DirName(), &parent));
456
457 ResourceEntry entry1;
458 entry1.set_parent_local_id(parent.local_id());
459 entry1.set_title(kPath1.BaseName().AsUTF8Unsafe());
460 entry1.mutable_file_info()->set_is_directory(true);
461 entry1.set_metadata_edit_state(ResourceEntry::DIRTY);
462 std::string local_id1;
463 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry1, &local_id1));
464
465 ResourceEntry entry2;
466 entry2.set_parent_local_id(local_id1);
467 entry2.set_title(kPath2.BaseName().AsUTF8Unsafe());
468 entry2.mutable_file_info()->set_is_directory(true);
469 entry2.set_metadata_edit_state(ResourceEntry::DIRTY);
470 std::string local_id2;
471 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry2, &local_id2));
472
473 // Start syncing the child first.
474 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id2);
[email protected]99bd5432014-02-03 06:53:38475 // Start syncing the parent later.
476 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id1);
477 base::RunLoop().RunUntilIdle();
478
479 // Both entries are synced.
480 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id1, &entry1));
481 EXPECT_EQ(ResourceEntry::CLEAN, entry1.metadata_edit_state());
482 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id2, &entry2));
483 EXPECT_EQ(ResourceEntry::CLEAN, entry2.metadata_edit_state());
484}
485
[email protected]e849db52014-07-23 07:02:09486TEST_F(SyncClientTest, WaitForUpdateTaskToComplete) {
487 // Create a directory locally.
488 const base::FilePath kPath(FILE_PATH_LITERAL("drive/root/dir1"));
489
490 ResourceEntry parent;
491 EXPECT_EQ(FILE_ERROR_OK,
492 metadata_->GetResourceEntryByPath(kPath.DirName(), &parent));
493
494 ResourceEntry entry;
495 entry.set_parent_local_id(parent.local_id());
496 entry.set_title(kPath.BaseName().AsUTF8Unsafe());
497 entry.mutable_file_info()->set_is_directory(true);
498 entry.set_metadata_edit_state(ResourceEntry::DIRTY);
499 std::string local_id;
500 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry, &local_id));
501
502 // Sync task is not yet avialable.
503 FileError error = FILE_ERROR_FAILED;
504 EXPECT_FALSE(sync_client_->WaitForUpdateTaskToComplete(
505 local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
506
507 // Start syncing the directory and wait for it to complete.
508 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id);
509
510 EXPECT_TRUE(sync_client_->WaitForUpdateTaskToComplete(
511 local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
512
513 base::RunLoop().RunUntilIdle();
514
515 // The callback is called.
516 EXPECT_EQ(FILE_ERROR_OK, error);
517}
518
[email protected]4e87c4862013-05-20 04:06:32519} // namespace internal
[email protected]d9d04df2012-10-12 07:06:35520} // namespace drive