Home | History | Annotate | Download | only in ext
      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.
      4 
      5 #include "skia/ext/refptr.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace skia {
     10 namespace {
     11 
     12 TEST(RefPtrTest, ReferenceCounting) {
     13   SkRefCnt* ref = new SkRefCnt();
     14   EXPECT_EQ(1, ref->getRefCnt());
     15 
     16   {
     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());
     21 
     22     EXPECT_EQ(ref, &*refptr1);
     23     EXPECT_EQ(ref, refptr1.get());
     24 
     25     {
     26       // Take a second reference for the second instance.
     27       RefPtr<SkRefCnt> refptr2(refptr1);
     28       EXPECT_EQ(2, ref->getRefCnt());
     29 
     30       RefPtr<SkRefCnt> refptr3;
     31       EXPECT_FALSE(refptr3);
     32 
     33       // Take a third reference for the third instance.
     34       refptr3 = refptr1;
     35       EXPECT_EQ(3, ref->getRefCnt());
     36 
     37       // Same object, so should have the same refcount.
     38       refptr2 = refptr3;
     39       EXPECT_EQ(3, ref->getRefCnt());
     40 
     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());
     45 
     46       EXPECT_FALSE(refptr2);
     47       EXPECT_EQ(NULL, refptr2.get());
     48 
     49       EXPECT_TRUE(refptr3);
     50       EXPECT_EQ(2, refptr3->getRefCnt());
     51       EXPECT_EQ(ref, &*refptr3);
     52       EXPECT_EQ(ref, refptr3.get());
     53     }
     54 
     55     // Drop a reference when the third object is destroyed.
     56     EXPECT_EQ(1, ref->getRefCnt());
     57   }
     58 }
     59 
     60 TEST(RefPtrTest, Construct) {
     61   SkRefCnt* ref = new SkRefCnt();
     62   EXPECT_EQ(1, ref->getRefCnt());
     63 
     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());
     68 
     69   EXPECT_EQ(ref, &*refptr1);
     70   EXPECT_EQ(ref, refptr1.get());
     71 
     72   RefPtr<SkRefCnt> refptr2(refptr1);
     73   EXPECT_EQ(2, ref->getRefCnt());
     74 }
     75 
     76 TEST(RefPtrTest, DeclareAndAssign) {
     77   SkRefCnt* ref = new SkRefCnt();
     78   EXPECT_EQ(1, ref->getRefCnt());
     79 
     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());
     84 
     85   EXPECT_EQ(ref, &*refptr1);
     86   EXPECT_EQ(ref, refptr1.get());
     87 
     88   RefPtr<SkRefCnt> refptr2 = refptr1;
     89   EXPECT_EQ(2, ref->getRefCnt());
     90 }
     91 
     92 TEST(RefPtrTest, Assign) {
     93   SkRefCnt* ref = new SkRefCnt();
     94   EXPECT_EQ(1, ref->getRefCnt());
     95 
     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());
    101 
    102   EXPECT_EQ(ref, &*refptr1);
    103   EXPECT_EQ(ref, refptr1.get());
    104 
    105   RefPtr<SkRefCnt> refptr2;
    106   refptr2 = refptr1;
    107   EXPECT_EQ(2, ref->getRefCnt());
    108 }
    109 
    110 class Subclass : public SkRefCnt {};
    111 
    112 TEST(RefPtrTest, Upcast) {
    113   RefPtr<Subclass> child = AdoptRef(new Subclass());
    114   EXPECT_EQ(1, child->getRefCnt());
    115 
    116   RefPtr<SkRefCnt> parent = child;
    117   EXPECT_TRUE(child);
    118   EXPECT_TRUE(parent);
    119 
    120   EXPECT_EQ(2, child->getRefCnt());
    121   EXPECT_EQ(2, parent->getRefCnt());
    122 }
    123 
    124 }  // namespace
    125 }  // namespace skia
    126