blob: 8245cfcdc50a94b3517b2a545b99315f971909b6 [file] [log] [blame]
battre@chromium.org6d025972012-04-17 11:48:06 +09001// 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.
4
5#include "base/task_runner_util.h"
6
7#include "base/bind.h"
skyostil97aefe12015-05-01 04:06:15 +09008#include "base/location.h"
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +09009#include "base/run_loop.h"
battre@chromium.org6d025972012-04-17 11:48:06 +090010#include "testing/gtest/include/gtest/gtest.h"
11
12namespace base {
13
14namespace {
15
16int ReturnFourtyTwo() {
17 return 42;
18}
19
20void StoreValue(int* destination, int value) {
21 *destination = value;
22}
23
ajwong@chromium.orgb8102192012-11-28 12:29:01 +090024void StoreDoubleValue(double* destination, double value) {
25 *destination = value;
26}
27
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +090028int g_foo_destruct_count = 0;
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +090029int g_foo_free_count = 0;
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +090030
31struct Foo {
32 ~Foo() {
33 ++g_foo_destruct_count;
34 }
35};
36
37scoped_ptr<Foo> CreateFoo() {
38 return scoped_ptr<Foo>(new Foo);
39}
40
41void ExpectFoo(scoped_ptr<Foo> foo) {
42 EXPECT_TRUE(foo.get());
43 scoped_ptr<Foo> local_foo(foo.Pass());
44 EXPECT_TRUE(local_foo.get());
45 EXPECT_FALSE(foo.get());
46}
47
viettrungluu@chromium.orgc494b362014-02-17 07:18:43 +090048struct FooDeleter {
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +090049 void operator()(Foo* foo) const {
50 ++g_foo_free_count;
rsleevi@chromium.org726c9242012-05-11 05:08:03 +090051 delete foo;
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +090052 };
53};
54
viettrungluu@chromium.orgc494b362014-02-17 07:18:43 +090055scoped_ptr<Foo, FooDeleter> CreateScopedFoo() {
56 return scoped_ptr<Foo, FooDeleter>(new Foo);
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +090057}
58
viettrungluu@chromium.orgc494b362014-02-17 07:18:43 +090059void ExpectScopedFoo(scoped_ptr<Foo, FooDeleter> foo) {
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +090060 EXPECT_TRUE(foo.get());
viettrungluu@chromium.orgc494b362014-02-17 07:18:43 +090061 scoped_ptr<Foo, FooDeleter> local_foo(foo.Pass());
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +090062 EXPECT_TRUE(local_foo.get());
63 EXPECT_FALSE(foo.get());
64}
65
battre@chromium.org6d025972012-04-17 11:48:06 +090066} // namespace
67
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +090068TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResult) {
battre@chromium.org6d025972012-04-17 11:48:06 +090069 int result = 0;
70
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +090071 MessageLoop message_loop;
skyostil97aefe12015-05-01 04:06:15 +090072 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +090073 Bind(&ReturnFourtyTwo),
74 Bind(&StoreValue, &result));
battre@chromium.org6d025972012-04-17 11:48:06 +090075
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +090076 RunLoop().RunUntilIdle();
battre@chromium.org6d025972012-04-17 11:48:06 +090077
78 EXPECT_EQ(42, result);
79}
80
ajwong@chromium.orgb8102192012-11-28 12:29:01 +090081TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultImplicitConvert) {
ajwong@chromium.orgb8102192012-11-28 12:29:01 +090082 double result = 0;
83
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +090084 MessageLoop message_loop;
skyostil97aefe12015-05-01 04:06:15 +090085 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +090086 Bind(&ReturnFourtyTwo),
87 Bind(&StoreDoubleValue, &result));
ajwong@chromium.orgb8102192012-11-28 12:29:01 +090088
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +090089 RunLoop().RunUntilIdle();
ajwong@chromium.orgb8102192012-11-28 12:29:01 +090090
91 EXPECT_DOUBLE_EQ(42.0, result);
92}
93
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +090094TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassed) {
95 g_foo_destruct_count = 0;
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +090096 g_foo_free_count = 0;
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +090097
98 MessageLoop message_loop;
skyostil97aefe12015-05-01 04:06:15 +090099 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
100 Bind(&CreateFoo), Bind(&ExpectFoo));
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +0900101
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +0900102 RunLoop().RunUntilIdle();
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +0900103
104 EXPECT_EQ(1, g_foo_destruct_count);
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +0900105 EXPECT_EQ(0, g_foo_free_count);
rsleevi@chromium.org726c9242012-05-11 05:08:03 +0900106}
107
108TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassedFreeProc) {
109 g_foo_destruct_count = 0;
110 g_foo_free_count = 0;
111
112 MessageLoop message_loop;
skyostil97aefe12015-05-01 04:06:15 +0900113 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
114 Bind(&CreateScopedFoo), Bind(&ExpectScopedFoo));
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +0900115
tfarina@chromium.org7d4a0ec2013-02-07 01:56:19 +0900116 RunLoop().RunUntilIdle();
rsleevi@chromium.orgbf65e902012-05-09 09:16:25 +0900117
118 EXPECT_EQ(1, g_foo_destruct_count);
119 EXPECT_EQ(1, g_foo_free_count);
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +0900120}
121
battre@chromium.org6d025972012-04-17 11:48:06 +0900122} // namespace base