OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "chrome/browser/sync_file_system/sync_task_manager.h" | 9 #include "chrome/browser/sync_file_system/sync_task_manager.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace sync_file_system { | 12 namespace sync_file_system { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 void IncrementAndAssign(int* counter, | 16 void IncrementAndAssign(int* counter, |
17 SyncStatusCode* status_out, | 17 SyncStatusCode* status_out, |
18 SyncStatusCode status) { | 18 SyncStatusCode status) { |
19 ++(*counter); | 19 ++(*counter); |
20 *status_out = status; | 20 *status_out = status; |
21 } | 21 } |
22 | 22 |
| 23 template <typename T> |
| 24 void IncrementAndAssignWithOwnedPointer(T* object, |
| 25 int* counter, |
| 26 SyncStatusCode* status_out, |
| 27 SyncStatusCode status) { |
| 28 ++(*counter); |
| 29 *status_out = status; |
| 30 } |
| 31 |
23 class TaskManagerClient | 32 class TaskManagerClient |
24 : public SyncTaskManager::Client, | 33 : public SyncTaskManager::Client, |
25 public base::SupportsWeakPtr<TaskManagerClient> { | 34 public base::SupportsWeakPtr<TaskManagerClient> { |
26 public: | 35 public: |
27 TaskManagerClient() | 36 TaskManagerClient() |
28 : maybe_schedule_next_task_count_(0), | 37 : maybe_schedule_next_task_count_(0), |
29 task_scheduled_count_(0), | 38 task_scheduled_count_(0), |
30 idle_task_scheduled_count_(0), | 39 idle_task_scheduled_count_(0), |
31 last_operation_status_(SYNC_STATUS_OK) { | 40 last_operation_status_(SYNC_STATUS_OK) { |
32 task_manager_.reset(new SyncTaskManager(AsWeakPtr())); | 41 task_manager_.reset(new SyncTaskManager(AsWeakPtr())); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 base::Bind(&IncrementAndAssign, &callback_count, &status)); | 237 base::Bind(&IncrementAndAssign, &callback_count, &status)); |
229 } | 238 } |
230 | 239 |
231 message_loop.RunUntilIdle(); | 240 message_loop.RunUntilIdle(); |
232 EXPECT_EQ(0, callback_count); | 241 EXPECT_EQ(0, callback_count); |
233 EXPECT_EQ(SYNC_STATUS_UNKNOWN, status); | 242 EXPECT_EQ(SYNC_STATUS_UNKNOWN, status); |
234 EXPECT_TRUE(task_started); | 243 EXPECT_TRUE(task_started); |
235 EXPECT_FALSE(task_completed); | 244 EXPECT_FALSE(task_completed); |
236 } | 245 } |
237 | 246 |
| 247 TEST(SyncTaskManagerTest, ScheduleAndCancelTask) { |
| 248 base::MessageLoop message_loop; |
| 249 |
| 250 int callback_count = 0; |
| 251 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 252 |
| 253 bool task_started = false; |
| 254 bool task_completed = false; |
| 255 |
| 256 { |
| 257 SyncTaskManager task_manager((base::WeakPtr<SyncTaskManager::Client>())); |
| 258 task_manager.Initialize(SYNC_STATUS_OK); |
| 259 MultihopSyncTask* task = new MultihopSyncTask( |
| 260 &task_started, &task_completed); |
| 261 task_manager.ScheduleTask( |
| 262 base::Bind(&MultihopSyncTask::Run, base::Unretained(task)), |
| 263 base::Bind(&IncrementAndAssignWithOwnedPointer<MultihopSyncTask>, |
| 264 base::Owned(task), &callback_count, &status)); |
| 265 } |
| 266 |
| 267 message_loop.RunUntilIdle(); |
| 268 EXPECT_EQ(0, callback_count); |
| 269 EXPECT_EQ(SYNC_STATUS_UNKNOWN, status); |
| 270 EXPECT_TRUE(task_started); |
| 271 EXPECT_FALSE(task_completed); |
| 272 } |
| 273 |
238 } // namespace sync_file_system | 274 } // namespace sync_file_system |
OLD | NEW |