Fix clang error caused by lack of OVERRIDE in r161270.
[chromium-blink-merge.git] / base / task_runner_util_unittest.cc
blob2d0cefe150011196023f78013e68f542d1d60d2b
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"
7 #include "base/bind.h"
8 #include "base/message_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace base {
13 namespace {
15 int ReturnFourtyTwo() {
16 return 42;
19 void StoreValue(int* destination, int value) {
20 *destination = value;
23 int g_foo_destruct_count = 0;
24 int g_foo_free_count = 0;
26 struct Foo {
27 ~Foo() {
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 {
45 ++g_foo_free_count;
46 delete foo;
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());
61 } // namespace
63 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResult) {
64 MessageLoop message_loop;
65 int result = 0;
67 PostTaskAndReplyWithResult(
68 message_loop.message_loop_proxy(),
69 FROM_HERE,
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;
80 g_foo_free_count = 0;
82 MessageLoop message_loop;
84 PostTaskAndReplyWithResult(
85 message_loop.message_loop_proxy(),
86 FROM_HERE,
87 Bind(&CreateFoo),
88 Bind(&ExpectFoo));
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;
98 g_foo_free_count = 0;
100 MessageLoop message_loop;
102 PostTaskAndReplyWithResult(
103 message_loop.message_loop_proxy(),
104 FROM_HERE,
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);
114 } // namespace base