OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/logging.h" | 5 #include "base/logging.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/platform_thread.h" | 7 #include "base/platform_thread.h" |
8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
9 #include "base/thread.h" | 9 #include "base/thread.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 | 283 |
284 loop.PostDelayedTask( | 284 loop.PostDelayedTask( |
285 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1); | 285 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1); |
286 | 286 |
287 loop.Run(); | 287 loop.Run(); |
288 EXPECT_EQ(0, num_tasks); | 288 EXPECT_EQ(0, num_tasks); |
289 | 289 |
290 EXPECT_TRUE(run_time2 > run_time1); | 290 EXPECT_TRUE(run_time2 > run_time1); |
291 } | 291 } |
292 | 292 |
| 293 class RecordDeletionTask : public Task { |
| 294 public: |
| 295 RecordDeletionTask(Task* post_on_delete, bool* was_deleted) |
| 296 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { |
| 297 } |
| 298 ~RecordDeletionTask() { |
| 299 *was_deleted_ = true; |
| 300 if (post_on_delete_) |
| 301 MessageLoop::current()->PostTask(FROM_HERE, post_on_delete_); |
| 302 } |
| 303 virtual void Run() {} |
| 304 private: |
| 305 Task* post_on_delete_; |
| 306 bool* was_deleted_; |
| 307 }; |
| 308 |
| 309 void RunTest_EnsureTaskDeletion(MessageLoop::Type message_loop_type) { |
| 310 bool a_was_deleted = false; |
| 311 bool b_was_deleted = false; |
| 312 { |
| 313 MessageLoop loop(message_loop_type); |
| 314 loop.PostTask( |
| 315 FROM_HERE, new RecordDeletionTask(NULL, &a_was_deleted)); |
| 316 loop.PostDelayedTask( |
| 317 FROM_HERE, new RecordDeletionTask(NULL, &b_was_deleted), 1000); |
| 318 } |
| 319 EXPECT_TRUE(a_was_deleted); |
| 320 EXPECT_TRUE(b_was_deleted); |
| 321 } |
| 322 |
| 323 void RunTest_EnsureTaskDeletion_Chain(MessageLoop::Type message_loop_type) { |
| 324 bool a_was_deleted = false; |
| 325 bool b_was_deleted = false; |
| 326 bool c_was_deleted = false; |
| 327 { |
| 328 MessageLoop loop(message_loop_type); |
| 329 RecordDeletionTask* a = new RecordDeletionTask(NULL, &a_was_deleted); |
| 330 RecordDeletionTask* b = new RecordDeletionTask(a, &b_was_deleted); |
| 331 RecordDeletionTask* c = new RecordDeletionTask(b, &c_was_deleted); |
| 332 loop.PostTask(FROM_HERE, c); |
| 333 } |
| 334 EXPECT_TRUE(a_was_deleted); |
| 335 EXPECT_TRUE(b_was_deleted); |
| 336 EXPECT_TRUE(c_was_deleted); |
| 337 } |
| 338 |
293 class NestingTest : public Task { | 339 class NestingTest : public Task { |
294 public: | 340 public: |
295 explicit NestingTest(int* depth) : depth_(depth) { | 341 explicit NestingTest(int* depth) : depth_(depth) { |
296 } | 342 } |
297 void Run() { | 343 void Run() { |
298 if (*depth_ > 0) { | 344 if (*depth_ > 0) { |
299 *depth_ -= 1; | 345 *depth_ -= 1; |
300 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(depth_)); | 346 MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(depth_)); |
301 | 347 |
302 MessageLoop::current()->SetNestableTasksAllowed(true); | 348 MessageLoop::current()->SetNestableTasksAllowed(true); |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); | 1088 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); |
1043 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); | 1089 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); |
1044 } | 1090 } |
1045 | 1091 |
1046 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { | 1092 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { |
1047 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); | 1093 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); |
1048 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); | 1094 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); |
1049 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); | 1095 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); |
1050 } | 1096 } |
1051 | 1097 |
| 1098 TEST(MessageLoopTest, EnsureTaskDeletion) { |
| 1099 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT); |
| 1100 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI); |
| 1101 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO); |
| 1102 } |
| 1103 |
| 1104 TEST(MessageLoopTest, EnsureTaskDeletion_Chain) { |
| 1105 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT); |
| 1106 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_UI); |
| 1107 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_IO); |
| 1108 } |
| 1109 |
1052 #if defined(OS_WIN) | 1110 #if defined(OS_WIN) |
1053 TEST(MessageLoopTest, Crasher) { | 1111 TEST(MessageLoopTest, Crasher) { |
1054 RunTest_Crasher(MessageLoop::TYPE_DEFAULT); | 1112 RunTest_Crasher(MessageLoop::TYPE_DEFAULT); |
1055 RunTest_Crasher(MessageLoop::TYPE_UI); | 1113 RunTest_Crasher(MessageLoop::TYPE_UI); |
1056 RunTest_Crasher(MessageLoop::TYPE_IO); | 1114 RunTest_Crasher(MessageLoop::TYPE_IO); |
1057 } | 1115 } |
1058 | 1116 |
1059 TEST(MessageLoopTest, CrasherNasty) { | 1117 TEST(MessageLoopTest, CrasherNasty) { |
1060 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT); | 1118 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT); |
1061 RunTest_CrasherNasty(MessageLoop::TYPE_UI); | 1119 RunTest_CrasherNasty(MessageLoop::TYPE_UI); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1110 TEST(MessageLoopTest, AutoresetEvents) { | 1168 TEST(MessageLoopTest, AutoresetEvents) { |
1111 // This test requires an IO loop | 1169 // This test requires an IO loop |
1112 RunTest_AutoresetEvents(MessageLoop::TYPE_IO); | 1170 RunTest_AutoresetEvents(MessageLoop::TYPE_IO); |
1113 } | 1171 } |
1114 | 1172 |
1115 TEST(MessageLoopTest, Dispatcher) { | 1173 TEST(MessageLoopTest, Dispatcher) { |
1116 // This test requires a UI loop | 1174 // This test requires a UI loop |
1117 RunTest_Dispatcher(MessageLoop::TYPE_UI); | 1175 RunTest_Dispatcher(MessageLoop::TYPE_UI); |
1118 } | 1176 } |
1119 #endif // defined(OS_WIN) | 1177 #endif // defined(OS_WIN) |
OLD | NEW |