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 |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 struct LeakTrait : public CallbackTrait { | 38 struct LeakTrait : public CallbackTrait { |
39 static const bool kRegisterAtExit = false; | 39 static const bool kRegisterAtExit = false; |
40 }; | 40 }; |
41 | 41 |
42 int* SingletonInt1() { | 42 int* SingletonInt1() { |
43 return Singleton<int>::get(); | 43 return Singleton<int>::get(); |
44 } | 44 } |
45 | 45 |
46 int* SingletonInt2() { | 46 int* SingletonInt2() { |
| 47 typedef int FooType; |
47 // Force to use a different singleton than SingletonInt1. | 48 // Force to use a different singleton than SingletonInt1. |
48 return Singleton<int, DefaultSingletonTraits<int> >::get(); | 49 return Singleton<FooType>::get(); |
49 } | 50 } |
50 | 51 |
51 class DummyDifferentiatingClass { | 52 class DummyDifferentiatingClass { |
52 }; | 53 }; |
53 | 54 |
54 int* SingletonInt3() { | 55 int* SingletonInt3() { |
55 // Force to use a different singleton than SingletonInt1 and SingletonInt2. | 56 // Force to use a different singleton than SingletonInt1 and SingletonInt2. |
56 // Note that any type can be used; int, float, std::wstring... | 57 // Note that any type can be used; int, float, std::wstring... |
57 return Singleton<int, DefaultSingletonTraits<int>, | 58 return Singleton<int, DefaultSingletonTraits<int>, |
58 DummyDifferentiatingClass>::get(); | 59 DummyDifferentiatingClass>::get(); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 int* singleton_int_4; | 128 int* singleton_int_4; |
128 int* singleton_int_5; | 129 int* singleton_int_5; |
129 CallbackFunc* leaky_singleton; | 130 CallbackFunc* leaky_singleton; |
130 | 131 |
131 { | 132 { |
132 base::ShadowingAtExitManager sem; | 133 base::ShadowingAtExitManager sem; |
133 { | 134 { |
134 singleton_int_1 = SingletonInt1(); | 135 singleton_int_1 = SingletonInt1(); |
135 } | 136 } |
136 // Ensure POD type initialization. | 137 // Ensure POD type initialization. |
137 EXPECT_EQ(*singleton_int_1, 0); | 138 EXPECT_EQ(0, *singleton_int_1); |
138 *singleton_int_1 = 1; | 139 *singleton_int_1 = 1; |
139 | 140 |
140 EXPECT_EQ(singleton_int_1, SingletonInt1()); | 141 EXPECT_EQ(singleton_int_1, SingletonInt1()); |
141 EXPECT_EQ(*singleton_int_1, 1); | 142 EXPECT_EQ(1, *singleton_int_1); |
142 | 143 |
143 { | 144 { |
144 singleton_int_2 = SingletonInt2(); | 145 singleton_int_2 = SingletonInt2(); |
145 } | 146 } |
146 // Same instance that 1. | 147 // Same instance that 1. |
147 EXPECT_EQ(*singleton_int_2, 1); | 148 EXPECT_EQ(1, *singleton_int_2); |
148 EXPECT_EQ(singleton_int_1, singleton_int_2); | 149 EXPECT_EQ(singleton_int_1, singleton_int_2); |
149 | 150 |
150 { | 151 { |
151 singleton_int_3 = SingletonInt3(); | 152 singleton_int_3 = SingletonInt3(); |
152 } | 153 } |
153 // Different instance than 1 and 2. | 154 // Different instance than 1 and 2. |
154 EXPECT_EQ(*singleton_int_3, 0); | 155 EXPECT_EQ(0, *singleton_int_3); |
155 EXPECT_NE(singleton_int_1, singleton_int_3); | 156 EXPECT_NE(singleton_int_1, singleton_int_3); |
156 *singleton_int_3 = 3; | 157 *singleton_int_3 = 3; |
157 EXPECT_EQ(*singleton_int_1, 1); | 158 EXPECT_EQ(1, *singleton_int_1); |
158 EXPECT_EQ(*singleton_int_2, 1); | 159 EXPECT_EQ(1, *singleton_int_2); |
159 | 160 |
160 { | 161 { |
161 singleton_int_4 = SingletonInt4(); | 162 singleton_int_4 = SingletonInt4(); |
162 } | 163 } |
163 // Use a lock for creation. Not really tested at length. | 164 // Use a lock for creation. Not really tested at length. |
164 EXPECT_EQ(*singleton_int_4, 0); | 165 EXPECT_EQ(*singleton_int_4, 0); |
165 *singleton_int_4 = 4; | 166 *singleton_int_4 = 4; |
166 EXPECT_NE(singleton_int_1, singleton_int_4); | 167 EXPECT_NE(singleton_int_1, singleton_int_4); |
167 EXPECT_NE(singleton_int_3, singleton_int_4); | 168 EXPECT_NE(singleton_int_3, singleton_int_4); |
168 | 169 |
(...skipping 26 matching lines...) Expand all Loading... |
195 EXPECT_EQ(*singleton_int_1, 0); | 196 EXPECT_EQ(*singleton_int_1, 0); |
196 } | 197 } |
197 { | 198 { |
198 singleton_int_5 = SingletonInt5(); | 199 singleton_int_5 = SingletonInt5(); |
199 EXPECT_EQ(*singleton_int_5, 5); | 200 EXPECT_EQ(*singleton_int_5, 5); |
200 } | 201 } |
201 } | 202 } |
202 // The leaky singleton shouldn't leak since SingletonLeak has not been called. | 203 // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
203 VerifiesCallbacksNotCalled(); | 204 VerifiesCallbacksNotCalled(); |
204 } | 205 } |
OLD | NEW |