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

Unified Diff: webrtc/base/callback_unittest.cc

Issue 1487493002: Add helper KeepRefUntilDone (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Changed back to use template Created 5 years, 1 month 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 | « talk/app/webrtc/java/jni/native_handle_impl.cc ('k') | webrtc/base/keep_ref_until_done.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/callback_unittest.cc
diff --git a/webrtc/base/callback_unittest.cc b/webrtc/base/callback_unittest.cc
index 66c939140ee2ef9e99e93416c2720819f204e105..db294cd96eb71a5d59e9accb98080dc50a494a32 100644
--- a/webrtc/base/callback_unittest.cc
+++ b/webrtc/base/callback_unittest.cc
@@ -11,6 +11,8 @@
#include "webrtc/base/bind.h"
#include "webrtc/base/callback.h"
#include "webrtc/base/gunit.h"
+#include "webrtc/base/keep_ref_until_done.h"
+#include "webrtc/base/refcount.h"
namespace rtc {
@@ -26,6 +28,21 @@ struct BindTester {
int b(int x) const { return x * x; }
};
+class RefCountedBindTester : public RefCountInterface {
+ public:
+ RefCountedBindTester() : count_(0) {}
+ int AddRef() const override {
+ return ++count_;
+ }
+ int Release() const {
+ return --count_;
+ }
+ int RefCount() const { return count_; }
+
+ private:
+ mutable int count_;
+};
+
} // namespace
TEST(CallbackTest, VoidReturn) {
@@ -78,4 +95,46 @@ TEST(CallbackTest, WithBind) {
EXPECT_EQ(25, cb1());
}
+TEST(KeepRefUntilDoneTest, simple) {
+ RefCountedBindTester t;
+ EXPECT_EQ(0, t.RefCount());
+ {
+ Callback0<void> cb = KeepRefUntilDone(&t);
+ EXPECT_EQ(1, t.RefCount());
+ cb();
+ EXPECT_EQ(1, t.RefCount());
+ cb();
+ EXPECT_EQ(1, t.RefCount());
+ }
+ EXPECT_EQ(0, t.RefCount());
+}
+
+TEST(KeepRefUntilDoneTest, copy) {
+ RefCountedBindTester t;
+ EXPECT_EQ(0, t.RefCount());
+ Callback0<void> cb2;
+ {
+ Callback0<void> cb = KeepRefUntilDone(&t);
+ EXPECT_EQ(1, t.RefCount());
+ cb2 = cb;
+ }
+ EXPECT_EQ(1, t.RefCount());
+ cb2 = Callback0<void>();
+ EXPECT_EQ(0, t.RefCount());
+}
+
+TEST(KeepRefUntilDoneTest, scopedref) {
+ RefCountedBindTester t;
+ EXPECT_EQ(0, t.RefCount());
+ {
+ scoped_refptr<RefCountedBindTester> t_scoped_ref(&t);
+ Callback0<void> cb = KeepRefUntilDone(t_scoped_ref);
+ t_scoped_ref = nullptr;
+ EXPECT_EQ(1, t.RefCount());
+ cb();
+ EXPECT_EQ(1, t.RefCount());
+ }
+ EXPECT_EQ(0, t.RefCount());
+}
+
} // namespace rtc
« no previous file with comments | « talk/app/webrtc/java/jni/native_handle_impl.cc ('k') | webrtc/base/keep_ref_until_done.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698