Chromium Code Reviews| Index: base/callback_unittest.cc |
| diff --git a/base/callback_unittest.cc b/base/callback_unittest.cc |
| index bc15927ddade2d6278e60d6e51c567430a2b8cbc..efc9f6c071eb75df9b71a013020204a927b7e35b 100644 |
| --- a/base/callback_unittest.cc |
| +++ b/base/callback_unittest.cc |
| @@ -3,7 +3,9 @@ |
| // found in the LICENSE file. |
| #include "base/callback.h" |
| +#include "base/prebind.h" |
| #include "base/scoped_ptr.h" |
| +#include "base/logging.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -38,3 +40,122 @@ TEST(Callback, ReturnValue) { |
| EXPECT_EQ(callback->Run(), 1); |
| } |
| + |
| +void one_arg(int n) { |
| + LOG(ERROR) << __func__ << " " << n; |
| +} |
| + |
| +void two_arg(int n, int m) { |
| + LOG(ERROR) << __func__ << " " << n << " " << m; |
| +} |
| + |
| +class Foo { |
| + public: |
| + void no_arg(void) { |
| + LOG(ERROR) << __func__; |
| + } |
| + |
| + void no_arg_const() const { |
| + LOG(ERROR) << __func__; |
| + } |
| + |
| + void one_arg(int n) { |
| + LOG(ERROR) << __func__ << n; |
| + } |
| + |
| + void one_arg_const(int n) const { |
| + LOG(ERROR) << __func__ << n; |
| + } |
| +}; |
| + |
| +class RefFoo { |
| + int n; |
| + public: |
| + RefFoo() : n(0) {} |
| + ~RefFoo() { if (n != 0) ADD_FAILURE() << "imbalanced ref: " << n; } |
| + void AddRef() { |
| + n++; |
| + } |
| + void Release() { |
| + n--; |
| + } |
| + |
| + void no_arg(void) { |
| + LOG(ERROR) << __func__; |
| + } |
| + |
| + void no_arg_const() const { |
| + LOG(ERROR) << __func__; |
| + } |
| + |
| + void one_arg(int n) { |
| + LOG(ERROR) << __func__ << n; |
| + } |
| + |
| + void one_arg_const(int n) const { |
| + LOG(ERROR) << __func__ << n; |
| + } |
| +}; |
| + |
| +class Bar : public Foo {}; |
| + |
| +TEST(Callback, Prebind_AllBound) { |
| + RefFoo f; // Must be declared before func for proper destruction order. |
| + base::Callback<void(void)> func = base::Prebind(&one_arg, 1); |
| + func.Run(); |
| + |
| + func = base::Prebind(&RefFoo::no_arg, &f); |
| + func.Run(); |
| + |
| + func = base::Prebind(&RefFoo::no_arg_const, &f); |
| + func.Run(); |
| +} |
| + |
| +TEST(Callback, Prebind_OneUnbound) { |
| + RefFoo f; // Must be declared before func for proper destruction order. |
| + base::Callback<void(int)> func = base::Prebind(&two_arg, 1); |
| + func.Run(2); |
| + |
| + func = base::Prebind(&RefFoo::one_arg, &f); |
|
akalin
2011/02/01 11:29:27
you may already know this, but you could overload
awong
2011/02/01 23:20:45
Oh....I actually forgot about that. Good point. I
|
| + func.Run(4); |
| + |
| + func = base::Prebind(&RefFoo::one_arg_const, &f); |
| + func.Run(6); |
| +} |
| + |
| +TEST(Callback, Prebind_AllBoundNoref) { |
| + Foo f; |
| + base::Callback<void(void)> func = |
| + base::Prebind(&Foo::no_arg, base::Unretained(&f)); |
| + func.Run(); |
| + |
| + func = base::Prebind(&Foo::no_arg_const, base::Unretained(&f)); |
| + func.Run(); |
| +} |
| + |
| +TEST(Callback, Prebind_OneUnboundNoRef) { |
| + Foo f; |
| + base::Callback<void(int)> func = |
| + base::Prebind(&Foo::one_arg, base::Unretained(&f)); |
| + func.Run(4); |
| + |
| + func = base::Prebind(&Foo::one_arg_const, base::Unretained(&f)); |
| + func.Run(6); |
| +} |
| + |
| +#if 0 |
| +TEST(Callback, Prebind_one_arg_nocompile) { |
|
akalin
2011/02/01 11:29:27
Just to be clear, are these two functions supposed
awong
2011/02/01 23:20:45
No, these are funcitons that I want explicitly to
|
| + Bar b; |
| + func = base::Prebind(&Foo::no_arg_const, &b); |
| + func.Run(); |
| +} |
| + |
| +TEST(Callback, Prebind_ref_with_unretained_nocompile) { |
| + RefFoo f; |
| + func = base::Prebind(&Foo::one_arg, base::Unretained(&f)); |
| + func.Run(4); |
| + |
| + func = base::Prebind(&Foo::one_arg_const, base::Unretained(&f)); |
| + func.Run(6); |
| +} |
| +#endif |