Tab Audio Mirroring: WebContentsAudioInputStream is a new implementation which repres...
[chromium-blink-merge.git] / base / task_runner_util_unittest.cc
blobd4d37692bace08028a619dccb7a752494cbaafdd
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 void StoreDoubleValue(double* destination, double value) {
24 *destination = value;
27 int g_foo_destruct_count = 0;
28 int g_foo_free_count = 0;
30 struct Foo {
31 ~Foo() {
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 {
49 ++g_foo_free_count;
50 delete foo;
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());
65 } // namespace
67 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResult) {
68 MessageLoop message_loop;
69 int result = 0;
71 PostTaskAndReplyWithResult(
72 message_loop.message_loop_proxy(),
73 FROM_HERE,
74 Bind(&ReturnFourtyTwo),
75 Bind(&StoreValue, &result));
77 message_loop.RunUntilIdle();
79 EXPECT_EQ(42, result);
82 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultImplicitConvert) {
83 MessageLoop message_loop;
84 double result = 0;
86 PostTaskAndReplyWithResult(
87 message_loop.message_loop_proxy(),
88 FROM_HERE,
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;
99 g_foo_free_count = 0;
101 MessageLoop message_loop;
103 PostTaskAndReplyWithResult(
104 message_loop.message_loop_proxy(),
105 FROM_HERE,
106 Bind(&CreateFoo),
107 Bind(&ExpectFoo));
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(),
123 FROM_HERE,
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);
133 } // namespace base