OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 MessageLoop::current()->set_exception_restoration(false); | 139 MessageLoop::current()->set_exception_restoration(false); |
140 | 140 |
141 EXPECT_EQ(foo->test_count(), 105); | 141 EXPECT_EQ(foo->test_count(), 105); |
142 EXPECT_EQ(foo->result(), "abacad"); | 142 EXPECT_EQ(foo->result(), "abacad"); |
143 } | 143 } |
144 | 144 |
145 // This function runs slowly to simulate a large amount of work being done. | 145 // This function runs slowly to simulate a large amount of work being done. |
146 static void SlowFunc(TimeDelta pause, int* quit_counter) { | 146 static void SlowFunc(TimeDelta pause, int* quit_counter) { |
147 PlatformThread::Sleep(pause); | 147 PlatformThread::Sleep(pause); |
148 if (--(*quit_counter) == 0) | 148 if (--(*quit_counter) == 0) |
149 MessageLoop::current()->Quit(); | 149 MessageLoop::current()->QuitWhenIdle(); |
150 } | 150 } |
151 | 151 |
152 // This function records the time when Run was called in a Time object, which is | 152 // This function records the time when Run was called in a Time object, which is |
153 // useful for building a variety of MessageLoop tests. | 153 // useful for building a variety of MessageLoop tests. |
154 static void RecordRunTimeFunc(Time* run_time, int* quit_counter) { | 154 static void RecordRunTimeFunc(Time* run_time, int* quit_counter) { |
155 *run_time = Time::Now(); | 155 *run_time = Time::Now(); |
156 | 156 |
157 // Cause our Run function to take some time to execute. As a result we can | 157 // Cause our Run function to take some time to execute. As a result we can |
158 // count on subsequent RecordRunTimeFunc()s running at a future time, | 158 // count on subsequent RecordRunTimeFunc()s running at a future time, |
159 // without worry about the resolution of our system clock being an issue. | 159 // without worry about the resolution of our system clock being an issue. |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 334 |
335 #if defined(OS_WIN) | 335 #if defined(OS_WIN) |
336 | 336 |
337 void SubPumpFunc() { | 337 void SubPumpFunc() { |
338 MessageLoop::current()->SetNestableTasksAllowed(true); | 338 MessageLoop::current()->SetNestableTasksAllowed(true); |
339 MSG msg; | 339 MSG msg; |
340 while (GetMessage(&msg, NULL, 0, 0)) { | 340 while (GetMessage(&msg, NULL, 0, 0)) { |
341 TranslateMessage(&msg); | 341 TranslateMessage(&msg); |
342 DispatchMessage(&msg); | 342 DispatchMessage(&msg); |
343 } | 343 } |
344 MessageLoop::current()->Quit(); | 344 MessageLoop::current()->QuitWhenIdle(); |
345 } | 345 } |
346 | 346 |
347 void RunTest_PostDelayedTask_SharedTimer_SubPump() { | 347 void RunTest_PostDelayedTask_SharedTimer_SubPump() { |
348 MessageLoop loop(MessageLoop::TYPE_UI); | 348 MessageLoop loop(MessageLoop::TYPE_UI); |
349 | 349 |
350 // Test that the interval of the timer, used to run the next delayed task, is | 350 // Test that the interval of the timer, used to run the next delayed task, is |
351 // set to a value corresponding to when the next delayed task should run. | 351 // set to a value corresponding to when the next delayed task should run. |
352 | 352 |
353 // By setting num_tasks to 1, we ensure that the first task to run causes the | 353 // By setting num_tasks to 1, we ensure that the first task to run causes the |
354 // run loop to exit. | 354 // run loop to exit. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 | 452 |
453 void NestingFunc(int* depth) { | 453 void NestingFunc(int* depth) { |
454 if (*depth > 0) { | 454 if (*depth > 0) { |
455 *depth -= 1; | 455 *depth -= 1; |
456 MessageLoop::current()->PostTask(FROM_HERE, | 456 MessageLoop::current()->PostTask(FROM_HERE, |
457 base::Bind(&NestingFunc, depth)); | 457 base::Bind(&NestingFunc, depth)); |
458 | 458 |
459 MessageLoop::current()->SetNestableTasksAllowed(true); | 459 MessageLoop::current()->SetNestableTasksAllowed(true); |
460 MessageLoop::current()->Run(); | 460 MessageLoop::current()->Run(); |
461 } | 461 } |
462 MessageLoop::current()->Quit(); | 462 MessageLoop::current()->QuitWhenIdle(); |
463 } | 463 } |
464 | 464 |
465 #if defined(OS_WIN) | 465 #if defined(OS_WIN) |
466 | 466 |
467 LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) { | 467 LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) { |
468 ADD_FAILURE() << "bad exception handler"; | 468 ADD_FAILURE() << "bad exception handler"; |
469 ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode); | 469 ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode); |
470 return EXCEPTION_EXECUTE_HANDLER; | 470 return EXCEPTION_EXECUTE_HANDLER; |
471 } | 471 } |
472 | 472 |
(...skipping 22 matching lines...) Expand all Loading... |
495 } | 495 } |
496 | 496 |
497 #elif defined(_M_X64) | 497 #elif defined(_M_X64) |
498 | 498 |
499 bad_array_[0] = 66; | 499 bad_array_[0] = 66; |
500 | 500 |
501 #else | 501 #else |
502 #error "needs architecture support" | 502 #error "needs architecture support" |
503 #endif | 503 #endif |
504 | 504 |
505 MessageLoop::current()->Quit(); | 505 MessageLoop::current()->QuitWhenIdle(); |
506 } | 506 } |
507 // Points the bad array to a valid memory location. | 507 // Points the bad array to a valid memory location. |
508 static void FixError() { | 508 static void FixError() { |
509 bad_array_ = &valid_store_; | 509 bad_array_ = &valid_store_; |
510 } | 510 } |
511 | 511 |
512 private: | 512 private: |
513 bool trash_SEH_handler_; | 513 bool trash_SEH_handler_; |
514 static volatile char* bad_array_; | 514 static volatile char* bad_array_; |
515 static char valid_store_; | 515 static char valid_store_; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 } | 719 } |
720 | 720 |
721 void RecursiveSlowFunc(TaskList* order, int cookie, int depth, | 721 void RecursiveSlowFunc(TaskList* order, int cookie, int depth, |
722 bool is_reentrant) { | 722 bool is_reentrant) { |
723 RecursiveFunc(order, cookie, depth, is_reentrant); | 723 RecursiveFunc(order, cookie, depth, is_reentrant); |
724 PlatformThread::Sleep(TimeDelta::FromMilliseconds(10)); | 724 PlatformThread::Sleep(TimeDelta::FromMilliseconds(10)); |
725 } | 725 } |
726 | 726 |
727 void QuitFunc(TaskList* order, int cookie) { | 727 void QuitFunc(TaskList* order, int cookie) { |
728 order->RecordStart(QUITMESSAGELOOP, cookie); | 728 order->RecordStart(QUITMESSAGELOOP, cookie); |
729 MessageLoop::current()->Quit(); | 729 MessageLoop::current()->QuitWhenIdle(); |
730 order->RecordEnd(QUITMESSAGELOOP, cookie); | 730 order->RecordEnd(QUITMESSAGELOOP, cookie); |
731 } | 731 } |
732 | 732 |
733 void SleepFunc(TaskList* order, int cookie, TimeDelta delay) { | 733 void SleepFunc(TaskList* order, int cookie, TimeDelta delay) { |
734 order->RecordStart(SLEEP, cookie); | 734 order->RecordStart(SLEEP, cookie); |
735 PlatformThread::Sleep(delay); | 735 PlatformThread::Sleep(delay); |
736 order->RecordEnd(SLEEP, cookie); | 736 order->RecordEnd(SLEEP, cookie); |
737 } | 737 } |
738 | 738 |
739 #if defined(OS_WIN) | 739 #if defined(OS_WIN) |
(...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1773 RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_UI); | 1773 RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_UI); |
1774 RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_IO); | 1774 RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_IO); |
1775 } | 1775 } |
1776 | 1776 |
1777 void PostNTasksThenQuit(int posts_remaining) { | 1777 void PostNTasksThenQuit(int posts_remaining) { |
1778 if (posts_remaining > 1) { | 1778 if (posts_remaining > 1) { |
1779 MessageLoop::current()->PostTask( | 1779 MessageLoop::current()->PostTask( |
1780 FROM_HERE, | 1780 FROM_HERE, |
1781 base::Bind(&PostNTasksThenQuit, posts_remaining - 1)); | 1781 base::Bind(&PostNTasksThenQuit, posts_remaining - 1)); |
1782 } else { | 1782 } else { |
1783 MessageLoop::current()->Quit(); | 1783 MessageLoop::current()->QuitWhenIdle(); |
1784 } | 1784 } |
1785 } | 1785 } |
1786 | 1786 |
1787 class DummyTaskObserver : public MessageLoop::TaskObserver { | 1787 class DummyTaskObserver : public MessageLoop::TaskObserver { |
1788 public: | 1788 public: |
1789 explicit DummyTaskObserver(int num_tasks) | 1789 explicit DummyTaskObserver(int num_tasks) |
1790 : num_tasks_started_(0), | 1790 : num_tasks_started_(0), |
1791 num_tasks_processed_(0), | 1791 num_tasks_processed_(0), |
1792 num_tasks_(num_tasks) {} | 1792 num_tasks_(num_tasks) {} |
1793 | 1793 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1885 | 1885 |
1886 #endif // defined(OS_WIN) | 1886 #endif // defined(OS_WIN) |
1887 | 1887 |
1888 #if defined(OS_POSIX) && !defined(OS_NACL) | 1888 #if defined(OS_POSIX) && !defined(OS_NACL) |
1889 | 1889 |
1890 namespace { | 1890 namespace { |
1891 | 1891 |
1892 class QuitDelegate : public MessageLoopForIO::Watcher { | 1892 class QuitDelegate : public MessageLoopForIO::Watcher { |
1893 public: | 1893 public: |
1894 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { | 1894 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { |
1895 MessageLoop::current()->Quit(); | 1895 MessageLoop::current()->QuitWhenIdle(); |
1896 } | 1896 } |
1897 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { | 1897 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { |
1898 MessageLoop::current()->Quit(); | 1898 MessageLoop::current()->QuitWhenIdle(); |
1899 } | 1899 } |
1900 }; | 1900 }; |
1901 | 1901 |
1902 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { | 1902 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { |
1903 // Simulate a MessageLoop that dies before an FileDescriptorWatcher. | 1903 // Simulate a MessageLoop that dies before an FileDescriptorWatcher. |
1904 // This could happen when people use the Singleton pattern or atexit. | 1904 // This could happen when people use the Singleton pattern or atexit. |
1905 | 1905 |
1906 // Create a file descriptor. Doesn't need to be readable or writable, | 1906 // Create a file descriptor. Doesn't need to be readable or writable, |
1907 // as we don't need to actually get any notifications. | 1907 // as we don't need to actually get any notifications. |
1908 // pipe() is just the easiest way to do it. | 1908 // pipe() is just the easiest way to do it. |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2052 EXPECT_EQ(foo->test_count(), 1); | 2052 EXPECT_EQ(foo->test_count(), 1); |
2053 EXPECT_EQ(foo->result(), "a"); | 2053 EXPECT_EQ(foo->result(), "a"); |
2054 } | 2054 } |
2055 | 2055 |
2056 TEST(MessageLoopTest, IsType) { | 2056 TEST(MessageLoopTest, IsType) { |
2057 MessageLoop loop(MessageLoop::TYPE_UI); | 2057 MessageLoop loop(MessageLoop::TYPE_UI); |
2058 EXPECT_TRUE(loop.IsType(MessageLoop::TYPE_UI)); | 2058 EXPECT_TRUE(loop.IsType(MessageLoop::TYPE_UI)); |
2059 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_IO)); | 2059 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_IO)); |
2060 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_DEFAULT)); | 2060 EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_DEFAULT)); |
2061 } | 2061 } |
OLD | NEW |