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