[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 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 | |
dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/memory/weak_ptr.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 14 | #include "testing/gmock/include/gmock/gmock.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | using ::testing::Mock; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::StrictMock; |
| 20 | |
| 21 | namespace base { |
| 22 | namespace { |
| 23 | |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 24 | class IncompleteType; |
| 25 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 26 | class NoRef { |
| 27 | public: |
| 28 | NoRef() {} |
| 29 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 30 | MOCK_METHOD0(VoidMethod0, void()); |
| 31 | MOCK_CONST_METHOD0(VoidConstMethod0, void()); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 32 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 33 | MOCK_METHOD0(IntMethod0, int()); |
| 34 | MOCK_CONST_METHOD0(IntConstMethod0, int()); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 35 | |
| 36 | private: |
| 37 | // Particularly important in this test to ensure no copies are made. |
| 38 | DISALLOW_COPY_AND_ASSIGN(NoRef); |
| 39 | }; |
| 40 | |
| 41 | class HasRef : public NoRef { |
| 42 | public: |
| 43 | HasRef() {} |
| 44 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 45 | MOCK_CONST_METHOD0(AddRef, void()); |
| 46 | MOCK_CONST_METHOD0(Release, bool()); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | // Particularly important in this test to ensure no copies are made. |
| 50 | DISALLOW_COPY_AND_ASSIGN(HasRef); |
| 51 | }; |
| 52 | |
[email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 53 | class HasRefPrivateDtor : public HasRef { |
| 54 | private: |
| 55 | ~HasRefPrivateDtor() {} |
| 56 | }; |
| 57 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 58 | static const int kParentValue = 1; |
| 59 | static const int kChildValue = 2; |
| 60 | |
| 61 | class Parent { |
| 62 | public: |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 63 | void AddRef() const {} |
| 64 | void Release() const {} |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 65 | virtual void VirtualSet() { value = kParentValue; } |
| 66 | void NonVirtualSet() { value = kParentValue; } |
| 67 | int value; |
| 68 | }; |
| 69 | |
| 70 | class Child : public Parent { |
| 71 | public: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 72 | void VirtualSet() override { value = kChildValue; } |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 73 | void NonVirtualSet() { value = kChildValue; } |
| 74 | }; |
| 75 | |
| 76 | class NoRefParent { |
| 77 | public: |
| 78 | virtual void VirtualSet() { value = kParentValue; } |
| 79 | void NonVirtualSet() { value = kParentValue; } |
| 80 | int value; |
| 81 | }; |
| 82 | |
| 83 | class NoRefChild : public NoRefParent { |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 84 | void VirtualSet() override { value = kChildValue; } |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 85 | void NonVirtualSet() { value = kChildValue; } |
| 86 | }; |
| 87 | |
| 88 | // Used for probing the number of copies that occur if a type must be coerced |
| 89 | // during argument forwarding in the Run() methods. |
| 90 | struct DerivedCopyCounter { |
| 91 | DerivedCopyCounter(int* copies, int* assigns) |
| 92 | : copies_(copies), assigns_(assigns) { |
| 93 | } |
| 94 | int* copies_; |
| 95 | int* assigns_; |
| 96 | }; |
| 97 | |
| 98 | // Used for probing the number of copies in an argument. |
| 99 | class CopyCounter { |
| 100 | public: |
| 101 | CopyCounter(int* copies, int* assigns) |
| 102 | : copies_(copies), assigns_(assigns) { |
| 103 | } |
| 104 | |
| 105 | CopyCounter(const CopyCounter& other) |
| 106 | : copies_(other.copies_), |
| 107 | assigns_(other.assigns_) { |
| 108 | (*copies_)++; |
| 109 | } |
| 110 | |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 111 | // Probing for copies from coercion. |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 112 | explicit CopyCounter(const DerivedCopyCounter& other) |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 113 | : copies_(other.copies_), |
| 114 | assigns_(other.assigns_) { |
| 115 | (*copies_)++; |
| 116 | } |
| 117 | |
| 118 | const CopyCounter& operator=(const CopyCounter& rhs) { |
| 119 | copies_ = rhs.copies_; |
| 120 | assigns_ = rhs.assigns_; |
| 121 | |
| 122 | if (assigns_) { |
| 123 | (*assigns_)++; |
| 124 | } |
| 125 | |
| 126 | return *this; |
| 127 | } |
| 128 | |
| 129 | int copies() const { |
| 130 | return *copies_; |
| 131 | } |
| 132 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 133 | private: |
| 134 | int* copies_; |
| 135 | int* assigns_; |
| 136 | }; |
| 137 | |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 138 | class DeleteCounter { |
| 139 | public: |
| 140 | explicit DeleteCounter(int* deletes) |
| 141 | : deletes_(deletes) { |
| 142 | } |
| 143 | |
| 144 | ~DeleteCounter() { |
| 145 | (*deletes_)++; |
| 146 | } |
| 147 | |
| 148 | void VoidMethod0() {} |
| 149 | |
| 150 | private: |
| 151 | int* deletes_; |
| 152 | }; |
| 153 | |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 154 | template <typename T> |
| 155 | T PassThru(T scoper) { |
dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 156 | return scoper; |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 159 | // Some test functions that we can Bind to. |
| 160 | template <typename T> |
| 161 | T PolymorphicIdentity(T t) { |
| 162 | return t; |
| 163 | } |
| 164 | |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 165 | template <typename... Ts> |
| 166 | struct VoidPolymorphic { |
| 167 | static void Run(Ts... t) {} |
| 168 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 169 | |
| 170 | int Identity(int n) { |
| 171 | return n; |
| 172 | } |
| 173 | |
| 174 | int ArrayGet(const int array[], int n) { |
| 175 | return array[n]; |
| 176 | } |
| 177 | |
| 178 | int Sum(int a, int b, int c, int d, int e, int f) { |
| 179 | return a + b + c + d + e + f; |
| 180 | } |
| 181 | |
| 182 | const char* CStringIdentity(const char* s) { |
| 183 | return s; |
| 184 | } |
| 185 | |
| 186 | int GetCopies(const CopyCounter& counter) { |
| 187 | return counter.copies(); |
| 188 | } |
| 189 | |
| 190 | int UnwrapNoRefParent(NoRefParent p) { |
| 191 | return p.value; |
| 192 | } |
| 193 | |
| 194 | int UnwrapNoRefParentPtr(NoRefParent* p) { |
| 195 | return p->value; |
| 196 | } |
| 197 | |
| 198 | int UnwrapNoRefParentConstRef(const NoRefParent& p) { |
| 199 | return p.value; |
| 200 | } |
| 201 | |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 202 | void RefArgSet(int &n) { |
| 203 | n = 2; |
| 204 | } |
| 205 | |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 206 | void PtrArgSet(int *n) { |
| 207 | *n = 2; |
| 208 | } |
| 209 | |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 210 | int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
| 211 | return n; |
| 212 | } |
| 213 | |
[email protected] | edd2f1b | 2013-06-22 20:32:50 | [diff] [blame] | 214 | int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) { |
| 215 | return n; |
| 216 | } |
| 217 | |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 218 | void TakesACallback(const Closure& callback) { |
| 219 | callback.Run(); |
| 220 | } |
| 221 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 222 | class BindTest : public ::testing::Test { |
| 223 | public: |
| 224 | BindTest() { |
| 225 | const_has_ref_ptr_ = &has_ref_; |
| 226 | const_no_ref_ptr_ = &no_ref_; |
| 227 | static_func_mock_ptr = &static_func_mock_; |
| 228 | } |
| 229 | |
| 230 | virtual ~BindTest() { |
| 231 | } |
| 232 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 233 | static void VoidFunc0() { |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 234 | static_func_mock_ptr->VoidMethod0(); |
| 235 | } |
| 236 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 237 | static int IntFunc0() { return static_func_mock_ptr->IntMethod0(); } |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 238 | |
| 239 | protected: |
| 240 | StrictMock<NoRef> no_ref_; |
| 241 | StrictMock<HasRef> has_ref_; |
| 242 | const HasRef* const_has_ref_ptr_; |
| 243 | const NoRef* const_no_ref_ptr_; |
| 244 | StrictMock<NoRef> static_func_mock_; |
| 245 | |
| 246 | // Used by the static functions to perform expectations. |
| 247 | static StrictMock<NoRef>* static_func_mock_ptr; |
| 248 | |
| 249 | private: |
| 250 | DISALLOW_COPY_AND_ASSIGN(BindTest); |
| 251 | }; |
| 252 | |
| 253 | StrictMock<NoRef>* BindTest::static_func_mock_ptr; |
| 254 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 255 | // Sanity check that we can instantiate a callback for each arity. |
| 256 | TEST_F(BindTest, ArityTest) { |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 257 | Callback<int()> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 258 | EXPECT_EQ(63, c0.Run()); |
| 259 | |
| 260 | Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2); |
| 261 | EXPECT_EQ(75, c1.Run(13)); |
| 262 | |
| 263 | Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4); |
| 264 | EXPECT_EQ(85, c2.Run(13, 12)); |
| 265 | |
| 266 | Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8); |
| 267 | EXPECT_EQ(92, c3.Run(13, 12, 11)); |
| 268 | |
| 269 | Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16); |
| 270 | EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); |
| 271 | |
| 272 | Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32); |
| 273 | EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); |
| 274 | |
| 275 | Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum); |
| 276 | EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); |
| 277 | } |
| 278 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 279 | // Test the Currying ability of the Callback system. |
| 280 | TEST_F(BindTest, CurryingTest) { |
| 281 | Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum); |
| 282 | EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); |
[email protected] | cea20fe4 | 2011-09-30 09:09:34 | [diff] [blame] | 283 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 284 | Callback<int(int,int,int,int,int)> c5 = Bind(c6, 32); |
| 285 | EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); |
[email protected] | cea20fe4 | 2011-09-30 09:09:34 | [diff] [blame] | 286 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 287 | Callback<int(int,int,int,int)> c4 = Bind(c5, 16); |
| 288 | EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); |
[email protected] | cea20fe4 | 2011-09-30 09:09:34 | [diff] [blame] | 289 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 290 | Callback<int(int,int,int)> c3 = Bind(c4, 8); |
| 291 | EXPECT_EQ(92, c3.Run(13, 12, 11)); |
[email protected] | cea20fe4 | 2011-09-30 09:09:34 | [diff] [blame] | 292 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 293 | Callback<int(int,int)> c2 = Bind(c3, 4); |
| 294 | EXPECT_EQ(85, c2.Run(13, 12)); |
[email protected] | cea20fe4 | 2011-09-30 09:09:34 | [diff] [blame] | 295 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 296 | Callback<int(int)> c1 = Bind(c2, 2); |
| 297 | EXPECT_EQ(75, c1.Run(13)); |
| 298 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 299 | Callback<int()> c0 = Bind(c1, 1); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 300 | EXPECT_EQ(63, c0.Run()); |
[email protected] | cea20fe4 | 2011-09-30 09:09:34 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 303 | // Test that currying the rvalue result of another Bind() works correctly. |
| 304 | // - rvalue should be usable as argument to Bind(). |
| 305 | // - multiple runs of resulting Callback remain valid. |
| 306 | TEST_F(BindTest, CurryingRvalueResultOfBind) { |
| 307 | int n = 0; |
| 308 | Closure cb = base::Bind(&TakesACallback, base::Bind(&PtrArgSet, &n)); |
| 309 | |
| 310 | // If we implement Bind() such that the return value has auto_ptr-like |
| 311 | // semantics, the second call here will fail because ownership of |
| 312 | // the internal BindState<> would have been transfered to a *temporary* |
| 313 | // constructon of a Callback object on the first call. |
| 314 | cb.Run(); |
| 315 | EXPECT_EQ(2, n); |
| 316 | |
| 317 | n = 0; |
| 318 | cb.Run(); |
| 319 | EXPECT_EQ(2, n); |
| 320 | } |
| 321 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 322 | // Function type support. |
| 323 | // - Normal function. |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 324 | // - Normal function bound with non-refcounted first argument. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 325 | // - Method bound to non-const object. |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 326 | // - Method bound to scoped_refptr. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 327 | // - Const method bound to non-const object. |
| 328 | // - Const method bound to const object. |
| 329 | // - Derived classes can be used with pointers to non-virtual base functions. |
| 330 | // - Derived classes can be used with pointers to virtual base functions (and |
| 331 | // preserve virtual dispatch). |
| 332 | TEST_F(BindTest, FunctionTypeSupport) { |
| 333 | EXPECT_CALL(static_func_mock_, VoidMethod0()); |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 334 | EXPECT_CALL(has_ref_, AddRef()).Times(5); |
| 335 | EXPECT_CALL(has_ref_, Release()).Times(5); |
| 336 | EXPECT_CALL(has_ref_, VoidMethod0()).Times(2); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 337 | EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2); |
| 338 | |
| 339 | Closure normal_cb = Bind(&VoidFunc0); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 340 | Callback<NoRef*()> normal_non_refcounted_cb = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 341 | Bind(&PolymorphicIdentity<NoRef*>, &no_ref_); |
| 342 | normal_cb.Run(); |
| 343 | EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run()); |
| 344 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 345 | Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_); |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 346 | Closure method_refptr_cb = Bind(&HasRef::VoidMethod0, |
| 347 | make_scoped_refptr(&has_ref_)); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 348 | Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0, |
| 349 | &has_ref_); |
| 350 | Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0, |
| 351 | const_has_ref_ptr_); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 352 | method_cb.Run(); |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 353 | method_refptr_cb.Run(); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 354 | const_method_nonconst_obj_cb.Run(); |
| 355 | const_method_const_obj_cb.Run(); |
| 356 | |
| 357 | Child child; |
| 358 | child.value = 0; |
| 359 | Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child); |
| 360 | virtual_set_cb.Run(); |
| 361 | EXPECT_EQ(kChildValue, child.value); |
| 362 | |
| 363 | child.value = 0; |
| 364 | Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child); |
| 365 | non_virtual_set_cb.Run(); |
| 366 | EXPECT_EQ(kParentValue, child.value); |
| 367 | } |
| 368 | |
| 369 | // Return value support. |
| 370 | // - Function with return value. |
| 371 | // - Method with return value. |
| 372 | // - Const method with return value. |
| 373 | TEST_F(BindTest, ReturnValues) { |
| 374 | EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
| 375 | EXPECT_CALL(has_ref_, AddRef()).Times(3); |
| 376 | EXPECT_CALL(has_ref_, Release()).Times(3); |
| 377 | EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337)); |
| 378 | EXPECT_CALL(has_ref_, IntConstMethod0()) |
| 379 | .WillOnce(Return(41337)) |
| 380 | .WillOnce(Return(51337)); |
| 381 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 382 | Callback<int()> normal_cb = Bind(&IntFunc0); |
| 383 | Callback<int()> method_cb = Bind(&HasRef::IntMethod0, &has_ref_); |
| 384 | Callback<int()> const_method_nonconst_obj_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 385 | Bind(&HasRef::IntConstMethod0, &has_ref_); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 386 | Callback<int()> const_method_const_obj_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 387 | Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_); |
| 388 | EXPECT_EQ(1337, normal_cb.Run()); |
| 389 | EXPECT_EQ(31337, method_cb.Run()); |
| 390 | EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run()); |
| 391 | EXPECT_EQ(51337, const_method_const_obj_cb.Run()); |
| 392 | } |
| 393 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 394 | // IgnoreResult adapter test. |
| 395 | // - Function with return value. |
| 396 | // - Method with return value. |
| 397 | // - Const Method with return. |
| 398 | // - Method with return value bound to WeakPtr<>. |
| 399 | // - Const Method with return bound to WeakPtr<>. |
| 400 | TEST_F(BindTest, IgnoreResult) { |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 401 | EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 402 | EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 403 | EXPECT_CALL(has_ref_, Release()).Times(2); |
| 404 | EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10)); |
| 405 | EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11)); |
| 406 | EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12)); |
| 407 | EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13)); |
| 408 | |
| 409 | Closure normal_func_cb = Bind(IgnoreResult(&IntFunc0)); |
| 410 | normal_func_cb.Run(); |
| 411 | |
| 412 | Closure non_void_method_cb = |
| 413 | Bind(IgnoreResult(&HasRef::IntMethod0), &has_ref_); |
| 414 | non_void_method_cb.Run(); |
| 415 | |
| 416 | Closure non_void_const_method_cb = |
| 417 | Bind(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_); |
| 418 | non_void_const_method_cb.Run(); |
| 419 | |
| 420 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 421 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 422 | |
| 423 | Closure non_void_weak_method_cb = |
| 424 | Bind(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr()); |
| 425 | non_void_weak_method_cb.Run(); |
| 426 | |
| 427 | Closure non_void_weak_const_method_cb = |
| 428 | Bind(IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr()); |
| 429 | non_void_weak_const_method_cb.Run(); |
| 430 | |
| 431 | weak_factory.InvalidateWeakPtrs(); |
| 432 | non_void_weak_const_method_cb.Run(); |
| 433 | non_void_weak_method_cb.Run(); |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 434 | } |
| 435 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 436 | // Argument binding tests. |
| 437 | // - Argument binding to primitive. |
| 438 | // - Argument binding to primitive pointer. |
| 439 | // - Argument binding to a literal integer. |
| 440 | // - Argument binding to a literal string. |
| 441 | // - Argument binding with template function. |
| 442 | // - Argument binding to an object. |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 443 | // - Argument binding to pointer to incomplete type. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 444 | // - Argument gets type converted. |
| 445 | // - Pointer argument gets converted. |
| 446 | // - Const Reference forces conversion. |
| 447 | TEST_F(BindTest, ArgumentBinding) { |
| 448 | int n = 2; |
| 449 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 450 | Callback<int()> bind_primitive_cb = Bind(&Identity, n); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 451 | EXPECT_EQ(n, bind_primitive_cb.Run()); |
| 452 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 453 | Callback<int*()> bind_primitive_pointer_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 454 | Bind(&PolymorphicIdentity<int*>, &n); |
| 455 | EXPECT_EQ(&n, bind_primitive_pointer_cb.Run()); |
| 456 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 457 | Callback<int()> bind_int_literal_cb = Bind(&Identity, 3); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 458 | EXPECT_EQ(3, bind_int_literal_cb.Run()); |
| 459 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 460 | Callback<const char*()> bind_string_literal_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 461 | Bind(&CStringIdentity, "hi"); |
| 462 | EXPECT_STREQ("hi", bind_string_literal_cb.Run()); |
| 463 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 464 | Callback<int()> bind_template_function_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 465 | Bind(&PolymorphicIdentity<int>, 4); |
| 466 | EXPECT_EQ(4, bind_template_function_cb.Run()); |
| 467 | |
| 468 | NoRefParent p; |
| 469 | p.value = 5; |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 470 | Callback<int()> bind_object_cb = Bind(&UnwrapNoRefParent, p); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 471 | EXPECT_EQ(5, bind_object_cb.Run()); |
| 472 | |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 473 | IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 474 | Callback<IncompleteType*()> bind_incomplete_ptr_cb = |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 475 | Bind(&PolymorphicIdentity<IncompleteType*>, incomplete_ptr); |
| 476 | EXPECT_EQ(incomplete_ptr, bind_incomplete_ptr_cb.Run()); |
| 477 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 478 | NoRefChild c; |
| 479 | c.value = 6; |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 480 | Callback<int()> bind_promotes_cb = Bind(&UnwrapNoRefParent, c); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 481 | EXPECT_EQ(6, bind_promotes_cb.Run()); |
| 482 | |
| 483 | c.value = 7; |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 484 | Callback<int()> bind_pointer_promotes_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 485 | Bind(&UnwrapNoRefParentPtr, &c); |
| 486 | EXPECT_EQ(7, bind_pointer_promotes_cb.Run()); |
| 487 | |
| 488 | c.value = 8; |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 489 | Callback<int()> bind_const_reference_promotes_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 490 | Bind(&UnwrapNoRefParentConstRef, c); |
| 491 | EXPECT_EQ(8, bind_const_reference_promotes_cb.Run()); |
| 492 | } |
| 493 | |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 494 | // Unbound argument type support tests. |
| 495 | // - Unbound value. |
| 496 | // - Unbound pointer. |
| 497 | // - Unbound reference. |
| 498 | // - Unbound const reference. |
| 499 | // - Unbound unsized array. |
| 500 | // - Unbound sized array. |
| 501 | // - Unbound array-of-arrays. |
| 502 | TEST_F(BindTest, UnboundArgumentTypeSupport) { |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 503 | Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic<int>::Run); |
| 504 | Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic<int*>::Run); |
| 505 | Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic<int&>::Run); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 506 | Callback<void(const int&)> unbound_const_ref_cb = |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 507 | Bind(&VoidPolymorphic<const int&>::Run); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 508 | Callback<void(int[])> unbound_unsized_array_cb = |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 509 | Bind(&VoidPolymorphic<int[]>::Run); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 510 | Callback<void(int[2])> unbound_sized_array_cb = |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 511 | Bind(&VoidPolymorphic<int[2]>::Run); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 512 | Callback<void(int[][2])> unbound_array_of_arrays_cb = |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 513 | Bind(&VoidPolymorphic<int[][2]>::Run); |
| 514 | |
| 515 | Callback<void(int&)> unbound_ref_with_bound_arg = |
| 516 | Bind(&VoidPolymorphic<int, int&>::Run, 1); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // Function with unbound reference parameter. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 520 | // - Original parameter is modified by callback. |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 521 | TEST_F(BindTest, UnboundReferenceSupport) { |
| 522 | int n = 0; |
| 523 | Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet); |
| 524 | unbound_ref_cb.Run(n); |
| 525 | EXPECT_EQ(2, n); |
| 526 | } |
| 527 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 528 | // Functions that take reference parameters. |
| 529 | // - Forced reference parameter type still stores a copy. |
| 530 | // - Forced const reference parameter type still stores a copy. |
| 531 | TEST_F(BindTest, ReferenceArgumentBinding) { |
| 532 | int n = 1; |
| 533 | int& ref_n = n; |
| 534 | const int& const_ref_n = n; |
| 535 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 536 | Callback<int()> ref_copies_cb = Bind(&Identity, ref_n); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 537 | EXPECT_EQ(n, ref_copies_cb.Run()); |
| 538 | n++; |
| 539 | EXPECT_EQ(n - 1, ref_copies_cb.Run()); |
| 540 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 541 | Callback<int()> const_ref_copies_cb = Bind(&Identity, const_ref_n); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 542 | EXPECT_EQ(n, const_ref_copies_cb.Run()); |
| 543 | n++; |
| 544 | EXPECT_EQ(n - 1, const_ref_copies_cb.Run()); |
| 545 | } |
| 546 | |
| 547 | // Check that we can pass in arrays and have them be stored as a pointer. |
| 548 | // - Array of values stores a pointer. |
| 549 | // - Array of const values stores a pointer. |
| 550 | TEST_F(BindTest, ArrayArgumentBinding) { |
| 551 | int array[4] = {1, 1, 1, 1}; |
| 552 | const int (*const_array_ptr)[4] = &array; |
| 553 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 554 | Callback<int()> array_cb = Bind(&ArrayGet, array, 1); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 555 | EXPECT_EQ(1, array_cb.Run()); |
| 556 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 557 | Callback<int()> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 558 | EXPECT_EQ(1, const_array_cb.Run()); |
| 559 | |
| 560 | array[1] = 3; |
| 561 | EXPECT_EQ(3, array_cb.Run()); |
| 562 | EXPECT_EQ(3, const_array_cb.Run()); |
| 563 | } |
| 564 | |
| 565 | // Verify SupportsAddRefAndRelease correctly introspects the class type for |
| 566 | // AddRef() and Release(). |
[email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 567 | // - Class with AddRef() and Release() |
| 568 | // - Class without AddRef() and Release() |
| 569 | // - Derived Class with AddRef() and Release() |
| 570 | // - Derived Class without AddRef() and Release() |
| 571 | // - Derived Class with AddRef() and Release() and a private destructor. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 572 | TEST_F(BindTest, SupportsAddRefAndRelease) { |
| 573 | EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value); |
| 574 | EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value); |
| 575 | |
| 576 | // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and |
| 577 | // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over |
| 578 | // inheritance. |
| 579 | EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value); |
| 580 | EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value); |
[email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 581 | |
| 582 | // This matters because the implementation creates a dummy class that |
| 583 | // inherits from the template type. |
| 584 | EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // Unretained() wrapper support. |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 588 | // - Method bound to Unretained() non-const object. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 589 | // - Const method bound to Unretained() non-const object. |
| 590 | // - Const method bound to Unretained() const object. |
| 591 | TEST_F(BindTest, Unretained) { |
| 592 | EXPECT_CALL(no_ref_, VoidMethod0()); |
| 593 | EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 594 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 595 | Callback<void()> method_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 596 | Bind(&NoRef::VoidMethod0, Unretained(&no_ref_)); |
| 597 | method_cb.Run(); |
| 598 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 599 | Callback<void()> const_method_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 600 | Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_)); |
| 601 | const_method_cb.Run(); |
| 602 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 603 | Callback<void()> const_method_const_ptr_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 604 | Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_)); |
| 605 | const_method_const_ptr_cb.Run(); |
| 606 | } |
| 607 | |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 608 | // WeakPtr() support. |
| 609 | // - Method bound to WeakPtr<> to non-const object. |
| 610 | // - Const method bound to WeakPtr<> to non-const object. |
| 611 | // - Const method bound to WeakPtr<> to const object. |
| 612 | // - Normal Function with WeakPtr<> as P1 can have return type and is |
| 613 | // not canceled. |
| 614 | TEST_F(BindTest, WeakPtr) { |
| 615 | EXPECT_CALL(no_ref_, VoidMethod0()); |
| 616 | EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 617 | |
| 618 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 619 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 620 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 621 | Closure method_cb = |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 622 | Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
| 623 | method_cb.Run(); |
| 624 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 625 | Closure const_method_cb = |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 626 | Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 627 | const_method_cb.Run(); |
| 628 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 629 | Closure const_method_const_ptr_cb = |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 630 | Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 631 | const_method_const_ptr_cb.Run(); |
| 632 | |
| 633 | Callback<int(int)> normal_func_cb = |
| 634 | Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); |
| 635 | EXPECT_EQ(1, normal_func_cb.Run(1)); |
| 636 | |
| 637 | weak_factory.InvalidateWeakPtrs(); |
| 638 | const_weak_factory.InvalidateWeakPtrs(); |
| 639 | |
| 640 | method_cb.Run(); |
| 641 | const_method_cb.Run(); |
| 642 | const_method_const_ptr_cb.Run(); |
| 643 | |
| 644 | // Still runs even after the pointers are invalidated. |
| 645 | EXPECT_EQ(2, normal_func_cb.Run(2)); |
| 646 | } |
| 647 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 648 | // ConstRef() wrapper support. |
| 649 | // - Binding w/o ConstRef takes a copy. |
| 650 | // - Binding a ConstRef takes a reference. |
| 651 | // - Binding ConstRef to a function ConstRef does not copy on invoke. |
| 652 | TEST_F(BindTest, ConstRef) { |
| 653 | int n = 1; |
| 654 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 655 | Callback<int()> copy_cb = Bind(&Identity, n); |
| 656 | Callback<int()> const_ref_cb = Bind(&Identity, ConstRef(n)); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 657 | EXPECT_EQ(n, copy_cb.Run()); |
| 658 | EXPECT_EQ(n, const_ref_cb.Run()); |
| 659 | n++; |
| 660 | EXPECT_EQ(n - 1, copy_cb.Run()); |
| 661 | EXPECT_EQ(n, const_ref_cb.Run()); |
| 662 | |
| 663 | int copies = 0; |
| 664 | int assigns = 0; |
| 665 | CopyCounter counter(&copies, &assigns); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 666 | Callback<int()> all_const_ref_cb = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 667 | Bind(&GetCopies, ConstRef(counter)); |
| 668 | EXPECT_EQ(0, all_const_ref_cb.Run()); |
| 669 | EXPECT_EQ(0, copies); |
| 670 | EXPECT_EQ(0, assigns); |
| 671 | } |
| 672 | |
[email protected] | edd2f1b | 2013-06-22 20:32:50 | [diff] [blame] | 673 | TEST_F(BindTest, ScopedRefptr) { |
| 674 | // BUG: The scoped_refptr should cause the only AddRef()/Release() pair. But |
| 675 | // due to a bug in base::Bind(), there's an extra call when invoking the |
| 676 | // callback. |
| 677 | // https://code.google.com/p/chromium/issues/detail?id=251937 |
| 678 | EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 679 | EXPECT_CALL(has_ref_, Release()).Times(2); |
| 680 | |
| 681 | const scoped_refptr<StrictMock<HasRef> > refptr(&has_ref_); |
| 682 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 683 | Callback<int()> scoped_refptr_const_ref_cb = |
[email protected] | edd2f1b | 2013-06-22 20:32:50 | [diff] [blame] | 684 | Bind(&FunctionWithScopedRefptrFirstParam, base::ConstRef(refptr), 1); |
| 685 | EXPECT_EQ(1, scoped_refptr_const_ref_cb.Run()); |
| 686 | } |
| 687 | |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 688 | // Test Owned() support. |
| 689 | TEST_F(BindTest, Owned) { |
| 690 | int deletes = 0; |
| 691 | DeleteCounter* counter = new DeleteCounter(&deletes); |
| 692 | |
| 693 | // If we don't capture, delete happens on Callback destruction/reset. |
| 694 | // return the same value. |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 695 | Callback<DeleteCounter*()> no_capture_cb = |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 696 | Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 697 | ASSERT_EQ(counter, no_capture_cb.Run()); |
| 698 | ASSERT_EQ(counter, no_capture_cb.Run()); |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 699 | EXPECT_EQ(0, deletes); |
| 700 | no_capture_cb.Reset(); // This should trigger a delete. |
| 701 | EXPECT_EQ(1, deletes); |
| 702 | |
| 703 | deletes = 0; |
| 704 | counter = new DeleteCounter(&deletes); |
| 705 | base::Closure own_object_cb = |
| 706 | Bind(&DeleteCounter::VoidMethod0, Owned(counter)); |
| 707 | own_object_cb.Run(); |
| 708 | EXPECT_EQ(0, deletes); |
| 709 | own_object_cb.Reset(); |
| 710 | EXPECT_EQ(1, deletes); |
| 711 | } |
| 712 | |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 713 | // Passed() wrapper support. |
| 714 | // - Passed() can be constructed from a pointer to scoper. |
| 715 | // - Passed() can be constructed from a scoper rvalue. |
| 716 | // - Using Passed() gives Callback Ownership. |
| 717 | // - Ownership is transferred from Callback to callee on the first Run(). |
| 718 | // - Callback supports unbound arguments. |
| 719 | TEST_F(BindTest, ScopedPtr) { |
| 720 | int deletes = 0; |
| 721 | |
| 722 | // Tests the Passed() function's support for pointers. |
| 723 | scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes)); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 724 | Callback<scoped_ptr<DeleteCounter>()> unused_callback = |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 725 | Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr)); |
| 726 | EXPECT_FALSE(ptr.get()); |
| 727 | EXPECT_EQ(0, deletes); |
| 728 | |
| 729 | // If we never invoke the Callback, it retains ownership and deletes. |
| 730 | unused_callback.Reset(); |
| 731 | EXPECT_EQ(1, deletes); |
| 732 | |
| 733 | // Tests the Passed() function's support for rvalues. |
| 734 | deletes = 0; |
| 735 | DeleteCounter* counter = new DeleteCounter(&deletes); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 736 | Callback<scoped_ptr<DeleteCounter>()> callback = |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 737 | Bind(&PassThru<scoped_ptr<DeleteCounter> >, |
| 738 | Passed(scoped_ptr<DeleteCounter>(counter))); |
| 739 | EXPECT_FALSE(ptr.get()); |
| 740 | EXPECT_EQ(0, deletes); |
| 741 | |
| 742 | // Check that ownership can be transferred back out. |
| 743 | scoped_ptr<DeleteCounter> result = callback.Run(); |
| 744 | ASSERT_EQ(counter, result.get()); |
| 745 | EXPECT_EQ(0, deletes); |
| 746 | |
| 747 | // Resetting does not delete since ownership was transferred. |
| 748 | callback.Reset(); |
| 749 | EXPECT_EQ(0, deletes); |
| 750 | |
| 751 | // Ensure that we actually did get ownership. |
| 752 | result.reset(); |
| 753 | EXPECT_EQ(1, deletes); |
| 754 | |
| 755 | // Test unbound argument forwarding. |
| 756 | Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound = |
| 757 | Bind(&PassThru<scoped_ptr<DeleteCounter> >); |
| 758 | ptr.reset(new DeleteCounter(&deletes)); |
dcheng | 9dfa123 | 2015-12-15 05:11:17 | [diff] [blame] | 759 | cb_unbound.Run(std::move(ptr)); |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 760 | } |
| 761 | |
dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 762 | TEST_F(BindTest, UniquePtr) { |
| 763 | int deletes = 0; |
| 764 | |
| 765 | // Tests the Passed() function's support for pointers. |
| 766 | std::unique_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes)); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 767 | Callback<std::unique_ptr<DeleteCounter>()> unused_callback = |
dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 768 | Bind(&PassThru<std::unique_ptr<DeleteCounter>>, Passed(&ptr)); |
| 769 | EXPECT_FALSE(ptr.get()); |
| 770 | EXPECT_EQ(0, deletes); |
| 771 | |
| 772 | // If we never invoke the Callback, it retains ownership and deletes. |
| 773 | unused_callback.Reset(); |
| 774 | EXPECT_EQ(1, deletes); |
| 775 | |
| 776 | // Tests the Passed() function's support for rvalues. |
| 777 | deletes = 0; |
| 778 | DeleteCounter* counter = new DeleteCounter(&deletes); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 779 | Callback<std::unique_ptr<DeleteCounter>()> callback = |
dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 780 | Bind(&PassThru<std::unique_ptr<DeleteCounter>>, |
| 781 | Passed(std::unique_ptr<DeleteCounter>(counter))); |
| 782 | EXPECT_FALSE(ptr.get()); |
| 783 | EXPECT_EQ(0, deletes); |
| 784 | |
| 785 | // Check that ownership can be transferred back out. |
| 786 | std::unique_ptr<DeleteCounter> result = callback.Run(); |
| 787 | ASSERT_EQ(counter, result.get()); |
| 788 | EXPECT_EQ(0, deletes); |
| 789 | |
| 790 | // Resetting does not delete since ownership was transferred. |
| 791 | callback.Reset(); |
| 792 | EXPECT_EQ(0, deletes); |
| 793 | |
| 794 | // Ensure that we actually did get ownership. |
| 795 | result.reset(); |
| 796 | EXPECT_EQ(1, deletes); |
| 797 | |
| 798 | // Test unbound argument forwarding. |
| 799 | Callback<std::unique_ptr<DeleteCounter>(std::unique_ptr<DeleteCounter>)> |
| 800 | cb_unbound = Bind(&PassThru<std::unique_ptr<DeleteCounter>>); |
| 801 | ptr.reset(new DeleteCounter(&deletes)); |
| 802 | cb_unbound.Run(std::move(ptr)); |
| 803 | } |
| 804 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 805 | // Argument Copy-constructor usage for non-reference parameters. |
| 806 | // - Bound arguments are only copied once. |
| 807 | // - Forwarded arguments are only copied once. |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 808 | // - Forwarded arguments with coercions are only copied twice (once for the |
| 809 | // coercion, and one for the final dispatch). |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 810 | TEST_F(BindTest, ArgumentCopies) { |
| 811 | int copies = 0; |
| 812 | int assigns = 0; |
| 813 | |
| 814 | CopyCounter counter(&copies, &assigns); |
| 815 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 816 | Callback<void()> copy_cb = |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 817 | Bind(&VoidPolymorphic<CopyCounter>::Run, counter); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 818 | EXPECT_GE(1, copies); |
| 819 | EXPECT_EQ(0, assigns); |
| 820 | |
| 821 | copies = 0; |
| 822 | assigns = 0; |
| 823 | Callback<void(CopyCounter)> forward_cb = |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 824 | Bind(&VoidPolymorphic<CopyCounter>::Run); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 825 | forward_cb.Run(counter); |
| 826 | EXPECT_GE(1, copies); |
| 827 | EXPECT_EQ(0, assigns); |
| 828 | |
| 829 | copies = 0; |
| 830 | assigns = 0; |
thakis | b25789fb | 2015-04-23 05:40:02 | [diff] [blame] | 831 | DerivedCopyCounter derived(&copies, &assigns); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 832 | Callback<void(CopyCounter)> coerce_cb = |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 833 | Bind(&VoidPolymorphic<CopyCounter>::Run); |
thakis | b25789fb | 2015-04-23 05:40:02 | [diff] [blame] | 834 | coerce_cb.Run(CopyCounter(derived)); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 835 | EXPECT_GE(2, copies); |
| 836 | EXPECT_EQ(0, assigns); |
| 837 | } |
| 838 | |
| 839 | // Callback construction and assignment tests. |
| 840 | // - Construction from an InvokerStorageHolder should not cause ref/deref. |
| 841 | // - Assignment from other callback should only cause one ref |
| 842 | // |
| 843 | // TODO(ajwong): Is there actually a way to test this? |
| 844 | |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 845 | #if defined(OS_WIN) |
| 846 | int __fastcall FastCallFunc(int n) { |
| 847 | return n; |
| 848 | } |
| 849 | |
| 850 | int __stdcall StdCallFunc(int n) { |
| 851 | return n; |
| 852 | } |
| 853 | |
| 854 | // Windows specific calling convention support. |
| 855 | // - Can bind a __fastcall function. |
| 856 | // - Can bind a __stdcall function. |
| 857 | TEST_F(BindTest, WindowsCallingConventions) { |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 858 | Callback<int()> fastcall_cb = Bind(&FastCallFunc, 1); |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 859 | EXPECT_EQ(1, fastcall_cb.Run()); |
| 860 | |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame^] | 861 | Callback<int()> stdcall_cb = Bind(&StdCallFunc, 2); |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 862 | EXPECT_EQ(2, stdcall_cb.Run()); |
| 863 | } |
| 864 | #endif |
| 865 | |
[email protected] | 8cf362c | 2012-11-20 08:28:14 | [diff] [blame] | 866 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
| 867 | |
| 868 | // Test null callbacks cause a DCHECK. |
| 869 | TEST(BindDeathTest, NullCallback) { |
| 870 | base::Callback<void(int)> null_cb; |
| 871 | ASSERT_TRUE(null_cb.is_null()); |
| 872 | EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
| 873 | } |
| 874 | |
| 875 | #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
| 876 | // GTEST_HAS_DEATH_TEST |
| 877 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 878 | } // namespace |
| 879 | } // namespace base |