OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/at_exit.h" | 5 #include "base/at_exit.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/singleton.h" | 8 #include "base/singleton.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 class ShadowingAtExitManager : public base::AtExitManager { | |
14 public: | |
15 ShadowingAtExitManager() : AtExitManager(true) { } | |
16 }; | |
17 | |
18 COMPILE_ASSERT(DefaultSingletonTraits<int>::kRegisterAtExit == true, a); | 13 COMPILE_ASSERT(DefaultSingletonTraits<int>::kRegisterAtExit == true, a); |
19 | 14 |
20 template<typename Type> | 15 template<typename Type> |
21 struct LockTrait : public DefaultSingletonTraits<Type> { | 16 struct LockTrait : public DefaultSingletonTraits<Type> { |
22 }; | 17 }; |
23 | 18 |
24 struct Init5Trait : public DefaultSingletonTraits<int> { | 19 struct Init5Trait : public DefaultSingletonTraits<int> { |
25 static int* New() { | 20 static int* New() { |
26 return new int(5); | 21 return new int(5); |
27 } | 22 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 122 |
128 TEST_F(SingletonTest, Basic) { | 123 TEST_F(SingletonTest, Basic) { |
129 int* singleton_int_1; | 124 int* singleton_int_1; |
130 int* singleton_int_2; | 125 int* singleton_int_2; |
131 int* singleton_int_3; | 126 int* singleton_int_3; |
132 int* singleton_int_4; | 127 int* singleton_int_4; |
133 int* singleton_int_5; | 128 int* singleton_int_5; |
134 CallbackFunc* leaky_singleton; | 129 CallbackFunc* leaky_singleton; |
135 | 130 |
136 { | 131 { |
137 ShadowingAtExitManager sem; | 132 base::ShadowingAtExitManager sem; |
138 { | 133 { |
139 singleton_int_1 = SingletonInt1(); | 134 singleton_int_1 = SingletonInt1(); |
140 } | 135 } |
141 // Ensure POD type initialization. | 136 // Ensure POD type initialization. |
142 EXPECT_EQ(*singleton_int_1, 0); | 137 EXPECT_EQ(*singleton_int_1, 0); |
143 *singleton_int_1 = 1; | 138 *singleton_int_1 = 1; |
144 | 139 |
145 EXPECT_EQ(singleton_int_1, SingletonInt1()); | 140 EXPECT_EQ(singleton_int_1, SingletonInt1()); |
146 EXPECT_EQ(*singleton_int_1, 1); | 141 EXPECT_EQ(*singleton_int_1, 1); |
147 | 142 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 EXPECT_TRUE(leaky_singleton); | 181 EXPECT_TRUE(leaky_singleton); |
187 } | 182 } |
188 | 183 |
189 // Verify that only the expected callback has been called. | 184 // Verify that only the expected callback has been called. |
190 VerifiesCallbacks(); | 185 VerifiesCallbacks(); |
191 // Delete the leaky singleton. It is interesting to note that Purify does | 186 // Delete the leaky singleton. It is interesting to note that Purify does |
192 // *not* detect the leak when this call is commented out. :( | 187 // *not* detect the leak when this call is commented out. :( |
193 DefaultSingletonTraits<CallbackFunc>::Delete(leaky_singleton); | 188 DefaultSingletonTraits<CallbackFunc>::Delete(leaky_singleton); |
194 | 189 |
195 { | 190 { |
196 ShadowingAtExitManager sem; | 191 base::ShadowingAtExitManager sem; |
197 // Verifiy that the variables were reset. | 192 // Verifiy that the variables were reset. |
198 { | 193 { |
199 singleton_int_1 = SingletonInt1(); | 194 singleton_int_1 = SingletonInt1(); |
200 EXPECT_EQ(*singleton_int_1, 0); | 195 EXPECT_EQ(*singleton_int_1, 0); |
201 } | 196 } |
202 { | 197 { |
203 singleton_int_5 = SingletonInt5(); | 198 singleton_int_5 = SingletonInt5(); |
204 EXPECT_EQ(*singleton_int_5, 5); | 199 EXPECT_EQ(*singleton_int_5, 5); |
205 } | 200 } |
206 } | 201 } |
207 // The leaky singleton shouldn't leak since SingletonLeak has not been called. | 202 // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
208 VerifiesCallbacksNotCalled(); | 203 VerifiesCallbacksNotCalled(); |
209 } | 204 } |
OLD | NEW |