• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2004 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 
11 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/referencecountedsingletonfactory.h"
13 
14 namespace rtc {
15 
16 class MyExistenceWatcher {
17  public:
MyExistenceWatcher()18   MyExistenceWatcher() { create_called_ = true; }
~MyExistenceWatcher()19   ~MyExistenceWatcher() { delete_called_ = true; }
20 
21   static bool create_called_;
22   static bool delete_called_;
23 };
24 
25 bool MyExistenceWatcher::create_called_ = false;
26 bool MyExistenceWatcher::delete_called_ = false;
27 
28 class TestReferenceCountedSingletonFactory :
29     public ReferenceCountedSingletonFactory<MyExistenceWatcher> {
30  protected:
SetupInstance()31   virtual bool SetupInstance() {
32     instance_.reset(new MyExistenceWatcher());
33     return true;
34   }
35 
CleanupInstance()36   virtual void CleanupInstance() {
37     instance_.reset();
38   }
39 };
40 
DoCreateAndGoOutOfScope(ReferenceCountedSingletonFactory<MyExistenceWatcher> * factory)41 static void DoCreateAndGoOutOfScope(
42     ReferenceCountedSingletonFactory<MyExistenceWatcher> *factory) {
43   rcsf_ptr<MyExistenceWatcher> ptr(factory);
44   ptr.get();
45   // and now ptr should go out of scope.
46 }
47 
TEST(ReferenceCountedSingletonFactory,ZeroReferenceCountCausesDeletion)48 TEST(ReferenceCountedSingletonFactory, ZeroReferenceCountCausesDeletion) {
49   TestReferenceCountedSingletonFactory factory;
50   MyExistenceWatcher::delete_called_ = false;
51   DoCreateAndGoOutOfScope(&factory);
52   EXPECT_TRUE(MyExistenceWatcher::delete_called_);
53 }
54 
TEST(ReferenceCountedSingletonFactory,NonZeroReferenceCountDoesNotDelete)55 TEST(ReferenceCountedSingletonFactory, NonZeroReferenceCountDoesNotDelete) {
56   TestReferenceCountedSingletonFactory factory;
57   rcsf_ptr<MyExistenceWatcher> ptr(&factory);
58   ptr.get();
59   MyExistenceWatcher::delete_called_ = false;
60   DoCreateAndGoOutOfScope(&factory);
61   EXPECT_FALSE(MyExistenceWatcher::delete_called_);
62 }
63 
TEST(ReferenceCountedSingletonFactory,ReturnedPointersReferToSameThing)64 TEST(ReferenceCountedSingletonFactory, ReturnedPointersReferToSameThing) {
65   TestReferenceCountedSingletonFactory factory;
66   rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
67 
68   EXPECT_EQ(one.get(), two.get());
69 }
70 
TEST(ReferenceCountedSingletonFactory,Release)71 TEST(ReferenceCountedSingletonFactory, Release) {
72   TestReferenceCountedSingletonFactory factory;
73 
74   rcsf_ptr<MyExistenceWatcher> one(&factory);
75   one.get();
76 
77   MyExistenceWatcher::delete_called_ = false;
78   one.release();
79   EXPECT_TRUE(MyExistenceWatcher::delete_called_);
80 }
81 
TEST(ReferenceCountedSingletonFactory,GetWithoutRelease)82 TEST(ReferenceCountedSingletonFactory, GetWithoutRelease) {
83   TestReferenceCountedSingletonFactory factory;
84   rcsf_ptr<MyExistenceWatcher> one(&factory);
85   one.get();
86 
87   MyExistenceWatcher::create_called_ = false;
88   one.get();
89   EXPECT_FALSE(MyExistenceWatcher::create_called_);
90 }
91 
TEST(ReferenceCountedSingletonFactory,GetAfterRelease)92 TEST(ReferenceCountedSingletonFactory, GetAfterRelease) {
93   TestReferenceCountedSingletonFactory factory;
94   rcsf_ptr<MyExistenceWatcher> one(&factory);
95 
96   MyExistenceWatcher::create_called_ = false;
97   one.release();
98   one.get();
99   EXPECT_TRUE(MyExistenceWatcher::create_called_);
100 }
101 
TEST(ReferenceCountedSingletonFactory,MultipleReleases)102 TEST(ReferenceCountedSingletonFactory, MultipleReleases) {
103   TestReferenceCountedSingletonFactory factory;
104   rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
105 
106   MyExistenceWatcher::create_called_ = false;
107   MyExistenceWatcher::delete_called_ = false;
108   one.release();
109   EXPECT_FALSE(MyExistenceWatcher::delete_called_);
110   one.release();
111   EXPECT_FALSE(MyExistenceWatcher::delete_called_);
112   one.release();
113   EXPECT_FALSE(MyExistenceWatcher::delete_called_);
114   one.get();
115   EXPECT_TRUE(MyExistenceWatcher::create_called_);
116 }
117 
TEST(ReferenceCountedSingletonFactory,Existentialism)118 TEST(ReferenceCountedSingletonFactory, Existentialism) {
119   TestReferenceCountedSingletonFactory factory;
120 
121   rcsf_ptr<MyExistenceWatcher> one(&factory);
122 
123   MyExistenceWatcher::create_called_ = false;
124   MyExistenceWatcher::delete_called_ = false;
125 
126   one.get();
127   EXPECT_TRUE(MyExistenceWatcher::create_called_);
128   one.release();
129   EXPECT_TRUE(MyExistenceWatcher::delete_called_);
130 }
131 
132 }  // namespace rtc
133