blob: 021d0f262d59bef2c405ed5857fc65e66ecd8cd6 [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]92b84f332012-03-21 20:45:217#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.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]5d13ab62013-08-22 07:45:1415#include "chrome/browser/chromeos/drive/change_list_processor.h"
[email protected]15de8142012-10-11 06:00:5416#include "chrome/browser/chromeos/drive/drive.pb.h"
[email protected]d2c08b72013-07-02 08:23:4817#include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
[email protected]eca3fc92013-05-01 03:53:4018#include "chrome/browser/chromeos/drive/file_cache.h"
[email protected]5170a132013-12-02 10:35:1319#include "chrome/browser/chromeos/drive/file_system/move_operation.h"
[email protected]b568b882013-06-10 04:38:0720#include "chrome/browser/chromeos/drive/file_system/operation_observer.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]0390b812013-06-19 00:27:5027#include "chrome/browser/drive/fake_drive_service.h"
[email protected]b568b882013-06-10 04:38:0728#include "content/public/test/test_browser_thread_bundle.h"
[email protected]e196bef32013-12-03 05:33:1329#include "google_apis/drive/test_util.h"
[email protected]92b84f332012-03-21 20:45:2130#include "testing/gtest/include/gtest/gtest.h"
31
[email protected]d9d04df2012-10-12 07:06:3532namespace drive {
[email protected]4e87c4862013-05-20 04:06:3233namespace internal {
[email protected]92b84f332012-03-21 20:45:2134
[email protected]80199362012-09-09 23:24:2935namespace {
36
[email protected]7a6860162013-07-03 12:31:4637// The content of files initially stored in the cache.
[email protected]b568b882013-06-10 04:38:0738const char kLocalContent[] = "Hello!";
[email protected]61a41c12013-06-06 06:23:3639
[email protected]b568b882013-06-10 04:38:0740// The content of files stored in the service.
41const char kRemoteContent[] = "World!";
42
43// SyncClientTestDriveService will return GDATA_CANCELLED when a request is
44// made with the specified resource ID.
[email protected]e50af7652013-06-20 06:39:3145class SyncClientTestDriveService : public ::drive::FakeDriveService {
[email protected]67e264a2013-06-03 05:39:0146 public:
[email protected]799d4ec2013-12-06 07:39:2347 SyncClientTestDriveService() : download_file_count_(0) {}
48
[email protected]b568b882013-06-10 04:38:0749 // FakeDriveService override:
[email protected]7a6860162013-07-03 12:31:4650 virtual google_apis::CancelCallback DownloadFile(
51 const base::FilePath& local_cache_path,
[email protected]67e264a2013-06-03 05:39:0152 const std::string& resource_id,
[email protected]7a6860162013-07-03 12:31:4653 const google_apis::DownloadActionCallback& download_action_callback,
54 const google_apis::GetContentCallback& get_content_callback,
55 const google_apis::ProgressCallback& progress_callback) OVERRIDE {
[email protected]799d4ec2013-12-06 07:39:2356 ++download_file_count_;
[email protected]b568b882013-06-10 04:38:0757 if (resource_id == resource_id_to_be_cancelled_) {
[email protected]b568b882013-06-10 04:38:0758 base::MessageLoopProxy::current()->PostTask(
59 FROM_HERE,
[email protected]7a6860162013-07-03 12:31:4660 base::Bind(download_action_callback,
[email protected]b568b882013-06-10 04:38:0761 google_apis::GDATA_CANCELLED,
[email protected]7a6860162013-07-03 12:31:4662 base::FilePath()));
[email protected]cef0f062013-06-14 16:13:0763 return google_apis::CancelCallback();
[email protected]61a41c12013-06-06 06:23:3664 }
[email protected]799d4ec2013-12-06 07:39:2365 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 }
[email protected]7a6860162013-07-03 12:31:4671 return FakeDriveService::DownloadFile(local_cache_path,
72 resource_id,
73 download_action_callback,
74 get_content_callback,
75 progress_callback);
[email protected]67e264a2013-06-03 05:39:0176 }
[email protected]aaa70a42012-06-05 22:22:5177
[email protected]799d4ec2013-12-06 07:39:2378 int download_file_count() const { return download_file_count_; }
79
[email protected]b568b882013-06-10 04:38:0780 void set_resource_id_to_be_cancelled(const std::string& resource_id) {
81 resource_id_to_be_cancelled_ = resource_id;
[email protected]67e264a2013-06-03 05:39:0182 }
[email protected]b83e5202012-06-27 07:50:2483
[email protected]799d4ec2013-12-06 07:39:2384 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
[email protected]67e264a2013-06-03 05:39:0190 private:
[email protected]799d4ec2013-12-06 07:39:2391 int download_file_count_;
[email protected]b568b882013-06-10 04:38:0792 std::string resource_id_to_be_cancelled_;
[email protected]799d4ec2013-12-06 07:39:2393 std::string resource_id_to_be_paused_;
94 base::Closure paused_action_;
[email protected]b568b882013-06-10 04:38:0795};
96
97class DummyOperationObserver : public file_system::OperationObserver {
[email protected]6bf581142013-06-11 17:13:0698 // OperationObserver override:
[email protected]b568b882013-06-10 04:38:0799 virtual void OnDirectoryChangedByOperation(
100 const base::FilePath& path) OVERRIDE {}
[email protected]6bf581142013-06-11 17:13:06101 virtual void OnCacheFileUploadNeededByOperation(
[email protected]450f0e62013-08-28 10:44:08102 const std::string& local_id) OVERRIDE {}
[email protected]67e264a2013-06-03 05:39:01103};
[email protected]85b62192012-06-29 19:56:38104
[email protected]80199362012-09-09 23:24:29105} // namespace
106
[email protected]d876d70b2013-04-23 20:06:15107class SyncClientTest : public testing::Test {
[email protected]ffd60e432012-03-24 20:36:00108 public:
[email protected]92b84f332012-03-21 20:45:21109 virtual void SetUp() OVERRIDE {
[email protected]92b84f332012-03-21 20:45:21110 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
111
[email protected]db20d212013-07-08 07:18:16112 pref_service_.reset(new TestingPrefServiceSimple);
113 test_util::RegisterDrivePrefs(pref_service_->registry());
[email protected]b568b882013-06-10 04:38:07114
[email protected]d2c08b72013-07-02 08:23:48115 fake_network_change_notifier_.reset(
116 new test_util::FakeNetworkChangeNotifier);
117
[email protected]b568b882013-06-10 04:38:07118 drive_service_.reset(new SyncClientTestDriveService);
[email protected]b21414aa2013-07-02 05:12:45119 drive_service_->LoadResourceListForWapi("gdata/empty_feed.json");
[email protected]b568b882013-06-10 04:38:07120 drive_service_->LoadAccountMetadataForWapi(
[email protected]b21414aa2013-07-02 05:12:45121 "gdata/account_metadata.json");
[email protected]b568b882013-06-10 04:38:07122
[email protected]db20d212013-07-08 07:18:16123 scheduler_.reset(new JobScheduler(pref_service_.get(),
[email protected]3cd8a6a2013-07-03 22:01:55124 drive_service_.get(),
125 base::MessageLoopProxy::current().get()));
[email protected]be427d72013-06-21 07:09:27126
127 metadata_storage_.reset(new ResourceMetadataStorage(
[email protected]3cd8a6a2013-07-03 22:01:55128 temp_dir_.path(), base::MessageLoopProxy::current().get()));
[email protected]be427d72013-06-21 07:09:27129 ASSERT_TRUE(metadata_storage_->Initialize());
130
131 metadata_.reset(new internal::ResourceMetadata(
132 metadata_storage_.get(), base::MessageLoopProxy::current()));
[email protected]34a1bbf32013-06-17 07:24:02133 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
[email protected]b568b882013-06-10 04:38:07134
[email protected]2df61e12013-06-21 16:00:09135 cache_.reset(new FileCache(metadata_storage_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12136 temp_dir_.path(),
[email protected]3cd8a6a2013-07-03 22:01:55137 base::MessageLoopProxy::current().get(),
[email protected]b568b882013-06-10 04:38:07138 NULL /* free_disk_space_getter */));
[email protected]34a1bbf32013-06-17 07:24:02139 ASSERT_TRUE(cache_->Initialize());
[email protected]e63bdc972012-11-22 12:10:48140
[email protected]b568b882013-06-10 04:38:07141 ASSERT_NO_FATAL_FAILURE(SetUpTestData());
142
[email protected]3cd8a6a2013-07-03 22:01:55143 sync_client_.reset(new SyncClient(base::MessageLoopProxy::current().get(),
[email protected]b568b882013-06-10 04:38:07144 &observer_,
145 scheduler_.get(),
146 metadata_.get(),
[email protected]e07f7b7b2013-06-19 03:43:12147 cache_.get(),
148 temp_dir_.path()));
[email protected]e0529fc2012-06-12 11:07:58149
[email protected]b83e5202012-06-27 07:50:24150 // Disable delaying so that DoSyncLoop() starts immediately.
151 sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0));
[email protected]92b84f332012-03-21 20:45:21152 }
153
[email protected]b568b882013-06-10 04:38:07154 // Adds a file to the service root and |resource_ids_|.
155 void AddFileEntry(const std::string& title) {
156 google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
157 scoped_ptr<google_apis::ResourceEntry> entry;
158 drive_service_->AddNewFile(
159 "text/plain",
160 kRemoteContent,
161 drive_service_->GetRootResourceId(),
162 title,
163 false, // shared_with_me
164 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
165 base::RunLoop().RunUntilIdle();
166 ASSERT_EQ(google_apis::HTTP_CREATED, error);
167 ASSERT_TRUE(entry);
168 resource_ids_[title] = entry->resource_id();
169 }
170
171 // Sets up data for tests.
172 void SetUpTestData() {
[email protected]e63bdc972012-11-22 12:10:48173 // Prepare a temp file.
[email protected]650b2d52013-02-10 03:41:45174 base::FilePath temp_file;
[email protected]03d9afc02013-12-03 17:55:52175 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file));
[email protected]b568b882013-06-10 04:38:07176 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file,
177 kLocalContent));
[email protected]b83e5202012-06-27 07:50:24178
[email protected]efd37ff2013-10-11 03:27:57179 // Add file entries to the service.
[email protected]b568b882013-06-10 04:38:07180 ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo"));
[email protected]b568b882013-06-10 04:38:07181 ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar"));
[email protected]b568b882013-06-10 04:38:07182 ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz"));
[email protected]b568b882013-06-10 04:38:07183 ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched"));
[email protected]b568b882013-06-10 04:38:07184 ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty"));
[email protected]562af1c12013-11-26 10:27:53185 ASSERT_NO_FATAL_FAILURE(AddFileEntry("removed"));
[email protected]5170a132013-12-02 10:35:13186 ASSERT_NO_FATAL_FAILURE(AddFileEntry("moved"));
[email protected]adf84402012-03-25 21:56:38187
[email protected]b568b882013-06-10 04:38:07188 // Load data from the service to the metadata.
189 FileError error = FILE_ERROR_FAILED;
190 internal::ChangeListLoader change_list_loader(
[email protected]3cd8a6a2013-07-03 22:01:55191 base::MessageLoopProxy::current().get(),
192 metadata_.get(),
[email protected]b99139b2013-09-06 11:56:45193 scheduler_.get(),
194 drive_service_.get());
[email protected]b568b882013-06-10 04:38:07195 change_list_loader.LoadIfNeeded(
196 DirectoryFetchInfo(),
197 google_apis::test_util::CreateCopyResultCallback(&error));
198 base::RunLoop().RunUntilIdle();
199 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]efd37ff2013-10-11 03:27:57200
201 // Prepare 3 pinned-but-not-present files.
202 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("foo")));
203 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("bar")));
204 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("baz")));
205
206 // Prepare a pinned-and-fetched file.
207 const std::string md5_fetched = "md5";
208 EXPECT_EQ(FILE_ERROR_OK,
209 cache_->Store(GetLocalId("fetched"), md5_fetched,
210 temp_file, FileCache::FILE_OPERATION_COPY));
211 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("fetched")));
212
213 // Prepare a pinned-and-fetched-and-dirty file.
214 const std::string md5_dirty = ""; // Don't care.
215 EXPECT_EQ(FILE_ERROR_OK,
216 cache_->Store(GetLocalId("dirty"), md5_dirty,
217 temp_file, FileCache::FILE_OPERATION_COPY));
218 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("dirty")));
219 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkDirty(GetLocalId("dirty")));
220
[email protected]562af1c12013-11-26 10:27:53221 // Prepare a removed file.
222 file_system::RemoveOperation remove_operation(
223 base::MessageLoopProxy::current().get(), &observer_, metadata_.get(),
224 cache_.get());
225 remove_operation.Remove(
226 metadata_->GetFilePath(GetLocalId("removed")),
227 false, // is_recursive
228 google_apis::test_util::CreateCopyResultCallback(&error));
229 base::RunLoop().RunUntilIdle();
230 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]5170a132013-12-02 10:35:13231
232 // Prepare a moved file.
233 file_system::MoveOperation move_operation(
234 base::MessageLoopProxy::current().get(), &observer_, metadata_.get());
235 move_operation.Move(
236 metadata_->GetFilePath(GetLocalId("moved")),
237 util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"),
238 false, // preserve_last_modified
239 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_;
[email protected]db20d212013-07-08 07:18:16255 scoped_ptr<TestingPrefServiceSimple> pref_service_;
[email protected]d2c08b72013-07-02 08:23:48256 scoped_ptr<test_util::FakeNetworkChangeNotifier>
257 fake_network_change_notifier_;
[email protected]b568b882013-06-10 04:38:07258 scoped_ptr<SyncClientTestDriveService> drive_service_;
259 DummyOperationObserver observer_;
260 scoped_ptr<JobScheduler> scheduler_;
[email protected]be427d72013-06-21 07:09:27261 scoped_ptr<ResourceMetadataStorage,
262 test_util::DestroyHelperForTests> metadata_storage_;
263 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
[email protected]4e87c4862013-05-20 04:06:32264 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
[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]b568b882013-06-10 04:38:07274 FileCacheEntry cache_entry;
275 // Pinned files get downloaded.
[email protected]efd37ff2013-10-11 03:27:57276 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07277 EXPECT_TRUE(cache_entry.is_present());
[email protected]b83e5202012-06-27 07:50:24278
[email protected]efd37ff2013-10-11 03:27:57279 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07280 EXPECT_TRUE(cache_entry.is_present());
281
[email protected]efd37ff2013-10-11 03:27:57282 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07283 EXPECT_TRUE(cache_entry.is_present());
284
285 // Dirty file gets uploaded.
[email protected]efd37ff2013-10-11 03:27:57286 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07287 EXPECT_FALSE(cache_entry.is_dirty());
[email protected]562af1c12013-11-26 10:27:53288
289 // Removed entry is not found.
290 google_apis::GDataErrorCode status = google_apis::GDATA_OTHER_ERROR;
291 scoped_ptr<google_apis::ResourceEntry> resource_entry;
292 drive_service_->GetResourceEntry(
293 resource_ids_["removed"],
294 google_apis::test_util::CreateCopyResultCallback(&status,
295 &resource_entry));
296 base::RunLoop().RunUntilIdle();
297 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
298 ASSERT_TRUE(resource_entry);
299 EXPECT_TRUE(resource_entry->deleted());
[email protected]5170a132013-12-02 10:35:13300
301 // Moved entry was moved.
302 status = google_apis::GDATA_OTHER_ERROR;
303 drive_service_->GetResourceEntry(
304 resource_ids_["moved"],
305 google_apis::test_util::CreateCopyResultCallback(&status,
306 &resource_entry));
307 base::RunLoop().RunUntilIdle();
308 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
309 ASSERT_TRUE(resource_entry);
310 EXPECT_EQ("moved_new_title", resource_entry->title());
[email protected]a5381772012-04-05 02:20:04311}
312
[email protected]bd2254d2013-06-12 16:00:47313TEST_F(SyncClientTest, AddFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57314 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07315 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01316
[email protected]b568b882013-06-10 04:38:07317 FileCacheEntry cache_entry;
[email protected]efd37ff2013-10-11 03:27:57318 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07319 EXPECT_TRUE(cache_entry.is_present());
[email protected]adf84402012-03-25 21:56:38320}
321
[email protected]bd2254d2013-06-12 16:00:47322TEST_F(SyncClientTest, AddFetchTaskAndCancelled) {
[email protected]61a41c12013-06-06 06:23:36323 // Trigger fetching of a file which results in cancellation.
[email protected]bd2254d2013-06-12 16:00:47324 drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]);
[email protected]efd37ff2013-10-11 03:27:57325 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07326 base::RunLoop().RunUntilIdle();
[email protected]61a41c12013-06-06 06:23:36327
328 // The file should be unpinned if the user wants the download to be cancelled.
[email protected]61a41c12013-06-06 06:23:36329 FileCacheEntry cache_entry;
[email protected]efd37ff2013-10-11 03:27:57330 EXPECT_FALSE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
[email protected]61a41c12013-06-06 06:23:36331}
332
[email protected]bd2254d2013-06-12 16:00:47333TEST_F(SyncClientTest, RemoveFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57334 sync_client_->AddFetchTask(GetLocalId("foo"));
335 sync_client_->AddFetchTask(GetLocalId("bar"));
336 sync_client_->AddFetchTask(GetLocalId("baz"));
[email protected]adf84402012-03-25 21:56:38337
[email protected]efd37ff2013-10-11 03:27:57338 sync_client_->RemoveFetchTask(GetLocalId("foo"));
339 sync_client_->RemoveFetchTask(GetLocalId("baz"));
[email protected]b568b882013-06-10 04:38:07340 base::RunLoop().RunUntilIdle();
[email protected]e07bbd972013-01-23 17:38:42341
[email protected]b568b882013-06-10 04:38:07342 // Only "bar" should be fetched.
343 FileCacheEntry cache_entry;
[email protected]efd37ff2013-10-11 03:27:57344 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07345 EXPECT_FALSE(cache_entry.is_present());
346
[email protected]efd37ff2013-10-11 03:27:57347 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07348 EXPECT_TRUE(cache_entry.is_present());
349
[email protected]efd37ff2013-10-11 03:27:57350 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry));
[email protected]b568b882013-06-10 04:38:07351 EXPECT_FALSE(cache_entry.is_present());
352
[email protected]adf84402012-03-25 21:56:38353}
354
[email protected]d876d70b2013-04-23 20:06:15355TEST_F(SyncClientTest, ExistingPinnedFiles) {
[email protected]85b62192012-06-29 19:56:38356 // Start checking the existing pinned files. This will collect the resource
357 // IDs of pinned files, with stale local cache files.
358 sync_client_->StartCheckingExistingPinnedFiles();
[email protected]b568b882013-06-10 04:38:07359 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01360
[email protected]b568b882013-06-10 04:38:07361 // "fetched" and "dirty" are the existing pinned files.
362 // The non-dirty one should be synced, but the dirty one should not.
363 base::FilePath cache_file;
364 std::string content;
[email protected]efd37ff2013-10-11 03:27:57365 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("fetched"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50366 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07367 EXPECT_EQ(kRemoteContent, content);
368 content.clear();
369
[email protected]efd37ff2013-10-11 03:27:57370 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("dirty"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50371 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07372 EXPECT_EQ(kLocalContent, content);
[email protected]189541ba2012-10-24 11:18:15373}
374
[email protected]d2c08b72013-07-02 08:23:48375TEST_F(SyncClientTest, RetryOnDisconnection) {
376 // Let the service go down.
377 drive_service_->set_offline(true);
378 // Change the network connection state after some delay, to test that
379 // FILE_ERROR_NO_CONNECTION is handled by SyncClient correctly.
380 // Without this delay, JobScheduler will keep the jobs unrun and SyncClient
381 // will receive no error.
382 base::MessageLoopProxy::current()->PostDelayedTask(
383 FROM_HERE,
384 base::Bind(&test_util::FakeNetworkChangeNotifier::SetConnectionType,
385 base::Unretained(fake_network_change_notifier_.get()),
386 net::NetworkChangeNotifier::CONNECTION_NONE),
387 TestTimeouts::tiny_timeout());
388
389 // Try fetch and upload.
[email protected]efd37ff2013-10-11 03:27:57390 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]882e3202013-11-25 08:33:50391 sync_client_->AddUploadTask(ClientContext(USER_INITIATED),
392 GetLocalId("dirty"));
[email protected]d2c08b72013-07-02 08:23:48393 base::RunLoop().RunUntilIdle();
394
395 // Not yet fetched nor uploaded.
396 FileCacheEntry cache_entry;
[email protected]efd37ff2013-10-11 03:27:57397 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
[email protected]d2c08b72013-07-02 08:23:48398 EXPECT_FALSE(cache_entry.is_present());
[email protected]efd37ff2013-10-11 03:27:57399 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
[email protected]d2c08b72013-07-02 08:23:48400 EXPECT_TRUE(cache_entry.is_dirty());
401
402 // Switch to online.
403 fake_network_change_notifier_->SetConnectionType(
404 net::NetworkChangeNotifier::CONNECTION_WIFI);
405 drive_service_->set_offline(false);
406 base::RunLoop().RunUntilIdle();
407
408 // Fetched and uploaded.
[email protected]efd37ff2013-10-11 03:27:57409 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
[email protected]d2c08b72013-07-02 08:23:48410 EXPECT_TRUE(cache_entry.is_present());
[email protected]efd37ff2013-10-11 03:27:57411 EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
[email protected]d2c08b72013-07-02 08:23:48412 EXPECT_FALSE(cache_entry.is_dirty());
413}
414
[email protected]799d4ec2013-12-06 07:39:23415TEST_F(SyncClientTest, ScheduleRerun) {
416 // Add a fetch task for "foo", this should result in being paused.
417 drive_service_->set_resource_id_to_be_paused(resource_ids_["foo"]);
418 sync_client_->AddFetchTask(GetLocalId("foo"));
419 base::RunLoop().RunUntilIdle();
420
421 // While the first task is paused, add a task again.
422 // This results in scheduling rerun of the task.
423 sync_client_->AddFetchTask(GetLocalId("foo"));
424 base::RunLoop().RunUntilIdle();
425
426 // Resume the paused task.
427 drive_service_->set_resource_id_to_be_paused(std::string());
428 ASSERT_FALSE(drive_service_->paused_action().is_null());
429 drive_service_->paused_action().Run();
430 base::RunLoop().RunUntilIdle();
431
432 // Task should be run twice.
433 EXPECT_EQ(2, drive_service_->download_file_count());
434}
435
[email protected]4e87c4862013-05-20 04:06:32436} // namespace internal
[email protected]d9d04df2012-10-12 07:06:35437} // namespace drive