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

Unified Diff: base/bind_unittest.cc

Issue 8209001: Callback API Change: Add in an Owned() wrapper to base::Bind(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed Created 9 years, 2 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
« no previous file with comments | « base/bind_helpers.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/bind_unittest.cc
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index acaf5626f182dcc4e49c9088895ff5bb36c68f89..0fdd24115012c53f8fa765f136fa7df457c6675e 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -133,6 +133,22 @@ class CopyCounter {
int* assigns_;
};
+class DeleteCounter {
+ public:
+ explicit DeleteCounter(int* deletes)
+ : deletes_(deletes) {
+ }
+
+ ~DeleteCounter() {
+ (*deletes_)++;
+ }
+
+ void VoidMethod0() {}
+
+ private:
+ int* deletes_;
+};
+
// Some test functions that we can Bind to.
template <typename T>
T PolymorphicIdentity(T t) {
@@ -187,11 +203,6 @@ int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
return n;
}
-// Only useful in no-compile tests.
-int UnwrapNoRefParentRef(Parent& p) {
- return p.value;
-}
-
class BindTest : public ::testing::Test {
public:
BindTest() {
@@ -595,6 +606,31 @@ TEST_F(BindTest, ConstRef) {
EXPECT_EQ(0, assigns);
}
+// Test Owned() support.
+TEST_F(BindTest, Owned) {
+ int deletes = 0;
+ DeleteCounter* counter = new DeleteCounter(&deletes);
+
+ // If we don't capture, delete happens on Callback destruction/reset.
+ // return the same value.
+ Callback<DeleteCounter*(void)> no_capture_cb =
+ Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
+ EXPECT_EQ(counter, no_capture_cb.Run());
+ EXPECT_EQ(counter, no_capture_cb.Run());
+ EXPECT_EQ(0, deletes);
+ no_capture_cb.Reset(); // This should trigger a delete.
+ EXPECT_EQ(1, deletes);
+
+ deletes = 0;
+ counter = new DeleteCounter(&deletes);
+ base::Closure own_object_cb =
+ Bind(&DeleteCounter::VoidMethod0, Owned(counter));
+ own_object_cb.Run();
+ EXPECT_EQ(0, deletes);
+ own_object_cb.Reset();
+ EXPECT_EQ(1, deletes);
+}
+
// Argument Copy-constructor usage for non-reference parameters.
// - Bound arguments are only copied once.
// - Forwarded arguments are only copied once.
« no previous file with comments | « base/bind_helpers.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698