perkj | 0489e49 | 2016-10-20 07:24:01 | [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 17:08:33 | [diff] [blame] | 11 | #include "rtc_base/ref_counted_object.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <memory> |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 14 | #include <string> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 15 | #include <utility> |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 16 | |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 17 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "rtc_base/ref_count.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "test/gtest.h" |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class A { |
| 26 | public: |
| 27 | A() {} |
| 28 | |
| 29 | private: |
| 30 | RTC_DISALLOW_COPY_AND_ASSIGN(A); |
| 31 | }; |
| 32 | |
| 33 | class RefClass : public RefCountInterface { |
| 34 | public: |
| 35 | RefClass() {} |
| 36 | |
| 37 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 15:11:23 | [diff] [blame] | 38 | ~RefClass() override {} |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class RefClassWithRvalue : public RefCountInterface { |
| 42 | public: |
| 43 | explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {} |
| 44 | |
| 45 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 15:11:23 | [diff] [blame] | 46 | ~RefClassWithRvalue() override {} |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 47 | |
| 48 | public: |
| 49 | std::unique_ptr<A> a_; |
| 50 | }; |
| 51 | |
| 52 | class RefClassWithMixedValues : public RefCountInterface { |
| 53 | public: |
| 54 | RefClassWithMixedValues(std::unique_ptr<A> a, int b, const std::string& c) |
| 55 | : a_(std::move(a)), b_(b), c_(c) {} |
| 56 | |
| 57 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 15:11:23 | [diff] [blame] | 58 | ~RefClassWithMixedValues() override {} |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 59 | |
| 60 | public: |
| 61 | std::unique_ptr<A> a_; |
| 62 | int b_; |
| 63 | std::string c_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace |
| 67 | |
Niels Möller | 6f72f56 | 2017-10-19 11:15:17 | [diff] [blame] | 68 | TEST(RefCountedObject, HasOneRef) { |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 69 | scoped_refptr<RefCountedObject<RefClass>> aref( |
| 70 | new RefCountedObject<RefClass>()); |
| 71 | EXPECT_TRUE(aref->HasOneRef()); |
Niels Möller | 6f72f56 | 2017-10-19 11:15:17 | [diff] [blame] | 72 | aref->AddRef(); |
| 73 | EXPECT_FALSE(aref->HasOneRef()); |
| 74 | EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained); |
| 75 | EXPECT_TRUE(aref->HasOneRef()); |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST(RefCountedObject, SupportRValuesInCtor) { |
| 79 | std::unique_ptr<A> a(new A()); |
| 80 | scoped_refptr<RefClassWithRvalue> ref( |
| 81 | new RefCountedObject<RefClassWithRvalue>(std::move(a))); |
| 82 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 83 | EXPECT_TRUE(a.get() == nullptr); |
| 84 | } |
| 85 | |
| 86 | TEST(RefCountedObject, SupportMixedTypesInCtor) { |
| 87 | std::unique_ptr<A> a(new A()); |
| 88 | int b = 9; |
| 89 | std::string c = "hello"; |
| 90 | scoped_refptr<RefClassWithMixedValues> ref( |
| 91 | new RefCountedObject<RefClassWithMixedValues>(std::move(a), b, c)); |
| 92 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 93 | EXPECT_TRUE(a.get() == nullptr); |
| 94 | EXPECT_EQ(b, ref->b_); |
| 95 | EXPECT_EQ(c, ref->c_); |
| 96 | } |
| 97 | |
| 98 | } // namespace rtc |