Home | History | Annotate | Download | only in drive_backend
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/sync_file_system/drive_backend/list_changes_task.h"
      6 
      7 #include "base/files/scoped_temp_dir.h"
      8 #include "base/format_macros.h"
      9 #include "base/run_loop.h"
     10 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
     11 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
     12 #include "chrome/browser/sync_file_system/drive_backend/register_app_task.h"
     13 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
     14 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_initializer.h"
     15 #include "chrome/browser/sync_file_system/drive_backend_v1/fake_drive_service_helper.h"
     16 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
     17 #include "content/public/test/test_browser_thread_bundle.h"
     18 #include "google_apis/drive/drive_api_parser.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 
     21 namespace sync_file_system {
     22 namespace drive_backend {
     23 
     24 namespace {
     25 
     26 const char kAppID[] = "app_id";
     27 const char kUnregisteredAppID[] = "app_id unregistered";
     28 
     29 }  // namespace
     30 
     31 class ListChangesTaskTest : public testing::Test,
     32                             public SyncEngineContext {
     33  public:
     34   ListChangesTaskTest() {}
     35   virtual ~ListChangesTaskTest() {}
     36 
     37   virtual void SetUp() OVERRIDE {
     38     ASSERT_TRUE(database_dir_.CreateUniqueTempDir());
     39 
     40     fake_drive_service_.reset(new drive::FakeDriveService);
     41     ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi(
     42         "sync_file_system/account_metadata.json"));
     43     ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi(
     44         "gdata/empty_feed.json"));
     45 
     46     drive_uploader_.reset(new drive::DriveUploader(
     47         fake_drive_service_.get(), base::MessageLoopProxy::current()));
     48 
     49     fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
     50         fake_drive_service_.get(), drive_uploader_.get()));
     51 
     52     SetUpRemoteFolders();
     53     InitializeMetadataDatabase();
     54     RegisterApp(kAppID);
     55   }
     56 
     57   virtual void TearDown() OVERRIDE {
     58     metadata_database_.reset();
     59     base::RunLoop().RunUntilIdle();
     60   }
     61 
     62   virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE {
     63     return fake_drive_service_.get();
     64   }
     65 
     66   virtual drive::DriveUploader* GetDriveUploader() OVERRIDE {
     67     return NULL;
     68   }
     69 
     70   virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE {
     71     return metadata_database_.get();
     72   }
     73 
     74   virtual RemoteChangeProcessor* GetRemoteChangeProcessor() OVERRIDE {
     75     return NULL;
     76   }
     77 
     78   virtual base::SequencedTaskRunner* GetBlockingTaskRunner() OVERRIDE {
     79     return base::MessageLoopProxy::current();
     80   }
     81 
     82  protected:
     83   SyncStatusCode RunTask(SyncTask* sync_task) {
     84     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
     85     sync_task->Run(CreateResultReceiver(&status));
     86     base::RunLoop().RunUntilIdle();
     87     return status;
     88   }
     89 
     90   size_t CountDirtyTracker() {
     91     return metadata_database_->dirty_trackers_.size();
     92   }
     93 
     94   FakeDriveServiceHelper* fake_drive_service_helper() {
     95     return fake_drive_service_helper_.get();
     96   }
     97 
     98   void SetUpChangesInFolder(const std::string& folder_id) {
     99     std::string new_file_id;
    100     ASSERT_EQ(google_apis::HTTP_SUCCESS,
    101               fake_drive_service_helper()->AddFile(
    102                   folder_id, "new file", "file contents", &new_file_id));
    103     std::string same_name_file_id;
    104     ASSERT_EQ(google_apis::HTTP_SUCCESS,
    105               fake_drive_service_helper()->AddFile(
    106                   folder_id, "new file", "file contents",
    107                   &same_name_file_id));
    108 
    109     std::string new_folder_id;
    110     ASSERT_EQ(google_apis::HTTP_CREATED,
    111               fake_drive_service_helper()->AddFolder(
    112                   folder_id, "new folder", &new_folder_id));
    113 
    114     std::string modified_file_id;
    115     ASSERT_EQ(google_apis::HTTP_SUCCESS,
    116               fake_drive_service_helper()->AddFile(
    117                   folder_id, "modified file", "file content",
    118                   &modified_file_id));
    119     ASSERT_EQ(google_apis::HTTP_SUCCESS,
    120               fake_drive_service_helper()->UpdateFile(
    121                   modified_file_id, "modified file content"));
    122 
    123 
    124     std::string deleted_file_id;
    125     ASSERT_EQ(google_apis::HTTP_SUCCESS,
    126               fake_drive_service_helper()->AddFile(
    127                   folder_id, "trashed file", "file content",
    128                   &deleted_file_id));
    129     ASSERT_EQ(google_apis::HTTP_NO_CONTENT,
    130               fake_drive_service_helper()->DeleteResource(deleted_file_id));
    131   }
    132 
    133   std::string root_resource_id() {
    134     return fake_drive_service_->GetRootResourceId();
    135   }
    136 
    137   std::string app_root_folder_id() {
    138     return app_root_folder_id_;
    139   }
    140 
    141   std::string unregistered_app_root_folder_id() {
    142     return unregistered_app_root_folder_id_;
    143   }
    144 
    145  private:
    146   void SetUpRemoteFolders() {
    147     ASSERT_EQ(google_apis::HTTP_CREATED,
    148               fake_drive_service_helper_->AddOrphanedFolder(
    149                   kSyncRootFolderTitle, &sync_root_folder_id_));
    150     ASSERT_EQ(google_apis::HTTP_CREATED,
    151               fake_drive_service_helper_->AddFolder(
    152                   sync_root_folder_id_, kAppID, &app_root_folder_id_));
    153     ASSERT_EQ(google_apis::HTTP_CREATED,
    154               fake_drive_service_helper_->AddFolder(
    155                   sync_root_folder_id_, kUnregisteredAppID,
    156                   &unregistered_app_root_folder_id_));
    157   }
    158 
    159   void InitializeMetadataDatabase() {
    160     SyncEngineInitializer initializer(this,
    161                                       base::MessageLoopProxy::current(),
    162                                       fake_drive_service_.get(),
    163                                       database_dir_.path());
    164     EXPECT_EQ(SYNC_STATUS_OK, RunTask(&initializer));
    165     metadata_database_ = initializer.PassMetadataDatabase();
    166   }
    167 
    168   void RegisterApp(const std::string& app_id) {
    169     RegisterAppTask register_app(this, app_id);
    170     EXPECT_EQ(SYNC_STATUS_OK, RunTask(&register_app));
    171   }
    172 
    173   std::string sync_root_folder_id_;
    174   std::string app_root_folder_id_;
    175   std::string unregistered_app_root_folder_id_;
    176 
    177   content::TestBrowserThreadBundle browser_threads_;
    178   base::ScopedTempDir database_dir_;
    179 
    180   scoped_ptr<drive::FakeDriveService> fake_drive_service_;
    181   scoped_ptr<drive::DriveUploader> drive_uploader_;
    182   scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
    183 
    184   scoped_ptr<MetadataDatabase> metadata_database_;
    185 
    186   DISALLOW_COPY_AND_ASSIGN(ListChangesTaskTest);
    187 };
    188 
    189 TEST_F(ListChangesTaskTest, NoChange) {
    190   size_t num_dirty_trackers = CountDirtyTracker();
    191 
    192   ListChangesTask list_changes(this);
    193   EXPECT_EQ(SYNC_STATUS_NO_CHANGE_TO_SYNC, RunTask(&list_changes));
    194 
    195   EXPECT_EQ(num_dirty_trackers, CountDirtyTracker());
    196 }
    197 
    198 TEST_F(ListChangesTaskTest, UnrelatedChange) {
    199   size_t num_dirty_trackers = CountDirtyTracker();
    200 
    201   SetUpChangesInFolder(root_resource_id());
    202   SetUpChangesInFolder(unregistered_app_root_folder_id());
    203 
    204   ListChangesTask list_changes(this);
    205   EXPECT_EQ(SYNC_STATUS_OK, RunTask(&list_changes));
    206 
    207   EXPECT_EQ(num_dirty_trackers, CountDirtyTracker());
    208 }
    209 
    210 TEST_F(ListChangesTaskTest, UnderTrackedFolder) {
    211   size_t num_dirty_trackers = CountDirtyTracker();
    212 
    213   SetUpChangesInFolder(app_root_folder_id());
    214 
    215   ListChangesTask list_changes(this);
    216   EXPECT_EQ(SYNC_STATUS_OK, RunTask(&list_changes));
    217 
    218   EXPECT_EQ(num_dirty_trackers + 4, CountDirtyTracker());
    219 }
    220 
    221 }  // namespace drive_backend
    222 }  // namespace sync_file_system
    223