| 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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 } | 192 } |
| 193 | 193 |
| 194 int UnwrapNoRefParentConstRef(const NoRefParent& p) { | 194 int UnwrapNoRefParentConstRef(const NoRefParent& p) { |
| 195 return p.value; | 195 return p.value; |
| 196 } | 196 } |
| 197 | 197 |
| 198 void RefArgSet(int &n) { | 198 void RefArgSet(int &n) { |
| 199 n = 2; | 199 n = 2; |
| 200 } | 200 } |
| 201 | 201 |
| 202 void PtrArgSet(int *n) { | |
| 203 *n = 2; | |
| 204 } | |
| 205 | |
| 206 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { | 202 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
| 207 return n; | 203 return n; |
| 208 } | 204 } |
| 209 | 205 |
| 210 void TakesACallback(const Closure& callback) { | |
| 211 callback.Run(); | |
| 212 } | |
| 213 | |
| 214 class BindTest : public ::testing::Test { | 206 class BindTest : public ::testing::Test { |
| 215 public: | 207 public: |
| 216 BindTest() { | 208 BindTest() { |
| 217 const_has_ref_ptr_ = &has_ref_; | 209 const_has_ref_ptr_ = &has_ref_; |
| 218 const_no_ref_ptr_ = &no_ref_; | 210 const_no_ref_ptr_ = &no_ref_; |
| 219 static_func_mock_ptr = &static_func_mock_; | 211 static_func_mock_ptr = &static_func_mock_; |
| 220 } | 212 } |
| 221 | 213 |
| 222 virtual ~BindTest() { | 214 virtual ~BindTest() { |
| 223 } | 215 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 Callback<int(int,int)> c2 = Bind(c3, 4); | 277 Callback<int(int,int)> c2 = Bind(c3, 4); |
| 286 EXPECT_EQ(85, c2.Run(13, 12)); | 278 EXPECT_EQ(85, c2.Run(13, 12)); |
| 287 | 279 |
| 288 Callback<int(int)> c1 = Bind(c2, 2); | 280 Callback<int(int)> c1 = Bind(c2, 2); |
| 289 EXPECT_EQ(75, c1.Run(13)); | 281 EXPECT_EQ(75, c1.Run(13)); |
| 290 | 282 |
| 291 Callback<int(void)> c0 = Bind(c1, 1); | 283 Callback<int(void)> c0 = Bind(c1, 1); |
| 292 EXPECT_EQ(63, c0.Run()); | 284 EXPECT_EQ(63, c0.Run()); |
| 293 } | 285 } |
| 294 | 286 |
| 295 // Test that currying the rvalue result of another Bind() works correctly. | |
| 296 // - rvalue should be usable as argument to Bind(). | |
| 297 // - multiple runs of resulting Callback remain valid. | |
| 298 TEST_F(BindTest, CurryingRvalueResultOfBind) { | |
| 299 int n = 0; | |
| 300 Closure cb = base::Bind(&TakesACallback, base::Bind(&PtrArgSet, &n)); | |
| 301 | |
| 302 // If we implement Bind() such that the return value has auto_ptr-like | |
| 303 // semantics, the second call here will fail because ownership of | |
| 304 // the internal BindState<> would have been transfered to a *temporary* | |
| 305 // constructon of a Callback object on the first call. | |
| 306 cb.Run(); | |
| 307 EXPECT_EQ(2, n); | |
| 308 | |
| 309 n = 0; | |
| 310 cb.Run(); | |
| 311 EXPECT_EQ(2, n); | |
| 312 } | |
| 313 | |
| 314 // Function type support. | 287 // Function type support. |
| 315 // - Normal function. | 288 // - Normal function. |
| 316 // - Normal function bound with non-refcounted first argument. | 289 // - Normal function bound with non-refcounted first argument. |
| 317 // - Method bound to non-const object. | 290 // - Method bound to non-const object. |
| 318 // - Method bound to scoped_refptr. | 291 // - Method bound to scoped_refptr. |
| 319 // - Const method bound to non-const object. | 292 // - Const method bound to non-const object. |
| 320 // - Const method bound to const object. | 293 // - Const method bound to const object. |
| 321 // - 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. |
| 322 // - Derived classes can be used with pointers to virtual base functions (and | 295 // - Derived classes can be used with pointers to virtual base functions (and |
| 323 // preserve virtual dispatch). | 296 // preserve virtual dispatch). |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 713 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
| 741 EXPECT_EQ(1, fastcall_cb.Run()); | 714 EXPECT_EQ(1, fastcall_cb.Run()); |
| 742 | 715 |
| 743 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 716 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
| 744 EXPECT_EQ(2, stdcall_cb.Run()); | 717 EXPECT_EQ(2, stdcall_cb.Run()); |
| 745 } | 718 } |
| 746 #endif | 719 #endif |
| 747 | 720 |
| 748 } // namespace | 721 } // namespace |
| 749 } // namespace base | 722 } // namespace base |
| OLD | NEW |