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

Side by Side Diff: base/callback_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/callback_list_unittest.nc ('k') | base/callback_unittest.nc » ('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/bind.h"
6 #include "base/callback.h"
7 #include "base/callback_helpers.h"
8 #include "base/callback_internal.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace base {
14
15 namespace {
16
17 struct FakeInvoker {
18 typedef void(RunType)(internal::BindStateBase*);
19 static void Run(internal::BindStateBase*) {
20 }
21 };
22
23 } // namespace
24
25 namespace internal {
26 template <typename Runnable, typename RunType, typename BoundArgsType>
27 struct BindState;
28
29 // White-box testpoints to inject into a Callback<> object for checking
30 // comparators and emptiness APIs. Use a BindState that is specialized
31 // based on a type we declared in the anonymous namespace above to remove any
32 // chance of colliding with another instantiation and breaking the
33 // one-definition-rule.
34 template <>
35 struct BindState<void(void), void(void), void(FakeInvoker)>
36 : public BindStateBase {
37 public:
38 BindState() : BindStateBase(&Destroy) {}
39 typedef FakeInvoker InvokerType;
40 private:
41 ~BindState() {}
42 static void Destroy(BindStateBase* self) {
43 delete static_cast<BindState*>(self);
44 }
45 };
46
47 template <>
48 struct BindState<void(void), void(void),
49 void(FakeInvoker, FakeInvoker)>
50 : public BindStateBase {
51 public:
52 BindState() : BindStateBase(&Destroy) {}
53 typedef FakeInvoker InvokerType;
54 private:
55 ~BindState() {}
56 static void Destroy(BindStateBase* self) {
57 delete static_cast<BindState*>(self);
58 }
59 };
60 } // namespace internal
61
62 namespace {
63
64 typedef internal::BindState<void(void), void(void), void(FakeInvoker)>
65 FakeBindState1;
66 typedef internal::BindState<void(void), void(void),
67 void(FakeInvoker, FakeInvoker)>
68 FakeBindState2;
69
70 class CallbackTest : public ::testing::Test {
71 public:
72 CallbackTest()
73 : callback_a_(new FakeBindState1()),
74 callback_b_(new FakeBindState2()) {
75 }
76
77 ~CallbackTest() override {}
78
79 protected:
80 Callback<void(void)> callback_a_;
81 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
82 Callback<void(void)> null_callback_;
83 };
84
85 // Ensure we can create unbound callbacks. We need this to be able to store
86 // them in class members that can be initialized later.
87 TEST_F(CallbackTest, DefaultConstruction) {
88 Callback<void(void)> c0;
89 Callback<void(int)> c1;
90 Callback<void(int,int)> c2;
91 Callback<void(int,int,int)> c3;
92 Callback<void(int,int,int,int)> c4;
93 Callback<void(int,int,int,int,int)> c5;
94 Callback<void(int,int,int,int,int,int)> c6;
95
96 EXPECT_TRUE(c0.is_null());
97 EXPECT_TRUE(c1.is_null());
98 EXPECT_TRUE(c2.is_null());
99 EXPECT_TRUE(c3.is_null());
100 EXPECT_TRUE(c4.is_null());
101 EXPECT_TRUE(c5.is_null());
102 EXPECT_TRUE(c6.is_null());
103 }
104
105 TEST_F(CallbackTest, IsNull) {
106 EXPECT_TRUE(null_callback_.is_null());
107 EXPECT_FALSE(callback_a_.is_null());
108 EXPECT_FALSE(callback_b_.is_null());
109 }
110
111 TEST_F(CallbackTest, Equals) {
112 EXPECT_TRUE(callback_a_.Equals(callback_a_));
113 EXPECT_FALSE(callback_a_.Equals(callback_b_));
114 EXPECT_FALSE(callback_b_.Equals(callback_a_));
115
116 // We should compare based on instance, not type.
117 Callback<void(void)> callback_c(new FakeBindState1());
118 Callback<void(void)> callback_a2 = callback_a_;
119 EXPECT_TRUE(callback_a_.Equals(callback_a2));
120 EXPECT_FALSE(callback_a_.Equals(callback_c));
121
122 // Empty, however, is always equal to empty.
123 Callback<void(void)> empty2;
124 EXPECT_TRUE(null_callback_.Equals(empty2));
125 }
126
127 TEST_F(CallbackTest, Reset) {
128 // Resetting should bring us back to empty.
129 ASSERT_FALSE(callback_a_.is_null());
130 ASSERT_FALSE(callback_a_.Equals(null_callback_));
131
132 callback_a_.Reset();
133
134 EXPECT_TRUE(callback_a_.is_null());
135 EXPECT_TRUE(callback_a_.Equals(null_callback_));
136 }
137
138 struct TestForReentrancy {
139 TestForReentrancy()
140 : cb_already_run(false),
141 cb(Bind(&TestForReentrancy::AssertCBIsNull, Unretained(this))) {
142 }
143 void AssertCBIsNull() {
144 ASSERT_TRUE(cb.is_null());
145 cb_already_run = true;
146 }
147 bool cb_already_run;
148 Closure cb;
149 };
150
151 TEST_F(CallbackTest, ResetAndReturn) {
152 TestForReentrancy tfr;
153 ASSERT_FALSE(tfr.cb.is_null());
154 ASSERT_FALSE(tfr.cb_already_run);
155 ResetAndReturn(&tfr.cb).Run();
156 ASSERT_TRUE(tfr.cb.is_null());
157 ASSERT_TRUE(tfr.cb_already_run);
158 }
159
160 class CallbackOwner : public base::RefCounted<CallbackOwner> {
161 public:
162 explicit CallbackOwner(bool* deleted) {
163 callback_ = Bind(&CallbackOwner::Unused, this);
164 deleted_ = deleted;
165 }
166 void Reset() {
167 callback_.Reset();
168 // We are deleted here if no-one else had a ref to us.
169 }
170
171 private:
172 friend class base::RefCounted<CallbackOwner>;
173 virtual ~CallbackOwner() {
174 *deleted_ = true;
175 }
176 void Unused() {
177 FAIL() << "Should never be called";
178 }
179
180 Closure callback_;
181 bool* deleted_;
182 };
183
184 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) {
185 bool deleted = false;
186 CallbackOwner* owner = new CallbackOwner(&deleted);
187 owner->Reset();
188 ASSERT_TRUE(deleted);
189 }
190
191 } // namespace
192 } // namespace base
OLDNEW
« no previous file with comments | « base/callback_list_unittest.nc ('k') | base/callback_unittest.nc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698