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 |
202 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { | 206 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
203 return n; | 207 return n; |
204 } | 208 } |
205 | 209 |
| 210 void TakesACallback(const Closure& callback) { |
| 211 callback.Run(); |
| 212 } |
| 213 |
206 class BindTest : public ::testing::Test { | 214 class BindTest : public ::testing::Test { |
207 public: | 215 public: |
208 BindTest() { | 216 BindTest() { |
209 const_has_ref_ptr_ = &has_ref_; | 217 const_has_ref_ptr_ = &has_ref_; |
210 const_no_ref_ptr_ = &no_ref_; | 218 const_no_ref_ptr_ = &no_ref_; |
211 static_func_mock_ptr = &static_func_mock_; | 219 static_func_mock_ptr = &static_func_mock_; |
212 } | 220 } |
213 | 221 |
214 virtual ~BindTest() { | 222 virtual ~BindTest() { |
215 } | 223 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 Callback<int(int,int)> c2 = Bind(c3, 4); | 285 Callback<int(int,int)> c2 = Bind(c3, 4); |
278 EXPECT_EQ(85, c2.Run(13, 12)); | 286 EXPECT_EQ(85, c2.Run(13, 12)); |
279 | 287 |
280 Callback<int(int)> c1 = Bind(c2, 2); | 288 Callback<int(int)> c1 = Bind(c2, 2); |
281 EXPECT_EQ(75, c1.Run(13)); | 289 EXPECT_EQ(75, c1.Run(13)); |
282 | 290 |
283 Callback<int(void)> c0 = Bind(c1, 1); | 291 Callback<int(void)> c0 = Bind(c1, 1); |
284 EXPECT_EQ(63, c0.Run()); | 292 EXPECT_EQ(63, c0.Run()); |
285 } | 293 } |
286 | 294 |
| 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 |
287 // Function type support. | 314 // Function type support. |
288 // - Normal function. | 315 // - Normal function. |
289 // - Normal function bound with non-refcounted first argument. | 316 // - Normal function bound with non-refcounted first argument. |
290 // - Method bound to non-const object. | 317 // - Method bound to non-const object. |
291 // - Method bound to scoped_refptr. | 318 // - Method bound to scoped_refptr. |
292 // - Const method bound to non-const object. | 319 // - Const method bound to non-const object. |
293 // - Const method bound to const object. | 320 // - Const method bound to const object. |
294 // - Derived classes can be used with pointers to non-virtual base functions. | 321 // - Derived classes can be used with pointers to non-virtual base functions. |
295 // - Derived classes can be used with pointers to virtual base functions (and | 322 // - Derived classes can be used with pointers to virtual base functions (and |
296 // preserve virtual dispatch). | 323 // preserve virtual dispatch). |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 740 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
714 EXPECT_EQ(1, fastcall_cb.Run()); | 741 EXPECT_EQ(1, fastcall_cb.Run()); |
715 | 742 |
716 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 743 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
717 EXPECT_EQ(2, stdcall_cb.Run()); | 744 EXPECT_EQ(2, stdcall_cb.Run()); |
718 } | 745 } |
719 #endif | 746 #endif |
720 | 747 |
721 } // namespace | 748 } // namespace |
722 } // namespace base | 749 } // namespace base |
OLD | NEW |