3
* Copyright 2004--2011, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#include "talk/base/gunit.h"
29
#include "talk/base/referencecountedsingletonfactory.h"
33
class MyExistenceWatcher {
35
MyExistenceWatcher() { create_called_ = true; }
36
~MyExistenceWatcher() { delete_called_ = true; }
38
static bool create_called_;
39
static bool delete_called_;
42
bool MyExistenceWatcher::create_called_ = false;
43
bool MyExistenceWatcher::delete_called_ = false;
45
class TestReferenceCountedSingletonFactory :
46
public ReferenceCountedSingletonFactory<MyExistenceWatcher> {
48
virtual bool SetupInstance() {
49
instance_.reset(new MyExistenceWatcher());
53
virtual void CleanupInstance() {
58
static void DoCreateAndGoOutOfScope(
59
ReferenceCountedSingletonFactory<MyExistenceWatcher> *factory) {
60
rcsf_ptr<MyExistenceWatcher> ptr(factory);
62
// and now ptr should go out of scope.
65
TEST(ReferenceCountedSingletonFactory, ZeroReferenceCountCausesDeletion) {
66
TestReferenceCountedSingletonFactory factory;
67
MyExistenceWatcher::delete_called_ = false;
68
DoCreateAndGoOutOfScope(&factory);
69
EXPECT_TRUE(MyExistenceWatcher::delete_called_);
72
TEST(ReferenceCountedSingletonFactory, NonZeroReferenceCountDoesNotDelete) {
73
TestReferenceCountedSingletonFactory factory;
74
rcsf_ptr<MyExistenceWatcher> ptr(&factory);
76
MyExistenceWatcher::delete_called_ = false;
77
DoCreateAndGoOutOfScope(&factory);
78
EXPECT_FALSE(MyExistenceWatcher::delete_called_);
81
TEST(ReferenceCountedSingletonFactory, ReturnedPointersReferToSameThing) {
82
TestReferenceCountedSingletonFactory factory;
83
rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
85
EXPECT_EQ(one.get(), two.get());
88
TEST(ReferenceCountedSingletonFactory, Release) {
89
TestReferenceCountedSingletonFactory factory;
91
rcsf_ptr<MyExistenceWatcher> one(&factory);
94
MyExistenceWatcher::delete_called_ = false;
96
EXPECT_TRUE(MyExistenceWatcher::delete_called_);
99
TEST(ReferenceCountedSingletonFactory, GetWithoutRelease) {
100
TestReferenceCountedSingletonFactory factory;
101
rcsf_ptr<MyExistenceWatcher> one(&factory);
104
MyExistenceWatcher::create_called_ = false;
106
EXPECT_FALSE(MyExistenceWatcher::create_called_);
109
TEST(ReferenceCountedSingletonFactory, GetAfterRelease) {
110
TestReferenceCountedSingletonFactory factory;
111
rcsf_ptr<MyExistenceWatcher> one(&factory);
113
MyExistenceWatcher::create_called_ = false;
116
EXPECT_TRUE(MyExistenceWatcher::create_called_);
119
TEST(ReferenceCountedSingletonFactory, MultipleReleases) {
120
TestReferenceCountedSingletonFactory factory;
121
rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
123
MyExistenceWatcher::create_called_ = false;
124
MyExistenceWatcher::delete_called_ = false;
126
EXPECT_FALSE(MyExistenceWatcher::delete_called_);
128
EXPECT_FALSE(MyExistenceWatcher::delete_called_);
130
EXPECT_FALSE(MyExistenceWatcher::delete_called_);
132
EXPECT_TRUE(MyExistenceWatcher::create_called_);
135
TEST(ReferenceCountedSingletonFactory, Existentialism) {
136
TestReferenceCountedSingletonFactory factory;
138
rcsf_ptr<MyExistenceWatcher> one(&factory);
140
MyExistenceWatcher::create_called_ = false;
141
MyExistenceWatcher::delete_called_ = false;
144
EXPECT_TRUE(MyExistenceWatcher::create_called_);
146
EXPECT_TRUE(MyExistenceWatcher::delete_called_);
149
} // namespace talk_base