Add TriggerOnUninstalled to ExtensionRegistry.
[chromium-blink-merge.git] / base / logging_unittest.cc
blob44a1be3d71e57c90497bdf30a8674a72a01af0e5
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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/logging.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace logging {
13 namespace {
15 using ::testing::Return;
17 // Needs to be global since log assert handlers can't maintain state.
18 int log_sink_call_count = 0;
20 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG)
21 void LogSink(const std::string& str) {
22 ++log_sink_call_count;
24 #endif
26 // Class to make sure any manipulations we do to the min log level are
27 // contained (i.e., do not affect other unit tests).
28 class LogStateSaver {
29 public:
30 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {}
32 ~LogStateSaver() {
33 SetMinLogLevel(old_min_log_level_);
34 SetLogAssertHandler(NULL);
35 SetLogReportHandler(NULL);
36 log_sink_call_count = 0;
39 private:
40 int old_min_log_level_;
42 DISALLOW_COPY_AND_ASSIGN(LogStateSaver);
45 class LoggingTest : public testing::Test {
46 private:
47 LogStateSaver log_state_saver_;
50 class MockLogSource {
51 public:
52 MOCK_METHOD0(Log, const char*());
55 TEST_F(LoggingTest, BasicLogging) {
56 MockLogSource mock_log_source;
57 EXPECT_CALL(mock_log_source, Log()).Times(DEBUG_MODE ? 16 : 8).
58 WillRepeatedly(Return("log message"));
60 SetMinLogLevel(LOG_INFO);
62 EXPECT_TRUE(LOG_IS_ON(INFO));
63 // As of g++-4.5, the first argument to EXPECT_EQ cannot be a
64 // constant expression.
65 const bool kIsDebugMode = (DEBUG_MODE != 0);
66 EXPECT_TRUE(kIsDebugMode == DLOG_IS_ON(INFO));
67 EXPECT_TRUE(VLOG_IS_ON(0));
69 LOG(INFO) << mock_log_source.Log();
70 LOG_IF(INFO, true) << mock_log_source.Log();
71 PLOG(INFO) << mock_log_source.Log();
72 PLOG_IF(INFO, true) << mock_log_source.Log();
73 VLOG(0) << mock_log_source.Log();
74 VLOG_IF(0, true) << mock_log_source.Log();
75 VPLOG(0) << mock_log_source.Log();
76 VPLOG_IF(0, true) << mock_log_source.Log();
78 DLOG(INFO) << mock_log_source.Log();
79 DLOG_IF(INFO, true) << mock_log_source.Log();
80 DPLOG(INFO) << mock_log_source.Log();
81 DPLOG_IF(INFO, true) << mock_log_source.Log();
82 DVLOG(0) << mock_log_source.Log();
83 DVLOG_IF(0, true) << mock_log_source.Log();
84 DVPLOG(0) << mock_log_source.Log();
85 DVPLOG_IF(0, true) << mock_log_source.Log();
88 TEST_F(LoggingTest, LogIsOn) {
89 #if defined(NDEBUG)
90 const bool kDfatalIsFatal = false;
91 #else // defined(NDEBUG)
92 const bool kDfatalIsFatal = true;
93 #endif // defined(NDEBUG)
95 SetMinLogLevel(LOG_INFO);
96 EXPECT_TRUE(LOG_IS_ON(INFO));
97 EXPECT_TRUE(LOG_IS_ON(WARNING));
98 EXPECT_TRUE(LOG_IS_ON(ERROR));
99 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
100 EXPECT_TRUE(LOG_IS_ON(FATAL));
101 EXPECT_TRUE(LOG_IS_ON(DFATAL));
103 SetMinLogLevel(LOG_WARNING);
104 EXPECT_FALSE(LOG_IS_ON(INFO));
105 EXPECT_TRUE(LOG_IS_ON(WARNING));
106 EXPECT_TRUE(LOG_IS_ON(ERROR));
107 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
108 EXPECT_TRUE(LOG_IS_ON(FATAL));
109 EXPECT_TRUE(LOG_IS_ON(DFATAL));
111 SetMinLogLevel(LOG_ERROR);
112 EXPECT_FALSE(LOG_IS_ON(INFO));
113 EXPECT_FALSE(LOG_IS_ON(WARNING));
114 EXPECT_TRUE(LOG_IS_ON(ERROR));
115 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
116 EXPECT_TRUE(LOG_IS_ON(FATAL));
117 EXPECT_TRUE(LOG_IS_ON(DFATAL));
119 SetMinLogLevel(LOG_ERROR_REPORT);
120 EXPECT_FALSE(LOG_IS_ON(INFO));
121 EXPECT_FALSE(LOG_IS_ON(WARNING));
122 EXPECT_FALSE(LOG_IS_ON(ERROR));
123 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
124 EXPECT_TRUE(LOG_IS_ON(FATAL));
125 EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL));
127 // LOG_IS_ON(ERROR_REPORT) should always be true.
128 SetMinLogLevel(LOG_FATAL);
129 EXPECT_FALSE(LOG_IS_ON(INFO));
130 EXPECT_FALSE(LOG_IS_ON(WARNING));
131 EXPECT_FALSE(LOG_IS_ON(ERROR));
132 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
133 EXPECT_TRUE(LOG_IS_ON(FATAL));
134 EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL));
136 // So should LOG_IS_ON(FATAL).
137 SetMinLogLevel(LOG_FATAL + 1);
138 EXPECT_FALSE(LOG_IS_ON(INFO));
139 EXPECT_FALSE(LOG_IS_ON(WARNING));
140 EXPECT_FALSE(LOG_IS_ON(ERROR));
141 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
142 EXPECT_TRUE(LOG_IS_ON(FATAL));
143 EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL));
146 TEST_F(LoggingTest, LoggingIsLazy) {
147 MockLogSource mock_log_source;
148 EXPECT_CALL(mock_log_source, Log()).Times(0);
150 SetMinLogLevel(LOG_WARNING);
152 EXPECT_FALSE(LOG_IS_ON(INFO));
153 EXPECT_FALSE(DLOG_IS_ON(INFO));
154 EXPECT_FALSE(VLOG_IS_ON(1));
156 LOG(INFO) << mock_log_source.Log();
157 LOG_IF(INFO, false) << mock_log_source.Log();
158 PLOG(INFO) << mock_log_source.Log();
159 PLOG_IF(INFO, false) << mock_log_source.Log();
160 VLOG(1) << mock_log_source.Log();
161 VLOG_IF(1, true) << mock_log_source.Log();
162 VPLOG(1) << mock_log_source.Log();
163 VPLOG_IF(1, true) << mock_log_source.Log();
165 DLOG(INFO) << mock_log_source.Log();
166 DLOG_IF(INFO, true) << mock_log_source.Log();
167 DPLOG(INFO) << mock_log_source.Log();
168 DPLOG_IF(INFO, true) << mock_log_source.Log();
169 DVLOG(1) << mock_log_source.Log();
170 DVLOG_IF(1, true) << mock_log_source.Log();
171 DVPLOG(1) << mock_log_source.Log();
172 DVPLOG_IF(1, true) << mock_log_source.Log();
175 // Official builds have CHECKs directly call BreakDebugger.
176 #if !defined(OFFICIAL_BUILD)
178 TEST_F(LoggingTest, CheckStreamsAreLazy) {
179 MockLogSource mock_log_source, uncalled_mock_log_source;
180 EXPECT_CALL(mock_log_source, Log()).Times(8).
181 WillRepeatedly(Return("check message"));
182 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0);
184 SetLogAssertHandler(&LogSink);
186 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log();
187 PCHECK(!mock_log_source.Log()) << mock_log_source.Log();
188 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log())
189 << uncalled_mock_log_source.Log();
190 CHECK_NE(mock_log_source.Log(), mock_log_source.Log())
191 << mock_log_source.Log();
194 #endif
196 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
197 #if !defined(NDEBUG)
198 int debug_only_variable = 1;
199 #endif
200 // These should avoid emitting references to |debug_only_variable|
201 // in release mode.
202 DLOG_IF(INFO, debug_only_variable) << "test";
203 DLOG_ASSERT(debug_only_variable) << "test";
204 DPLOG_IF(INFO, debug_only_variable) << "test";
205 DVLOG_IF(1, debug_only_variable) << "test";
208 TEST_F(LoggingTest, DcheckStreamsAreLazy) {
209 MockLogSource mock_log_source;
210 EXPECT_CALL(mock_log_source, Log()).Times(0);
211 #if DCHECK_IS_ON
212 DCHECK(true) << mock_log_source.Log();
213 DCHECK_EQ(0, 0) << mock_log_source.Log();
214 #else
215 DCHECK(mock_log_source.Log()) << mock_log_source.Log();
216 DPCHECK(mock_log_source.Log()) << mock_log_source.Log();
217 DCHECK_EQ(0, 0) << mock_log_source.Log();
218 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL))
219 << mock_log_source.Log();
220 #endif
223 TEST_F(LoggingTest, Dcheck) {
224 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
225 // Release build.
226 EXPECT_FALSE(DCHECK_IS_ON);
227 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
228 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON)
229 // Release build with real DCHECKS.
230 SetLogAssertHandler(&LogSink);
231 EXPECT_TRUE(DCHECK_IS_ON);
232 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
233 #else
234 // Debug build.
235 SetLogAssertHandler(&LogSink);
236 EXPECT_TRUE(DCHECK_IS_ON);
237 EXPECT_TRUE(DLOG_IS_ON(DCHECK));
238 #endif
240 EXPECT_EQ(0, log_sink_call_count);
241 DCHECK(false);
242 EXPECT_EQ(DCHECK_IS_ON ? 1 : 0, log_sink_call_count);
243 DPCHECK(false);
244 EXPECT_EQ(DCHECK_IS_ON ? 2 : 0, log_sink_call_count);
245 DCHECK_EQ(0, 1);
246 EXPECT_EQ(DCHECK_IS_ON ? 3 : 0, log_sink_call_count);
249 TEST_F(LoggingTest, DcheckReleaseBehavior) {
250 int some_variable = 1;
251 // These should still reference |some_variable| so we don't get
252 // unused variable warnings.
253 DCHECK(some_variable) << "test";
254 DPCHECK(some_variable) << "test";
255 DCHECK_EQ(some_variable, 1) << "test";
258 } // namespace
260 } // namespace logging