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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 1, false)); | 463 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 1, false)); |
464 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, true)); | 464 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, true)); |
465 EXPECT_EQ(order.Get(17), TaskItem(RECURSIVE, 3, false)); | 465 EXPECT_EQ(order.Get(17), TaskItem(RECURSIVE, 3, false)); |
466 } | 466 } |
467 | 467 |
468 #endif // defined(OS_WIN) | 468 #endif // defined(OS_WIN) |
469 | 469 |
470 void PostNTasksThenQuit(int posts_remaining) { | 470 void PostNTasksThenQuit(int posts_remaining) { |
471 if (posts_remaining > 1) { | 471 if (posts_remaining > 1) { |
472 ThreadTaskRunnerHandle::Get()->PostTask( | 472 ThreadTaskRunnerHandle::Get()->PostTask( |
473 FROM_HERE, Bind(&PostNTasksThenQuit, posts_remaining - 1)); | 473 FROM_HERE, BindOnce(&PostNTasksThenQuit, posts_remaining - 1)); |
474 } else { | 474 } else { |
475 MessageLoop::current()->QuitWhenIdle(); | 475 MessageLoop::current()->QuitWhenIdle(); |
476 } | 476 } |
477 } | 477 } |
478 | 478 |
479 #if defined(OS_WIN) | 479 #if defined(OS_WIN) |
480 | 480 |
481 class TestIOHandler : public MessageLoopForIO::IOHandler { | 481 class TestIOHandler : public MessageLoopForIO::IOHandler { |
482 public: | 482 public: |
483 TestIOHandler(const wchar_t* name, HANDLE signal, bool wait); | 483 TestIOHandler(const wchar_t* name, HANDLE signal, bool wait); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 | 675 |
676 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver); | 676 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver); |
677 }; | 677 }; |
678 | 678 |
679 TEST(MessageLoopTest, TaskObserver) { | 679 TEST(MessageLoopTest, TaskObserver) { |
680 const int kNumPosts = 6; | 680 const int kNumPosts = 6; |
681 DummyTaskObserver observer(kNumPosts); | 681 DummyTaskObserver observer(kNumPosts); |
682 | 682 |
683 MessageLoop loop; | 683 MessageLoop loop; |
684 loop.AddTaskObserver(&observer); | 684 loop.AddTaskObserver(&observer); |
685 loop.task_runner()->PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumPosts)); | 685 loop.task_runner()->PostTask(FROM_HERE, |
| 686 BindOnce(&PostNTasksThenQuit, kNumPosts)); |
686 RunLoop().Run(); | 687 RunLoop().Run(); |
687 loop.RemoveTaskObserver(&observer); | 688 loop.RemoveTaskObserver(&observer); |
688 | 689 |
689 EXPECT_EQ(kNumPosts, observer.num_tasks_started()); | 690 EXPECT_EQ(kNumPosts, observer.num_tasks_started()); |
690 EXPECT_EQ(kNumPosts, observer.num_tasks_processed()); | 691 EXPECT_EQ(kNumPosts, observer.num_tasks_processed()); |
691 } | 692 } |
692 | 693 |
693 #if defined(OS_WIN) | 694 #if defined(OS_WIN) |
694 TEST(MessageLoopTest, IOHandler) { | 695 TEST(MessageLoopTest, IOHandler) { |
695 RunTest_IOHandler(); | 696 RunTest_IOHandler(); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 // all the pending tasks have been destroyed). | 853 // all the pending tasks have been destroyed). |
853 MessageLoop* loop = new MessageLoop; | 854 MessageLoop* loop = new MessageLoop; |
854 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); | 855 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
855 | 856 |
856 bool task_destroyed = false; | 857 bool task_destroyed = false; |
857 bool destruction_observer_called = false; | 858 bool destruction_observer_called = false; |
858 | 859 |
859 MLDestructionObserver observer(&task_destroyed, &destruction_observer_called); | 860 MLDestructionObserver observer(&task_destroyed, &destruction_observer_called); |
860 loop->AddDestructionObserver(&observer); | 861 loop->AddDestructionObserver(&observer); |
861 loop->task_runner()->PostDelayedTask( | 862 loop->task_runner()->PostDelayedTask( |
862 FROM_HERE, Bind(&DestructionObserverProbe::Run, | 863 FROM_HERE, |
863 new DestructionObserverProbe( | 864 BindOnce(&DestructionObserverProbe::Run, |
864 &task_destroyed, &destruction_observer_called)), | 865 new DestructionObserverProbe(&task_destroyed, |
| 866 &destruction_observer_called)), |
865 kDelay); | 867 kDelay); |
866 delete loop; | 868 delete loop; |
867 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); | 869 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); |
868 // The task should have been destroyed when we deleted the loop. | 870 // The task should have been destroyed when we deleted the loop. |
869 EXPECT_TRUE(task_destroyed); | 871 EXPECT_TRUE(task_destroyed); |
870 EXPECT_TRUE(destruction_observer_called); | 872 EXPECT_TRUE(destruction_observer_called); |
871 } | 873 } |
872 | 874 |
873 | 875 |
874 // Verify that MessageLoop sets ThreadMainTaskRunner::current() and it | 876 // Verify that MessageLoop sets ThreadMainTaskRunner::current() and it |
875 // posts tasks on that message loop. | 877 // posts tasks on that message loop. |
876 TEST(MessageLoopTest, ThreadMainTaskRunner) { | 878 TEST(MessageLoopTest, ThreadMainTaskRunner) { |
877 MessageLoop loop; | 879 MessageLoop loop; |
878 | 880 |
879 scoped_refptr<Foo> foo(new Foo()); | 881 scoped_refptr<Foo> foo(new Foo()); |
880 std::string a("a"); | 882 std::string a("a"); |
881 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind( | 883 ThreadTaskRunnerHandle::Get()->PostTask( |
882 &Foo::Test1ConstRef, foo, a)); | 884 FROM_HERE, BindOnce(&Foo::Test1ConstRef, foo, a)); |
883 | 885 |
884 // Post quit task; | 886 // Post quit task; |
885 ThreadTaskRunnerHandle::Get()->PostTask( | 887 ThreadTaskRunnerHandle::Get()->PostTask( |
886 FROM_HERE, | 888 FROM_HERE, |
887 Bind(&MessageLoop::QuitWhenIdle, Unretained(MessageLoop::current()))); | 889 BindOnce(&MessageLoop::QuitWhenIdle, Unretained(MessageLoop::current()))); |
888 | 890 |
889 // Now kick things off | 891 // Now kick things off |
890 RunLoop().Run(); | 892 RunLoop().Run(); |
891 | 893 |
892 EXPECT_EQ(foo->test_count(), 1); | 894 EXPECT_EQ(foo->test_count(), 1); |
893 EXPECT_EQ(foo->result(), "a"); | 895 EXPECT_EQ(foo->result(), "a"); |
894 } | 896 } |
895 | 897 |
896 TEST(MessageLoopTest, IsType) { | 898 TEST(MessageLoopTest, IsType) { |
897 MessageLoop loop(MessageLoop::TYPE_UI); | 899 MessageLoop loop(MessageLoop::TYPE_UI); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1000 EXPECT_EQ(new_runner, ThreadTaskRunnerHandle::Get()); | 1002 EXPECT_EQ(new_runner, ThreadTaskRunnerHandle::Get()); |
1001 } | 1003 } |
1002 | 1004 |
1003 TEST(MessageLoopTest, OriginalRunnerWorks) { | 1005 TEST(MessageLoopTest, OriginalRunnerWorks) { |
1004 MessageLoop loop; | 1006 MessageLoop loop; |
1005 scoped_refptr<SingleThreadTaskRunner> new_runner(new TestSimpleTaskRunner()); | 1007 scoped_refptr<SingleThreadTaskRunner> new_runner(new TestSimpleTaskRunner()); |
1006 scoped_refptr<SingleThreadTaskRunner> original_runner(loop.task_runner()); | 1008 scoped_refptr<SingleThreadTaskRunner> original_runner(loop.task_runner()); |
1007 loop.SetTaskRunner(new_runner); | 1009 loop.SetTaskRunner(new_runner); |
1008 | 1010 |
1009 scoped_refptr<Foo> foo(new Foo()); | 1011 scoped_refptr<Foo> foo(new Foo()); |
1010 original_runner->PostTask(FROM_HERE, | 1012 original_runner->PostTask(FROM_HERE, BindOnce(&Foo::Test1ConstRef, foo, "a")); |
1011 Bind(&Foo::Test1ConstRef, foo, "a")); | |
1012 RunLoop().RunUntilIdle(); | 1013 RunLoop().RunUntilIdle(); |
1013 EXPECT_EQ(1, foo->test_count()); | 1014 EXPECT_EQ(1, foo->test_count()); |
1014 } | 1015 } |
1015 | 1016 |
1016 TEST(MessageLoopTest, DeleteUnboundLoop) { | 1017 TEST(MessageLoopTest, DeleteUnboundLoop) { |
1017 // It should be possible to delete an unbound message loop on a thread which | 1018 // It should be possible to delete an unbound message loop on a thread which |
1018 // already has another active loop. This happens when thread creation fails. | 1019 // already has another active loop. This happens when thread creation fails. |
1019 MessageLoop loop; | 1020 MessageLoop loop; |
1020 std::unique_ptr<MessageLoop> unbound_loop(MessageLoop::CreateUnbound( | 1021 std::unique_ptr<MessageLoop> unbound_loop(MessageLoop::CreateUnbound( |
1021 MessageLoop::TYPE_DEFAULT, MessageLoop::MessagePumpFactoryCallback())); | 1022 MessageLoop::TYPE_DEFAULT, MessageLoop::MessagePumpFactoryCallback())); |
(...skipping 12 matching lines...) Expand all Loading... |
1034 | 1035 |
1035 { | 1036 { |
1036 std::string kThreadName("bar"); | 1037 std::string kThreadName("bar"); |
1037 base::Thread thread(kThreadName); | 1038 base::Thread thread(kThreadName); |
1038 ASSERT_TRUE(thread.StartAndWaitForTesting()); | 1039 ASSERT_TRUE(thread.StartAndWaitForTesting()); |
1039 EXPECT_EQ(kThreadName, thread.message_loop()->GetThreadName()); | 1040 EXPECT_EQ(kThreadName, thread.message_loop()->GetThreadName()); |
1040 } | 1041 } |
1041 } | 1042 } |
1042 | 1043 |
1043 } // namespace base | 1044 } // namespace base |
OLD | NEW |