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