OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/file_system_provider/queue.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/files/file.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace chromeos { |
| 15 namespace file_system_provider { |
| 16 namespace { |
| 17 |
| 18 void OnAbort(int* abort_counter, |
| 19 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 20 (*abort_counter)++; |
| 21 callback.Run(base::File::FILE_ERROR_FAILED); |
| 22 } |
| 23 |
| 24 AbortCallback OnRun(int* run_counter, int* abort_counter) { |
| 25 (*run_counter)++; |
| 26 return base::Bind(&OnAbort, abort_counter); |
| 27 } |
| 28 |
| 29 void OnAbortCallback(std::vector<base::File::Error>* log, |
| 30 base::File::Error result) { |
| 31 log->push_back(result); |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 class FileSystemProviderQueueTest : public testing::Test { |
| 37 protected: |
| 38 FileSystemProviderQueueTest() {} |
| 39 virtual ~FileSystemProviderQueueTest() {} |
| 40 |
| 41 content::TestBrowserThreadBundle thread_bundle_; |
| 42 }; |
| 43 |
| 44 TEST_F(FileSystemProviderQueueTest, NewToken) { |
| 45 Queue queue(1); |
| 46 EXPECT_EQ(1u, queue.NewToken()); |
| 47 EXPECT_EQ(2u, queue.NewToken()); |
| 48 EXPECT_EQ(3u, queue.NewToken()); |
| 49 } |
| 50 |
| 51 TEST_F(FileSystemProviderQueueTest, Enqueue_OneAtOnce) { |
| 52 Queue queue(1); |
| 53 const size_t first_token = queue.NewToken(); |
| 54 int first_counter = 0; |
| 55 int first_abort_counter = 0; |
| 56 queue.Enqueue(first_token, |
| 57 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 58 |
| 59 const size_t second_token = queue.NewToken(); |
| 60 int second_counter = 0; |
| 61 int second_abort_counter = 0; |
| 62 const AbortCallback abort_callback = queue.Enqueue( |
| 63 second_token, base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
| 64 |
| 65 base::RunLoop().RunUntilIdle(); |
| 66 EXPECT_EQ(1, first_counter); |
| 67 EXPECT_EQ(0, first_abort_counter); |
| 68 EXPECT_EQ(0, second_counter); |
| 69 EXPECT_EQ(0, second_abort_counter); |
| 70 |
| 71 // Complete the first task, which should not run the second one, yet. |
| 72 queue.Complete(first_token); |
| 73 base::RunLoop().RunUntilIdle(); |
| 74 EXPECT_EQ(1, first_counter); |
| 75 EXPECT_EQ(0, first_abort_counter); |
| 76 EXPECT_EQ(0, second_counter); |
| 77 EXPECT_EQ(0, second_abort_counter); |
| 78 |
| 79 // Removing the first task from the queue should run the second task. |
| 80 queue.Remove(first_token); |
| 81 base::RunLoop().RunUntilIdle(); |
| 82 EXPECT_EQ(1, first_counter); |
| 83 EXPECT_EQ(0, first_abort_counter); |
| 84 EXPECT_EQ(1, second_counter); |
| 85 EXPECT_EQ(0, second_abort_counter); |
| 86 |
| 87 const size_t third_token = queue.NewToken(); |
| 88 int third_counter = 0; |
| 89 int third_abort_counter = 0; |
| 90 queue.Enqueue(third_token, |
| 91 base::Bind(&OnRun, &third_counter, &third_abort_counter)); |
| 92 |
| 93 // The second task is still running, so the third one is blocked. |
| 94 base::RunLoop().RunUntilIdle(); |
| 95 EXPECT_EQ(1, first_counter); |
| 96 EXPECT_EQ(0, first_abort_counter); |
| 97 EXPECT_EQ(1, second_counter); |
| 98 EXPECT_EQ(0, second_abort_counter); |
| 99 EXPECT_EQ(0, third_counter); |
| 100 EXPECT_EQ(0, third_abort_counter); |
| 101 |
| 102 // After aborting the second task, the third should run. |
| 103 std::vector<base::File::Error> abort_callback_log; |
| 104 abort_callback.Run(base::Bind(&OnAbortCallback, &abort_callback_log)); |
| 105 base::RunLoop().RunUntilIdle(); |
| 106 EXPECT_EQ(1, first_counter); |
| 107 EXPECT_EQ(0, first_abort_counter); |
| 108 EXPECT_EQ(1, second_counter); |
| 109 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); |
| 113 EXPECT_EQ(0, third_abort_counter); |
| 114 } |
| 115 |
| 116 TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) { |
| 117 Queue queue(2); |
| 118 const size_t first_token = queue.NewToken(); |
| 119 int first_counter = 0; |
| 120 int first_abort_counter = 0; |
| 121 queue.Enqueue(first_token, |
| 122 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 123 |
| 124 const size_t second_token = queue.NewToken(); |
| 125 int second_counter = 0; |
| 126 int second_abort_counter = 0; |
| 127 queue.Enqueue(second_token, |
| 128 base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
| 129 |
| 130 const size_t third_token = queue.NewToken(); |
| 131 int third_counter = 0; |
| 132 int third_abort_counter = 0; |
| 133 queue.Enqueue(third_token, |
| 134 base::Bind(&OnRun, &third_counter, &third_abort_counter)); |
| 135 |
| 136 base::RunLoop().RunUntilIdle(); |
| 137 EXPECT_EQ(1, first_counter); |
| 138 EXPECT_EQ(0, first_abort_counter); |
| 139 EXPECT_EQ(1, second_counter); |
| 140 EXPECT_EQ(0, second_abort_counter); |
| 141 EXPECT_EQ(0, third_counter); |
| 142 EXPECT_EQ(0, third_abort_counter); |
| 143 |
| 144 // Completing and removing the second task, should start the last one. |
| 145 queue.Complete(second_token); |
| 146 queue.Remove(second_token); |
| 147 base::RunLoop().RunUntilIdle(); |
| 148 EXPECT_EQ(1, first_counter); |
| 149 EXPECT_EQ(0, first_abort_counter); |
| 150 EXPECT_EQ(1, second_counter); |
| 151 EXPECT_EQ(0, second_abort_counter); |
| 152 EXPECT_EQ(1, third_counter); |
| 153 EXPECT_EQ(0, third_abort_counter); |
| 154 } |
| 155 |
| 156 #if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST) |
| 157 |
| 158 TEST_F(FileSystemProviderQueueTest, InvalidUsage_DuplicatedTokens) { |
| 159 Queue queue(1); |
| 160 const size_t first_token = queue.NewToken(); |
| 161 int first_counter = 0; |
| 162 int first_abort_counter = 0; |
| 163 queue.Enqueue(first_token, |
| 164 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 165 |
| 166 // Use the first token on purpose. |
| 167 int second_counter = 0; |
| 168 int second_abort_counter = 0; |
| 169 EXPECT_DEATH(queue.Enqueue(first_token, base::Bind(&OnRun, &second_counter, |
| 170 &second_abort_counter)), |
| 171 ""); |
| 172 } |
| 173 |
| 174 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteNotStarted) { |
| 175 Queue queue(1); |
| 176 const size_t first_token = queue.NewToken(); |
| 177 int first_counter = 0; |
| 178 int first_abort_counter = 0; |
| 179 queue.Enqueue(first_token, |
| 180 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 181 |
| 182 // Completing and removing the first task, which however hasn't started. |
| 183 // That should not invoke the second task. |
| 184 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 185 EXPECT_DEATH(queue.Remove(first_token), ""); |
| 186 } |
| 187 |
| 188 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveNotCompleted) { |
| 189 Queue queue(1); |
| 190 const size_t first_token = queue.NewToken(); |
| 191 int first_counter = 0; |
| 192 int first_abort_counter = 0; |
| 193 queue.Enqueue(first_token, |
| 194 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 195 |
| 196 base::RunLoop().RunUntilIdle(); |
| 197 |
| 198 // Remove before completing. |
| 199 EXPECT_DEATH(queue.Remove(first_token), ""); |
| 200 } |
| 201 |
| 202 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterAborting) { |
| 203 Queue queue(1); |
| 204 const size_t first_token = queue.NewToken(); |
| 205 int first_counter = 0; |
| 206 int first_abort_counter = 0; |
| 207 AbortCallback first_abort_callback = queue.Enqueue( |
| 208 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 209 |
| 210 base::RunLoop().RunUntilIdle(); |
| 211 |
| 212 // Run, then abort. |
| 213 std::vector<base::File::Error> first_abort_callback_log; |
| 214 first_abort_callback.Run( |
| 215 base::Bind(&OnAbortCallback, &first_abort_callback_log)); |
| 216 |
| 217 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 218 } |
| 219 |
| 220 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveAfterAborting) { |
| 221 Queue queue(1); |
| 222 const size_t first_token = queue.NewToken(); |
| 223 int first_counter = 0; |
| 224 int first_abort_counter = 0; |
| 225 AbortCallback first_abort_callback = queue.Enqueue( |
| 226 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 227 |
| 228 base::RunLoop().RunUntilIdle(); |
| 229 |
| 230 // Abort after executing. |
| 231 std::vector<base::File::Error> first_abort_callback_log; |
| 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 } |
| 240 |
| 241 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteTwice) { |
| 242 Queue queue(1); |
| 243 const size_t first_token = queue.NewToken(); |
| 244 int first_counter = 0; |
| 245 int first_abort_counter = 0; |
| 246 AbortCallback first_abort_callback = queue.Enqueue( |
| 247 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 248 |
| 249 base::RunLoop().RunUntilIdle(); |
| 250 |
| 251 queue.Complete(first_token); |
| 252 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 253 } |
| 254 |
| 255 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveTwice) { |
| 256 Queue queue(1); |
| 257 const size_t first_token = queue.NewToken(); |
| 258 int first_counter = 0; |
| 259 int first_abort_counter = 0; |
| 260 AbortCallback first_abort_callback = queue.Enqueue( |
| 261 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 262 |
| 263 base::RunLoop().RunUntilIdle(); |
| 264 |
| 265 queue.Complete(first_token); |
| 266 queue.Remove(first_token); |
| 267 EXPECT_DEATH(queue.Complete(first_token), ""); |
| 268 } |
| 269 |
| 270 #endif |
| 271 |
| 272 TEST_F(FileSystemProviderQueueTest, Enqueue_Abort) { |
| 273 Queue queue(1); |
| 274 const size_t first_token = queue.NewToken(); |
| 275 int first_counter = 0; |
| 276 int first_abort_counter = 0; |
| 277 const AbortCallback first_abort_callback = queue.Enqueue( |
| 278 first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
| 279 |
| 280 const size_t second_token = queue.NewToken(); |
| 281 int second_counter = 0; |
| 282 int second_abort_counter = 0; |
| 283 const AbortCallback second_abort_callback = queue.Enqueue( |
| 284 second_token, base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
| 285 |
| 286 base::RunLoop().RunUntilIdle(); |
| 287 EXPECT_EQ(1, first_counter); |
| 288 EXPECT_EQ(0, first_abort_counter); |
| 289 EXPECT_EQ(0, second_counter); |
| 290 EXPECT_EQ(0, second_abort_counter); |
| 291 |
| 292 // Abort the first task while it's being executed. |
| 293 std::vector<base::File::Error> first_abort_callback_log; |
| 294 first_abort_callback.Run( |
| 295 base::Bind(&OnAbortCallback, &first_abort_callback_log)); |
| 296 |
| 297 // Abort the second task, before it's started. |
| 298 EXPECT_EQ(0, second_counter); |
| 299 std::vector<base::File::Error> second_abort_callback_log; |
| 300 second_abort_callback.Run( |
| 301 base::Bind(&OnAbortCallback, &second_abort_callback_log)); |
| 302 |
| 303 base::RunLoop().RunUntilIdle(); |
| 304 EXPECT_EQ(1, first_counter); |
| 305 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); |
| 309 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 } |
| 322 |
| 323 } // namespace file_system_provider |
| 324 } // namespace chromeos |
OLD | NEW |