perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "rtc_base/ref_counted_object.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <memory> |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 14 | #include <string> |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 15 | #include <type_traits> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <utility> |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/ref_count.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "test/gtest.h" |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class A { |
| 27 | public: |
| 28 | A() {} |
| 29 | |
| 30 | private: |
| 31 | RTC_DISALLOW_COPY_AND_ASSIGN(A); |
| 32 | }; |
| 33 | |
| 34 | class RefClass : public RefCountInterface { |
| 35 | public: |
| 36 | RefClass() {} |
| 37 | |
| 38 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 39 | ~RefClass() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class RefClassWithRvalue : public RefCountInterface { |
| 43 | public: |
| 44 | explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {} |
| 45 | |
| 46 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 47 | ~RefClassWithRvalue() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | std::unique_ptr<A> a_; |
| 51 | }; |
| 52 | |
| 53 | class RefClassWithMixedValues : public RefCountInterface { |
| 54 | public: |
| 55 | RefClassWithMixedValues(std::unique_ptr<A> a, int b, const std::string& c) |
| 56 | : a_(std::move(a)), b_(b), c_(c) {} |
| 57 | |
| 58 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 59 | ~RefClassWithMixedValues() override {} |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 60 | |
| 61 | public: |
| 62 | std::unique_ptr<A> a_; |
| 63 | int b_; |
| 64 | std::string c_; |
| 65 | }; |
| 66 | |
| 67 | } // namespace |
| 68 | |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 69 | TEST(RefCountedObject, HasOneRef) { |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 70 | scoped_refptr<RefCountedObject<RefClass>> aref( |
| 71 | new RefCountedObject<RefClass>()); |
| 72 | EXPECT_TRUE(aref->HasOneRef()); |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 73 | aref->AddRef(); |
| 74 | EXPECT_FALSE(aref->HasOneRef()); |
| 75 | EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained); |
| 76 | EXPECT_TRUE(aref->HasOneRef()); |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(RefCountedObject, SupportRValuesInCtor) { |
| 80 | std::unique_ptr<A> a(new A()); |
| 81 | scoped_refptr<RefClassWithRvalue> ref( |
| 82 | new RefCountedObject<RefClassWithRvalue>(std::move(a))); |
| 83 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 84 | EXPECT_TRUE(a.get() == nullptr); |
| 85 | } |
| 86 | |
| 87 | TEST(RefCountedObject, SupportMixedTypesInCtor) { |
| 88 | std::unique_ptr<A> a(new A()); |
| 89 | int b = 9; |
| 90 | std::string c = "hello"; |
| 91 | scoped_refptr<RefClassWithMixedValues> ref( |
| 92 | new RefCountedObject<RefClassWithMixedValues>(std::move(a), b, c)); |
| 93 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 94 | EXPECT_TRUE(a.get() == nullptr); |
| 95 | EXPECT_EQ(b, ref->b_); |
| 96 | EXPECT_EQ(c, ref->c_); |
| 97 | } |
| 98 | |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 99 | TEST(FinalRefCountedObject, CanWrapIntoScopedRefptr) { |
| 100 | using WrappedTyped = FinalRefCountedObject<A>; |
| 101 | static_assert(!std::is_polymorphic<WrappedTyped>::value, ""); |
| 102 | scoped_refptr<WrappedTyped> ref(new WrappedTyped()); |
| 103 | EXPECT_TRUE(ref.get()); |
| 104 | EXPECT_TRUE(ref->HasOneRef()); |
| 105 | // Test reference counter is updated on some simple operations. |
| 106 | scoped_refptr<WrappedTyped> ref2 = ref; |
| 107 | EXPECT_FALSE(ref->HasOneRef()); |
| 108 | EXPECT_FALSE(ref2->HasOneRef()); |
| 109 | |
| 110 | ref = nullptr; |
| 111 | EXPECT_TRUE(ref2->HasOneRef()); |
| 112 | } |
| 113 | |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 114 | } // namespace rtc |