Chromecast: extensions to the BrowserCdm interface.
[chromium-blink-merge.git] / skia / ext / refptr_unittest.cc
blob1d63ed1b1e929b1041783a24c6f3fd32a854b968
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "skia/ext/refptr.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace skia {
10 namespace {
12 TEST(RefPtrTest, ReferenceCounting) {
13 SkRefCnt* ref = new SkRefCnt();
14 EXPECT_EQ(1, ref->getRefCnt());
17 // Adopt the reference from the caller on creation.
18 RefPtr<SkRefCnt> refptr1 = AdoptRef(ref);
19 EXPECT_EQ(1, ref->getRefCnt());
20 EXPECT_EQ(1, refptr1->getRefCnt());
22 EXPECT_EQ(ref, &*refptr1);
23 EXPECT_EQ(ref, refptr1.get());
26 // Take a second reference for the second instance.
27 RefPtr<SkRefCnt> refptr2(refptr1);
28 EXPECT_EQ(2, ref->getRefCnt());
30 RefPtr<SkRefCnt> refptr3;
31 EXPECT_FALSE(refptr3);
33 // Take a third reference for the third instance.
34 refptr3 = refptr1;
35 EXPECT_EQ(3, ref->getRefCnt());
37 // Same object, so should have the same refcount.
38 refptr2 = refptr3;
39 EXPECT_EQ(3, ref->getRefCnt());
41 // Drop the object from refptr2, so it should lose its reference.
42 EXPECT_TRUE(refptr2);
43 refptr2.clear();
44 EXPECT_EQ(2, ref->getRefCnt());
46 EXPECT_FALSE(refptr2);
47 EXPECT_EQ(NULL, refptr2.get());
49 EXPECT_TRUE(refptr3);
50 EXPECT_EQ(2, refptr3->getRefCnt());
51 EXPECT_EQ(ref, &*refptr3);
52 EXPECT_EQ(ref, refptr3.get());
55 // Drop a reference when the third object is destroyed.
56 EXPECT_EQ(1, ref->getRefCnt());
60 TEST(RefPtrTest, Construct) {
61 SkRefCnt* ref = new SkRefCnt();
62 EXPECT_EQ(1, ref->getRefCnt());
64 // Adopt the reference from the caller on creation.
65 RefPtr<SkRefCnt> refptr1(AdoptRef(ref));
66 EXPECT_EQ(1, ref->getRefCnt());
67 EXPECT_EQ(1, refptr1->getRefCnt());
69 EXPECT_EQ(ref, &*refptr1);
70 EXPECT_EQ(ref, refptr1.get());
72 RefPtr<SkRefCnt> refptr2(refptr1);
73 EXPECT_EQ(2, ref->getRefCnt());
76 TEST(RefPtrTest, DeclareAndAssign) {
77 SkRefCnt* ref = new SkRefCnt();
78 EXPECT_EQ(1, ref->getRefCnt());
80 // Adopt the reference from the caller on creation.
81 RefPtr<SkRefCnt> refptr1 = AdoptRef(ref);
82 EXPECT_EQ(1, ref->getRefCnt());
83 EXPECT_EQ(1, refptr1->getRefCnt());
85 EXPECT_EQ(ref, &*refptr1);
86 EXPECT_EQ(ref, refptr1.get());
88 RefPtr<SkRefCnt> refptr2 = refptr1;
89 EXPECT_EQ(2, ref->getRefCnt());
92 TEST(RefPtrTest, Assign) {
93 SkRefCnt* ref = new SkRefCnt();
94 EXPECT_EQ(1, ref->getRefCnt());
96 // Adopt the reference from the caller on creation.
97 RefPtr<SkRefCnt> refptr1;
98 refptr1 = AdoptRef(ref);
99 EXPECT_EQ(1, ref->getRefCnt());
100 EXPECT_EQ(1, refptr1->getRefCnt());
102 EXPECT_EQ(ref, &*refptr1);
103 EXPECT_EQ(ref, refptr1.get());
105 RefPtr<SkRefCnt> refptr2;
106 refptr2 = refptr1;
107 EXPECT_EQ(2, ref->getRefCnt());
110 class Subclass : public SkRefCnt {};
112 TEST(RefPtrTest, Upcast) {
113 RefPtr<Subclass> child = AdoptRef(new Subclass());
114 EXPECT_EQ(1, child->getRefCnt());
116 RefPtr<SkRefCnt> parent = child;
117 EXPECT_TRUE(child);
118 EXPECT_TRUE(parent);
120 EXPECT_EQ(2, child->getRefCnt());
121 EXPECT_EQ(2, parent->getRefCnt());
124 } // namespace
125 } // namespace skia