Home | History | Annotate | Download | only in sync
      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/chromeos/drive/sync/remove_performer.h"
      6 
      7 #include "base/task_runner_util.h"
      8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
      9 #include "chrome/browser/chromeos/drive/file_system_util.h"
     10 #include "chrome/browser/chromeos/drive/job_scheduler.h"
     11 #include "chrome/browser/chromeos/drive/resource_metadata.h"
     12 #include "chrome/browser/drive/fake_drive_service.h"
     13 #include "google_apis/drive/drive_api_parser.h"
     14 #include "google_apis/drive/test_util.h"
     15 
     16 namespace drive {
     17 namespace internal {
     18 
     19 typedef file_system::OperationTestBase RemovePerformerTest;
     20 
     21 TEST_F(RemovePerformerTest, RemoveFile) {
     22   RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
     23                             metadata());
     24 
     25   base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
     26   base::FilePath file_in_subdir(
     27       FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
     28 
     29   // Remove a file in root.
     30   ResourceEntry entry;
     31   FileError error = FILE_ERROR_FAILED;
     32   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry));
     33   performer.Remove(entry.local_id(),
     34                    ClientContext(USER_INITIATED),
     35                    google_apis::test_util::CreateCopyResultCallback(&error));
     36   test_util::RunBlockingPoolTask();
     37   EXPECT_EQ(FILE_ERROR_OK, error);
     38 
     39   // Remove a file in subdirectory.
     40   error = FILE_ERROR_FAILED;
     41   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry));
     42   const std::string resource_id = entry.resource_id();
     43 
     44   performer.Remove(entry.local_id(),
     45                    ClientContext(USER_INITIATED),
     46                    google_apis::test_util::CreateCopyResultCallback(&error));
     47   test_util::RunBlockingPoolTask();
     48   EXPECT_EQ(FILE_ERROR_OK, error);
     49 
     50   // Verify the file is indeed removed in the server.
     51   google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
     52   scoped_ptr<google_apis::FileResource> gdata_entry;
     53   fake_service()->GetFileResource(
     54       resource_id,
     55       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
     56                                                        &gdata_entry));
     57   test_util::RunBlockingPoolTask();
     58   ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
     59   EXPECT_TRUE(gdata_entry->labels().is_trashed());
     60 
     61   // Try removing non-existing file.
     62   error = FILE_ERROR_FAILED;
     63   performer.Remove("non-existing-id",
     64                    ClientContext(USER_INITIATED),
     65                    google_apis::test_util::CreateCopyResultCallback(&error));
     66   test_util::RunBlockingPoolTask();
     67   EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
     68 }
     69 
     70 TEST_F(RemovePerformerTest, RemoveShared) {
     71   RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
     72                             metadata());
     73 
     74   const base::FilePath kPathInMyDrive(FILE_PATH_LITERAL(
     75       "drive/root/shared.txt"));
     76   const base::FilePath kPathInOther(FILE_PATH_LITERAL(
     77       "drive/other/shared.txt"));
     78 
     79   // Prepare a shared file to the root folder.
     80   google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
     81   scoped_ptr<google_apis::FileResource> gdata_entry;
     82   fake_service()->AddNewFile(
     83       "text/plain",
     84       "dummy content",
     85       fake_service()->GetRootResourceId(),
     86       kPathInMyDrive.BaseName().AsUTF8Unsafe(),
     87       true,  // shared_with_me,
     88       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
     89                                                        &gdata_entry));
     90   test_util::RunBlockingPoolTask();
     91   ASSERT_EQ(google_apis::HTTP_CREATED, gdata_error);
     92   CheckForUpdates();
     93 
     94   // Remove it. Locally, the file should be moved to drive/other.
     95   ResourceEntry entry;
     96   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInMyDrive, &entry));
     97   FileError error = FILE_ERROR_FAILED;
     98   performer.Remove(entry.local_id(),
     99                    ClientContext(USER_INITIATED),
    100                    google_apis::test_util::CreateCopyResultCallback(&error));
    101   test_util::RunBlockingPoolTask();
    102   EXPECT_EQ(FILE_ERROR_OK, error);
    103   EXPECT_EQ(FILE_ERROR_NOT_FOUND,
    104             GetLocalResourceEntry(kPathInMyDrive, &entry));
    105   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInOther, &entry));
    106 
    107   // Remotely, the entry should have lost its parent.
    108   gdata_error = google_apis::GDATA_OTHER_ERROR;
    109   const std::string resource_id = gdata_entry->file_id();
    110   fake_service()->GetFileResource(
    111       resource_id,
    112       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
    113                                                        &gdata_entry));
    114   test_util::RunBlockingPoolTask();
    115   ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
    116   EXPECT_FALSE(gdata_entry->labels().is_trashed());  // It's not deleted.
    117   EXPECT_TRUE(gdata_entry->parents().empty());
    118 }
    119 
    120 TEST_F(RemovePerformerTest, RemoveLocallyCreatedFile) {
    121   RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
    122                             metadata());
    123 
    124   // Add an entry without resource ID.
    125   ResourceEntry entry;
    126   entry.set_title("New File.txt");
    127   entry.set_parent_local_id(util::kDriveTrashDirLocalId);
    128 
    129   FileError error = FILE_ERROR_FAILED;
    130   std::string local_id;
    131   base::PostTaskAndReplyWithResult(
    132       blocking_task_runner(),
    133       FROM_HERE,
    134       base::Bind(&ResourceMetadata::AddEntry,
    135                  base::Unretained(metadata()),
    136                  entry,
    137                  &local_id),
    138       google_apis::test_util::CreateCopyResultCallback(&error));
    139   test_util::RunBlockingPoolTask();
    140   EXPECT_EQ(FILE_ERROR_OK, error);
    141 
    142   // Remove the entry.
    143   performer.Remove(local_id, ClientContext(USER_INITIATED),
    144                    google_apis::test_util::CreateCopyResultCallback(&error));
    145   test_util::RunBlockingPoolTask();
    146   EXPECT_EQ(FILE_ERROR_OK, error);
    147   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntryById(local_id, &entry));
    148 }
    149 
    150 }  // namespace internal
    151 }  // namespace drive
    152