| 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 <vector> |
| 6 |
| 5 #include "base/eintr_wrapper.h" | 7 #include "base/eintr_wrapper.h" |
| 6 #include "base/logging.h" | 8 #include "base/logging.h" |
| 7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 8 #include "base/platform_thread.h" | 10 #include "base/platform_thread.h" |
| 9 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 10 #include "base/task.h" | 12 #include "base/task.h" |
| 11 #include "base/thread.h" | 13 #include "base/thread.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 15 |
| 14 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| (...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 | 834 |
| 833 // Poll for the MessageBox. Don't do this at home! At the speed we do it, | 835 // Poll for the MessageBox. Don't do this at home! At the speed we do it, |
| 834 // you will never realize one MessageBox was shown. | 836 // you will never realize one MessageBox was shown. |
| 835 for (; expect_window_;) { | 837 for (; expect_window_;) { |
| 836 HWND window = FindWindow(L"#32770", kMessageBoxTitle); | 838 HWND window = FindWindow(L"#32770", kMessageBoxTitle); |
| 837 if (window) { | 839 if (window) { |
| 838 // Dismiss it. | 840 // Dismiss it. |
| 839 for (;;) { | 841 for (;;) { |
| 840 HWND button = FindWindowEx(window, NULL, L"Button", NULL); | 842 HWND button = FindWindowEx(window, NULL, L"Button", NULL); |
| 841 if (button != NULL) { | 843 if (button != NULL) { |
| 842 EXPECT_TRUE(0 == SendMessage(button, WM_LBUTTONDOWN, 0, 0)); | 844 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONDOWN, 0, 0)); |
| 843 EXPECT_TRUE(0 == SendMessage(button, WM_LBUTTONUP, 0, 0)); | 845 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONUP, 0, 0)); |
| 844 break; | 846 break; |
| 845 } | 847 } |
| 846 } | 848 } |
| 847 break; | 849 break; |
| 848 } | 850 } |
| 849 } | 851 } |
| 850 } | 852 } |
| 851 | 853 |
| 852 private: | 854 private: |
| 853 MessageLoop* target_; | 855 MessageLoop* target_; |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 } | 1454 } |
| 1453 | 1455 |
| 1454 TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) { | 1456 TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) { |
| 1455 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true); | 1457 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true); |
| 1456 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); | 1458 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); |
| 1457 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); | 1459 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); |
| 1458 } | 1460 } |
| 1459 | 1461 |
| 1460 class DummyTask : public Task { | 1462 class DummyTask : public Task { |
| 1461 public: | 1463 public: |
| 1462 DummyTask(int num_tasks) : num_tasks_(num_tasks) {} | 1464 explicit DummyTask(int num_tasks) : num_tasks_(num_tasks) {} |
| 1463 | 1465 |
| 1464 virtual void Run() { | 1466 virtual void Run() { |
| 1465 if (num_tasks_ > 1) { | 1467 if (num_tasks_ > 1) { |
| 1466 MessageLoop::current()->PostTask( | 1468 MessageLoop::current()->PostTask( |
| 1467 FROM_HERE, | 1469 FROM_HERE, |
| 1468 new DummyTask(num_tasks_ - 1)); | 1470 new DummyTask(num_tasks_ - 1)); |
| 1469 } else { | 1471 } else { |
| 1470 MessageLoop::current()->Quit(); | 1472 MessageLoop::current()->Quit(); |
| 1471 } | 1473 } |
| 1472 } | 1474 } |
| 1473 | 1475 |
| 1474 private: | 1476 private: |
| 1475 const int num_tasks_; | 1477 const int num_tasks_; |
| 1476 }; | 1478 }; |
| 1477 | 1479 |
| 1478 class DummyTaskObserver : public MessageLoop::TaskObserver { | 1480 class DummyTaskObserver : public MessageLoop::TaskObserver { |
| 1479 public: | 1481 public: |
| 1480 DummyTaskObserver(int num_tasks) | 1482 explicit DummyTaskObserver(int num_tasks) |
| 1481 : num_tasks_started_(0), | 1483 : num_tasks_started_(0), |
| 1482 num_tasks_processed_(0), | 1484 num_tasks_processed_(0), |
| 1483 num_tasks_(num_tasks) {} | 1485 num_tasks_(num_tasks) {} |
| 1484 | 1486 |
| 1485 virtual ~DummyTaskObserver() {} | 1487 virtual ~DummyTaskObserver() {} |
| 1486 | 1488 |
| 1487 virtual void WillProcessTask(base::TimeTicks /* birth_time */) { | 1489 virtual void WillProcessTask(base::TimeTicks /* birth_time */) { |
| 1488 num_tasks_started_++; | 1490 num_tasks_started_++; |
| 1489 EXPECT_LE(num_tasks_started_, num_tasks_); | 1491 EXPECT_LE(num_tasks_started_, num_tasks_); |
| 1490 EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1); | 1492 EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1586 | 1588 |
| 1587 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { | 1589 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { |
| 1588 // Simulate a MessageLoop that dies before an FileDescriptorWatcher. | 1590 // Simulate a MessageLoop that dies before an FileDescriptorWatcher. |
| 1589 // This could happen when people use the Singleton pattern or atexit. | 1591 // This could happen when people use the Singleton pattern or atexit. |
| 1590 | 1592 |
| 1591 // Create a file descriptor. Doesn't need to be readable or writable, | 1593 // Create a file descriptor. Doesn't need to be readable or writable, |
| 1592 // as we don't need to actually get any notifications. | 1594 // as we don't need to actually get any notifications. |
| 1593 // pipe() is just the easiest way to do it. | 1595 // pipe() is just the easiest way to do it. |
| 1594 int pipefds[2]; | 1596 int pipefds[2]; |
| 1595 int err = pipe(pipefds); | 1597 int err = pipe(pipefds); |
| 1596 ASSERT_TRUE(err == 0); | 1598 ASSERT_EQ(0, err); |
| 1597 int fd = pipefds[1]; | 1599 int fd = pipefds[1]; |
| 1598 { | 1600 { |
| 1599 // Arrange for controller to live longer than message loop. | 1601 // Arrange for controller to live longer than message loop. |
| 1600 base::MessagePumpLibevent::FileDescriptorWatcher controller; | 1602 base::MessagePumpLibevent::FileDescriptorWatcher controller; |
| 1601 { | 1603 { |
| 1602 MessageLoopForIO message_loop; | 1604 MessageLoopForIO message_loop; |
| 1603 | 1605 |
| 1604 QuitDelegate delegate; | 1606 QuitDelegate delegate; |
| 1605 message_loop.WatchFileDescriptor(fd, | 1607 message_loop.WatchFileDescriptor(fd, |
| 1606 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); | 1608 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
| 1607 // and don't run the message loop, just destroy it. | 1609 // and don't run the message loop, just destroy it. |
| 1608 } | 1610 } |
| 1609 } | 1611 } |
| 1610 if (HANDLE_EINTR(close(pipefds[0])) < 0) | 1612 if (HANDLE_EINTR(close(pipefds[0])) < 0) |
| 1611 PLOG(ERROR) << "close"; | 1613 PLOG(ERROR) << "close"; |
| 1612 if (HANDLE_EINTR(close(pipefds[1])) < 0) | 1614 if (HANDLE_EINTR(close(pipefds[1])) < 0) |
| 1613 PLOG(ERROR) << "close"; | 1615 PLOG(ERROR) << "close"; |
| 1614 } | 1616 } |
| 1615 | 1617 |
| 1616 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { | 1618 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { |
| 1617 // Verify that it's ok to call StopWatchingFileDescriptor(). | 1619 // Verify that it's ok to call StopWatchingFileDescriptor(). |
| 1618 // (Errors only showed up in valgrind.) | 1620 // (Errors only showed up in valgrind.) |
| 1619 int pipefds[2]; | 1621 int pipefds[2]; |
| 1620 int err = pipe(pipefds); | 1622 int err = pipe(pipefds); |
| 1621 ASSERT_TRUE(err == 0); | 1623 ASSERT_EQ(0, err); |
| 1622 int fd = pipefds[1]; | 1624 int fd = pipefds[1]; |
| 1623 { | 1625 { |
| 1624 // Arrange for message loop to live longer than controller. | 1626 // Arrange for message loop to live longer than controller. |
| 1625 MessageLoopForIO message_loop; | 1627 MessageLoopForIO message_loop; |
| 1626 { | 1628 { |
| 1627 base::MessagePumpLibevent::FileDescriptorWatcher controller; | 1629 base::MessagePumpLibevent::FileDescriptorWatcher controller; |
| 1628 | 1630 |
| 1629 QuitDelegate delegate; | 1631 QuitDelegate delegate; |
| 1630 message_loop.WatchFileDescriptor(fd, | 1632 message_loop.WatchFileDescriptor(fd, |
| 1631 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); | 1633 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
| 1632 controller.StopWatchingFileDescriptor(); | 1634 controller.StopWatchingFileDescriptor(); |
| 1633 } | 1635 } |
| 1634 } | 1636 } |
| 1635 if (HANDLE_EINTR(close(pipefds[0])) < 0) | 1637 if (HANDLE_EINTR(close(pipefds[0])) < 0) |
| 1636 PLOG(ERROR) << "close"; | 1638 PLOG(ERROR) << "close"; |
| 1637 if (HANDLE_EINTR(close(pipefds[1])) < 0) | 1639 if (HANDLE_EINTR(close(pipefds[1])) < 0) |
| 1638 PLOG(ERROR) << "close"; | 1640 PLOG(ERROR) << "close"; |
| 1639 } | 1641 } |
| 1640 | 1642 |
| 1641 } // namespace | 1643 } // namespace |
| 1642 | 1644 |
| 1643 #endif // defined(OS_POSIX) | 1645 #endif // defined(OS_POSIX) |
| OLD | NEW |