| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/open_file_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/open_file_operation.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/chromeos/drive/drive.pb.h" | 12 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 13 #include "chrome/browser/chromeos/drive/file_errors.h" | 13 #include "chrome/browser/chromeos/drive/file_errors.h" |
| 14 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" | 14 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
| 15 #include "chrome/browser/google_apis/test_util.h" | 15 #include "chrome/browser/google_apis/test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace drive { | 18 namespace drive { |
| 19 namespace file_system { | 19 namespace file_system { |
| 20 | 20 |
| 21 class OpenFileOperationTest : public OperationTestBase { | 21 class OpenFileOperationTest : public OperationTestBase { |
| 22 protected: | 22 protected: |
| 23 virtual void SetUp() { | 23 virtual void SetUp() { |
| 24 OperationTestBase::SetUp(); | 24 OperationTestBase::SetUp(); |
| 25 | 25 |
| 26 operation_.reset(new OpenFileOperation( | 26 operation_.reset(new OpenFileOperation( |
| 27 blocking_task_runner(), observer(), scheduler(), metadata(), cache(), | 27 blocking_task_runner(), observer(), scheduler(), metadata(), cache(), |
| 28 temp_dir(), &open_files_)); | 28 temp_dir())); |
| 29 } | 29 } |
| 30 | 30 |
| 31 std::map<base::FilePath, int> open_files_; | |
| 32 scoped_ptr<OpenFileOperation> operation_; | 31 scoped_ptr<OpenFileOperation> operation_; |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 TEST_F(OpenFileOperationTest, OpenExistingFile) { | 34 TEST_F(OpenFileOperationTest, OpenExistingFile) { |
| 36 const base::FilePath file_in_root( | 35 const base::FilePath file_in_root( |
| 37 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 36 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 38 ResourceEntry src_entry; | 37 ResourceEntry src_entry; |
| 39 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 38 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 40 const int64 file_size = src_entry.file_info().size(); | 39 const int64 file_size = src_entry.file_info().size(); |
| 41 | 40 |
| 42 FileError error = FILE_ERROR_FAILED; | 41 FileError error = FILE_ERROR_FAILED; |
| 43 base::FilePath file_path; | 42 base::FilePath file_path; |
| 43 base::Closure close_callback; |
| 44 operation_->OpenFile( | 44 operation_->OpenFile( |
| 45 file_in_root, | 45 file_in_root, |
| 46 OPEN_FILE, | 46 OPEN_FILE, |
| 47 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 47 google_apis::test_util::CreateCopyResultCallback( |
| 48 &error, &file_path, &close_callback)); |
| 48 test_util::RunBlockingPoolTask(); | 49 test_util::RunBlockingPoolTask(); |
| 49 | 50 |
| 50 EXPECT_EQ(FILE_ERROR_OK, error); | 51 EXPECT_EQ(FILE_ERROR_OK, error); |
| 51 ASSERT_TRUE(base::PathExists(file_path)); | 52 ASSERT_TRUE(base::PathExists(file_path)); |
| 52 int64 local_file_size; | 53 int64 local_file_size; |
| 53 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 54 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
| 54 EXPECT_EQ(file_size, local_file_size); | 55 EXPECT_EQ(file_size, local_file_size); |
| 55 | 56 |
| 56 // The file_path should be added into the set. | 57 ASSERT_FALSE(close_callback.is_null()); |
| 57 EXPECT_EQ(1, open_files_[file_in_root]); | 58 close_callback.Run(); |
| 59 EXPECT_EQ( |
| 60 1U, |
| 61 observer()->upload_needed_resource_ids().count(src_entry.resource_id())); |
| 58 } | 62 } |
| 59 | 63 |
| 60 TEST_F(OpenFileOperationTest, OpenNonExistingFile) { | 64 TEST_F(OpenFileOperationTest, OpenNonExistingFile) { |
| 61 const base::FilePath file_in_root( | 65 const base::FilePath file_in_root( |
| 62 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 66 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
| 63 | 67 |
| 64 FileError error = FILE_ERROR_FAILED; | 68 FileError error = FILE_ERROR_FAILED; |
| 65 base::FilePath file_path; | 69 base::FilePath file_path; |
| 70 base::Closure close_callback; |
| 66 operation_->OpenFile( | 71 operation_->OpenFile( |
| 67 file_in_root, | 72 file_in_root, |
| 68 OPEN_FILE, | 73 OPEN_FILE, |
| 69 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 74 google_apis::test_util::CreateCopyResultCallback( |
| 75 &error, &file_path, &close_callback)); |
| 70 test_util::RunBlockingPoolTask(); | 76 test_util::RunBlockingPoolTask(); |
| 71 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | 77 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
| 72 | 78 EXPECT_TRUE(close_callback.is_null()); |
| 73 // The file shouldn't be in the set of opened files. | |
| 74 EXPECT_EQ(0U, open_files_.count(file_in_root)); | |
| 75 } | 79 } |
| 76 | 80 |
| 77 TEST_F(OpenFileOperationTest, CreateExistingFile) { | 81 TEST_F(OpenFileOperationTest, CreateExistingFile) { |
| 78 const base::FilePath file_in_root( | 82 const base::FilePath file_in_root( |
| 79 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 83 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 80 ResourceEntry src_entry; | 84 ResourceEntry src_entry; |
| 81 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 85 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 82 | 86 |
| 83 FileError error = FILE_ERROR_FAILED; | 87 FileError error = FILE_ERROR_FAILED; |
| 84 base::FilePath file_path; | 88 base::FilePath file_path; |
| 89 base::Closure close_callback; |
| 85 operation_->OpenFile( | 90 operation_->OpenFile( |
| 86 file_in_root, | 91 file_in_root, |
| 87 CREATE_FILE, | 92 CREATE_FILE, |
| 88 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 93 google_apis::test_util::CreateCopyResultCallback( |
| 94 &error, &file_path, &close_callback)); |
| 89 test_util::RunBlockingPoolTask(); | 95 test_util::RunBlockingPoolTask(); |
| 90 | 96 |
| 91 EXPECT_EQ(FILE_ERROR_EXISTS, error); | 97 EXPECT_EQ(FILE_ERROR_EXISTS, error); |
| 92 | 98 EXPECT_TRUE(close_callback.is_null()); |
| 93 // The file shouldn't be in the set of opened files. | |
| 94 EXPECT_EQ(0U, open_files_.count(file_in_root)); | |
| 95 } | 99 } |
| 96 | 100 |
| 97 TEST_F(OpenFileOperationTest, CreateNonExistingFile) { | 101 TEST_F(OpenFileOperationTest, CreateNonExistingFile) { |
| 98 const base::FilePath file_in_root( | 102 const base::FilePath file_in_root( |
| 99 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 103 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
| 100 | 104 |
| 101 FileError error = FILE_ERROR_FAILED; | 105 FileError error = FILE_ERROR_FAILED; |
| 102 base::FilePath file_path; | 106 base::FilePath file_path; |
| 107 base::Closure close_callback; |
| 103 operation_->OpenFile( | 108 operation_->OpenFile( |
| 104 file_in_root, | 109 file_in_root, |
| 105 CREATE_FILE, | 110 CREATE_FILE, |
| 106 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 111 google_apis::test_util::CreateCopyResultCallback( |
| 112 &error, &file_path, &close_callback)); |
| 107 test_util::RunBlockingPoolTask(); | 113 test_util::RunBlockingPoolTask(); |
| 108 | 114 |
| 109 EXPECT_EQ(FILE_ERROR_OK, error); | 115 EXPECT_EQ(FILE_ERROR_OK, error); |
| 110 ASSERT_TRUE(base::PathExists(file_path)); | 116 ASSERT_TRUE(base::PathExists(file_path)); |
| 111 int64 local_file_size; | 117 int64 local_file_size; |
| 112 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 118 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
| 113 EXPECT_EQ(0, local_file_size); // Should be an empty file. | 119 EXPECT_EQ(0, local_file_size); // Should be an empty file. |
| 114 | 120 |
| 115 // The file_path should be added into the set. | 121 ASSERT_FALSE(close_callback.is_null()); |
| 116 EXPECT_EQ(1, open_files_[file_in_root]); | 122 close_callback.Run(); |
| 123 // Here we don't know about the resource id, so just make sure |
| 124 // OnCacheFileUploadNeededByOperation is called actually. |
| 125 EXPECT_EQ(1U, observer()->upload_needed_resource_ids().size()); |
| 117 } | 126 } |
| 118 | 127 |
| 119 TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) { | 128 TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) { |
| 120 const base::FilePath file_in_root( | 129 const base::FilePath file_in_root( |
| 121 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 130 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 122 ResourceEntry src_entry; | 131 ResourceEntry src_entry; |
| 123 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 132 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 124 const int64 file_size = src_entry.file_info().size(); | 133 const int64 file_size = src_entry.file_info().size(); |
| 125 | 134 |
| 126 FileError error = FILE_ERROR_FAILED; | 135 FileError error = FILE_ERROR_FAILED; |
| 127 base::FilePath file_path; | 136 base::FilePath file_path; |
| 137 base::Closure close_callback; |
| 128 operation_->OpenFile( | 138 operation_->OpenFile( |
| 129 file_in_root, | 139 file_in_root, |
| 130 OPEN_OR_CREATE_FILE, | 140 OPEN_OR_CREATE_FILE, |
| 131 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 141 google_apis::test_util::CreateCopyResultCallback( |
| 142 &error, &file_path, &close_callback)); |
| 132 test_util::RunBlockingPoolTask(); | 143 test_util::RunBlockingPoolTask(); |
| 133 | 144 |
| 134 EXPECT_EQ(FILE_ERROR_OK, error); | 145 EXPECT_EQ(FILE_ERROR_OK, error); |
| 135 ASSERT_TRUE(base::PathExists(file_path)); | 146 ASSERT_TRUE(base::PathExists(file_path)); |
| 136 int64 local_file_size; | 147 int64 local_file_size; |
| 137 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 148 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
| 138 EXPECT_EQ(file_size, local_file_size); | 149 EXPECT_EQ(file_size, local_file_size); |
| 139 | 150 |
| 140 // The file_path should be added into the set. | 151 ASSERT_FALSE(close_callback.is_null()); |
| 141 EXPECT_EQ(1, open_files_[file_in_root]); | 152 close_callback.Run(); |
| 153 EXPECT_EQ( |
| 154 1U, |
| 155 observer()->upload_needed_resource_ids().count(src_entry.resource_id())); |
| 142 } | 156 } |
| 143 | 157 |
| 144 TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) { | 158 TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) { |
| 145 const base::FilePath file_in_root( | 159 const base::FilePath file_in_root( |
| 146 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 160 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
| 147 | 161 |
| 148 FileError error = FILE_ERROR_FAILED; | 162 FileError error = FILE_ERROR_FAILED; |
| 149 base::FilePath file_path; | 163 base::FilePath file_path; |
| 164 base::Closure close_callback; |
| 150 operation_->OpenFile( | 165 operation_->OpenFile( |
| 151 file_in_root, | 166 file_in_root, |
| 152 OPEN_OR_CREATE_FILE, | 167 OPEN_OR_CREATE_FILE, |
| 153 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 168 google_apis::test_util::CreateCopyResultCallback( |
| 169 &error, &file_path, &close_callback)); |
| 154 test_util::RunBlockingPoolTask(); | 170 test_util::RunBlockingPoolTask(); |
| 155 | 171 |
| 156 EXPECT_EQ(FILE_ERROR_OK, error); | 172 EXPECT_EQ(FILE_ERROR_OK, error); |
| 157 ASSERT_TRUE(base::PathExists(file_path)); | 173 ASSERT_TRUE(base::PathExists(file_path)); |
| 158 int64 local_file_size; | 174 int64 local_file_size; |
| 159 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 175 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
| 160 EXPECT_EQ(0, local_file_size); // Should be an empty file. | 176 EXPECT_EQ(0, local_file_size); // Should be an empty file. |
| 161 | 177 |
| 162 // The file_path should be added into the set. | 178 ASSERT_FALSE(close_callback.is_null()); |
| 163 EXPECT_EQ(1, open_files_[file_in_root]); | 179 close_callback.Run(); |
| 180 // Here we don't know about the resource id, so just make sure |
| 181 // OnCacheFileUploadNeededByOperation is called actually. |
| 182 EXPECT_EQ(1U, observer()->upload_needed_resource_ids().size()); |
| 164 } | 183 } |
| 165 | 184 |
| 166 TEST_F(OpenFileOperationTest, OpenFileTwice) { | 185 TEST_F(OpenFileOperationTest, OpenFileTwice) { |
| 167 const base::FilePath file_in_root( | 186 const base::FilePath file_in_root( |
| 168 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 187 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 169 ResourceEntry src_entry; | 188 ResourceEntry src_entry; |
| 170 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 189 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 171 const int64 file_size = src_entry.file_info().size(); | 190 const int64 file_size = src_entry.file_info().size(); |
| 172 | 191 |
| 173 FileError error = FILE_ERROR_FAILED; | 192 FileError error = FILE_ERROR_FAILED; |
| 174 base::FilePath file_path; | 193 base::FilePath file_path; |
| 194 base::Closure close_callback; |
| 175 operation_->OpenFile( | 195 operation_->OpenFile( |
| 176 file_in_root, | 196 file_in_root, |
| 177 OPEN_FILE, | 197 OPEN_FILE, |
| 178 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 198 google_apis::test_util::CreateCopyResultCallback( |
| 199 &error, &file_path, &close_callback)); |
| 179 test_util::RunBlockingPoolTask(); | 200 test_util::RunBlockingPoolTask(); |
| 180 | 201 |
| 181 EXPECT_EQ(FILE_ERROR_OK, error); | 202 EXPECT_EQ(FILE_ERROR_OK, error); |
| 182 ASSERT_TRUE(base::PathExists(file_path)); | 203 ASSERT_TRUE(base::PathExists(file_path)); |
| 183 int64 local_file_size; | 204 int64 local_file_size; |
| 184 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 205 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
| 185 EXPECT_EQ(file_size, local_file_size); | 206 EXPECT_EQ(file_size, local_file_size); |
| 186 | 207 |
| 187 // The file_path should be added into the set. | |
| 188 EXPECT_EQ(1, open_files_[file_in_root]); | |
| 189 | |
| 190 // Open again. | 208 // Open again. |
| 191 error = FILE_ERROR_FAILED; | 209 error = FILE_ERROR_FAILED; |
| 210 base::Closure close_callback2; |
| 192 operation_->OpenFile( | 211 operation_->OpenFile( |
| 193 file_in_root, | 212 file_in_root, |
| 194 OPEN_FILE, | 213 OPEN_FILE, |
| 195 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 214 google_apis::test_util::CreateCopyResultCallback( |
| 215 &error, &file_path, &close_callback2)); |
| 196 test_util::RunBlockingPoolTask(); | 216 test_util::RunBlockingPoolTask(); |
| 197 | 217 |
| 198 EXPECT_EQ(FILE_ERROR_OK, error); | 218 EXPECT_EQ(FILE_ERROR_OK, error); |
| 199 ASSERT_TRUE(base::PathExists(file_path)); | 219 ASSERT_TRUE(base::PathExists(file_path)); |
| 200 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 220 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
| 201 EXPECT_EQ(file_size, local_file_size); | 221 EXPECT_EQ(file_size, local_file_size); |
| 202 | 222 |
| 203 // The file_path should be added into the set. | 223 ASSERT_FALSE(close_callback.is_null()); |
| 204 EXPECT_EQ(2, open_files_[file_in_root]); | 224 ASSERT_FALSE(close_callback2.is_null()); |
| 225 |
| 226 close_callback.Run(); |
| 227 |
| 228 // There still remains a client opening the file, so it shouldn't be |
| 229 // uploaded yet. |
| 230 EXPECT_TRUE(observer()->upload_needed_resource_ids().empty()); |
| 231 |
| 232 close_callback2.Run(); |
| 233 |
| 234 // Here, all the clients close the file, so it should be uploaded then. |
| 235 EXPECT_EQ( |
| 236 1U, |
| 237 observer()->upload_needed_resource_ids().count(src_entry.resource_id())); |
| 205 } | 238 } |
| 206 | 239 |
| 207 } // namespace file_system | 240 } // namespace file_system |
| 208 } // namespace drive | 241 } // namespace drive |
| OLD | NEW |