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 int g_foo_destruct_count
= 0;
24 int g_foo_free_count
= 0;
28 ++g_foo_destruct_count
;
32 scoped_ptr
<Foo
> CreateFoo() {
33 return scoped_ptr
<Foo
>(new Foo
);
36 void ExpectFoo(scoped_ptr
<Foo
> foo
) {
37 EXPECT_TRUE(foo
.get());
38 scoped_ptr
<Foo
> local_foo(foo
.Pass());
39 EXPECT_TRUE(local_foo
.get());
40 EXPECT_FALSE(foo
.get());
43 struct FreeFooFunctor
{
44 void operator()(Foo
* foo
) const {
50 scoped_ptr_malloc
<Foo
, FreeFooFunctor
> CreateScopedFoo() {
51 return scoped_ptr_malloc
<Foo
, FreeFooFunctor
>(new Foo
);
54 void ExpectScopedFoo(scoped_ptr_malloc
<Foo
, FreeFooFunctor
> foo
) {
55 EXPECT_TRUE(foo
.get());
56 scoped_ptr_malloc
<Foo
, FreeFooFunctor
> local_foo(foo
.Pass());
57 EXPECT_TRUE(local_foo
.get());
58 EXPECT_FALSE(foo
.get());
63 TEST(TaskRunnerHelpersTest
, PostTaskAndReplyWithResult
) {
64 MessageLoop message_loop
;
67 PostTaskAndReplyWithResult(
68 message_loop
.message_loop_proxy(),
70 Bind(&ReturnFourtyTwo
),
71 Bind(&StoreValue
, &result
));
73 message_loop
.RunAllPending();
75 EXPECT_EQ(42, result
);
78 TEST(TaskRunnerHelpersTest
, PostTaskAndReplyWithResultPassed
) {
79 g_foo_destruct_count
= 0;
82 MessageLoop message_loop
;
84 PostTaskAndReplyWithResult(
85 message_loop
.message_loop_proxy(),
90 message_loop
.RunAllPending();
92 EXPECT_EQ(1, g_foo_destruct_count
);
93 EXPECT_EQ(0, g_foo_free_count
);
96 TEST(TaskRunnerHelpersTest
, PostTaskAndReplyWithResultPassedFreeProc
) {
97 g_foo_destruct_count
= 0;
100 MessageLoop message_loop
;
102 PostTaskAndReplyWithResult(
103 message_loop
.message_loop_proxy(),
105 Bind(&CreateScopedFoo
),
106 Bind(&ExpectScopedFoo
));
108 message_loop
.RunAllPending();
110 EXPECT_EQ(1, g_foo_destruct_count
);
111 EXPECT_EQ(1, g_foo_free_count
);