| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/fileapi/fileapi_worker.h" | 5 #include "chrome/browser/chromeos/drive/fileapi/fileapi_worker.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "components/drive/dummy_file_system.h" | 13 #include "components/drive/dummy_file_system.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "content/public/test/test_utils.h" | 15 #include "content/public/test/test_utils.h" |
| 14 #include "google_apis/drive/test_util.h" | 16 #include "google_apis/drive/test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 bool closed() const { return closed_; } | 61 bool closed() const { return closed_; } |
| 60 | 62 |
| 61 private: | 63 private: |
| 62 const base::FilePath local_file_path_; | 64 const base::FilePath local_file_path_; |
| 63 const OpenMode expected_open_mode_; | 65 const OpenMode expected_open_mode_; |
| 64 bool closed_; | 66 bool closed_; |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 // Helper function of testing OpenFile() for write access. It checks that the | 69 // Helper function of testing OpenFile() for write access. It checks that the |
| 68 // file handle correctly writes to the expected file. | 70 // file handle correctly writes to the expected file. |
| 69 void VerifyWrite( | 71 void VerifyWrite(int64_t expected_size, |
| 70 int64 expected_size, | 72 const base::FilePath& expected_written_path, |
| 71 const base::FilePath& expected_written_path, | 73 const std::string& write_data, |
| 72 const std::string& write_data, | 74 base::File file, |
| 73 base::File file, | 75 const base::Closure& close_callback) { |
| 74 const base::Closure& close_callback) { | |
| 75 // Check that the file was properly opened. | 76 // Check that the file was properly opened. |
| 76 EXPECT_TRUE(file.IsValid()); | 77 EXPECT_TRUE(file.IsValid()); |
| 77 EXPECT_FALSE(close_callback.is_null()); | 78 EXPECT_FALSE(close_callback.is_null()); |
| 78 | 79 |
| 79 // Check that the file has the expected length (i.e., truncated or not) | 80 // Check that the file has the expected length (i.e., truncated or not) |
| 80 base::File::Info info; | 81 base::File::Info info; |
| 81 EXPECT_TRUE(file.GetInfo(&info)); | 82 EXPECT_TRUE(file.GetInfo(&info)); |
| 82 EXPECT_EQ(expected_size, info.size); | 83 EXPECT_EQ(expected_size, info.size); |
| 83 | 84 |
| 84 // Write some data. | 85 // Write some data. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 161 |
| 161 TEST_F(FileApiWorkerTest, OpenFileForCreateWrite) { | 162 TEST_F(FileApiWorkerTest, OpenFileForCreateWrite) { |
| 162 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 163 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 163 const std::string kWriteData = "byebye"; | 164 const std::string kWriteData = "byebye"; |
| 164 | 165 |
| 165 base::FilePath temp_path; | 166 base::FilePath temp_path; |
| 166 base::CreateTemporaryFile(&temp_path); | 167 base::CreateTemporaryFile(&temp_path); |
| 167 | 168 |
| 168 // CREATE => CREATE (fails if file exists.) | 169 // CREATE => CREATE (fails if file exists.) |
| 169 TestFileSystemForOpenFile file_system(temp_path, CREATE_FILE); | 170 TestFileSystemForOpenFile file_system(temp_path, CREATE_FILE); |
| 170 const int64 kExpectedSize = 0; | 171 const int64_t kExpectedSize = 0; |
| 171 | 172 |
| 172 OpenFile(kDummyPath, | 173 OpenFile(kDummyPath, |
| 173 base::File::FLAG_CREATE | base::File::FLAG_WRITE, | 174 base::File::FLAG_CREATE | base::File::FLAG_WRITE, |
| 174 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 175 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 175 &file_system); | 176 &file_system); |
| 176 content::RunAllBlockingPoolTasksUntilIdle(); | 177 content::RunAllBlockingPoolTasksUntilIdle(); |
| 177 EXPECT_TRUE(file_system.closed()); | 178 EXPECT_TRUE(file_system.closed()); |
| 178 } | 179 } |
| 179 | 180 |
| 180 TEST_F(FileApiWorkerTest, OpenFileForOpenAlwaysWrite) { | 181 TEST_F(FileApiWorkerTest, OpenFileForOpenAlwaysWrite) { |
| 181 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 182 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 182 const std::string kWriteData = "byebye"; | 183 const std::string kWriteData = "byebye"; |
| 183 const std::string kInitialData = "hello"; | 184 const std::string kInitialData = "hello"; |
| 184 | 185 |
| 185 base::FilePath temp_path; | 186 base::FilePath temp_path; |
| 186 base::CreateTemporaryFile(&temp_path); | 187 base::CreateTemporaryFile(&temp_path); |
| 187 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); | 188 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); |
| 188 | 189 |
| 189 // OPEN_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) | 190 // OPEN_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) |
| 190 // No truncation should take place. | 191 // No truncation should take place. |
| 191 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); | 192 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); |
| 192 const int64 kExpectedSize = static_cast<int64>(kInitialData.size()); | 193 const int64_t kExpectedSize = static_cast<int64_t>(kInitialData.size()); |
| 193 | 194 |
| 194 OpenFile(kDummyPath, | 195 OpenFile(kDummyPath, |
| 195 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE, | 196 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE, |
| 196 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 197 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 197 &file_system); | 198 &file_system); |
| 198 content::RunAllBlockingPoolTasksUntilIdle(); | 199 content::RunAllBlockingPoolTasksUntilIdle(); |
| 199 EXPECT_TRUE(file_system.closed()); | 200 EXPECT_TRUE(file_system.closed()); |
| 200 } | 201 } |
| 201 | 202 |
| 202 TEST_F(FileApiWorkerTest, OpenFileForOpenTruncatedWrite) { | 203 TEST_F(FileApiWorkerTest, OpenFileForOpenTruncatedWrite) { |
| 203 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 204 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 204 const std::string kInitialData = "hello"; | 205 const std::string kInitialData = "hello"; |
| 205 const std::string kWriteData = "byebye"; | 206 const std::string kWriteData = "byebye"; |
| 206 | 207 |
| 207 base::FilePath temp_path; | 208 base::FilePath temp_path; |
| 208 base::CreateTemporaryFile(&temp_path); | 209 base::CreateTemporaryFile(&temp_path); |
| 209 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); | 210 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); |
| 210 | 211 |
| 211 // OPEN_TRUNCATED => OPEN (failure when the file did not exist.) | 212 // OPEN_TRUNCATED => OPEN (failure when the file did not exist.) |
| 212 // It should truncate the file before passing to the callback. | 213 // It should truncate the file before passing to the callback. |
| 213 TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE); | 214 TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE); |
| 214 const int64 kExpectedSize = 0; | 215 const int64_t kExpectedSize = 0; |
| 215 | 216 |
| 216 OpenFile(kDummyPath, | 217 OpenFile(kDummyPath, |
| 217 base::File::FLAG_OPEN_TRUNCATED | base::File::FLAG_WRITE, | 218 base::File::FLAG_OPEN_TRUNCATED | base::File::FLAG_WRITE, |
| 218 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 219 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 219 &file_system); | 220 &file_system); |
| 220 content::RunAllBlockingPoolTasksUntilIdle(); | 221 content::RunAllBlockingPoolTasksUntilIdle(); |
| 221 EXPECT_TRUE(file_system.closed()); | 222 EXPECT_TRUE(file_system.closed()); |
| 222 } | 223 } |
| 223 | 224 |
| 224 TEST_F(FileApiWorkerTest, OpenFileForOpenCreateAlwaysWrite) { | 225 TEST_F(FileApiWorkerTest, OpenFileForOpenCreateAlwaysWrite) { |
| 225 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 226 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 226 const std::string kInitialData = "hello"; | 227 const std::string kInitialData = "hello"; |
| 227 const std::string kWriteData = "byebye"; | 228 const std::string kWriteData = "byebye"; |
| 228 | 229 |
| 229 base::FilePath temp_path; | 230 base::FilePath temp_path; |
| 230 base::CreateTemporaryFile(&temp_path); | 231 base::CreateTemporaryFile(&temp_path); |
| 231 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); | 232 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); |
| 232 | 233 |
| 233 // CREATE_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) | 234 // CREATE_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) |
| 234 // It should truncate the file before passing to the callback. | 235 // It should truncate the file before passing to the callback. |
| 235 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); | 236 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); |
| 236 const int64 kExpectedSize = 0; | 237 const int64_t kExpectedSize = 0; |
| 237 | 238 |
| 238 OpenFile(kDummyPath, | 239 OpenFile(kDummyPath, |
| 239 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, | 240 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, |
| 240 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 241 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 241 &file_system); | 242 &file_system); |
| 242 content::RunAllBlockingPoolTasksUntilIdle(); | 243 content::RunAllBlockingPoolTasksUntilIdle(); |
| 243 EXPECT_TRUE(file_system.closed()); | 244 EXPECT_TRUE(file_system.closed()); |
| 244 } | 245 } |
| 245 | 246 |
| 246 TEST_F(FileApiWorkerTest, OpenFileForOpenRead) { | 247 TEST_F(FileApiWorkerTest, OpenFileForOpenRead) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 257 OpenFile(kDummyPath, | 258 OpenFile(kDummyPath, |
| 258 base::File::FLAG_OPEN | base::File::FLAG_READ, | 259 base::File::FLAG_OPEN | base::File::FLAG_READ, |
| 259 base::Bind(&VerifyRead, kInitialData), | 260 base::Bind(&VerifyRead, kInitialData), |
| 260 &file_system); | 261 &file_system); |
| 261 content::RunAllBlockingPoolTasksUntilIdle(); | 262 content::RunAllBlockingPoolTasksUntilIdle(); |
| 262 EXPECT_TRUE(file_system.closed()); | 263 EXPECT_TRUE(file_system.closed()); |
| 263 } | 264 } |
| 264 | 265 |
| 265 } // namespace fileapi_internal | 266 } // namespace fileapi_internal |
| 266 } // namespace drive | 267 } // namespace drive |
| OLD | NEW |