OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bind.h" |
| 6 #include "base/callback.h" |
5 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
6 #include "base/logging.h" | 8 #include "base/logging.h" |
7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/test/logging_utils.h" |
8 | 11 |
9 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 | 14 |
12 namespace logging { | 15 namespace logging { |
13 | 16 |
14 namespace { | 17 namespace { |
15 | 18 |
16 using ::testing::Return; | 19 using ::testing::Return; |
17 | 20 |
18 // Needs to be global since log assert handlers can't maintain state. | 21 // Needs to be global since log assert handlers can't maintain state. |
19 int log_sink_call_count = 0; | 22 int log_sink_call_count = 0; |
20 | 23 |
21 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) | 24 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
22 void LogSink(const std::string& str) { | 25 void LogSink(const char* file, |
| 26 int line, |
| 27 size_t message_start, |
| 28 size_t stack_start, |
| 29 const std::string& str) { |
23 ++log_sink_call_count; | 30 ++log_sink_call_count; |
24 } | 31 } |
25 #endif | 32 #endif |
26 | 33 |
27 // Class to make sure any manipulations we do to the min log level are | 34 // Class to make sure any manipulations we do to the min log level are |
28 // contained (i.e., do not affect other unit tests). | 35 // contained (i.e., do not affect other unit tests). |
29 class LogStateSaver { | 36 class LogStateSaver { |
30 public: | 37 public: |
31 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} | 38 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} |
32 | 39 |
33 ~LogStateSaver() { | 40 ~LogStateSaver() { |
34 SetMinLogLevel(old_min_log_level_); | 41 SetMinLogLevel(old_min_log_level_); |
35 SetLogAssertHandler(NULL); | |
36 log_sink_call_count = 0; | 42 log_sink_call_count = 0; |
37 } | 43 } |
38 | 44 |
39 private: | 45 private: |
40 int old_min_log_level_; | 46 int old_min_log_level_; |
41 | 47 |
42 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); | 48 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); |
43 }; | 49 }; |
44 | 50 |
45 class LoggingTest : public testing::Test { | 51 class LoggingTest : public testing::Test { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 177 |
172 // Official builds have CHECKs directly call BreakDebugger. | 178 // Official builds have CHECKs directly call BreakDebugger. |
173 #if !defined(OFFICIAL_BUILD) | 179 #if !defined(OFFICIAL_BUILD) |
174 | 180 |
175 TEST_F(LoggingTest, CheckStreamsAreLazy) { | 181 TEST_F(LoggingTest, CheckStreamsAreLazy) { |
176 MockLogSource mock_log_source, uncalled_mock_log_source; | 182 MockLogSource mock_log_source, uncalled_mock_log_source; |
177 EXPECT_CALL(mock_log_source, Log()).Times(8). | 183 EXPECT_CALL(mock_log_source, Log()).Times(8). |
178 WillRepeatedly(Return("check message")); | 184 WillRepeatedly(Return("check message")); |
179 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); | 185 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); |
180 | 186 |
181 SetLogAssertHandler(&LogSink); | 187 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
182 | 188 |
183 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); | 189 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); |
184 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); | 190 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); |
185 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) | 191 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) |
186 << uncalled_mock_log_source.Log(); | 192 << uncalled_mock_log_source.Log(); |
187 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) | 193 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) |
188 << mock_log_source.Log(); | 194 << mock_log_source.Log(); |
189 } | 195 } |
190 | 196 |
191 #endif | 197 #endif |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 } | 231 } |
226 void DcheckEmptyFunction2() {} | 232 void DcheckEmptyFunction2() {} |
227 | 233 |
228 TEST_F(LoggingTest, Dcheck) { | 234 TEST_F(LoggingTest, Dcheck) { |
229 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 235 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
230 // Release build. | 236 // Release build. |
231 EXPECT_FALSE(DCHECK_IS_ON()); | 237 EXPECT_FALSE(DCHECK_IS_ON()); |
232 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 238 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); |
233 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) | 239 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) |
234 // Release build with real DCHECKS. | 240 // Release build with real DCHECKS. |
235 SetLogAssertHandler(&LogSink); | 241 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
236 EXPECT_TRUE(DCHECK_IS_ON()); | 242 EXPECT_TRUE(DCHECK_IS_ON()); |
237 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 243 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
238 #else | 244 #else |
239 // Debug build. | 245 // Debug build. |
240 SetLogAssertHandler(&LogSink); | 246 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
241 EXPECT_TRUE(DCHECK_IS_ON()); | 247 EXPECT_TRUE(DCHECK_IS_ON()); |
242 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 248 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
243 #endif | 249 #endif |
244 | 250 |
245 EXPECT_EQ(0, log_sink_call_count); | 251 EXPECT_EQ(0, log_sink_call_count); |
246 DCHECK(false); | 252 DCHECK(false); |
247 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 253 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); |
248 DPCHECK(false); | 254 DPCHECK(false); |
249 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | 255 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); |
250 DCHECK_EQ(0, 1); | 256 DCHECK_EQ(0, 1); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 std::wstring wstr = L"Hello World"; | 347 std::wstring wstr = L"Hello World"; |
342 std::ostringstream ostr; | 348 std::ostringstream ostr; |
343 ostr << wstr; | 349 ostr << wstr; |
344 EXPECT_EQ("Hello World", ostr.str()); | 350 EXPECT_EQ("Hello World", ostr.str()); |
345 } | 351 } |
346 } // namespace nested_test | 352 } // namespace nested_test |
347 | 353 |
348 } // namespace | 354 } // namespace |
349 | 355 |
350 } // namespace logging | 356 } // namespace logging |
OLD | NEW |