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

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: Addressed comments. Now uses RefCountedInterface instead of a 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
Index: webrtc/base/callback_unittest.cc
diff --git a/webrtc/base/callback_unittest.cc b/webrtc/base/callback_unittest.cc
index 66c939140ee2ef9e99e93416c2720819f204e105..7615e6ae2437545d68c28d0a459ef7d593db50e7 100644
--- a/webrtc/base/callback_unittest.cc
+++ b/webrtc/base/callback_unittest.cc
@@ -8,9 +8,11 @@
* be found in the AUTHORS file in the root of the source tree.
*/
+#include <webrtc/base/keep_ref_until_done.h>
magjed_webrtc 2015/11/30 15:44:34 This is not sorted anymore. Did you pass presubmit
perkj_webrtc 2015/11/30 16:41:31 Done.
#include "webrtc/base/bind.h"
#include "webrtc/base/callback.h"
#include "webrtc/base/gunit.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,32 @@ 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());
+}
+
} // namespace rtc

Powered by Google App Engine
This is Rietveld 408576698