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::_; |
22 using ::testing::Mock; | 22 using ::testing::Mock; |
23 using ::testing::Return; | 23 using ::testing::Return; |
24 using ::testing::StrictMock; | 24 using ::testing::StrictMock; |
25 | 25 |
26 namespace base { | 26 namespace base { |
| 27 |
| 28 using internal::OnceCallback; |
| 29 using internal::RepeatingCallback; |
| 30 using internal::OnceClosure; |
| 31 using internal::RepeatingClosure; |
| 32 using internal::BindOnce; |
| 33 using internal::BindRepeating; |
| 34 |
27 namespace { | 35 namespace { |
28 | 36 |
29 class IncompleteType; | 37 class IncompleteType; |
30 | 38 |
31 class NoRef { | 39 class NoRef { |
32 public: | 40 public: |
33 NoRef() {} | 41 NoRef() {} |
34 | 42 |
35 MOCK_METHOD0(VoidMethod0, void()); | 43 MOCK_METHOD0(VoidMethod0, void()); |
36 MOCK_CONST_METHOD0(VoidConstMethod0, void()); | 44 MOCK_CONST_METHOD0(VoidConstMethod0, void()); |
(...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 cb.Run(6); | 1042 cb.Run(6); |
1035 EXPECT_EQ(6, x); | 1043 EXPECT_EQ(6, x); |
1036 cb.Run(7); | 1044 cb.Run(7); |
1037 EXPECT_EQ(42, x); | 1045 EXPECT_EQ(42, x); |
1038 } | 1046 } |
1039 | 1047 |
1040 TEST_F(BindTest, Cancellation) { | 1048 TEST_F(BindTest, Cancellation) { |
1041 EXPECT_CALL(no_ref_, VoidMethodWithIntArg(_)).Times(2); | 1049 EXPECT_CALL(no_ref_, VoidMethodWithIntArg(_)).Times(2); |
1042 | 1050 |
1043 WeakPtrFactory<NoRef> weak_factory(&no_ref_); | 1051 WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
1044 base::Callback<void(int)> cb = | 1052 RepeatingCallback<void(int)> cb = |
1045 Bind(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); | 1053 BindRepeating(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); |
1046 Closure cb2 = Bind(cb, 8); | 1054 RepeatingClosure cb2 = BindRepeating(cb, 8); |
| 1055 OnceClosure cb3 = BindOnce(cb, 8); |
| 1056 |
| 1057 OnceCallback<void(int)> cb4 = |
| 1058 BindOnce(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); |
| 1059 EXPECT_FALSE(cb4.IsCancelled()); |
| 1060 |
| 1061 OnceClosure cb5 = BindOnce(std::move(cb4), 8); |
1047 | 1062 |
1048 EXPECT_FALSE(cb.IsCancelled()); | 1063 EXPECT_FALSE(cb.IsCancelled()); |
1049 EXPECT_FALSE(cb2.IsCancelled()); | 1064 EXPECT_FALSE(cb2.IsCancelled()); |
| 1065 EXPECT_FALSE(cb3.IsCancelled()); |
| 1066 EXPECT_FALSE(cb5.IsCancelled()); |
1050 | 1067 |
1051 cb.Run(6); | 1068 cb.Run(6); |
1052 cb2.Run(); | 1069 cb2.Run(); |
1053 | 1070 |
1054 weak_factory.InvalidateWeakPtrs(); | 1071 weak_factory.InvalidateWeakPtrs(); |
1055 | 1072 |
1056 EXPECT_TRUE(cb.IsCancelled()); | 1073 EXPECT_TRUE(cb.IsCancelled()); |
1057 EXPECT_TRUE(cb2.IsCancelled()); | 1074 EXPECT_TRUE(cb2.IsCancelled()); |
| 1075 EXPECT_TRUE(cb3.IsCancelled()); |
| 1076 EXPECT_TRUE(cb5.IsCancelled()); |
1058 | 1077 |
1059 cb.Run(6); | 1078 cb.Run(6); |
1060 cb2.Run(); | 1079 cb2.Run(); |
| 1080 std::move(cb3).Run(); |
| 1081 std::move(cb5).Run(); |
1061 } | 1082 } |
1062 | 1083 |
1063 TEST_F(BindTest, OnceCallback) { | 1084 TEST_F(BindTest, OnceCallback) { |
1064 using internal::OnceClosure; | 1085 using internal::OnceClosure; |
1065 using internal::RepeatingClosure; | 1086 using internal::RepeatingClosure; |
1066 using internal::BindOnce; | 1087 using internal::BindOnce; |
1067 using internal::BindRepeating; | 1088 using internal::BindRepeating; |
1068 using internal::OnceCallback; | 1089 using internal::OnceCallback; |
1069 | 1090 |
1070 // Check if Callback variants have declarations of conversions as expected. | 1091 // Check if Callback variants have declarations of conversions as expected. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1179 | 1200 |
1180 // Test null callbacks cause a DCHECK. | 1201 // Test null callbacks cause a DCHECK. |
1181 TEST(BindDeathTest, NullCallback) { | 1202 TEST(BindDeathTest, NullCallback) { |
1182 base::Callback<void(int)> null_cb; | 1203 base::Callback<void(int)> null_cb; |
1183 ASSERT_TRUE(null_cb.is_null()); | 1204 ASSERT_TRUE(null_cb.is_null()); |
1184 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); | 1205 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); |
1185 } | 1206 } |
1186 | 1207 |
1187 } // namespace | 1208 } // namespace |
1188 } // namespace base | 1209 } // namespace base |
OLD | NEW |