Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: base/task_runner_util_unittest.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/task_runner_util.h ('k') | base/template_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "base/bind.h"
8 #include "base/location.h"
9 #include "base/run_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13
14 namespace {
15
16 int ReturnFourtyTwo() {
17 return 42;
18 }
19
20 void StoreValue(int* destination, int value) {
21 *destination = value;
22 }
23
24 void StoreDoubleValue(double* destination, double value) {
25 *destination = value;
26 }
27
28 int g_foo_destruct_count = 0;
29 int g_foo_free_count = 0;
30
31 struct Foo {
32 ~Foo() {
33 ++g_foo_destruct_count;
34 }
35 };
36
37 scoped_ptr<Foo> CreateFoo() {
38 return scoped_ptr<Foo>(new Foo);
39 }
40
41 void 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
48 struct FooDeleter {
49 void operator()(Foo* foo) const {
50 ++g_foo_free_count;
51 delete foo;
52 };
53 };
54
55 scoped_ptr<Foo, FooDeleter> CreateScopedFoo() {
56 return scoped_ptr<Foo, FooDeleter>(new Foo);
57 }
58
59 void ExpectScopedFoo(scoped_ptr<Foo, FooDeleter> foo) {
60 EXPECT_TRUE(foo.get());
61 scoped_ptr<Foo, FooDeleter> local_foo(foo.Pass());
62 EXPECT_TRUE(local_foo.get());
63 EXPECT_FALSE(foo.get());
64 }
65
66 } // namespace
67
68 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResult) {
69 int result = 0;
70
71 MessageLoop message_loop;
72 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
73 Bind(&ReturnFourtyTwo),
74 Bind(&StoreValue, &result));
75
76 RunLoop().RunUntilIdle();
77
78 EXPECT_EQ(42, result);
79 }
80
81 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultImplicitConvert) {
82 double result = 0;
83
84 MessageLoop message_loop;
85 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
86 Bind(&ReturnFourtyTwo),
87 Bind(&StoreDoubleValue, &result));
88
89 RunLoop().RunUntilIdle();
90
91 EXPECT_DOUBLE_EQ(42.0, result);
92 }
93
94 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassed) {
95 g_foo_destruct_count = 0;
96 g_foo_free_count = 0;
97
98 MessageLoop message_loop;
99 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
100 Bind(&CreateFoo), Bind(&ExpectFoo));
101
102 RunLoop().RunUntilIdle();
103
104 EXPECT_EQ(1, g_foo_destruct_count);
105 EXPECT_EQ(0, g_foo_free_count);
106 }
107
108 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassedFreeProc) {
109 g_foo_destruct_count = 0;
110 g_foo_free_count = 0;
111
112 MessageLoop message_loop;
113 PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
114 Bind(&CreateScopedFoo), Bind(&ExpectScopedFoo));
115
116 RunLoop().RunUntilIdle();
117
118 EXPECT_EQ(1, g_foo_destruct_count);
119 EXPECT_EQ(1, g_foo_free_count);
120 }
121
122 } // namespace base
OLDNEW
« no previous file with comments | « base/task_runner_util.h ('k') | base/template_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698