Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2833)

Unified Diff: base/callback_unittest.cc

Issue 6109007: Unified callback system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: Ready for another structural review. (not optimized) Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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"
scherkus (not reviewing) 2011/02/01 01:00:10 include order
awong 2011/02/01 01:21:38 Will fix in next pass...I'm about to regenerate th
#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;
scherkus (not reviewing) 2011/02/01 01:00:10 move this to private section?
awong 2011/02/01 01:21:38 Yep. This will get recreated by pump in a bit.
+ 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);
+ 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
scherkus (not reviewing) 2011/02/01 01:00:10 :)
awong 2011/02/01 01:21:38 *sigh* Yeah, I wish I had a better method for thi
+TEST(Callback, Prebind_one_arg_nocompile) {
+ 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

Powered by Google App Engine
This is Rietveld 408576698