blob: 3fc7fd20f746b3a1bbee202236cee9f0cf1f0434 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/gunit.h"
29#include "talk/base/referencecountedsingletonfactory.h"
30
31namespace talk_base {
32
33class MyExistenceWatcher {
34 public:
35 MyExistenceWatcher() { create_called_ = true; }
36 ~MyExistenceWatcher() { delete_called_ = true; }
37
38 static bool create_called_;
39 static bool delete_called_;
40};
41
42bool MyExistenceWatcher::create_called_ = false;
43bool MyExistenceWatcher::delete_called_ = false;
44
45class TestReferenceCountedSingletonFactory :
46 public ReferenceCountedSingletonFactory<MyExistenceWatcher> {
47 protected:
48 virtual bool SetupInstance() {
49 instance_.reset(new MyExistenceWatcher());
50 return true;
51 }
52
53 virtual void CleanupInstance() {
54 instance_.reset();
55 }
56};
57
58static void DoCreateAndGoOutOfScope(
59 ReferenceCountedSingletonFactory<MyExistenceWatcher> *factory) {
60 rcsf_ptr<MyExistenceWatcher> ptr(factory);
61 ptr.get();
62 // and now ptr should go out of scope.
63}
64
65TEST(ReferenceCountedSingletonFactory, ZeroReferenceCountCausesDeletion) {
66 TestReferenceCountedSingletonFactory factory;
67 MyExistenceWatcher::delete_called_ = false;
68 DoCreateAndGoOutOfScope(&factory);
69 EXPECT_TRUE(MyExistenceWatcher::delete_called_);
70}
71
72TEST(ReferenceCountedSingletonFactory, NonZeroReferenceCountDoesNotDelete) {
73 TestReferenceCountedSingletonFactory factory;
74 rcsf_ptr<MyExistenceWatcher> ptr(&factory);
75 ptr.get();
76 MyExistenceWatcher::delete_called_ = false;
77 DoCreateAndGoOutOfScope(&factory);
78 EXPECT_FALSE(MyExistenceWatcher::delete_called_);
79}
80
81TEST(ReferenceCountedSingletonFactory, ReturnedPointersReferToSameThing) {
82 TestReferenceCountedSingletonFactory factory;
83 rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
84
85 EXPECT_EQ(one.get(), two.get());
86}
87
88TEST(ReferenceCountedSingletonFactory, Release) {
89 TestReferenceCountedSingletonFactory factory;
90
91 rcsf_ptr<MyExistenceWatcher> one(&factory);
92 one.get();
93
94 MyExistenceWatcher::delete_called_ = false;
95 one.release();
96 EXPECT_TRUE(MyExistenceWatcher::delete_called_);
97}
98
99TEST(ReferenceCountedSingletonFactory, GetWithoutRelease) {
100 TestReferenceCountedSingletonFactory factory;
101 rcsf_ptr<MyExistenceWatcher> one(&factory);
102 one.get();
103
104 MyExistenceWatcher::create_called_ = false;
105 one.get();
106 EXPECT_FALSE(MyExistenceWatcher::create_called_);
107}
108
109TEST(ReferenceCountedSingletonFactory, GetAfterRelease) {
110 TestReferenceCountedSingletonFactory factory;
111 rcsf_ptr<MyExistenceWatcher> one(&factory);
112
113 MyExistenceWatcher::create_called_ = false;
114 one.release();
115 one.get();
116 EXPECT_TRUE(MyExistenceWatcher::create_called_);
117}
118
119TEST(ReferenceCountedSingletonFactory, MultipleReleases) {
120 TestReferenceCountedSingletonFactory factory;
121 rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
122
123 MyExistenceWatcher::create_called_ = false;
124 MyExistenceWatcher::delete_called_ = false;
125 one.release();
126 EXPECT_FALSE(MyExistenceWatcher::delete_called_);
127 one.release();
128 EXPECT_FALSE(MyExistenceWatcher::delete_called_);
129 one.release();
130 EXPECT_FALSE(MyExistenceWatcher::delete_called_);
131 one.get();
132 EXPECT_TRUE(MyExistenceWatcher::create_called_);
133}
134
135TEST(ReferenceCountedSingletonFactory, Existentialism) {
136 TestReferenceCountedSingletonFactory factory;
137
138 rcsf_ptr<MyExistenceWatcher> one(&factory);
139
140 MyExistenceWatcher::create_called_ = false;
141 MyExistenceWatcher::delete_called_ = false;
142
143 one.get();
144 EXPECT_TRUE(MyExistenceWatcher::create_called_);
145 one.release();
146 EXPECT_TRUE(MyExistenceWatcher::delete_called_);
147}
148
149} // namespace talk_base