| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/test/gtest_util.h" | 16 #include "base/test/gtest_util.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using ::testing::_; |
| 21 using ::testing::Mock; | 22 using ::testing::Mock; |
| 22 using ::testing::Return; | 23 using ::testing::Return; |
| 23 using ::testing::StrictMock; | 24 using ::testing::StrictMock; |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 class IncompleteType; | 29 class IncompleteType; |
| 29 | 30 |
| 30 class NoRef { | 31 class NoRef { |
| 31 public: | 32 public: |
| 32 NoRef() {} | 33 NoRef() {} |
| 33 | 34 |
| 34 MOCK_METHOD0(VoidMethod0, void()); | 35 MOCK_METHOD0(VoidMethod0, void()); |
| 35 MOCK_CONST_METHOD0(VoidConstMethod0, void()); | 36 MOCK_CONST_METHOD0(VoidConstMethod0, void()); |
| 36 | 37 |
| 37 MOCK_METHOD0(IntMethod0, int()); | 38 MOCK_METHOD0(IntMethod0, int()); |
| 38 MOCK_CONST_METHOD0(IntConstMethod0, int()); | 39 MOCK_CONST_METHOD0(IntConstMethod0, int()); |
| 39 | 40 |
| 41 MOCK_METHOD1(VoidMethodWithIntArg, void(int)); |
| 42 |
| 40 private: | 43 private: |
| 41 // Particularly important in this test to ensure no copies are made. | 44 // Particularly important in this test to ensure no copies are made. |
| 42 DISALLOW_COPY_AND_ASSIGN(NoRef); | 45 DISALLOW_COPY_AND_ASSIGN(NoRef); |
| 43 }; | 46 }; |
| 44 | 47 |
| 45 class HasRef : public NoRef { | 48 class HasRef : public NoRef { |
| 46 public: | 49 public: |
| 47 HasRef() {} | 50 HasRef() {} |
| 48 | 51 |
| 49 MOCK_CONST_METHOD0(AddRef, void()); | 52 MOCK_CONST_METHOD0(AddRef, void()); |
| (...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 int x = 1; | 1031 int x = 1; |
| 1029 base::Callback<void(int)> cb = | 1032 base::Callback<void(int)> cb = |
| 1030 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); | 1033 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); |
| 1031 cb.Run(6); | 1034 cb.Run(6); |
| 1032 EXPECT_EQ(6, x); | 1035 EXPECT_EQ(6, x); |
| 1033 cb.Run(7); | 1036 cb.Run(7); |
| 1034 EXPECT_EQ(42, x); | 1037 EXPECT_EQ(42, x); |
| 1035 } | 1038 } |
| 1036 | 1039 |
| 1037 TEST_F(BindTest, Cancellation) { | 1040 TEST_F(BindTest, Cancellation) { |
| 1038 EXPECT_CALL(no_ref_, VoidMethod0()).Times(2); | 1041 EXPECT_CALL(no_ref_, VoidMethodWithIntArg(_)).Times(2); |
| 1039 | 1042 |
| 1040 WeakPtrFactory<NoRef> weak_factory(&no_ref_); | 1043 WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 1041 Closure cb = Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); | 1044 base::Callback<void(int)> cb = |
| 1042 Closure cb2 = Bind(cb); | 1045 Bind(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); |
| 1046 Closure cb2 = Bind(cb, 8); |
| 1043 | 1047 |
| 1044 EXPECT_FALSE(cb.IsCancelled()); | 1048 EXPECT_FALSE(cb.IsCancelled()); |
| 1045 EXPECT_FALSE(cb2.IsCancelled()); | 1049 EXPECT_FALSE(cb2.IsCancelled()); |
| 1046 | 1050 |
| 1047 cb.Run(); | 1051 cb.Run(6); |
| 1048 cb2.Run(); | 1052 cb2.Run(); |
| 1049 | 1053 |
| 1050 weak_factory.InvalidateWeakPtrs(); | 1054 weak_factory.InvalidateWeakPtrs(); |
| 1051 | 1055 |
| 1052 EXPECT_TRUE(cb.IsCancelled()); | 1056 EXPECT_TRUE(cb.IsCancelled()); |
| 1053 EXPECT_TRUE(cb2.IsCancelled()); | 1057 EXPECT_TRUE(cb2.IsCancelled()); |
| 1054 | 1058 |
| 1055 cb.Run(); | 1059 cb.Run(6); |
| 1056 cb2.Run(); | 1060 cb2.Run(); |
| 1057 } | 1061 } |
| 1058 | 1062 |
| 1059 TEST_F(BindTest, OnceCallback) { | 1063 TEST_F(BindTest, OnceCallback) { |
| 1060 using internal::OnceClosure; | 1064 using internal::OnceClosure; |
| 1061 using internal::RepeatingClosure; | 1065 using internal::RepeatingClosure; |
| 1062 using internal::BindOnce; | 1066 using internal::BindOnce; |
| 1063 using internal::BindRepeating; | 1067 using internal::BindRepeating; |
| 1064 using internal::OnceCallback; | 1068 using internal::OnceCallback; |
| 1065 | 1069 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1175 | 1179 |
| 1176 // Test null callbacks cause a DCHECK. | 1180 // Test null callbacks cause a DCHECK. |
| 1177 TEST(BindDeathTest, NullCallback) { | 1181 TEST(BindDeathTest, NullCallback) { |
| 1178 base::Callback<void(int)> null_cb; | 1182 base::Callback<void(int)> null_cb; |
| 1179 ASSERT_TRUE(null_cb.is_null()); | 1183 ASSERT_TRUE(null_cb.is_null()); |
| 1180 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); | 1184 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); |
| 1181 } | 1185 } |
| 1182 | 1186 |
| 1183 } // namespace | 1187 } // namespace |
| 1184 } // namespace base | 1188 } // namespace base |
| OLD | NEW |