| 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)); | |
| 105 base::RunLoop().RunUntilIdle(); | 106 base::RunLoop().RunUntilIdle(); |
| 106 EXPECT_EQ(1, first_counter); | 107 EXPECT_EQ(1, first_counter); |
| 107 EXPECT_EQ(0, first_abort_counter); | 108 EXPECT_EQ(0, first_abort_counter); |
| 108 EXPECT_EQ(1, second_counter); | 109 EXPECT_EQ(1, second_counter); |
| 109 EXPECT_EQ(1, second_abort_counter); | 110 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); | 111 EXPECT_EQ(1, third_counter); |
| 113 EXPECT_EQ(0, third_abort_counter); | 112 EXPECT_EQ(0, third_abort_counter); |
| 114 } | 113 } |
| 115 | 114 |
| 116 TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) { | 115 TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) { |
| 117 Queue queue(2); | 116 Queue queue(2); |
| 118 const size_t first_token = queue.NewToken(); | 117 const size_t first_token = queue.NewToken(); |
| 119 int first_counter = 0; | 118 int first_counter = 0; |
| 120 int first_abort_counter = 0; | 119 int first_abort_counter = 0; |
| 121 queue.Enqueue(first_token, | 120 queue.Enqueue(first_token, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 int first_abort_counter = 0; | 177 int first_abort_counter = 0; |
| 179 queue.Enqueue(first_token, | 178 queue.Enqueue(first_token, |
| 180 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 179 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 181 | 180 |
| 182 // Completing and removing the first task, which however hasn't started. | 181 // Completing and removing the first task, which however hasn't started. |
| 183 // That should not invoke the second task. | 182 // That should not invoke the second task. |
| 184 EXPECT_DEATH(queue.Complete(first_token), ""); | 183 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 185 EXPECT_DEATH(queue.Remove(first_token), ""); | 184 EXPECT_DEATH(queue.Remove(first_token), ""); |
| 186 } | 185 } |
| 187 | 186 |
| 188 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveNotCompleted) { | 187 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveNotCompletedNorAborted) { |
| 189 Queue queue(1); | 188 Queue queue(1); |
| 190 const size_t first_token = queue.NewToken(); | 189 const size_t first_token = queue.NewToken(); |
| 191 int first_counter = 0; | 190 int first_counter = 0; |
| 192 int first_abort_counter = 0; | 191 int first_abort_counter = 0; |
| 193 queue.Enqueue(first_token, | 192 queue.Enqueue(first_token, |
| 194 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 193 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 195 | 194 |
| 196 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 197 | 196 |
| 198 // Remove before completing. | 197 // Remove before completing. |
| 199 EXPECT_DEATH(queue.Remove(first_token), ""); | 198 EXPECT_DEATH(queue.Remove(first_token), ""); |
| 200 } | 199 } |
| 201 | 200 |
| 202 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterAborting) { | 201 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterAborting) { |
| 203 Queue queue(1); | 202 Queue queue(1); |
| 204 const size_t first_token = queue.NewToken(); | 203 const size_t first_token = queue.NewToken(); |
| 205 int first_counter = 0; | 204 int first_counter = 0; |
| 206 int first_abort_counter = 0; | 205 int first_abort_counter = 0; |
| 207 AbortCallback first_abort_callback = queue.Enqueue( | 206 queue.Enqueue(first_token, |
| 208 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 207 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 209 | 208 |
| 210 base::RunLoop().RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
| 211 | 210 |
| 212 // Run, then abort. | 211 // Run, then abort. |
| 213 std::vector<base::File::Error> first_abort_callback_log; | 212 std::vector<base::File::Error> first_abort_callback_log; |
| 214 first_abort_callback.Run( | 213 queue.Abort(first_token); |
| 215 base::Bind(&OnAbortCallback, &first_abort_callback_log)); | |
| 216 | 214 |
| 217 EXPECT_DEATH(queue.Complete(first_token), ""); | 215 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 218 } | 216 } |
| 219 | 217 |
| 220 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveAfterAborting) { | 218 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortAfterCompleting) { |
| 221 Queue queue(1); | 219 Queue queue(1); |
| 222 const size_t first_token = queue.NewToken(); | 220 const size_t first_token = queue.NewToken(); |
| 223 int first_counter = 0; | 221 int first_counter = 0; |
| 224 int first_abort_counter = 0; | 222 int first_abort_counter = 0; |
| 225 AbortCallback first_abort_callback = queue.Enqueue( | 223 queue.Enqueue(first_token, |
| 226 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 224 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 227 | 225 |
| 228 base::RunLoop().RunUntilIdle(); | 226 base::RunLoop().RunUntilIdle(); |
| 229 | 227 |
| 230 // Abort after executing. | 228 queue.Complete(first_token); |
| 231 std::vector<base::File::Error> first_abort_callback_log; | 229 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 } | 230 } |
| 240 | 231 |
| 241 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteTwice) { | 232 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteTwice) { |
| 242 Queue queue(1); | 233 Queue queue(1); |
| 243 const size_t first_token = queue.NewToken(); | 234 const size_t first_token = queue.NewToken(); |
| 244 int first_counter = 0; | 235 int first_counter = 0; |
| 245 int first_abort_counter = 0; | 236 int first_abort_counter = 0; |
| 246 AbortCallback first_abort_callback = queue.Enqueue( | 237 queue.Enqueue(first_token, |
| 247 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 238 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 248 | 239 |
| 249 base::RunLoop().RunUntilIdle(); | 240 base::RunLoop().RunUntilIdle(); |
| 250 | 241 |
| 251 queue.Complete(first_token); | 242 queue.Complete(first_token); |
| 252 EXPECT_DEATH(queue.Complete(first_token), ""); | 243 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 253 } | 244 } |
| 254 | 245 |
| 246 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortTwice) { |
| 247 Queue queue(1); |
| 248 const size_t first_token = queue.NewToken(); |
| 249 int first_counter = 0; |
| 250 int first_abort_counter = 0; |
| 251 queue.Enqueue(first_token, |
| 252 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 253 |
| 254 base::RunLoop().RunUntilIdle(); |
| 255 |
| 256 queue.Abort(first_token); |
| 257 EXPECT_DEATH(queue.Abort(first_token), ""); |
| 258 } |
| 259 |
| 255 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveTwice) { | 260 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveTwice) { |
| 256 Queue queue(1); | 261 Queue queue(1); |
| 257 const size_t first_token = queue.NewToken(); | 262 const size_t first_token = queue.NewToken(); |
| 258 int first_counter = 0; | 263 int first_counter = 0; |
| 259 int first_abort_counter = 0; | 264 int first_abort_counter = 0; |
| 260 AbortCallback first_abort_callback = queue.Enqueue( | 265 queue.Enqueue(first_token, |
| 261 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 266 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 262 | 267 |
| 263 base::RunLoop().RunUntilIdle(); | 268 base::RunLoop().RunUntilIdle(); |
| 264 | 269 |
| 270 queue.Complete(first_token); |
| 271 queue.Remove(first_token); |
| 272 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 273 } |
| 274 |
| 275 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortAfterRemoving) { |
| 276 Queue queue(1); |
| 277 const size_t first_token = queue.NewToken(); |
| 278 int first_counter = 0; |
| 279 int first_abort_counter = 0; |
| 280 queue.Enqueue(first_token, |
| 281 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 282 |
| 283 base::RunLoop().RunUntilIdle(); |
| 284 |
| 285 queue.Complete(first_token); |
| 286 queue.Remove(first_token); |
| 287 EXPECT_DEATH(queue.Abort(first_token), ""); |
| 288 } |
| 289 |
| 290 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterRemoving) { |
| 291 Queue queue(1); |
| 292 const size_t first_token = queue.NewToken(); |
| 293 int first_counter = 0; |
| 294 int first_abort_counter = 0; |
| 295 queue.Enqueue(first_token, |
| 296 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 297 |
| 298 base::RunLoop().RunUntilIdle(); |
| 299 |
| 265 queue.Complete(first_token); | 300 queue.Complete(first_token); |
| 266 queue.Remove(first_token); | 301 queue.Remove(first_token); |
| 267 EXPECT_DEATH(queue.Complete(first_token), ""); | 302 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 268 } | 303 } |
| 269 | 304 |
| 305 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortNonAbortable) { |
| 306 Queue queue(1); |
| 307 const size_t first_token = queue.NewToken(); |
| 308 int first_counter = 0; |
| 309 int first_abort_counter = 0; |
| 310 queue.Enqueue(first_token, base::Bind(&OnRunNonAbortable, &first_counter, |
| 311 &first_abort_counter)); |
| 312 |
| 313 base::RunLoop().RunUntilIdle(); |
| 314 |
| 315 EXPECT_DEATH(queue.Abort(first_token), ""); |
| 316 } |
| 317 |
| 270 #endif | 318 #endif |
| 271 | 319 |
| 272 TEST_F(FileSystemProviderQueueTest, Enqueue_Abort) { | 320 TEST_F(FileSystemProviderQueueTest, Enqueue_Abort) { |
| 273 Queue queue(1); | 321 Queue queue(1); |
| 274 const size_t first_token = queue.NewToken(); | 322 const size_t first_token = queue.NewToken(); |
| 275 int first_counter = 0; | 323 int first_counter = 0; |
| 276 int first_abort_counter = 0; | 324 int first_abort_counter = 0; |
| 277 const AbortCallback first_abort_callback = queue.Enqueue( | 325 queue.Enqueue(first_token, |
| 278 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 326 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 279 | 327 |
| 280 const size_t second_token = queue.NewToken(); | 328 const size_t second_token = queue.NewToken(); |
| 281 int second_counter = 0; | 329 int second_counter = 0; |
| 282 int second_abort_counter = 0; | 330 int second_abort_counter = 0; |
| 283 const AbortCallback second_abort_callback = queue.Enqueue( | 331 queue.Enqueue(second_token, |
| 284 second_token, base::Bind(&OnRun, &second_counter, &second_abort_counter)); | 332 base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
| 285 | 333 |
| 286 base::RunLoop().RunUntilIdle(); | 334 base::RunLoop().RunUntilIdle(); |
| 287 EXPECT_EQ(1, first_counter); | 335 EXPECT_EQ(1, first_counter); |
| 288 EXPECT_EQ(0, first_abort_counter); | 336 EXPECT_EQ(0, first_abort_counter); |
| 289 EXPECT_EQ(0, second_counter); | 337 EXPECT_EQ(0, second_counter); |
| 290 EXPECT_EQ(0, second_abort_counter); | 338 EXPECT_EQ(0, second_abort_counter); |
| 291 | 339 |
| 292 // Abort the first task while it's being executed. | 340 // Abort the first task while it's being executed. |
| 293 std::vector<base::File::Error> first_abort_callback_log; | 341 queue.Abort(first_token); |
| 294 first_abort_callback.Run( | |
| 295 base::Bind(&OnAbortCallback, &first_abort_callback_log)); | |
| 296 | 342 |
| 297 // Abort the second task, before it's started. | 343 // Abort the second task, before it's started. |
| 298 EXPECT_EQ(0, second_counter); | 344 EXPECT_EQ(0, second_counter); |
| 299 std::vector<base::File::Error> second_abort_callback_log; | 345 queue.Abort(second_token); |
| 300 second_abort_callback.Run( | |
| 301 base::Bind(&OnAbortCallback, &second_abort_callback_log)); | |
| 302 | 346 |
| 303 base::RunLoop().RunUntilIdle(); | 347 base::RunLoop().RunUntilIdle(); |
| 304 EXPECT_EQ(1, first_counter); | 348 EXPECT_EQ(1, first_counter); |
| 305 EXPECT_EQ(1, first_abort_counter); | 349 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); | 350 EXPECT_EQ(0, second_counter); |
| 309 EXPECT_EQ(0, second_abort_counter); | 351 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 } | 352 } |
| 322 | 353 |
| 323 } // namespace file_system_provider | 354 } // namespace file_system_provider |
| 324 } // namespace chromeos | 355 } // namespace chromeos |
| OLD | NEW |