OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind_lambda.h" |
7 #include "base/callback.h" | 8 #include "base/callback.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 using ::testing::Mock; | 15 using ::testing::Mock; |
15 using ::testing::Return; | 16 using ::testing::Return; |
16 using ::testing::StrictMock; | 17 using ::testing::StrictMock; |
(...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 copies = 0; | 780 copies = 0; |
780 assigns = 0; | 781 assigns = 0; |
781 DerivedCopyCounter dervied(&copies, &assigns); | 782 DerivedCopyCounter dervied(&copies, &assigns); |
782 Callback<void(CopyCounter)> coerce_cb = | 783 Callback<void(CopyCounter)> coerce_cb = |
783 Bind(&VoidPolymorphic1<CopyCounter>); | 784 Bind(&VoidPolymorphic1<CopyCounter>); |
784 coerce_cb.Run(CopyCounter(dervied)); | 785 coerce_cb.Run(CopyCounter(dervied)); |
785 EXPECT_GE(2, copies); | 786 EXPECT_GE(2, copies); |
786 EXPECT_EQ(0, assigns); | 787 EXPECT_EQ(0, assigns); |
787 } | 788 } |
788 | 789 |
| 790 TEST_F(BindTest, Lambdas) { |
| 791 static const int kValueToSet = 55; |
| 792 int value_set_from_lambda = 0; |
| 793 |
| 794 Callback<void(int)> callback = |
| 795 BindLambda([&](int new_val) { value_set_from_lambda = new_val; }); |
| 796 callback.Run(kValueToSet); |
| 797 EXPECT_EQ(kValueToSet, value_set_from_lambda); |
| 798 } |
| 799 |
| 800 TEST_F(BindTest, LambdasNestedInBind) { |
| 801 static const int kValueToSet = 55; |
| 802 int value_set_from_lambda = 0; |
| 803 |
| 804 Callback<void()> callback = |
| 805 Bind(BindLambda([&](int new_val) { value_set_from_lambda = new_val; }), |
| 806 kValueToSet); |
| 807 callback.Run(); |
| 808 EXPECT_EQ(kValueToSet, value_set_from_lambda); |
| 809 } |
| 810 |
789 // Callback construction and assignment tests. | 811 // Callback construction and assignment tests. |
790 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 812 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
791 // - Assignment from other callback should only cause one ref | 813 // - Assignment from other callback should only cause one ref |
792 // | 814 // |
793 // TODO(ajwong): Is there actually a way to test this? | 815 // TODO(ajwong): Is there actually a way to test this? |
794 | 816 |
795 #if defined(OS_WIN) | 817 #if defined(OS_WIN) |
796 int __fastcall FastCallFunc(int n) { | 818 int __fastcall FastCallFunc(int n) { |
797 return n; | 819 return n; |
798 } | 820 } |
(...skipping 21 matching lines...) Expand all Loading... |
820 base::Callback<void(int)> null_cb; | 842 base::Callback<void(int)> null_cb; |
821 ASSERT_TRUE(null_cb.is_null()); | 843 ASSERT_TRUE(null_cb.is_null()); |
822 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 844 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
823 } | 845 } |
824 | 846 |
825 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 847 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
826 // GTEST_HAS_DEATH_TEST | 848 // GTEST_HAS_DEATH_TEST |
827 | 849 |
828 } // namespace | 850 } // namespace |
829 } // namespace base | 851 } // namespace base |
OLD | NEW |