• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/task_runner_util.h"
6 
7 #include <utility>
8 
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 
17 namespace {
18 
ReturnFourtyTwo()19 int ReturnFourtyTwo() {
20   return 42;
21 }
22 
StoreValue(int * destination,int value)23 void StoreValue(int* destination, int value) {
24   *destination = value;
25 }
26 
StoreDoubleValue(double * destination,double value)27 void StoreDoubleValue(double* destination, double value) {
28   *destination = value;
29 }
30 
31 int g_foo_destruct_count = 0;
32 int g_foo_free_count = 0;
33 
34 struct Foo {
~Foobase::__anon47d20de20111::Foo35   ~Foo() {
36     ++g_foo_destruct_count;
37   }
38 };
39 
CreateFoo()40 std::unique_ptr<Foo> CreateFoo() {
41   return std::unique_ptr<Foo>(new Foo);
42 }
43 
ExpectFoo(std::unique_ptr<Foo> foo)44 void ExpectFoo(std::unique_ptr<Foo> foo) {
45   EXPECT_TRUE(foo.get());
46   std::unique_ptr<Foo> local_foo(std::move(foo));
47   EXPECT_TRUE(local_foo.get());
48   EXPECT_FALSE(foo.get());
49 }
50 
51 struct FooDeleter {
operator ()base::__anon47d20de20111::FooDeleter52   void operator()(Foo* foo) const {
53     ++g_foo_free_count;
54     delete foo;
55   };
56 };
57 
CreateScopedFoo()58 std::unique_ptr<Foo, FooDeleter> CreateScopedFoo() {
59   return std::unique_ptr<Foo, FooDeleter>(new Foo);
60 }
61 
ExpectScopedFoo(std::unique_ptr<Foo,FooDeleter> foo)62 void ExpectScopedFoo(std::unique_ptr<Foo, FooDeleter> foo) {
63   EXPECT_TRUE(foo.get());
64   std::unique_ptr<Foo, FooDeleter> local_foo(std::move(foo));
65   EXPECT_TRUE(local_foo.get());
66   EXPECT_FALSE(foo.get());
67 }
68 
69 }  // namespace
70 
TEST(TaskRunnerHelpersTest,PostTaskAndReplyWithResult)71 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResult) {
72   int result = 0;
73 
74   MessageLoop message_loop;
75   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
76                              Bind(&ReturnFourtyTwo),
77                              Bind(&StoreValue, &result));
78 
79   RunLoop().RunUntilIdle();
80 
81   EXPECT_EQ(42, result);
82 }
83 
TEST(TaskRunnerHelpersTest,PostTaskAndReplyWithResultImplicitConvert)84 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultImplicitConvert) {
85   double result = 0;
86 
87   MessageLoop message_loop;
88   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
89                              Bind(&ReturnFourtyTwo),
90                              Bind(&StoreDoubleValue, &result));
91 
92   RunLoop().RunUntilIdle();
93 
94   EXPECT_DOUBLE_EQ(42.0, result);
95 }
96 
TEST(TaskRunnerHelpersTest,PostTaskAndReplyWithResultPassed)97 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassed) {
98   g_foo_destruct_count = 0;
99   g_foo_free_count = 0;
100 
101   MessageLoop message_loop;
102   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
103                              Bind(&CreateFoo), Bind(&ExpectFoo));
104 
105   RunLoop().RunUntilIdle();
106 
107   EXPECT_EQ(1, g_foo_destruct_count);
108   EXPECT_EQ(0, g_foo_free_count);
109 }
110 
TEST(TaskRunnerHelpersTest,PostTaskAndReplyWithResultPassedFreeProc)111 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassedFreeProc) {
112   g_foo_destruct_count = 0;
113   g_foo_free_count = 0;
114 
115   MessageLoop message_loop;
116   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
117                              Bind(&CreateScopedFoo), Bind(&ExpectScopedFoo));
118 
119   RunLoop().RunUntilIdle();
120 
121   EXPECT_EQ(1, g_foo_destruct_count);
122   EXPECT_EQ(1, g_foo_free_count);
123 }
124 
125 }  // namespace base
126