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. |