| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/drive/file_system/create_directory_operation.h
" | 5 #include "chrome/browser/chromeos/drive/file_system/create_directory_operation.h
" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
| 8 #include "base/message_loop.h" | |
| 9 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
| 10 #include "chrome/browser/chromeos/drive/resource_metadata.h" | |
| 11 #include "chrome/browser/chromeos/drive/test_util.h" | |
| 12 #include "chrome/browser/google_apis/test_util.h" | 8 #include "chrome/browser/google_apis/test_util.h" |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "content/public/test/test_browser_thread.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 10 |
| 17 namespace drive { | 11 namespace drive { |
| 18 namespace file_system { | 12 namespace file_system { |
| 19 namespace { | |
| 20 | 13 |
| 21 // Creates a resource entry instance. The resource id is "resource_id:{title}", | 14 class CreateDirectoryOperationTest : public OperationTestBase { |
| 22 // where {title} is the given |title|. | 15 protected: |
| 23 ResourceEntry CreateResourceEntry(const std::string& title, | 16 // Returns FILE_ERROR_OK if a directory is found at |path|. |
| 24 const std::string& parent_resource_id, | 17 FileError FindDirectory(const base::FilePath& path) { |
| 25 bool is_directory) { | 18 ResourceEntry entry; |
| 26 ResourceEntry entry; | 19 FileError error = GetLocalResourceEntry(path, &entry); |
| 27 const std::string resource_id = "resource_id:" + title; | 20 if (error == FILE_ERROR_OK && !entry.file_info().is_directory()) |
| 28 entry.set_title(title); | 21 error = FILE_ERROR_NOT_A_DIRECTORY; |
| 29 entry.set_resource_id(resource_id); | 22 return error; |
| 30 entry.set_parent_resource_id(parent_resource_id); | 23 } |
| 24 }; |
| 31 | 25 |
| 32 PlatformFileInfoProto* file_info = entry.mutable_file_info(); | 26 TEST_F(CreateDirectoryOperationTest, CreateDirectory) { |
| 33 file_info->set_is_directory(is_directory); | 27 CreateDirectoryOperation operation(blocking_task_runner(), |
| 28 observer(), |
| 29 scheduler(), |
| 30 metadata()); |
| 34 | 31 |
| 35 return entry; | 32 const base::FilePath kExistingFile( |
| 36 } | 33 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 34 const base::FilePath kExistingDirectory( |
| 35 FILE_PATH_LITERAL("drive/root/Directory 1")); |
| 36 const base::FilePath kNewDirectory1( |
| 37 FILE_PATH_LITERAL("drive/root/New Directory")); |
| 38 const base::FilePath kNewDirectory2 = |
| 39 kNewDirectory1.AppendASCII("New Directory 2/a/b/c"); |
| 37 | 40 |
| 38 } // namespace | 41 // Create a new directory, not recursively. |
| 42 EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory1)); |
| 39 | 43 |
| 40 TEST(CreateDirectoryOperationTest, GetExistingDeepestDirectory) { | |
| 41 MessageLoopForUI message_loop; | |
| 42 content::TestBrowserThread ui_thread( | |
| 43 content::BrowserThread::UI, &message_loop); | |
| 44 | |
| 45 // Set up a Metadata instance. | |
| 46 base::ScopedTempDir temp_dir; | |
| 47 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 48 scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests> | |
| 49 metadata(new internal::ResourceMetadata( | |
| 50 temp_dir.path(), message_loop.message_loop_proxy())); | |
| 51 FileError error = FILE_ERROR_FAILED; | 44 FileError error = FILE_ERROR_FAILED; |
| 52 metadata->Initialize( | 45 operation.CreateDirectory( |
| 46 kNewDirectory1, |
| 47 true, // is_exclusive |
| 48 false, // is_recursive |
| 53 google_apis::test_util::CreateCopyResultCallback(&error)); | 49 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 54 google_apis::test_util::RunBlockingPoolTask(); | 50 google_apis::test_util::RunBlockingPoolTask(); |
| 55 ASSERT_EQ(FILE_ERROR_OK, error); | 51 EXPECT_EQ(FILE_ERROR_OK, error); |
| 52 EXPECT_EQ(FILE_ERROR_OK, FindDirectory(kNewDirectory1)); |
| 53 EXPECT_EQ(1U, observer()->get_changed_paths().size()); |
| 54 EXPECT_EQ(1U, |
| 55 observer()->get_changed_paths().count(kNewDirectory1.DirName())); |
| 56 | 56 |
| 57 // Create a testee directory tree. | 57 // Create a new directory recursively. |
| 58 // drive/root/dir1/ | 58 EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory2)); |
| 59 // drive/root/dir1/dir2/ | 59 operation.CreateDirectory( |
| 60 // drive/root/dir1/dir2/file3 | 60 kNewDirectory2, |
| 61 // drive/root/dir1/file4 | 61 true, // is_exclusive |
| 62 // drive/root/dir5/ | 62 false, // is_recursive |
| 63 const char kTestRootResourceId[] = "test_root"; | 63 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 64 ASSERT_EQ(FILE_ERROR_OK, metadata->AddEntry( | 64 google_apis::test_util::RunBlockingPoolTask(); |
| 65 util::CreateMyDriveRootEntry(kTestRootResourceId))); | 65 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
| 66 ASSERT_EQ(FILE_ERROR_OK, metadata->AddEntry( | 66 EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory2)); |
| 67 CreateResourceEntry("dir1", kTestRootResourceId, true))); | |
| 68 ASSERT_EQ(FILE_ERROR_OK, metadata->AddEntry( | |
| 69 CreateResourceEntry("dir2", "resource_id:dir1", true))); | |
| 70 ASSERT_EQ(FILE_ERROR_OK, metadata->AddEntry( | |
| 71 CreateResourceEntry("file3", "resource_id:dir2", false))); | |
| 72 ASSERT_EQ(FILE_ERROR_OK, metadata->AddEntry( | |
| 73 CreateResourceEntry("file4", "resource_id:dir1", false))); | |
| 74 ASSERT_EQ(FILE_ERROR_OK, metadata->AddEntry( | |
| 75 CreateResourceEntry("dir5", kTestRootResourceId, true))); | |
| 76 | 67 |
| 77 ResourceEntry entry; | 68 operation.CreateDirectory( |
| 78 // Searching grand root and mydrive root. | 69 kNewDirectory2, |
| 79 EXPECT_EQ( | 70 true, // is_exclusive |
| 80 FILE_PATH_LITERAL("drive"), | 71 true, // is_recursive |
| 81 CreateDirectoryOperation::GetExistingDeepestDirectory( | 72 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 82 metadata.get(), base::FilePath(FILE_PATH_LITERAL("drive")), | 73 google_apis::test_util::RunBlockingPoolTask(); |
| 83 &entry).value()); | 74 EXPECT_EQ(FILE_ERROR_OK, error); |
| 84 EXPECT_EQ(util::kDriveGrandRootSpecialResourceId, entry.resource_id()); | 75 EXPECT_EQ(FILE_ERROR_OK, FindDirectory(kNewDirectory2)); |
| 85 EXPECT_EQ( | |
| 86 FILE_PATH_LITERAL("drive/root"), | |
| 87 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 88 metadata.get(), base::FilePath(FILE_PATH_LITERAL("drive/root")), | |
| 89 &entry).value()); | |
| 90 EXPECT_EQ(kTestRootResourceId, entry.resource_id()); | |
| 91 | 76 |
| 92 // Searching existing directories. | 77 // Try to create an existing directory. |
| 93 EXPECT_EQ( | 78 operation.CreateDirectory( |
| 94 FILE_PATH_LITERAL("drive/root/dir1"), | 79 kExistingDirectory, |
| 95 CreateDirectoryOperation::GetExistingDeepestDirectory( | 80 true, // is_exclusive |
| 96 metadata.get(), base::FilePath(FILE_PATH_LITERAL("drive/root/dir1")), | 81 false, // is_recursive |
| 97 &entry).value()); | 82 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 98 EXPECT_EQ("resource_id:dir1", entry.resource_id()); | 83 google_apis::test_util::RunBlockingPoolTask(); |
| 99 EXPECT_EQ( | 84 EXPECT_EQ(FILE_ERROR_EXISTS, error); |
| 100 FILE_PATH_LITERAL("drive/root/dir1/dir2"), | |
| 101 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 102 metadata.get(), | |
| 103 base::FilePath(FILE_PATH_LITERAL("drive/root/dir1/dir2")), | |
| 104 &entry).value()); | |
| 105 EXPECT_EQ("resource_id:dir2", entry.resource_id()); | |
| 106 | 85 |
| 107 // Searching missing directories. | 86 operation.CreateDirectory( |
| 108 EXPECT_EQ( | 87 kExistingDirectory, |
| 109 FILE_PATH_LITERAL("drive/root/dir1/dir2"), | 88 false, // is_exclusive |
| 110 CreateDirectoryOperation::GetExistingDeepestDirectory( | 89 false, // is_recursive |
| 111 metadata.get(), | 90 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 112 base::FilePath(FILE_PATH_LITERAL("drive/root/dir1/dir2/dir3")), | 91 google_apis::test_util::RunBlockingPoolTask(); |
| 113 &entry).value()); | 92 EXPECT_EQ(FILE_ERROR_OK, error); |
| 114 EXPECT_EQ("resource_id:dir2", entry.resource_id()); | |
| 115 EXPECT_EQ( | |
| 116 FILE_PATH_LITERAL("drive/root/dir1/dir2"), | |
| 117 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 118 metadata.get(), | |
| 119 base::FilePath(FILE_PATH_LITERAL("drive/root/dir1/dir2/dir3/dir4")), | |
| 120 &entry).value()); | |
| 121 EXPECT_EQ("resource_id:dir2", entry.resource_id()); | |
| 122 EXPECT_EQ( | |
| 123 FILE_PATH_LITERAL("drive/root/dir1"), | |
| 124 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 125 metadata.get(), | |
| 126 base::FilePath(FILE_PATH_LITERAL("drive/root/dir1/dir2-1/dir3")), | |
| 127 &entry).value()); | |
| 128 EXPECT_EQ("resource_id:dir1", entry.resource_id()); | |
| 129 EXPECT_EQ( | |
| 130 FILE_PATH_LITERAL("drive/root/dir5"), | |
| 131 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 132 metadata.get(), | |
| 133 base::FilePath(FILE_PATH_LITERAL("drive/root/dir5/dir6/dir7/dir8")), | |
| 134 &entry).value()); | |
| 135 EXPECT_EQ("resource_id:dir5", entry.resource_id()); | |
| 136 | 93 |
| 137 // If the path points to a file (not a directory), failed to find. | 94 // Try to create a directory with a path for an existing file. |
| 138 EXPECT_EQ( | 95 operation.CreateDirectory( |
| 139 FILE_PATH_LITERAL(""), | 96 kExistingFile, |
| 140 CreateDirectoryOperation::GetExistingDeepestDirectory( | 97 false, // is_exclusive |
| 141 metadata.get(), | 98 true, // is_recursive |
| 142 base::FilePath(FILE_PATH_LITERAL("drive/root/dir1/dir2/file3")), | 99 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 143 &entry).value()); | 100 google_apis::test_util::RunBlockingPoolTask(); |
| 144 EXPECT_EQ( | 101 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
| 145 FILE_PATH_LITERAL(""), | |
| 146 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 147 metadata.get(), | |
| 148 base::FilePath(FILE_PATH_LITERAL("drive/root/dir1/file4")), | |
| 149 &entry).value()); | |
| 150 | 102 |
| 151 // Searching missing directory under a "file" should be also failed. | 103 // Try to create a directory under a file. |
| 152 EXPECT_EQ( | 104 operation.CreateDirectory( |
| 153 FILE_PATH_LITERAL(""), | 105 kExistingFile.AppendASCII("New Directory"), |
| 154 CreateDirectoryOperation::GetExistingDeepestDirectory( | 106 false, // is_exclusive |
| 155 metadata.get(), | 107 true, // is_recursive |
| 156 base::FilePath(FILE_PATH_LITERAL("drive/root/dir1/file4/dir5")), | 108 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 157 &entry).value()); | 109 google_apis::test_util::RunBlockingPoolTask(); |
| 158 | 110 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
| 159 // Searching path not starting "drive" should be also failed. | |
| 160 EXPECT_EQ( | |
| 161 FILE_PATH_LITERAL(""), | |
| 162 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 163 metadata.get(), | |
| 164 base::FilePath(FILE_PATH_LITERAL("123456")), | |
| 165 &entry).value()); | |
| 166 EXPECT_EQ( | |
| 167 FILE_PATH_LITERAL(""), | |
| 168 CreateDirectoryOperation::GetExistingDeepestDirectory( | |
| 169 metadata.get(), | |
| 170 base::FilePath(FILE_PATH_LITERAL("foo/bar")), | |
| 171 &entry).value()); | |
| 172 } | 111 } |
| 173 | 112 |
| 174 } // namespace file_system | 113 } // namespace file_system |
| 175 } // namespace drive | 114 } // namespace drive |
| OLD | NEW |