blob: eacf731782a7ce70d0e5e217028a236ffb65282a [file] [log] [blame]
perkj0489e492016-10-20 00:24:01 -07001/*
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 Olssona4d87372019-07-05 19:08:33 +020011#include "rtc_base/ref_counted_object.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <memory>
perkj0489e492016-10-20 00:24:01 -070014#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <utility>
perkj0489e492016-10-20 00:24:01 -070016
Mirko Bonadeid9708072019-01-25 20:26:48 +010017#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/ref_count.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "test/gtest.h"
perkj0489e492016-10-20 00:24:01 -070020
21namespace rtc {
22
23namespace {
24
25class A {
26 public:
27 A() {}
28
29 private:
30 RTC_DISALLOW_COPY_AND_ASSIGN(A);
31};
32
33class RefClass : public RefCountInterface {
34 public:
35 RefClass() {}
36
37 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080038 ~RefClass() override {}
perkj0489e492016-10-20 00:24:01 -070039};
40
41class RefClassWithRvalue : public RefCountInterface {
42 public:
43 explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {}
44
45 protected:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080046 ~RefClassWithRvalue() override {}
perkj0489e492016-10-20 00:24:01 -070047
48 public:
49 std::unique_ptr<A> a_;
50};
51
52class 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:
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080058 ~RefClassWithMixedValues() override {}
perkj0489e492016-10-20 00:24:01 -070059
60 public:
61 std::unique_ptr<A> a_;
62 int b_;
63 std::string c_;
64};
65
66} // namespace
67
Niels Möller6f72f562017-10-19 13:15:17 +020068TEST(RefCountedObject, HasOneRef) {
perkj0489e492016-10-20 00:24:01 -070069 scoped_refptr<RefCountedObject<RefClass>> aref(
70 new RefCountedObject<RefClass>());
71 EXPECT_TRUE(aref->HasOneRef());
Niels Möller6f72f562017-10-19 13:15:17 +020072 aref->AddRef();
73 EXPECT_FALSE(aref->HasOneRef());
74 EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained);
75 EXPECT_TRUE(aref->HasOneRef());
perkj0489e492016-10-20 00:24:01 -070076}
77
78TEST(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
86TEST(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