| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/browser/chromeos/drive/dummy_file_system.h" | 11 #include "chrome/browser/chromeos/drive/dummy_file_system.h" |
| 12 #include "chrome/browser/chromeos/drive/test_util.h" | |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "content/public/test/test_utils.h" |
| 14 #include "google_apis/drive/test_util.h" | 14 #include "google_apis/drive/test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace drive { | 17 namespace drive { |
| 18 namespace fileapi_internal { | 18 namespace fileapi_internal { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Increments |num_called| for checking how many times the closure is called. | 21 // Increments |num_called| for checking how many times the closure is called. |
| 22 void Increment(int* num_called) { | 22 void Increment(int* num_called) { |
| 23 ++*num_called; | 23 ++*num_called; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 base::CreateTemporaryFile(&temp_path); | 166 base::CreateTemporaryFile(&temp_path); |
| 167 | 167 |
| 168 // CREATE => CREATE (fails if file exists.) | 168 // CREATE => CREATE (fails if file exists.) |
| 169 TestFileSystemForOpenFile file_system(temp_path, CREATE_FILE); | 169 TestFileSystemForOpenFile file_system(temp_path, CREATE_FILE); |
| 170 const int64 kExpectedSize = 0; | 170 const int64 kExpectedSize = 0; |
| 171 | 171 |
| 172 OpenFile(kDummyPath, | 172 OpenFile(kDummyPath, |
| 173 base::File::FLAG_CREATE | base::File::FLAG_WRITE, | 173 base::File::FLAG_CREATE | base::File::FLAG_WRITE, |
| 174 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 174 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 175 &file_system); | 175 &file_system); |
| 176 test_util::RunBlockingPoolTask(); | 176 content::RunAllBlockingPoolTasksUntilIdle(); |
| 177 EXPECT_TRUE(file_system.closed()); | 177 EXPECT_TRUE(file_system.closed()); |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST_F(FileApiWorkerTest, OpenFileForOpenAlwaysWrite) { | 180 TEST_F(FileApiWorkerTest, OpenFileForOpenAlwaysWrite) { |
| 181 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 181 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 182 const std::string kWriteData = "byebye"; | 182 const std::string kWriteData = "byebye"; |
| 183 const std::string kInitialData = "hello"; | 183 const std::string kInitialData = "hello"; |
| 184 | 184 |
| 185 base::FilePath temp_path; | 185 base::FilePath temp_path; |
| 186 base::CreateTemporaryFile(&temp_path); | 186 base::CreateTemporaryFile(&temp_path); |
| 187 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); | 187 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); |
| 188 | 188 |
| 189 // OPEN_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) | 189 // OPEN_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) |
| 190 // No truncation should take place. | 190 // No truncation should take place. |
| 191 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); | 191 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); |
| 192 const int64 kExpectedSize = static_cast<int64>(kInitialData.size()); | 192 const int64 kExpectedSize = static_cast<int64>(kInitialData.size()); |
| 193 | 193 |
| 194 OpenFile(kDummyPath, | 194 OpenFile(kDummyPath, |
| 195 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE, | 195 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE, |
| 196 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 196 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 197 &file_system); | 197 &file_system); |
| 198 test_util::RunBlockingPoolTask(); | 198 content::RunAllBlockingPoolTasksUntilIdle(); |
| 199 EXPECT_TRUE(file_system.closed()); | 199 EXPECT_TRUE(file_system.closed()); |
| 200 } | 200 } |
| 201 | 201 |
| 202 TEST_F(FileApiWorkerTest, OpenFileForOpenTruncatedWrite) { | 202 TEST_F(FileApiWorkerTest, OpenFileForOpenTruncatedWrite) { |
| 203 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 203 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 204 const std::string kInitialData = "hello"; | 204 const std::string kInitialData = "hello"; |
| 205 const std::string kWriteData = "byebye"; | 205 const std::string kWriteData = "byebye"; |
| 206 | 206 |
| 207 base::FilePath temp_path; | 207 base::FilePath temp_path; |
| 208 base::CreateTemporaryFile(&temp_path); | 208 base::CreateTemporaryFile(&temp_path); |
| 209 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); | 209 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); |
| 210 | 210 |
| 211 // OPEN_TRUNCATED => OPEN (failure when the file did not exist.) | 211 // OPEN_TRUNCATED => OPEN (failure when the file did not exist.) |
| 212 // It should truncate the file before passing to the callback. | 212 // It should truncate the file before passing to the callback. |
| 213 TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE); | 213 TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE); |
| 214 const int64 kExpectedSize = 0; | 214 const int64 kExpectedSize = 0; |
| 215 | 215 |
| 216 OpenFile(kDummyPath, | 216 OpenFile(kDummyPath, |
| 217 base::File::FLAG_OPEN_TRUNCATED | base::File::FLAG_WRITE, | 217 base::File::FLAG_OPEN_TRUNCATED | base::File::FLAG_WRITE, |
| 218 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 218 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 219 &file_system); | 219 &file_system); |
| 220 test_util::RunBlockingPoolTask(); | 220 content::RunAllBlockingPoolTasksUntilIdle(); |
| 221 EXPECT_TRUE(file_system.closed()); | 221 EXPECT_TRUE(file_system.closed()); |
| 222 } | 222 } |
| 223 | 223 |
| 224 TEST_F(FileApiWorkerTest, OpenFileForOpenCreateAlwaysWrite) { | 224 TEST_F(FileApiWorkerTest, OpenFileForOpenCreateAlwaysWrite) { |
| 225 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 225 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 226 const std::string kInitialData = "hello"; | 226 const std::string kInitialData = "hello"; |
| 227 const std::string kWriteData = "byebye"; | 227 const std::string kWriteData = "byebye"; |
| 228 | 228 |
| 229 base::FilePath temp_path; | 229 base::FilePath temp_path; |
| 230 base::CreateTemporaryFile(&temp_path); | 230 base::CreateTemporaryFile(&temp_path); |
| 231 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); | 231 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); |
| 232 | 232 |
| 233 // CREATE_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) | 233 // CREATE_ALWAYS => OPEN_OR_CREATE (success whether file exists or not.) |
| 234 // It should truncate the file before passing to the callback. | 234 // It should truncate the file before passing to the callback. |
| 235 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); | 235 TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE); |
| 236 const int64 kExpectedSize = 0; | 236 const int64 kExpectedSize = 0; |
| 237 | 237 |
| 238 OpenFile(kDummyPath, | 238 OpenFile(kDummyPath, |
| 239 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, | 239 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, |
| 240 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), | 240 base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData), |
| 241 &file_system); | 241 &file_system); |
| 242 test_util::RunBlockingPoolTask(); | 242 content::RunAllBlockingPoolTasksUntilIdle(); |
| 243 EXPECT_TRUE(file_system.closed()); | 243 EXPECT_TRUE(file_system.closed()); |
| 244 } | 244 } |
| 245 | 245 |
| 246 TEST_F(FileApiWorkerTest, OpenFileForOpenRead) { | 246 TEST_F(FileApiWorkerTest, OpenFileForOpenRead) { |
| 247 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); | 247 const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever"); |
| 248 const std::string kInitialData = "hello"; | 248 const std::string kInitialData = "hello"; |
| 249 | 249 |
| 250 base::FilePath temp_path; | 250 base::FilePath temp_path; |
| 251 base::CreateTemporaryFile(&temp_path); | 251 base::CreateTemporaryFile(&temp_path); |
| 252 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); | 252 google_apis::test_util::WriteStringToFile(temp_path, kInitialData); |
| 253 | 253 |
| 254 // OPEN => OPEN (failure when the file did not exist.) | 254 // OPEN => OPEN (failure when the file did not exist.) |
| 255 TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE); | 255 TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE); |
| 256 | 256 |
| 257 OpenFile(kDummyPath, | 257 OpenFile(kDummyPath, |
| 258 base::File::FLAG_OPEN | base::File::FLAG_READ, | 258 base::File::FLAG_OPEN | base::File::FLAG_READ, |
| 259 base::Bind(&VerifyRead, kInitialData), | 259 base::Bind(&VerifyRead, kInitialData), |
| 260 &file_system); | 260 &file_system); |
| 261 test_util::RunBlockingPoolTask(); | 261 content::RunAllBlockingPoolTasksUntilIdle(); |
| 262 EXPECT_TRUE(file_system.closed()); | 262 EXPECT_TRUE(file_system.closed()); |
| 263 } | 263 } |
| 264 | 264 |
| 265 } // namespace fileapi_internal | 265 } // namespace fileapi_internal |
| 266 } // namespace drive | 266 } // namespace drive |
| OLD | NEW |