| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/file_system_provider/queue.h" | 5 #include "chrome/browser/chromeos/file_system_provider/queue.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace chromeos { | 14 namespace chromeos { |
| 15 namespace file_system_provider { | 15 namespace file_system_provider { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 void OnAbort(int* abort_counter, | 18 void OnAbort(int* abort_counter) { |
| 19 const storage::AsyncFileUtil::StatusCallback& callback) { | 19 ++(*abort_counter); |
| 20 (*abort_counter)++; | |
| 21 callback.Run(base::File::FILE_ERROR_FAILED); | |
| 22 } | 20 } |
| 23 | 21 |
| 24 AbortCallback OnRun(int* run_counter, int* abort_counter) { | 22 AbortCallback OnRun(int* run_counter, int* abort_counter) { |
| 25 (*run_counter)++; | 23 ++(*run_counter); |
| 26 return base::Bind(&OnAbort, abort_counter); | 24 return base::Bind(&OnAbort, abort_counter); |
| 27 } | 25 } |
| 28 | 26 |
| 29 void OnAbortCallback(std::vector<base::File::Error>* log, | 27 #if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST) |
| 30 base::File::Error result) { | 28 |
| 31 log->push_back(result); | 29 AbortCallback OnRunNonAbortable(int* run_counter, int* abort_counter) { |
| 30 ++(*run_counter); |
| 31 return AbortCallback(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 #endif |
| 35 |
| 34 } // namespace | 36 } // namespace |
| 35 | 37 |
| 36 class FileSystemProviderQueueTest : public testing::Test { | 38 class FileSystemProviderQueueTest : public testing::Test { |
| 37 protected: | 39 protected: |
| 38 FileSystemProviderQueueTest() {} | 40 FileSystemProviderQueueTest() {} |
| 39 virtual ~FileSystemProviderQueueTest() {} | 41 virtual ~FileSystemProviderQueueTest() {} |
| 40 | 42 |
| 41 content::TestBrowserThreadBundle thread_bundle_; | 43 content::TestBrowserThreadBundle thread_bundle_; |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 TEST_F(FileSystemProviderQueueTest, NewToken) { | 46 TEST_F(FileSystemProviderQueueTest, NewToken) { |
| 45 Queue queue(1); | 47 Queue queue(1); |
| 46 EXPECT_EQ(1u, queue.NewToken()); | 48 EXPECT_EQ(1u, queue.NewToken()); |
| 47 EXPECT_EQ(2u, queue.NewToken()); | 49 EXPECT_EQ(2u, queue.NewToken()); |
| 48 EXPECT_EQ(3u, queue.NewToken()); | 50 EXPECT_EQ(3u, queue.NewToken()); |
| 49 } | 51 } |
| 50 | 52 |
| 51 TEST_F(FileSystemProviderQueueTest, Enqueue_OneAtOnce) { | 53 TEST_F(FileSystemProviderQueueTest, Enqueue_OneAtOnce) { |
| 52 Queue queue(1); | 54 Queue queue(1); |
| 53 const size_t first_token = queue.NewToken(); | 55 const size_t first_token = queue.NewToken(); |
| 54 int first_counter = 0; | 56 int first_counter = 0; |
| 55 int first_abort_counter = 0; | 57 int first_abort_counter = 0; |
| 56 queue.Enqueue(first_token, | 58 queue.Enqueue(first_token, |
| 57 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 59 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 58 | 60 |
| 59 const size_t second_token = queue.NewToken(); | 61 const size_t second_token = queue.NewToken(); |
| 60 int second_counter = 0; | 62 int second_counter = 0; |
| 61 int second_abort_counter = 0; | 63 int second_abort_counter = 0; |
| 62 const AbortCallback abort_callback = queue.Enqueue( | 64 queue.Enqueue(second_token, |
| 63 second_token, base::Bind(&OnRun, &second_counter, &second_abort_counter)); | 65 base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
| 64 | 66 |
| 65 base::RunLoop().RunUntilIdle(); | 67 base::RunLoop().RunUntilIdle(); |
| 66 EXPECT_EQ(1, first_counter); | 68 EXPECT_EQ(1, first_counter); |
| 67 EXPECT_EQ(0, first_abort_counter); | 69 EXPECT_EQ(0, first_abort_counter); |
| 68 EXPECT_EQ(0, second_counter); | 70 EXPECT_EQ(0, second_counter); |
| 69 EXPECT_EQ(0, second_abort_counter); | 71 EXPECT_EQ(0, second_abort_counter); |
| 70 | 72 |
| 71 // Complete the first task, which should not run the second one, yet. | 73 // Complete the first task, which should not run the second one, yet. |
| 72 queue.Complete(first_token); | 74 queue.Complete(first_token); |
| 73 base::RunLoop().RunUntilIdle(); | 75 base::RunLoop().RunUntilIdle(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 93 // The second task is still running, so the third one is blocked. | 95 // The second task is still running, so the third one is blocked. |
| 94 base::RunLoop().RunUntilIdle(); | 96 base::RunLoop().RunUntilIdle(); |
| 95 EXPECT_EQ(1, first_counter); | 97 EXPECT_EQ(1, first_counter); |
| 96 EXPECT_EQ(0, first_abort_counter); | 98 EXPECT_EQ(0, first_abort_counter); |
| 97 EXPECT_EQ(1, second_counter); | 99 EXPECT_EQ(1, second_counter); |
| 98 EXPECT_EQ(0, second_abort_counter); | 100 EXPECT_EQ(0, second_abort_counter); |
| 99 EXPECT_EQ(0, third_counter); | 101 EXPECT_EQ(0, third_counter); |
| 100 EXPECT_EQ(0, third_abort_counter); | 102 EXPECT_EQ(0, third_abort_counter); |
| 101 | 103 |
| 102 // After aborting the second task, the third should run. | 104 // After aborting the second task, the third should run. |
| 103 std::vector<base::File::Error> abort_callback_log; | 105 queue.Abort(second_token); |
| 104 abort_callback.Run(base::Bind(&OnAbortCallback, &abort_callback_log)); | 106 queue.Remove(second_token); |
| 105 base::RunLoop().RunUntilIdle(); | 107 base::RunLoop().RunUntilIdle(); |
| 106 EXPECT_EQ(1, first_counter); | 108 EXPECT_EQ(1, first_counter); |
| 107 EXPECT_EQ(0, first_abort_counter); | 109 EXPECT_EQ(0, first_abort_counter); |
| 108 EXPECT_EQ(1, second_counter); | 110 EXPECT_EQ(1, second_counter); |
| 109 EXPECT_EQ(1, second_abort_counter); | 111 EXPECT_EQ(1, second_abort_counter); |
| 110 ASSERT_EQ(1u, abort_callback_log.size()); | |
| 111 EXPECT_EQ(base::File::FILE_ERROR_FAILED, abort_callback_log[0]); | |
| 112 EXPECT_EQ(1, third_counter); | 112 EXPECT_EQ(1, third_counter); |
| 113 EXPECT_EQ(0, third_abort_counter); | 113 EXPECT_EQ(0, third_abort_counter); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) { | 116 TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) { |
| 117 Queue queue(2); | 117 Queue queue(2); |
| 118 const size_t first_token = queue.NewToken(); | 118 const size_t first_token = queue.NewToken(); |
| 119 int first_counter = 0; | 119 int first_counter = 0; |
| 120 int first_abort_counter = 0; | 120 int first_abort_counter = 0; |
| 121 queue.Enqueue(first_token, | 121 queue.Enqueue(first_token, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 int first_abort_counter = 0; | 178 int first_abort_counter = 0; |
| 179 queue.Enqueue(first_token, | 179 queue.Enqueue(first_token, |
| 180 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 180 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 181 | 181 |
| 182 // Completing and removing the first task, which however hasn't started. | 182 // Completing and removing the first task, which however hasn't started. |
| 183 // That should not invoke the second task. | 183 // That should not invoke the second task. |
| 184 EXPECT_DEATH(queue.Complete(first_token), ""); | 184 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 185 EXPECT_DEATH(queue.Remove(first_token), ""); | 185 EXPECT_DEATH(queue.Remove(first_token), ""); |
| 186 } | 186 } |
| 187 | 187 |
| 188 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveNotCompleted) { | 188 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveNotCompletedNorAborted) { |
| 189 Queue queue(1); | 189 Queue queue(1); |
| 190 const size_t first_token = queue.NewToken(); | 190 const size_t first_token = queue.NewToken(); |
| 191 int first_counter = 0; | 191 int first_counter = 0; |
| 192 int first_abort_counter = 0; | 192 int first_abort_counter = 0; |
| 193 queue.Enqueue(first_token, | 193 queue.Enqueue(first_token, |
| 194 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 194 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 195 | 195 |
| 196 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
| 197 | 197 |
| 198 // Remove before completing. | 198 // Remove before completing. |
| 199 EXPECT_DEATH(queue.Remove(first_token), ""); | 199 EXPECT_DEATH(queue.Remove(first_token), ""); |
| 200 } | 200 } |
| 201 | 201 |
| 202 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterAborting) { | 202 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterAborting) { |
| 203 Queue queue(1); | 203 Queue queue(1); |
| 204 const size_t first_token = queue.NewToken(); | 204 const size_t first_token = queue.NewToken(); |
| 205 int first_counter = 0; | 205 int first_counter = 0; |
| 206 int first_abort_counter = 0; | 206 int first_abort_counter = 0; |
| 207 AbortCallback first_abort_callback = queue.Enqueue( | 207 queue.Enqueue(first_token, |
| 208 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 208 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 209 | 209 |
| 210 base::RunLoop().RunUntilIdle(); | 210 base::RunLoop().RunUntilIdle(); |
| 211 | 211 |
| 212 // Run, then abort. | 212 // Run, then abort. |
| 213 std::vector<base::File::Error> first_abort_callback_log; | 213 std::vector<base::File::Error> first_abort_callback_log; |
| 214 first_abort_callback.Run( | 214 queue.Abort(first_token); |
| 215 base::Bind(&OnAbortCallback, &first_abort_callback_log)); | |
| 216 | 215 |
| 217 EXPECT_DEATH(queue.Complete(first_token), ""); | 216 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 218 } | 217 } |
| 219 | 218 |
| 220 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveAfterAborting) { | 219 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortAfterCompleting) { |
| 221 Queue queue(1); | 220 Queue queue(1); |
| 222 const size_t first_token = queue.NewToken(); | 221 const size_t first_token = queue.NewToken(); |
| 223 int first_counter = 0; | 222 int first_counter = 0; |
| 224 int first_abort_counter = 0; | 223 int first_abort_counter = 0; |
| 225 AbortCallback first_abort_callback = queue.Enqueue( | 224 queue.Enqueue(first_token, |
| 226 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 225 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 227 | 226 |
| 228 base::RunLoop().RunUntilIdle(); | 227 base::RunLoop().RunUntilIdle(); |
| 229 | 228 |
| 230 // Abort after executing. | 229 queue.Complete(first_token); |
| 231 std::vector<base::File::Error> first_abort_callback_log; | 230 EXPECT_DEATH(queue.Abort(first_token), ""); |
| 232 first_abort_callback.Run( | |
| 233 base::Bind(&OnAbortCallback, &first_abort_callback_log)); | |
| 234 | |
| 235 base::RunLoop().RunUntilIdle(); | |
| 236 | |
| 237 // Remove before completing. | |
| 238 EXPECT_DEATH(queue.Remove(first_token), ""); | |
| 239 } | 231 } |
| 240 | 232 |
| 241 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteTwice) { | 233 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteTwice) { |
| 242 Queue queue(1); | 234 Queue queue(1); |
| 243 const size_t first_token = queue.NewToken(); | 235 const size_t first_token = queue.NewToken(); |
| 244 int first_counter = 0; | 236 int first_counter = 0; |
| 245 int first_abort_counter = 0; | 237 int first_abort_counter = 0; |
| 246 AbortCallback first_abort_callback = queue.Enqueue( | 238 queue.Enqueue(first_token, |
| 247 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 239 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 248 | 240 |
| 249 base::RunLoop().RunUntilIdle(); | 241 base::RunLoop().RunUntilIdle(); |
| 250 | 242 |
| 251 queue.Complete(first_token); | 243 queue.Complete(first_token); |
| 252 EXPECT_DEATH(queue.Complete(first_token), ""); | 244 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 253 } | 245 } |
| 254 | 246 |
| 247 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortTwice) { |
| 248 Queue queue(1); |
| 249 const size_t first_token = queue.NewToken(); |
| 250 int first_counter = 0; |
| 251 int first_abort_counter = 0; |
| 252 queue.Enqueue(first_token, |
| 253 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 254 |
| 255 base::RunLoop().RunUntilIdle(); |
| 256 |
| 257 queue.Abort(first_token); |
| 258 EXPECT_DEATH(queue.Abort(first_token), ""); |
| 259 } |
| 260 |
| 255 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveTwice) { | 261 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveTwice) { |
| 256 Queue queue(1); | 262 Queue queue(1); |
| 257 const size_t first_token = queue.NewToken(); | 263 const size_t first_token = queue.NewToken(); |
| 258 int first_counter = 0; | 264 int first_counter = 0; |
| 259 int first_abort_counter = 0; | 265 int first_abort_counter = 0; |
| 260 AbortCallback first_abort_callback = queue.Enqueue( | 266 queue.Enqueue(first_token, |
| 261 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 267 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 262 | 268 |
| 263 base::RunLoop().RunUntilIdle(); | 269 base::RunLoop().RunUntilIdle(); |
| 264 | 270 |
| 271 queue.Complete(first_token); |
| 272 queue.Remove(first_token); |
| 273 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 274 } |
| 275 |
| 276 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortAfterRemoving) { |
| 277 Queue queue(1); |
| 278 const size_t first_token = queue.NewToken(); |
| 279 int first_counter = 0; |
| 280 int first_abort_counter = 0; |
| 281 queue.Enqueue(first_token, |
| 282 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 283 |
| 284 base::RunLoop().RunUntilIdle(); |
| 285 |
| 286 queue.Complete(first_token); |
| 287 queue.Remove(first_token); |
| 288 EXPECT_DEATH(queue.Abort(first_token), ""); |
| 289 } |
| 290 |
| 291 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterRemoving) { |
| 292 Queue queue(1); |
| 293 const size_t first_token = queue.NewToken(); |
| 294 int first_counter = 0; |
| 295 int first_abort_counter = 0; |
| 296 queue.Enqueue(first_token, |
| 297 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 298 |
| 299 base::RunLoop().RunUntilIdle(); |
| 300 |
| 265 queue.Complete(first_token); | 301 queue.Complete(first_token); |
| 266 queue.Remove(first_token); | 302 queue.Remove(first_token); |
| 267 EXPECT_DEATH(queue.Complete(first_token), ""); | 303 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 268 } | 304 } |
| 269 | 305 |
| 306 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortNonAbortable) { |
| 307 Queue queue(1); |
| 308 const size_t first_token = queue.NewToken(); |
| 309 int first_counter = 0; |
| 310 int first_abort_counter = 0; |
| 311 queue.Enqueue(first_token, base::Bind(&OnRunNonAbortable, &first_counter, |
| 312 &first_abort_counter)); |
| 313 |
| 314 base::RunLoop().RunUntilIdle(); |
| 315 |
| 316 EXPECT_DEATH(queue.Abort(first_token), ""); |
| 317 } |
| 318 |
| 270 #endif | 319 #endif |
| 271 | 320 |
| 272 TEST_F(FileSystemProviderQueueTest, Enqueue_Abort) { | 321 TEST_F(FileSystemProviderQueueTest, Enqueue_Abort) { |
| 273 Queue queue(1); | 322 Queue queue(1); |
| 274 const size_t first_token = queue.NewToken(); | 323 const size_t first_token = queue.NewToken(); |
| 275 int first_counter = 0; | 324 int first_counter = 0; |
| 276 int first_abort_counter = 0; | 325 int first_abort_counter = 0; |
| 277 const AbortCallback first_abort_callback = queue.Enqueue( | 326 queue.Enqueue(first_token, |
| 278 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 327 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 279 | 328 |
| 280 const size_t second_token = queue.NewToken(); | 329 const size_t second_token = queue.NewToken(); |
| 281 int second_counter = 0; | 330 int second_counter = 0; |
| 282 int second_abort_counter = 0; | 331 int second_abort_counter = 0; |
| 283 const AbortCallback second_abort_callback = queue.Enqueue( | 332 queue.Enqueue(second_token, |
| 284 second_token, base::Bind(&OnRun, &second_counter, &second_abort_counter)); | 333 base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
| 285 | 334 |
| 286 base::RunLoop().RunUntilIdle(); | 335 base::RunLoop().RunUntilIdle(); |
| 287 EXPECT_EQ(1, first_counter); | 336 EXPECT_EQ(1, first_counter); |
| 288 EXPECT_EQ(0, first_abort_counter); | 337 EXPECT_EQ(0, first_abort_counter); |
| 289 EXPECT_EQ(0, second_counter); | 338 EXPECT_EQ(0, second_counter); |
| 290 EXPECT_EQ(0, second_abort_counter); | 339 EXPECT_EQ(0, second_abort_counter); |
| 291 | 340 |
| 292 // Abort the first task while it's being executed. | 341 // Abort the first task while it's being executed. |
| 293 std::vector<base::File::Error> first_abort_callback_log; | 342 queue.Abort(first_token); |
| 294 first_abort_callback.Run( | 343 queue.Remove(first_token); |
| 295 base::Bind(&OnAbortCallback, &first_abort_callback_log)); | |
| 296 | 344 |
| 297 // Abort the second task, before it's started. | 345 // Abort the second task, before it's started. |
| 298 EXPECT_EQ(0, second_counter); | 346 EXPECT_EQ(0, second_counter); |
| 299 std::vector<base::File::Error> second_abort_callback_log; | 347 queue.Abort(second_token); |
| 300 second_abort_callback.Run( | 348 queue.Remove(second_token); |
| 301 base::Bind(&OnAbortCallback, &second_abort_callback_log)); | |
| 302 | 349 |
| 303 base::RunLoop().RunUntilIdle(); | 350 base::RunLoop().RunUntilIdle(); |
| 304 EXPECT_EQ(1, first_counter); | 351 EXPECT_EQ(1, first_counter); |
| 305 EXPECT_EQ(1, first_abort_counter); | 352 EXPECT_EQ(1, first_abort_counter); |
| 306 ASSERT_EQ(1u, first_abort_callback_log.size()); | |
| 307 EXPECT_EQ(base::File::FILE_ERROR_FAILED, first_abort_callback_log[0]); | |
| 308 EXPECT_EQ(0, second_counter); | 353 EXPECT_EQ(0, second_counter); |
| 309 EXPECT_EQ(0, second_abort_counter); | 354 EXPECT_EQ(0, second_abort_counter); |
| 310 ASSERT_EQ(1u, second_abort_callback_log.size()); | |
| 311 EXPECT_EQ(base::File::FILE_OK, second_abort_callback_log[0]); | |
| 312 | |
| 313 // Aborting again, should result in the FILE_ERROR_INVALID_MODIFICATION error | |
| 314 // code. | |
| 315 second_abort_callback.Run( | |
| 316 base::Bind(&OnAbortCallback, &second_abort_callback_log)); | |
| 317 | |
| 318 ASSERT_EQ(2u, second_abort_callback_log.size()); | |
| 319 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, | |
| 320 second_abort_callback_log[1]); | |
| 321 } | 355 } |
| 322 | 356 |
| 323 } // namespace file_system_provider | 357 } // namespace file_system_provider |
| 324 } // namespace chromeos | 358 } // namespace chromeos |
| OLD | NEW |