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
3 // found in the LICENSE file.
5 #include "base/task_runner_util.h"
8 #include "base/message_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 int ReturnFourtyTwo() {
19 void StoreValue(int* destination
, int value
) {
23 void StoreDoubleValue(double* destination
, double value
) {
27 int g_foo_destruct_count
= 0;
28 int g_foo_free_count
= 0;
32 ++g_foo_destruct_count
;
36 scoped_ptr
<Foo
> CreateFoo() {
37 return scoped_ptr
<Foo
>(new Foo
);
40 void ExpectFoo(scoped_ptr
<Foo
> foo
) {
41 EXPECT_TRUE(foo
.get());
42 scoped_ptr
<Foo
> local_foo(foo
.Pass());
43 EXPECT_TRUE(local_foo
.get());
44 EXPECT_FALSE(foo
.get());
47 struct FreeFooFunctor
{
48 void operator()(Foo
* foo
) const {
54 scoped_ptr_malloc
<Foo
, FreeFooFunctor
> CreateScopedFoo() {
55 return scoped_ptr_malloc
<Foo
, FreeFooFunctor
>(new Foo
);
58 void ExpectScopedFoo(scoped_ptr_malloc
<Foo
, FreeFooFunctor
> foo
) {
59 EXPECT_TRUE(foo
.get());
60 scoped_ptr_malloc
<Foo
, FreeFooFunctor
> local_foo(foo
.Pass());
61 EXPECT_TRUE(local_foo
.get());
62 EXPECT_FALSE(foo
.get());
67 TEST(TaskRunnerHelpersTest
, PostTaskAndReplyWithResult
) {
68 MessageLoop message_loop
;
71 PostTaskAndReplyWithResult(
72 message_loop
.message_loop_proxy(),
74 Bind(&ReturnFourtyTwo
),
75 Bind(&StoreValue
, &result
));
77 message_loop
.RunUntilIdle();
79 EXPECT_EQ(42, result
);
82 TEST(TaskRunnerHelpersTest
, PostTaskAndReplyWithResultImplicitConvert
) {
83 MessageLoop message_loop
;
86 PostTaskAndReplyWithResult(
87 message_loop
.message_loop_proxy(),
89 Bind(&ReturnFourtyTwo
),
90 Bind(&StoreDoubleValue
, &result
));
92 message_loop
.RunUntilIdle();
94 EXPECT_DOUBLE_EQ(42.0, result
);
97 TEST(TaskRunnerHelpersTest
, PostTaskAndReplyWithResultPassed
) {
98 g_foo_destruct_count
= 0;
101 MessageLoop message_loop
;
103 PostTaskAndReplyWithResult(
104 message_loop
.message_loop_proxy(),
109 message_loop
.RunUntilIdle();
111 EXPECT_EQ(1, g_foo_destruct_count
);
112 EXPECT_EQ(0, g_foo_free_count
);
115 TEST(TaskRunnerHelpersTest
, PostTaskAndReplyWithResultPassedFreeProc
) {
116 g_foo_destruct_count
= 0;
117 g_foo_free_count
= 0;
119 MessageLoop message_loop
;
121 PostTaskAndReplyWithResult(
122 message_loop
.message_loop_proxy(),
124 Bind(&CreateScopedFoo
),
125 Bind(&ExpectScopedFoo
));
127 message_loop
.RunUntilIdle();
129 EXPECT_EQ(1, g_foo_destruct_count
);
130 EXPECT_EQ(1, g_foo_free_count
);