blob: eacf731782a7ce70d0e5e217028a236ffb65282a [file] [log] [blame]
perkj0489e492016-10-20 07:24:011/*
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 17:08:3311#include "rtc_base/ref_counted_object.h"
12
Yves Gerey3e707812018-11-28 15:47:4913#include <memory>
perkj0489e492016-10-20 07:24:0114#include <string>
Yves Gerey3e707812018-11-28 15:47:4915#include <utility>
perkj0489e492016-10-20 07:24:0116
Mirko Bonadeid9708072019-01-25 19:26:4817#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0018#include "rtc_base/ref_count.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "test/gtest.h"
perkj0489e492016-10-20 07:24:0120
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 15:11:2338 ~RefClass() override {}
perkj0489e492016-10-20 07:24:0139};
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 15:11:2346 ~RefClassWithRvalue() override {}
perkj0489e492016-10-20 07:24:0147
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 15:11:2358 ~RefClassWithMixedValues() override {}
perkj0489e492016-10-20 07:24:0159
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 11:15:1768TEST(RefCountedObject, HasOneRef) {
perkj0489e492016-10-20 07:24:0169 scoped_refptr<RefCountedObject<RefClass>> aref(
70 new RefCountedObject<RefClass>());
71 EXPECT_TRUE(aref->HasOneRef());
Niels Möller6f72f562017-10-19 11:15:1772 aref->AddRef();
73 EXPECT_FALSE(aref->HasOneRef());
74 EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained);
75 EXPECT_TRUE(aref->HasOneRef());
perkj0489e492016-10-20 07:24:0176}
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