Home | History | Annotate | Download | only in file_system
      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/file_system/remove_operation.h"
      6 
      7 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
      8 #include "chrome/browser/chromeos/drive/file_system_util.h"
      9 #include "google_apis/drive/test_util.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace drive {
     13 namespace file_system {
     14 
     15 typedef OperationTestBase RemoveOperationTest;
     16 
     17 TEST_F(RemoveOperationTest, RemoveFile) {
     18   RemoveOperation operation(blocking_task_runner(), observer(), metadata(),
     19                             cache());
     20 
     21   base::FilePath nonexisting_file(
     22       FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
     23   base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
     24   base::FilePath file_in_subdir(
     25       FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
     26 
     27   // Remove a file in root.
     28   ResourceEntry entry;
     29   FileError error = FILE_ERROR_FAILED;
     30   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry));
     31   operation.Remove(file_in_root,
     32                    false,  // is_recursive
     33                    google_apis::test_util::CreateCopyResultCallback(&error));
     34   test_util::RunBlockingPoolTask();
     35   EXPECT_EQ(FILE_ERROR_OK, error);
     36   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(file_in_root, &entry));
     37 
     38   const std::string id_file_in_root = entry.local_id();
     39   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntryById(id_file_in_root, &entry));
     40   EXPECT_EQ(util::kDriveTrashDirLocalId, entry.parent_local_id());
     41 
     42   // Remove a file in subdirectory.
     43   error = FILE_ERROR_FAILED;
     44   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry));
     45   const std::string resource_id = entry.resource_id();
     46 
     47   operation.Remove(file_in_subdir,
     48                    false,  // is_recursive
     49                    google_apis::test_util::CreateCopyResultCallback(&error));
     50   test_util::RunBlockingPoolTask();
     51   EXPECT_EQ(FILE_ERROR_OK, error);
     52   EXPECT_EQ(FILE_ERROR_NOT_FOUND,
     53             GetLocalResourceEntry(file_in_subdir, &entry));
     54 
     55   const std::string id_file_in_subdir = entry.local_id();
     56   EXPECT_EQ(FILE_ERROR_OK,
     57             GetLocalResourceEntryById(id_file_in_subdir, &entry));
     58   EXPECT_EQ(util::kDriveTrashDirLocalId, entry.parent_local_id());
     59 
     60   // Try removing non-existing file.
     61   error = FILE_ERROR_FAILED;
     62   ASSERT_EQ(FILE_ERROR_NOT_FOUND,
     63             GetLocalResourceEntry(nonexisting_file, &entry));
     64   operation.Remove(nonexisting_file,
     65                    false,  // is_recursive
     66                    google_apis::test_util::CreateCopyResultCallback(&error));
     67   test_util::RunBlockingPoolTask();
     68   EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
     69 
     70   // Verify observer notifications.
     71   EXPECT_EQ(2U, observer()->get_changed_paths().size());
     72   EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root.DirName()));
     73   EXPECT_TRUE(observer()->get_changed_paths().count(file_in_subdir.DirName()));
     74 
     75   EXPECT_EQ(2U, observer()->updated_local_ids().size());
     76   EXPECT_TRUE(observer()->updated_local_ids().count(id_file_in_root));
     77   EXPECT_TRUE(observer()->updated_local_ids().count(id_file_in_subdir));
     78 }
     79 
     80 TEST_F(RemoveOperationTest, RemoveDirectory) {
     81   RemoveOperation operation(blocking_task_runner(), observer(), metadata(),
     82                             cache());
     83 
     84   base::FilePath empty_dir(FILE_PATH_LITERAL(
     85       "drive/root/Directory 1/Sub Directory Folder/Sub Sub Directory Folder"));
     86   base::FilePath non_empty_dir(FILE_PATH_LITERAL(
     87       "drive/root/Directory 1"));
     88   base::FilePath file_in_non_empty_dir(FILE_PATH_LITERAL(
     89       "drive/root/Directory 1/SubDirectory File 1.txt"));
     90 
     91   // Empty directory can be removed even with is_recursive = false.
     92   FileError error = FILE_ERROR_FAILED;
     93   ResourceEntry entry;
     94   operation.Remove(empty_dir,
     95                    false,  // is_recursive
     96                    google_apis::test_util::CreateCopyResultCallback(&error));
     97   test_util::RunBlockingPoolTask();
     98   EXPECT_EQ(FILE_ERROR_OK, error);
     99   EXPECT_EQ(FILE_ERROR_NOT_FOUND,
    100             GetLocalResourceEntry(empty_dir, &entry));
    101 
    102   // Non-empty directory, cannot.
    103   error = FILE_ERROR_FAILED;
    104   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(non_empty_dir, &entry));
    105   operation.Remove(non_empty_dir,
    106                    false,  // is_recursive
    107                    google_apis::test_util::CreateCopyResultCallback(&error));
    108   test_util::RunBlockingPoolTask();
    109   EXPECT_EQ(FILE_ERROR_NOT_EMPTY, error);
    110   EXPECT_EQ(FILE_ERROR_OK,
    111             GetLocalResourceEntry(non_empty_dir, &entry));
    112 
    113   // With is_recursive = true, it can be deleted, however. Descendant entries
    114   // are removed together.
    115   error = FILE_ERROR_FAILED;
    116   ASSERT_EQ(FILE_ERROR_OK,
    117             GetLocalResourceEntry(file_in_non_empty_dir, &entry));
    118   operation.Remove(non_empty_dir,
    119                    true,  // is_recursive
    120                    google_apis::test_util::CreateCopyResultCallback(&error));
    121   test_util::RunBlockingPoolTask();
    122   EXPECT_EQ(FILE_ERROR_OK, error);
    123   EXPECT_EQ(FILE_ERROR_NOT_FOUND,
    124             GetLocalResourceEntry(non_empty_dir, &entry));
    125   EXPECT_EQ(FILE_ERROR_NOT_FOUND,
    126             GetLocalResourceEntry(file_in_non_empty_dir, &entry));
    127 }
    128 
    129 }  // namespace file_system
    130 }  // namespace drive
    131