Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / base / memory / ref_counted_unittest.cc
1 // Copyright (c) 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 "base/memory/ref_counted.h"
6
7 #include "base/test/opaque_ref_counted.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace {
11
12 class SelfAssign : public base::RefCounted<SelfAssign> {
13   friend class base::RefCounted<SelfAssign>;
14
15   ~SelfAssign() {}
16 };
17
18 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> {
19  public:
20   CheckDerivedMemberAccess() {
21     // This shouldn't compile if we don't have access to the member variable.
22     SelfAssign** pptr = &ptr_;
23     EXPECT_EQ(*pptr, ptr_);
24   }
25 };
26
27 class ScopedRefPtrToSelf : public base::RefCounted<ScopedRefPtrToSelf> {
28  public:
29   ScopedRefPtrToSelf() : self_ptr_(this) {}
30
31   static bool was_destroyed() { return was_destroyed_; }
32
33   void SelfDestruct() { self_ptr_ = NULL; }
34
35  private:
36   friend class base::RefCounted<ScopedRefPtrToSelf>;
37   ~ScopedRefPtrToSelf() { was_destroyed_ = true; }
38
39   static bool was_destroyed_;
40
41   scoped_refptr<ScopedRefPtrToSelf> self_ptr_;
42 };
43
44 bool ScopedRefPtrToSelf::was_destroyed_ = false;
45
46 }  // end namespace
47
48 TEST(RefCountedUnitTest, TestSelfAssignment) {
49   SelfAssign* p = new SelfAssign;
50   scoped_refptr<SelfAssign> var(p);
51   var = var;
52   EXPECT_EQ(var.get(), p);
53 }
54
55 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) {
56   CheckDerivedMemberAccess check;
57 }
58
59 TEST(RefCountedUnitTest, ScopedRefPtrToSelf) {
60   ScopedRefPtrToSelf* check = new ScopedRefPtrToSelf();
61   EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed());
62   check->SelfDestruct();
63   EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed());
64 }
65
66 TEST(RefCountedUnitTest, ScopedRefPtrToOpaque) {
67   scoped_refptr<base::OpaqueRefCounted> p = base::MakeOpaqueRefCounted();
68   base::TestOpaqueRefCounted(p);
69
70   scoped_refptr<base::OpaqueRefCounted> q;
71   q = p;
72   base::TestOpaqueRefCounted(p);
73   base::TestOpaqueRefCounted(q);
74 }