henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/callback.h" |
| 12 | #include "rtc_base/bind.h" |
| 13 | #include "rtc_base/gunit.h" |
| 14 | #include "rtc_base/keep_ref_until_done.h" |
| 15 | #include "rtc_base/refcount.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | void f() {} |
| 22 | int g() { return 42; } |
| 23 | int h(int x) { return x * x; } |
| 24 | void i(int& x) { x *= x; } // NOLINT: Testing refs |
| 25 | |
| 26 | struct BindTester { |
| 27 | int a() { return 24; } |
| 28 | int b(int x) const { return x * x; } |
| 29 | }; |
| 30 | |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 31 | class RefCountedBindTester : public RefCountInterface { |
| 32 | public: |
| 33 | RefCountedBindTester() : count_(0) {} |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 34 | void AddRef() const override { ++count_; } |
| 35 | RefCountReleaseStatus Release() const override { |
| 36 | --count_; |
| 37 | return count_ == 0 ? RefCountReleaseStatus::kDroppedLastRef |
| 38 | : RefCountReleaseStatus::kOtherRefsRemained; |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 39 | } |
| 40 | int RefCount() const { return count_; } |
| 41 | |
| 42 | private: |
| 43 | mutable int count_; |
| 44 | }; |
| 45 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | } // namespace |
| 47 | |
| 48 | TEST(CallbackTest, VoidReturn) { |
| 49 | Callback0<void> cb; |
| 50 | EXPECT_TRUE(cb.empty()); |
| 51 | cb(); // Executing an empty callback should not crash. |
| 52 | cb = Callback0<void>(&f); |
| 53 | EXPECT_FALSE(cb.empty()); |
| 54 | cb(); |
| 55 | } |
| 56 | |
| 57 | TEST(CallbackTest, IntReturn) { |
| 58 | Callback0<int> cb; |
| 59 | EXPECT_TRUE(cb.empty()); |
| 60 | cb = Callback0<int>(&g); |
| 61 | EXPECT_FALSE(cb.empty()); |
| 62 | EXPECT_EQ(42, cb()); |
| 63 | EXPECT_EQ(42, cb()); |
| 64 | } |
| 65 | |
| 66 | TEST(CallbackTest, OneParam) { |
| 67 | Callback1<int, int> cb1(&h); |
| 68 | EXPECT_FALSE(cb1.empty()); |
| 69 | EXPECT_EQ(9, cb1(-3)); |
| 70 | EXPECT_EQ(100, cb1(10)); |
| 71 | |
| 72 | // Try clearing a callback. |
| 73 | cb1 = Callback1<int, int>(); |
| 74 | EXPECT_TRUE(cb1.empty()); |
| 75 | |
| 76 | // Try a callback with a ref parameter. |
| 77 | Callback1<void, int&> cb2(&i); |
| 78 | int x = 3; |
| 79 | cb2(x); |
| 80 | EXPECT_EQ(9, x); |
| 81 | cb2(x); |
| 82 | EXPECT_EQ(81, x); |
| 83 | } |
| 84 | |
| 85 | TEST(CallbackTest, WithBind) { |
| 86 | BindTester t; |
| 87 | Callback0<int> cb1 = Bind(&BindTester::a, &t); |
| 88 | EXPECT_EQ(24, cb1()); |
| 89 | EXPECT_EQ(24, cb1()); |
| 90 | cb1 = Bind(&BindTester::b, &t, 10); |
| 91 | EXPECT_EQ(100, cb1()); |
| 92 | EXPECT_EQ(100, cb1()); |
| 93 | cb1 = Bind(&BindTester::b, &t, 5); |
| 94 | EXPECT_EQ(25, cb1()); |
| 95 | EXPECT_EQ(25, cb1()); |
| 96 | } |
| 97 | |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 98 | TEST(KeepRefUntilDoneTest, simple) { |
| 99 | RefCountedBindTester t; |
| 100 | EXPECT_EQ(0, t.RefCount()); |
| 101 | { |
| 102 | Callback0<void> cb = KeepRefUntilDone(&t); |
| 103 | EXPECT_EQ(1, t.RefCount()); |
| 104 | cb(); |
| 105 | EXPECT_EQ(1, t.RefCount()); |
| 106 | cb(); |
| 107 | EXPECT_EQ(1, t.RefCount()); |
| 108 | } |
| 109 | EXPECT_EQ(0, t.RefCount()); |
| 110 | } |
| 111 | |
| 112 | TEST(KeepRefUntilDoneTest, copy) { |
| 113 | RefCountedBindTester t; |
| 114 | EXPECT_EQ(0, t.RefCount()); |
| 115 | Callback0<void> cb2; |
| 116 | { |
| 117 | Callback0<void> cb = KeepRefUntilDone(&t); |
| 118 | EXPECT_EQ(1, t.RefCount()); |
| 119 | cb2 = cb; |
| 120 | } |
| 121 | EXPECT_EQ(1, t.RefCount()); |
| 122 | cb2 = Callback0<void>(); |
| 123 | EXPECT_EQ(0, t.RefCount()); |
| 124 | } |
| 125 | |
| 126 | TEST(KeepRefUntilDoneTest, scopedref) { |
| 127 | RefCountedBindTester t; |
| 128 | EXPECT_EQ(0, t.RefCount()); |
| 129 | { |
| 130 | scoped_refptr<RefCountedBindTester> t_scoped_ref(&t); |
| 131 | Callback0<void> cb = KeepRefUntilDone(t_scoped_ref); |
| 132 | t_scoped_ref = nullptr; |
| 133 | EXPECT_EQ(1, t.RefCount()); |
| 134 | cb(); |
| 135 | EXPECT_EQ(1, t.RefCount()); |
| 136 | } |
| 137 | EXPECT_EQ(0, t.RefCount()); |
| 138 | } |
| 139 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 140 | } // namespace rtc |