summaryrefslogtreecommitdiffstats
path: root/skia/ext/refptr_unittest.cc
blob: bf54b03ac54aeeb41342da5eacef180e4c765e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "skia/ext/refptr.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace skia {
namespace {

TEST(RefPtrTest, ReferenceCounting) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  {
    // Adopt the reference from the caller on creation.
    RefPtr<SkRefCnt> refptr1 = AdoptRef(ref);
    EXPECT_TRUE(ref->unique());
    EXPECT_TRUE(refptr1->unique());

    EXPECT_EQ(ref, &*refptr1);
    EXPECT_EQ(ref, refptr1.get());

    {
      // Take a second reference for the second instance.
      RefPtr<SkRefCnt> refptr2(refptr1);
      EXPECT_FALSE(ref->unique());

      RefPtr<SkRefCnt> refptr3;
      EXPECT_FALSE(refptr3);

      // Take a third reference for the third instance.
      refptr3 = refptr1;
      EXPECT_FALSE(ref->unique());

      // Same object, so should have the same refcount.
      refptr2 = refptr3;
      EXPECT_FALSE(ref->unique());

      // Drop the object from refptr2, so it should lose its reference.
      EXPECT_TRUE(refptr2);
      refptr2.clear();
      EXPECT_FALSE(ref->unique());

      EXPECT_FALSE(refptr2);
      EXPECT_EQ(NULL, refptr2.get());

      EXPECT_TRUE(refptr3);
      EXPECT_FALSE(refptr3->unique());
      EXPECT_EQ(ref, &*refptr3);
      EXPECT_EQ(ref, refptr3.get());
    }

    // Drop a reference when the third object is destroyed.
    EXPECT_TRUE(ref->unique());
  }
}

TEST(RefPtrTest, Construct) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  // Adopt the reference from the caller on creation.
  RefPtr<SkRefCnt> refptr1(AdoptRef(ref));
  EXPECT_TRUE(ref->unique());
  EXPECT_TRUE(refptr1->unique());

  EXPECT_EQ(ref, &*refptr1);
  EXPECT_EQ(ref, refptr1.get());

  RefPtr<SkRefCnt> refptr2(refptr1);
  EXPECT_FALSE(ref->unique());
}

TEST(RefPtrTest, DeclareAndAssign) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  // Adopt the reference from the caller on creation.
  RefPtr<SkRefCnt> refptr1 = AdoptRef(ref);
  EXPECT_TRUE(ref->unique());
  EXPECT_TRUE(refptr1->unique());

  EXPECT_EQ(ref, &*refptr1);
  EXPECT_EQ(ref, refptr1.get());

  RefPtr<SkRefCnt> refptr2 = refptr1;
  EXPECT_FALSE(ref->unique());
}

TEST(RefPtrTest, Assign) {
  SkRefCnt* ref = new SkRefCnt();
  EXPECT_TRUE(ref->unique());

  // Adopt the reference from the caller on creation.
  RefPtr<SkRefCnt> refptr1;
  refptr1 = AdoptRef(ref);
  EXPECT_TRUE(ref->unique());
  EXPECT_TRUE(refptr1->unique());

  EXPECT_EQ(ref, &*refptr1);
  EXPECT_EQ(ref, refptr1.get());

  RefPtr<SkRefCnt> refptr2;
  refptr2 = refptr1;
  EXPECT_FALSE(ref->unique());
}

class Subclass : public SkRefCnt {};

TEST(RefPtrTest, Upcast) {
  RefPtr<Subclass> child = AdoptRef(new Subclass());
  EXPECT_TRUE(child->unique());

  RefPtr<SkRefCnt> parent = child;
  EXPECT_TRUE(child);
  EXPECT_TRUE(parent);

  EXPECT_FALSE(child->unique());
  EXPECT_FALSE(parent->unique());
}

}  // namespace
}  // namespace skia