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 "base/task_runner_util.h" | 5 #include "base/task_runner_util.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 void ExpectFoo(scoped_ptr<Foo> foo) { | 36 void ExpectFoo(scoped_ptr<Foo> foo) { |
37 EXPECT_TRUE(foo.get()); | 37 EXPECT_TRUE(foo.get()); |
38 scoped_ptr<Foo> local_foo(foo.Pass()); | 38 scoped_ptr<Foo> local_foo(foo.Pass()); |
39 EXPECT_TRUE(local_foo.get()); | 39 EXPECT_TRUE(local_foo.get()); |
40 EXPECT_FALSE(foo.get()); | 40 EXPECT_FALSE(foo.get()); |
41 } | 41 } |
42 | 42 |
43 struct FreeFooFunctor { | 43 struct FreeFooFunctor { |
44 void operator()(Foo* foo) const { | 44 void operator()(Foo* foo) const { |
45 ++g_foo_free_count; | 45 ++g_foo_free_count; |
| 46 delete foo; |
46 }; | 47 }; |
47 }; | 48 }; |
48 | 49 |
49 scoped_ptr_malloc<Foo, FreeFooFunctor> CreateScopedFoo() { | 50 scoped_ptr_malloc<Foo, FreeFooFunctor> CreateScopedFoo() { |
50 return scoped_ptr_malloc<Foo, FreeFooFunctor>(new Foo); | 51 return scoped_ptr_malloc<Foo, FreeFooFunctor>(new Foo); |
51 } | 52 } |
52 | 53 |
53 void ExpectScopedFoo(scoped_ptr_malloc<Foo, FreeFooFunctor> foo) { | 54 void ExpectScopedFoo(scoped_ptr_malloc<Foo, FreeFooFunctor> foo) { |
54 EXPECT_TRUE(foo.get()); | 55 EXPECT_TRUE(foo.get()); |
55 scoped_ptr_malloc<Foo, FreeFooFunctor> local_foo(foo.Pass()); | 56 scoped_ptr_malloc<Foo, FreeFooFunctor> local_foo(foo.Pass()); |
(...skipping 27 matching lines...) Expand all Loading... |
83 PostTaskAndReplyWithResult( | 84 PostTaskAndReplyWithResult( |
84 message_loop.message_loop_proxy(), | 85 message_loop.message_loop_proxy(), |
85 FROM_HERE, | 86 FROM_HERE, |
86 Bind(&CreateFoo), | 87 Bind(&CreateFoo), |
87 Bind(&ExpectFoo)); | 88 Bind(&ExpectFoo)); |
88 | 89 |
89 message_loop.RunAllPending(); | 90 message_loop.RunAllPending(); |
90 | 91 |
91 EXPECT_EQ(1, g_foo_destruct_count); | 92 EXPECT_EQ(1, g_foo_destruct_count); |
92 EXPECT_EQ(0, g_foo_free_count); | 93 EXPECT_EQ(0, g_foo_free_count); |
| 94 } |
| 95 |
| 96 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassedFreeProc) { |
| 97 g_foo_destruct_count = 0; |
| 98 g_foo_free_count = 0; |
| 99 |
| 100 MessageLoop message_loop; |
93 | 101 |
94 PostTaskAndReplyWithResult( | 102 PostTaskAndReplyWithResult( |
95 message_loop.message_loop_proxy(), | 103 message_loop.message_loop_proxy(), |
96 FROM_HERE, | 104 FROM_HERE, |
97 Bind(&CreateScopedFoo), | 105 Bind(&CreateScopedFoo), |
98 Bind(&ExpectScopedFoo)); | 106 Bind(&ExpectScopedFoo)); |
99 | 107 |
100 message_loop.RunAllPending(); | 108 message_loop.RunAllPending(); |
101 | 109 |
102 EXPECT_EQ(1, g_foo_destruct_count); | 110 EXPECT_EQ(1, g_foo_destruct_count); |
103 EXPECT_EQ(1, g_foo_free_count); | 111 EXPECT_EQ(1, g_foo_free_count); |
104 } | 112 } |
105 | 113 |
106 } // namespace base | 114 } // namespace base |
OLD | NEW |