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/logging.h" |
| 6 #include "base/bind.h" |
| 7 #include "base/callback.h" |
5 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
6 #include "base/logging.h" | |
7 #include "base/macros.h" | 9 #include "base/macros.h" |
8 | 10 |
9 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 #if defined(OS_POSIX) | 14 #if defined(OS_POSIX) |
13 #include <signal.h> | 15 #include <signal.h> |
14 #include <unistd.h> | 16 #include <unistd.h> |
15 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
16 #endif // OS_POSIX | 18 #endif // OS_POSIX |
(...skipping 10 matching lines...) Expand all Loading... |
27 namespace logging { | 29 namespace logging { |
28 | 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 using ::testing::Return; | 33 using ::testing::Return; |
32 | 34 |
33 // Needs to be global since log assert handlers can't maintain state. | 35 // Needs to be global since log assert handlers can't maintain state. |
34 int log_sink_call_count = 0; | 36 int log_sink_call_count = 0; |
35 | 37 |
36 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) | 38 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
37 void LogSink(const std::string& str) { | 39 void LogSink(const char* file, |
| 40 int line, |
| 41 const base::StringPiece& message, |
| 42 const base::StringPiece& stack_trace) { |
38 ++log_sink_call_count; | 43 ++log_sink_call_count; |
39 } | 44 } |
40 #endif | 45 #endif |
41 | 46 |
42 // Class to make sure any manipulations we do to the min log level are | 47 // Class to make sure any manipulations we do to the min log level are |
43 // contained (i.e., do not affect other unit tests). | 48 // contained (i.e., do not affect other unit tests). |
44 class LogStateSaver { | 49 class LogStateSaver { |
45 public: | 50 public: |
46 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} | 51 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} |
47 | 52 |
48 ~LogStateSaver() { | 53 ~LogStateSaver() { |
49 SetMinLogLevel(old_min_log_level_); | 54 SetMinLogLevel(old_min_log_level_); |
50 SetLogAssertHandler(NULL); | |
51 log_sink_call_count = 0; | 55 log_sink_call_count = 0; |
52 } | 56 } |
53 | 57 |
54 private: | 58 private: |
55 int old_min_log_level_; | 59 int old_min_log_level_; |
56 | 60 |
57 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); | 61 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); |
58 }; | 62 }; |
59 | 63 |
60 class LoggingTest : public testing::Test { | 64 class LoggingTest : public testing::Test { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 | 188 |
185 // Official builds have CHECKs directly call BreakDebugger. | 189 // Official builds have CHECKs directly call BreakDebugger. |
186 #if !defined(OFFICIAL_BUILD) | 190 #if !defined(OFFICIAL_BUILD) |
187 | 191 |
188 TEST_F(LoggingTest, CheckStreamsAreLazy) { | 192 TEST_F(LoggingTest, CheckStreamsAreLazy) { |
189 MockLogSource mock_log_source, uncalled_mock_log_source; | 193 MockLogSource mock_log_source, uncalled_mock_log_source; |
190 EXPECT_CALL(mock_log_source, Log()).Times(8). | 194 EXPECT_CALL(mock_log_source, Log()).Times(8). |
191 WillRepeatedly(Return("check message")); | 195 WillRepeatedly(Return("check message")); |
192 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); | 196 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); |
193 | 197 |
194 SetLogAssertHandler(&LogSink); | 198 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
195 | 199 |
196 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); | 200 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); |
197 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); | 201 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); |
198 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) | 202 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) |
199 << uncalled_mock_log_source.Log(); | 203 << uncalled_mock_log_source.Log(); |
200 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) | 204 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) |
201 << mock_log_source.Log(); | 205 << mock_log_source.Log(); |
202 } | 206 } |
203 | 207 |
204 #endif | 208 #endif |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 } | 390 } |
387 void DcheckEmptyFunction2() {} | 391 void DcheckEmptyFunction2() {} |
388 | 392 |
389 TEST_F(LoggingTest, Dcheck) { | 393 TEST_F(LoggingTest, Dcheck) { |
390 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 394 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
391 // Release build. | 395 // Release build. |
392 EXPECT_FALSE(DCHECK_IS_ON()); | 396 EXPECT_FALSE(DCHECK_IS_ON()); |
393 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 397 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); |
394 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) | 398 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) |
395 // Release build with real DCHECKS. | 399 // Release build with real DCHECKS. |
396 SetLogAssertHandler(&LogSink); | 400 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
397 EXPECT_TRUE(DCHECK_IS_ON()); | 401 EXPECT_TRUE(DCHECK_IS_ON()); |
398 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 402 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
399 #else | 403 #else |
400 // Debug build. | 404 // Debug build. |
401 SetLogAssertHandler(&LogSink); | 405 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
402 EXPECT_TRUE(DCHECK_IS_ON()); | 406 EXPECT_TRUE(DCHECK_IS_ON()); |
403 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 407 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
404 #endif | 408 #endif |
405 | 409 |
406 EXPECT_EQ(0, log_sink_call_count); | 410 EXPECT_EQ(0, log_sink_call_count); |
407 DCHECK(false); | 411 DCHECK(false); |
408 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 412 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); |
409 DPCHECK(false); | 413 DPCHECK(false); |
410 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | 414 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); |
411 DCHECK_EQ(0, 1); | 415 DCHECK_EQ(0, 1); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 std::wstring wstr = L"Hello World"; | 506 std::wstring wstr = L"Hello World"; |
503 std::ostringstream ostr; | 507 std::ostringstream ostr; |
504 ostr << wstr; | 508 ostr << wstr; |
505 EXPECT_EQ("Hello World", ostr.str()); | 509 EXPECT_EQ("Hello World", ostr.str()); |
506 } | 510 } |
507 } // namespace nested_test | 511 } // namespace nested_test |
508 | 512 |
509 } // namespace | 513 } // namespace |
510 | 514 |
511 } // namespace logging | 515 } // namespace logging |
OLD | NEW |