| 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 |
| (...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1027 | 1027 |
| 1028 int x = 1; | 1028 int x = 1; |
| 1029 base::Callback<void(int)> cb = | 1029 base::Callback<void(int)> cb = |
| 1030 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); | 1030 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); |
| 1031 cb.Run(6); | 1031 cb.Run(6); |
| 1032 EXPECT_EQ(6, x); | 1032 EXPECT_EQ(6, x); |
| 1033 cb.Run(7); | 1033 cb.Run(7); |
| 1034 EXPECT_EQ(42, x); | 1034 EXPECT_EQ(42, x); |
| 1035 } | 1035 } |
| 1036 | 1036 |
| 1037 TEST_F(BindTest, Cancellation) { |
| 1038 EXPECT_CALL(no_ref_, VoidMethod0()).Times(2); |
| 1039 |
| 1040 WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 1041 Closure cb = Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
| 1042 Closure cb2 = Bind(cb); |
| 1043 |
| 1044 EXPECT_FALSE(cb.IsCancelled()); |
| 1045 EXPECT_FALSE(cb2.IsCancelled()); |
| 1046 |
| 1047 cb.Run(); |
| 1048 cb2.Run(); |
| 1049 |
| 1050 weak_factory.InvalidateWeakPtrs(); |
| 1051 |
| 1052 EXPECT_TRUE(cb.IsCancelled()); |
| 1053 EXPECT_TRUE(cb2.IsCancelled()); |
| 1054 |
| 1055 cb.Run(); |
| 1056 cb2.Run(); |
| 1057 } |
| 1058 |
| 1037 // Callback construction and assignment tests. | 1059 // Callback construction and assignment tests. |
| 1038 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 1060 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
| 1039 // - Assignment from other callback should only cause one ref | 1061 // - Assignment from other callback should only cause one ref |
| 1040 // | 1062 // |
| 1041 // TODO(ajwong): Is there actually a way to test this? | 1063 // TODO(ajwong): Is there actually a way to test this? |
| 1042 | 1064 |
| 1043 #if defined(OS_WIN) | 1065 #if defined(OS_WIN) |
| 1044 int __fastcall FastCallFunc(int n) { | 1066 int __fastcall FastCallFunc(int n) { |
| 1045 return n; | 1067 return n; |
| 1046 } | 1068 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1063 | 1085 |
| 1064 // Test null callbacks cause a DCHECK. | 1086 // Test null callbacks cause a DCHECK. |
| 1065 TEST(BindDeathTest, NullCallback) { | 1087 TEST(BindDeathTest, NullCallback) { |
| 1066 base::Callback<void(int)> null_cb; | 1088 base::Callback<void(int)> null_cb; |
| 1067 ASSERT_TRUE(null_cb.is_null()); | 1089 ASSERT_TRUE(null_cb.is_null()); |
| 1068 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); | 1090 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); |
| 1069 } | 1091 } |
| 1070 | 1092 |
| 1071 } // namespace | 1093 } // namespace |
| 1072 } // namespace base | 1094 } // namespace base |
| OLD | NEW |