license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 5 | #include "base/at_exit.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | #include "base/file_util.h" |
| 7 | #include "base/path_service.h" |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 8 | #include "base/singleton.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 13 | COMPILE_ASSERT(DefaultSingletonTraits<int>::kRegisterAtExit == true, a); |
| 14 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 15 | typedef void (*CallbackFunc)(); |
| 16 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 17 | class IntSingleton { |
| 18 | public: |
| 19 | static IntSingleton* GetInstance() { |
| 20 | return Singleton<IntSingleton>::get(); |
| 21 | } |
| 22 | |
| 23 | int value_; |
| 24 | }; |
| 25 | |
| 26 | class Init5Singleton { |
| 27 | public: |
| 28 | struct Trait; |
| 29 | |
| 30 | static Init5Singleton* GetInstance() { |
| 31 | return Singleton<Init5Singleton, Trait>::get(); |
| 32 | } |
| 33 | |
| 34 | int value_; |
| 35 | }; |
| 36 | |
| 37 | struct Init5Singleton::Trait : public DefaultSingletonTraits<Init5Singleton> { |
| 38 | static Init5Singleton* New() { |
| 39 | Init5Singleton* instance = new Init5Singleton(); |
| 40 | instance->value_ = 5; |
| 41 | return instance; |
[email protected] | 5820d2f0 | 2010-12-11 10:23:37 | [diff] [blame] | 42 | } |
| 43 | }; |
[email protected] | 49ab556 | 2010-12-11 09:13:54 | [diff] [blame] | 44 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 45 | int* SingletonInt() { |
| 46 | return &IntSingleton::GetInstance()->value_; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int* SingletonInt5() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 50 | return &Init5Singleton::GetInstance()->value_; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 53 | template <typename Type> |
| 54 | struct CallbackTrait : public DefaultSingletonTraits<Type> { |
| 55 | static void Delete(Type* instance) { |
| 56 | if (instance->callback_) |
| 57 | (instance->callback_)(); |
| 58 | DefaultSingletonTraits<Type>::Delete(instance); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | class CallbackSingleton { |
| 63 | public: |
| 64 | CallbackSingleton() : callback_(NULL) { } |
| 65 | CallbackFunc callback_; |
| 66 | }; |
| 67 | |
| 68 | class CallbackSingletonWithNoLeakTrait : public CallbackSingleton { |
| 69 | public: |
| 70 | struct Trait : public CallbackTrait<CallbackSingletonWithNoLeakTrait> { }; |
| 71 | |
| 72 | CallbackSingletonWithNoLeakTrait() : CallbackSingleton() { } |
| 73 | |
| 74 | static CallbackSingletonWithNoLeakTrait* GetInstance() { |
| 75 | return Singleton<CallbackSingletonWithNoLeakTrait, Trait>::get(); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | class CallbackSingletonWithLeakTrait : public CallbackSingleton { |
| 80 | public: |
| 81 | struct Trait : public CallbackTrait<CallbackSingletonWithLeakTrait> { |
| 82 | static const bool kRegisterAtExit = false; |
| 83 | }; |
| 84 | |
| 85 | CallbackSingletonWithLeakTrait() : CallbackSingleton() { } |
| 86 | |
| 87 | static CallbackSingletonWithLeakTrait* GetInstance() { |
| 88 | return Singleton<CallbackSingletonWithLeakTrait, Trait>::get(); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | class CallbackSingletonWithStaticTrait : public CallbackSingleton { |
| 93 | public: |
| 94 | struct Trait; |
| 95 | |
| 96 | CallbackSingletonWithStaticTrait() : CallbackSingleton() { } |
| 97 | |
| 98 | static CallbackSingletonWithStaticTrait* GetInstance() { |
| 99 | return Singleton<CallbackSingletonWithStaticTrait, Trait>::get(); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | struct CallbackSingletonWithStaticTrait::Trait |
| 104 | : public StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait> { |
| 105 | static void Delete(CallbackSingletonWithStaticTrait* instance) { |
| 106 | if (instance->callback_) |
| 107 | (instance->callback_)(); |
| 108 | StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait>::Delete( |
| 109 | instance); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 114 | void SingletonNoLeak(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 115 | CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void SingletonLeak(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 119 | CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | CallbackFunc* GetLeakySingleton() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 123 | return &CallbackSingletonWithLeakTrait::GetInstance()->callback_; |
| 124 | } |
| 125 | |
| 126 | void DeleteLeakySingleton() { |
| 127 | DefaultSingletonTraits<CallbackSingletonWithLeakTrait>::Delete( |
| 128 | CallbackSingletonWithLeakTrait::GetInstance()); |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 131 | void SingletonStatic(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 132 | CallbackSingletonWithStaticTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | CallbackFunc* GetStaticSingleton() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 136 | return &CallbackSingletonWithStaticTrait::GetInstance()->callback_; |
| 137 | } |
| 138 | |
| 139 | void ResurrectStaticSingleton() { |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 142 | } // namespace |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | |
| 144 | class SingletonTest : public testing::Test { |
| 145 | public: |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 146 | SingletonTest() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 147 | |
| 148 | virtual void SetUp() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | non_leak_called_ = false; |
| 150 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 151 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | } |
| 153 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 154 | protected: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 155 | void VerifiesCallbacks() { |
| 156 | EXPECT_TRUE(non_leak_called_); |
| 157 | EXPECT_FALSE(leaky_called_); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 158 | EXPECT_TRUE(static_called_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 159 | non_leak_called_ = false; |
| 160 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 161 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void VerifiesCallbacksNotCalled() { |
| 165 | EXPECT_FALSE(non_leak_called_); |
| 166 | EXPECT_FALSE(leaky_called_); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 167 | EXPECT_FALSE(static_called_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 168 | non_leak_called_ = false; |
| 169 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 170 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 173 | static void CallbackNoLeak() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 174 | non_leak_called_ = true; |
| 175 | } |
| 176 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 177 | static void CallbackLeak() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | leaky_called_ = true; |
| 179 | } |
| 180 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 181 | static void CallbackStatic() { |
| 182 | static_called_ = true; |
| 183 | } |
| 184 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 185 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 186 | static bool non_leak_called_; |
| 187 | static bool leaky_called_; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 188 | static bool static_called_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | bool SingletonTest::non_leak_called_ = false; |
| 192 | bool SingletonTest::leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 193 | bool SingletonTest::static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 194 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 195 | TEST_F(SingletonTest, Basic) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 196 | int* singleton_int; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 197 | int* singleton_int_5; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 198 | CallbackFunc* leaky_singleton; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 199 | CallbackFunc* static_singleton; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 200 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 201 | { |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 202 | base::ShadowingAtExitManager sem; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 204 | singleton_int = SingletonInt(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | } |
| 206 | // Ensure POD type initialization. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 207 | EXPECT_EQ(*singleton_int, 0); |
| 208 | *singleton_int = 1; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 210 | EXPECT_EQ(singleton_int, SingletonInt()); |
| 211 | EXPECT_EQ(*singleton_int, 1); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 212 | |
| 213 | { |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 214 | singleton_int_5 = SingletonInt5(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | } |
| 216 | // Is default initialized to 5. |
| 217 | EXPECT_EQ(*singleton_int_5, 5); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 218 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 219 | SingletonNoLeak(&CallbackNoLeak); |
| 220 | SingletonLeak(&CallbackLeak); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 221 | SingletonStatic(&CallbackStatic); |
| 222 | static_singleton = GetStaticSingleton(); |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 223 | leaky_singleton = GetLeakySingleton(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 224 | EXPECT_TRUE(leaky_singleton); |
| 225 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | |
| 227 | // Verify that only the expected callback has been called. |
| 228 | VerifiesCallbacks(); |
| 229 | // Delete the leaky singleton. It is interesting to note that Purify does |
| 230 | // *not* detect the leak when this call is commented out. :( |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 231 | DeleteLeakySingleton(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 232 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 233 | // The static singleton can't be acquired post-atexit. |
| 234 | EXPECT_EQ(NULL, GetStaticSingleton()); |
| 235 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 236 | { |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 237 | base::ShadowingAtExitManager sem; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 238 | // Verifiy that the variables were reset. |
| 239 | { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 240 | singleton_int = SingletonInt(); |
| 241 | EXPECT_EQ(*singleton_int, 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | } |
| 243 | { |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 244 | singleton_int_5 = SingletonInt5(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 245 | EXPECT_EQ(*singleton_int_5, 5); |
| 246 | } |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 247 | { |
| 248 | // Resurrect the static singleton, and assert that it |
| 249 | // still points to the same (static) memory. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame^] | 250 | CallbackSingletonWithStaticTrait::Trait::Resurrect(); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 251 | EXPECT_EQ(GetStaticSingleton(), static_singleton); |
| 252 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 253 | } |
| 254 | // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | VerifiesCallbacksNotCalled(); |
| 256 | } |