| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16); | 253 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16); |
| 254 EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); | 254 EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); |
| 255 | 255 |
| 256 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32); | 256 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32); |
| 257 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); | 257 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); |
| 258 | 258 |
| 259 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum); | 259 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum); |
| 260 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); | 260 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); |
| 261 } | 261 } |
| 262 | 262 |
| 263 // Bind should be able to take existing Callbacks and convert to a Closure. | 263 // Test the Currying ability of the Callback system. |
| 264 TEST_F(BindTest, CallbackBindMore) { | 264 TEST_F(BindTest, CurryingTest) { |
| 265 int output = 0; | 265 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum); |
| 266 Closure c; | 266 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); |
| 267 | 267 |
| 268 Callback<void(int)> c1 = Bind(&OutputSum, &output, 16, 8, 4, 2); | 268 Callback<int(int,int,int,int,int)> c5 = Bind(c6, 32); |
| 269 c = Bind(c1, 10); | 269 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); |
| 270 c.Run(); | |
| 271 EXPECT_EQ(40, output); | |
| 272 | 270 |
| 273 Callback<void(int,int)> c2 = Bind(&OutputSum, &output, 16, 8, 4); | 271 Callback<int(int,int,int,int)> c4 = Bind(c5, 16); |
| 274 c = Bind(c2, 10, 9); | 272 EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); |
| 275 c.Run(); | |
| 276 EXPECT_EQ(47, output); | |
| 277 | 273 |
| 278 Callback<void(int,int,int)> c3 = Bind(&OutputSum, &output, 16, 8); | 274 Callback<int(int,int,int)> c3 = Bind(c4, 8); |
| 279 c = Bind(c3, 10, 9, 8); | 275 EXPECT_EQ(92, c3.Run(13, 12, 11)); |
| 280 c.Run(); | |
| 281 EXPECT_EQ(51, output); | |
| 282 | 276 |
| 283 Callback<void(int,int,int,int)> c4 = Bind(&OutputSum, &output, 16); | 277 Callback<int(int,int)> c2 = Bind(c3, 4); |
| 284 c = Bind(c4, 10, 9, 8, 7); | 278 EXPECT_EQ(85, c2.Run(13, 12)); |
| 285 c.Run(); | |
| 286 EXPECT_EQ(50, output); | |
| 287 | 279 |
| 288 Callback<void(int,int,int,int,int)> c5 = Bind(&OutputSum, &output); | 280 Callback<int(int)> c1 = Bind(c2, 2); |
| 289 c = Bind(c5, 10, 9, 8, 7, 6); | 281 EXPECT_EQ(75, c1.Run(13)); |
| 290 c.Run(); | 282 |
| 291 EXPECT_EQ(40, output); | 283 Callback<int(void)> c0 = Bind(c1, 1); |
| 284 EXPECT_EQ(63, c0.Run()); |
| 292 } | 285 } |
| 293 | 286 |
| 294 // Function type support. | 287 // Function type support. |
| 295 // - Normal function. | 288 // - Normal function. |
| 296 // - Normal function bound with non-refcounted first argument. | 289 // - Normal function bound with non-refcounted first argument. |
| 297 // - Method bound to non-const object. | 290 // - Method bound to non-const object. |
| 298 // - Method bound to scoped_refptr. | 291 // - Method bound to scoped_refptr. |
| 299 // - Const method bound to non-const object. | 292 // - Const method bound to non-const object. |
| 300 // - Const method bound to const object. | 293 // - Const method bound to const object. |
| 301 // - Derived classes can be used with pointers to non-virtual base functions. | 294 // - Derived classes can be used with pointers to non-virtual base functions. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 Callback<int(void)> const_method_nonconst_obj_cb = | 349 Callback<int(void)> const_method_nonconst_obj_cb = |
| 357 Bind(&HasRef::IntConstMethod0, &has_ref_); | 350 Bind(&HasRef::IntConstMethod0, &has_ref_); |
| 358 Callback<int(void)> const_method_const_obj_cb = | 351 Callback<int(void)> const_method_const_obj_cb = |
| 359 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_); | 352 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_); |
| 360 EXPECT_EQ(1337, normal_cb.Run()); | 353 EXPECT_EQ(1337, normal_cb.Run()); |
| 361 EXPECT_EQ(31337, method_cb.Run()); | 354 EXPECT_EQ(31337, method_cb.Run()); |
| 362 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run()); | 355 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run()); |
| 363 EXPECT_EQ(51337, const_method_const_obj_cb.Run()); | 356 EXPECT_EQ(51337, const_method_const_obj_cb.Run()); |
| 364 } | 357 } |
| 365 | 358 |
| 366 // IgnoreReturn adapter test. | 359 // IgnoreResult adapter test. |
| 367 // - Function with return value, and no params can be converted to Closure. | 360 // - Function with return value. |
| 368 TEST_F(BindTest, IgnoreReturn) { | 361 // - Method with return value. |
| 362 // - Const Method with return. |
| 363 // - Method with return value bound to WeakPtr<>. |
| 364 // - Const Method with return bound to WeakPtr<>. |
| 365 TEST_F(BindTest, IgnoreResult) { |
| 369 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); | 366 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
| 370 Callback<int(void)> normal_cb = Bind(&IntFunc0); | 367 EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 371 Closure c = IgnoreReturn(normal_cb); | 368 EXPECT_CALL(has_ref_, Release()).Times(2); |
| 372 c.Run(); | 369 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10)); |
| 370 EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11)); |
| 371 EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12)); |
| 372 EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13)); |
| 373 |
| 374 Closure normal_func_cb = Bind(IgnoreResult(&IntFunc0)); |
| 375 normal_func_cb.Run(); |
| 376 |
| 377 Closure non_void_method_cb = |
| 378 Bind(IgnoreResult(&HasRef::IntMethod0), &has_ref_); |
| 379 non_void_method_cb.Run(); |
| 380 |
| 381 Closure non_void_const_method_cb = |
| 382 Bind(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_); |
| 383 non_void_const_method_cb.Run(); |
| 384 |
| 385 WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 386 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 387 |
| 388 Closure non_void_weak_method_cb = |
| 389 Bind(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr()); |
| 390 non_void_weak_method_cb.Run(); |
| 391 |
| 392 Closure non_void_weak_const_method_cb = |
| 393 Bind(IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr()); |
| 394 non_void_weak_const_method_cb.Run(); |
| 395 |
| 396 weak_factory.InvalidateWeakPtrs(); |
| 397 non_void_weak_const_method_cb.Run(); |
| 398 non_void_weak_method_cb.Run(); |
| 373 } | 399 } |
| 374 | 400 |
| 375 // Argument binding tests. | 401 // Argument binding tests. |
| 376 // - Argument binding to primitive. | 402 // - Argument binding to primitive. |
| 377 // - Argument binding to primitive pointer. | 403 // - Argument binding to primitive pointer. |
| 378 // - Argument binding to a literal integer. | 404 // - Argument binding to a literal integer. |
| 379 // - Argument binding to a literal string. | 405 // - Argument binding to a literal string. |
| 380 // - Argument binding with template function. | 406 // - Argument binding with template function. |
| 381 // - Argument binding to an object. | 407 // - Argument binding to an object. |
| 382 // - Argument binding to pointer to incomplete type. | 408 // - Argument binding to pointer to incomplete type. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 Bind(&VoidPolymorphic1<const int&>); | 472 Bind(&VoidPolymorphic1<const int&>); |
| 447 Callback<void(int[])> unbound_unsized_array_cb = | 473 Callback<void(int[])> unbound_unsized_array_cb = |
| 448 Bind(&VoidPolymorphic1<int[]>); | 474 Bind(&VoidPolymorphic1<int[]>); |
| 449 Callback<void(int[2])> unbound_sized_array_cb = | 475 Callback<void(int[2])> unbound_sized_array_cb = |
| 450 Bind(&VoidPolymorphic1<int[2]>); | 476 Bind(&VoidPolymorphic1<int[2]>); |
| 451 Callback<void(int[][2])> unbound_array_of_arrays_cb = | 477 Callback<void(int[][2])> unbound_array_of_arrays_cb = |
| 452 Bind(&VoidPolymorphic1<int[][2]>); | 478 Bind(&VoidPolymorphic1<int[][2]>); |
| 453 } | 479 } |
| 454 | 480 |
| 455 // Function with unbound reference parameter. | 481 // Function with unbound reference parameter. |
| 456 // - Original paraemter is modified by callback. | 482 // - Original parameter is modified by callback. |
| 457 TEST_F(BindTest, UnboundReferenceSupport) { | 483 TEST_F(BindTest, UnboundReferenceSupport) { |
| 458 int n = 0; | 484 int n = 0; |
| 459 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet); | 485 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet); |
| 460 unbound_ref_cb.Run(n); | 486 unbound_ref_cb.Run(n); |
| 461 EXPECT_EQ(2, n); | 487 EXPECT_EQ(2, n); |
| 462 } | 488 } |
| 463 | 489 |
| 464 // Functions that take reference parameters. | 490 // Functions that take reference parameters. |
| 465 // - Forced reference parameter type still stores a copy. | 491 // - Forced reference parameter type still stores a copy. |
| 466 // - Forced const reference parameter type still stores a copy. | 492 // - Forced const reference parameter type still stores a copy. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 // - Const method bound to WeakPtr<> to const object. | 573 // - Const method bound to WeakPtr<> to const object. |
| 548 // - Normal Function with WeakPtr<> as P1 can have return type and is | 574 // - Normal Function with WeakPtr<> as P1 can have return type and is |
| 549 // not canceled. | 575 // not canceled. |
| 550 TEST_F(BindTest, WeakPtr) { | 576 TEST_F(BindTest, WeakPtr) { |
| 551 EXPECT_CALL(no_ref_, VoidMethod0()); | 577 EXPECT_CALL(no_ref_, VoidMethod0()); |
| 552 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); | 578 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 553 | 579 |
| 554 WeakPtrFactory<NoRef> weak_factory(&no_ref_); | 580 WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 555 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); | 581 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 556 | 582 |
| 557 Callback<void(void)> method_cb = | 583 Closure method_cb = |
| 558 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); | 584 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
| 559 method_cb.Run(); | 585 method_cb.Run(); |
| 560 | 586 |
| 561 Callback<void(void)> const_method_cb = | 587 Closure const_method_cb = |
| 562 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); | 588 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 563 const_method_cb.Run(); | 589 const_method_cb.Run(); |
| 564 | 590 |
| 565 Callback<void(void)> const_method_const_ptr_cb = | 591 Closure const_method_const_ptr_cb = |
| 566 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); | 592 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 567 const_method_const_ptr_cb.Run(); | 593 const_method_const_ptr_cb.Run(); |
| 568 | 594 |
| 569 Callback<int(int)> normal_func_cb = | 595 Callback<int(int)> normal_func_cb = |
| 570 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); | 596 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); |
| 571 EXPECT_EQ(1, normal_func_cb.Run(1)); | 597 EXPECT_EQ(1, normal_func_cb.Run(1)); |
| 572 | 598 |
| 573 weak_factory.InvalidateWeakPtrs(); | 599 weak_factory.InvalidateWeakPtrs(); |
| 574 const_weak_factory.InvalidateWeakPtrs(); | 600 const_weak_factory.InvalidateWeakPtrs(); |
| 575 | 601 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 713 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
| 688 EXPECT_EQ(1, fastcall_cb.Run()); | 714 EXPECT_EQ(1, fastcall_cb.Run()); |
| 689 | 715 |
| 690 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 716 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
| 691 EXPECT_EQ(2, stdcall_cb.Run()); | 717 EXPECT_EQ(2, stdcall_cb.Run()); |
| 692 } | 718 } |
| 693 #endif | 719 #endif |
| 694 | 720 |
| 695 } // namespace | 721 } // namespace |
| 696 } // namespace base | 722 } // namespace base |
| OLD | NEW |