OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 private: | 75 private: |
76 friend class base::RefCounted<Foo>; | 76 friend class base::RefCounted<Foo>; |
77 | 77 |
78 ~Foo() {} | 78 ~Foo() {} |
79 | 79 |
80 int test_count_; | 80 int test_count_; |
81 std::string result_; | 81 std::string result_; |
82 }; | 82 }; |
83 | 83 |
84 // TODO(ajwong): Remove this once we've finished getting rid of the PostTask() | |
85 // compatibility methods. | |
86 void RunTest_PostLegacyTask(MessageLoop::Type message_loop_type) { | |
87 MessageLoop loop(message_loop_type); | |
88 | |
89 // Add tests to message loop | |
90 scoped_refptr<Foo> foo(new Foo()); | |
91 std::string a("a"), b("b"), c("c"), d("d"); | |
92 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | |
93 foo.get(), &Foo::Test0)); | |
94 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | |
95 foo.get(), &Foo::Test1ConstRef, a)); | |
96 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | |
97 foo.get(), &Foo::Test1Ptr, &b)); | |
98 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | |
99 foo.get(), &Foo::Test1Int, 100)); | |
100 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | |
101 foo.get(), &Foo::Test2Ptr, &a, &c)); | |
102 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | |
103 foo.get(), &Foo::Test2Mixed, a, &d)); | |
104 | |
105 // After all tests, post a message that will shut down the message loop | |
106 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
107 &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); | |
108 | |
109 // Now kick things off | |
110 MessageLoop::current()->Run(); | |
111 | |
112 EXPECT_EQ(foo->test_count(), 105); | |
113 EXPECT_EQ(foo->result(), "abacad"); | |
114 } | |
115 | |
116 void RunTest_PostTask(MessageLoop::Type message_loop_type) { | 84 void RunTest_PostTask(MessageLoop::Type message_loop_type) { |
117 MessageLoop loop(message_loop_type); | 85 MessageLoop loop(message_loop_type); |
118 | 86 |
119 // Add tests to message loop | 87 // Add tests to message loop |
120 scoped_refptr<Foo> foo(new Foo()); | 88 scoped_refptr<Foo> foo(new Foo()); |
121 std::string a("a"), b("b"), c("c"), d("d"); | 89 std::string a("a"), b("b"), c("c"), d("d"); |
122 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 90 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
123 &Foo::Test0, foo.get())); | 91 &Foo::Test0, foo.get())); |
124 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 92 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
125 &Foo::Test1ConstRef, foo.get(), a)); | 93 &Foo::Test1ConstRef, foo.get(), a)); |
(...skipping 1166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1292 | 1260 |
1293 #endif // defined(OS_WIN) | 1261 #endif // defined(OS_WIN) |
1294 | 1262 |
1295 } // namespace | 1263 } // namespace |
1296 | 1264 |
1297 //----------------------------------------------------------------------------- | 1265 //----------------------------------------------------------------------------- |
1298 // Each test is run against each type of MessageLoop. That way we are sure | 1266 // Each test is run against each type of MessageLoop. That way we are sure |
1299 // that message loops work properly in all configurations. Of course, in some | 1267 // that message loops work properly in all configurations. Of course, in some |
1300 // cases, a unit test may only be for a particular type of loop. | 1268 // cases, a unit test may only be for a particular type of loop. |
1301 | 1269 |
1302 TEST(MessageLoopTest, PostLegacyTask) { | |
1303 RunTest_PostLegacyTask(MessageLoop::TYPE_DEFAULT); | |
1304 RunTest_PostLegacyTask(MessageLoop::TYPE_UI); | |
1305 RunTest_PostLegacyTask(MessageLoop::TYPE_IO); | |
1306 } | |
1307 | |
1308 TEST(MessageLoopTest, PostTask) { | 1270 TEST(MessageLoopTest, PostTask) { |
1309 RunTest_PostTask(MessageLoop::TYPE_DEFAULT); | 1271 RunTest_PostTask(MessageLoop::TYPE_DEFAULT); |
1310 RunTest_PostTask(MessageLoop::TYPE_UI); | 1272 RunTest_PostTask(MessageLoop::TYPE_UI); |
1311 RunTest_PostTask(MessageLoop::TYPE_IO); | 1273 RunTest_PostTask(MessageLoop::TYPE_IO); |
1312 } | 1274 } |
1313 | 1275 |
1314 TEST(MessageLoopTest, PostTask_SEH) { | 1276 TEST(MessageLoopTest, PostTask_SEH) { |
1315 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT); | 1277 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT); |
1316 RunTest_PostTask_SEH(MessageLoop::TYPE_UI); | 1278 RunTest_PostTask_SEH(MessageLoop::TYPE_UI); |
1317 RunTest_PostTask_SEH(MessageLoop::TYPE_IO); | 1279 RunTest_PostTask_SEH(MessageLoop::TYPE_IO); |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1693 base::Bind(&DestructionObserverProbe::Run, | 1655 base::Bind(&DestructionObserverProbe::Run, |
1694 new DestructionObserverProbe(&task_destroyed, | 1656 new DestructionObserverProbe(&task_destroyed, |
1695 &destruction_observer_called)), | 1657 &destruction_observer_called)), |
1696 kDelayMS); | 1658 kDelayMS); |
1697 delete loop; | 1659 delete loop; |
1698 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); | 1660 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); |
1699 // The task should have been destroyed when we deleted the loop. | 1661 // The task should have been destroyed when we deleted the loop. |
1700 EXPECT_TRUE(task_destroyed); | 1662 EXPECT_TRUE(task_destroyed); |
1701 EXPECT_TRUE(destruction_observer_called); | 1663 EXPECT_TRUE(destruction_observer_called); |
1702 } | 1664 } |
OLD | NEW |