sync_client_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/sync_client.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/file_util.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/scoped_temp_dir.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/run_loop.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/test_timeouts.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/drive/change_list_loader.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/drive/drive.pb.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/file_cache.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/drive/job_scheduler.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/drive/resource_metadata.h"
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/test_util.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/google_apis/fake_drive_service.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/google_apis/test_util.h"
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace drive {
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace internal {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The content of files iniitally stored in the cache.
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kLocalContent[] = "Hello!";
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The content of files stored in the service.
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kRemoteContent[] = "World!";
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// SyncClientTestDriveService will return GDATA_CANCELLED when a request is
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// made with the specified resource ID.
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class SyncClientTestDriveService : public google_apis::FakeDriveService {
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // FakeDriveService override:
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void GetResourceEntry(
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const std::string& resource_id,
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const google_apis::GetResourceEntryCallback& callback) OVERRIDE {
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (resource_id == resource_id_to_be_cancelled_) {
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<google_apis::ResourceEntry> null;
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      base::MessageLoopProxy::current()->PostTask(
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          FROM_HERE,
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          base::Bind(callback,
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                     google_apis::GDATA_CANCELLED,
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                     base::Passed(&null)));
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return;
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FakeDriveService::GetResourceEntry(resource_id, callback);
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void set_resource_id_to_be_cancelled(const std::string& resource_id) {
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    resource_id_to_be_cancelled_ = resource_id;
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string resource_id_to_be_cancelled_;
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class DummyOperationObserver : public file_system::OperationObserver {
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void OnDirectoryChangedByOperation(
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const base::FilePath& path) OVERRIDE {}
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SyncClientTest : public testing::Test {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    profile_.reset(new TestingProfile);
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    drive_service_.reset(new SyncClientTestDriveService);
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    drive_service_->LoadResourceListForWapi("chromeos/gdata/empty_feed.json");
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    drive_service_->LoadAccountMetadataForWapi(
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        "chromeos/gdata/account_metadata.json");
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    scheduler_.reset(new JobScheduler(profile_.get(), drive_service_.get()));
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    metadata_.reset(new internal::ResourceMetadata(
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        temp_dir_.path(), base::MessageLoopProxy::current()));
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FileError error = FILE_ERROR_FAILED;
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    metadata_->Initialize(
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        google_apis::test_util::CreateCopyResultCallback(&error));
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_EQ(FILE_ERROR_OK, error);
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    cache_.reset(new FileCache(temp_dir_.path(),
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                               base::MessageLoopProxy::current(),
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                               NULL /* free_disk_space_getter */));
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool success = false;
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    cache_->RequestInitialize(
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        google_apis::test_util::CreateCopyResultCallback(&success));
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ASSERT_TRUE(success);
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_NO_FATAL_FAILURE(SetUpTestData());
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    sync_client_.reset(new SyncClient(base::MessageLoopProxy::current(),
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      &observer_,
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      scheduler_.get(),
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      metadata_.get(),
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      cache_.get()));
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Disable delaying so that DoSyncLoop() starts immediately.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0));
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sync_client_.reset();
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    cache_.reset();
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    metadata_.reset();
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Adds a file to the service root and |resource_ids_|.
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void AddFileEntry(const std::string& title) {
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    scoped_ptr<google_apis::ResourceEntry> entry;
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    drive_service_->AddNewFile(
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        "text/plain",
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        kRemoteContent,
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        drive_service_->GetRootResourceId(),
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        title,
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        false,  // shared_with_me
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        google_apis::test_util::CreateCopyResultCallback(&error, &entry));
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_EQ(google_apis::HTTP_CREATED, error);
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_TRUE(entry);
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    resource_ids_[title] = entry->resource_id();
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Sets up data for tests.
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void SetUpTestData() {
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Prepare a temp file.
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath temp_file;
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(),
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                    &temp_file));
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file,
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                          kLocalContent));
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Prepare 3 pinned-but-not-present files.
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo"));
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["foo"], std::string()));
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar"));
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["bar"], std::string()));
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz"));
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["baz"], std::string()));
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Prepare a pinned-and-fetched file.
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string md5_fetched = "md5";
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched"));
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK,
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              cache_->Store(resource_ids_["fetched"], md5_fetched,
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            temp_file, FileCache::FILE_OPERATION_COPY));
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK,
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              cache_->Pin(resource_ids_["fetched"], md5_fetched));
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Prepare a pinned-and-fetched-and-dirty file.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string md5_dirty = "";  // Don't care.
168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty"));
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK,
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              cache_->Store(resource_ids_["dirty"], md5_dirty,
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            temp_file, FileCache::FILE_OPERATION_COPY));
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["dirty"], md5_dirty));
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK,
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              cache_->MarkDirty(resource_ids_["dirty"], md5_dirty));
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Load data from the service to the metadata.
177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FileError error = FILE_ERROR_FAILED;
178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    internal::ChangeListLoader change_list_loader(
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::MessageLoopProxy::current(), metadata_.get(), scheduler_.get());
180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    change_list_loader.LoadIfNeeded(
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DirectoryFetchInfo(),
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        google_apis::test_util::CreateCopyResultCallback(&error));
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(FILE_ERROR_OK, error);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content::TestBrowserThreadBundle thread_bundle_;
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ScopedTempDir temp_dir_;
190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestingProfile> profile_;
191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<SyncClientTestDriveService> drive_service_;
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DummyOperationObserver observer_;
193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<JobScheduler> scheduler_;
194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests>
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      metadata_;
19690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<SyncClient> sync_client_;
198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::map<std::string, std::string> resource_ids_;  // Name-to-id map.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(SyncClientTest, StartProcessingBacklog) {
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sync_client_->StartProcessingBacklog();
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FileCacheEntry cache_entry;
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Pinned files get downloaded.
208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(),
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_entry.is_present());
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["bar"], std::string(),
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_entry.is_present());
215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["baz"], std::string(),
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_entry.is_present());
219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Dirty file gets uploaded.
221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["dirty"], std::string(),
222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
223868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(cache_entry.is_dirty());
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(SyncClientTest, OnCachePinned) {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This file will be fetched by GetFileByResourceId() as OnCachePinned()
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will kick off the sync loop.
229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->OnCachePinned(resource_ids_["foo"], std::string());
230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FileCacheEntry cache_entry;
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(),
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_entry.is_present());
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(SyncClientTest, OnCachePinnedAndCancelled) {
239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]);
240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Trigger fetching of a file which results in cancellation.
241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FileError error = FILE_ERROR_FAILED;
242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  cache_->PinOnUIThread(
243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      resource_ids_["foo"], std::string(),
244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      google_apis::test_util::CreateCopyResultCallback(&error));
245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(FILE_ERROR_OK, error);
247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The file should be unpinned if the user wants the download to be cancelled.
249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FileCacheEntry cache_entry;
250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(),
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                     &cache_entry));
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(SyncClientTest, OnCacheUnpinned) {
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->AddResourceIdForTesting(SyncClient::FETCH,
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                        resource_ids_["foo"]);
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->AddResourceIdForTesting(SyncClient::FETCH,
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                        resource_ids_["bar"]);
259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->AddResourceIdForTesting(SyncClient::FETCH,
260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                        resource_ids_["baz"]);
261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(3U,
262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            sync_client_->GetResourceIdsForTesting(SyncClient::FETCH).size());
263868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->OnCacheUnpinned(resource_ids_["foo"], std::string());
265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->OnCacheUnpinned(resource_ids_["baz"], std::string());
266868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
267868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
268868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Only "bar" should be fetched.
269868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FileCacheEntry cache_entry;
270868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(),
271868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
272868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(cache_entry.is_present());
273868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
274868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["bar"], std::string(),
275868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_entry.is_present());
277868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
278868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["baz"], std::string(),
279868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    &cache_entry));
280868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(cache_entry.is_present());
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(SyncClientTest, Deduplication) {
285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->AddResourceIdForTesting(SyncClient::FETCH,
286868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                        resource_ids_["foo"]);
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the delay so that DoSyncLoop() is delayed.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sync_client_->set_delay_for_testing(TestTimeouts::action_max_timeout());
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Raise OnCachePinned() event. This shouldn't result in adding the second
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // task, as tasks are de-duplicated.
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sync_client_->OnCachePinned(resource_ids_["foo"], std::string());
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(1U,
295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            sync_client_->GetResourceIdsForTesting(SyncClient::FETCH).size());
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(SyncClientTest, ExistingPinnedFiles) {
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Start checking the existing pinned files. This will collect the resource
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IDs of pinned files, with stale local cache files.
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sync_client_->StartCheckingExistingPinnedFiles();
302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // "fetched" and "dirty" are the existing pinned files.
305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The non-dirty one should be synced, but the dirty one should not.
306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::FilePath cache_file;
307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string content;
308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(resource_ids_["fetched"],
309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                           std::string(), &cache_file));
310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(file_util::ReadFileToString(cache_file, &content));
311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(kRemoteContent, content);
312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  content.clear();
313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
314868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(resource_ids_["dirty"],
315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                           std::string(), &cache_file));
316868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(file_util::ReadFileToString(cache_file, &content));
317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(kLocalContent, content);
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace internal
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace drive
322