 Chromium Code Reviews
 Chromium Code Reviews Issue 1487493002:
  Add helper KeepRefUntilDone  (Closed) 
  Base URL: https://chromium.googlesource.com/external/webrtc.git@master
    
  
    Issue 1487493002:
  Add helper KeepRefUntilDone  (Closed) 
  Base URL: https://chromium.googlesource.com/external/webrtc.git@master| 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 |