Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: mojo/public/cpp/environment/tests/logging_unittest.cc

Issue 1765243002: Remove Mojo bindings environment. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 #include <stdint.h>
6 #include <stdlib.h>
7
8 #include <sstream>
9 #include <string>
10
11 #include "mojo/public/cpp/environment/environment.h"
12 #include "mojo/public/cpp/environment/logging.h"
13 #include "mojo/public/cpp/system/macros.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace mojo {
17 namespace {
18
19 // The current logging system strips the path, so we need our filename.
20 const char kOurFilename[] = "logging_unittest.cc";
21
22 class PtrToMemberHelper {
23 public:
24 int member;
25 };
26
27 bool DcheckTestHelper(bool* was_called) {
28 *was_called = true;
29 return false;
30 }
31
32 class LoggingTest : public testing::Test {
33 public:
34 LoggingTest() : environment_(nullptr, &kMockLogger, nullptr) {
35 minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
36 ResetMockLogger();
37 }
38 ~LoggingTest() override {}
39
40 protected:
41 // Note: Does not reset |minimum_log_level_|.
42 static void ResetMockLogger() {
43 log_message_was_called_ = false;
44 last_log_level_ = MOJO_LOG_LEVEL_INFO;
45 last_source_file_.clear();
46 last_source_line_ = 0;
47 last_message_.clear();
48 }
49
50 // A function returning |bool| that shouldn't be called.
51 static bool NotCalledCondition() {
52 not_called_condition_was_called_ = true;
53 return false;
54 }
55
56 static bool log_message_was_called() { return log_message_was_called_; }
57 static MojoLogLevel last_log_level() { return last_log_level_; }
58 static const std::string& last_source_file() { return last_source_file_; }
59 static uint32_t last_source_line() { return last_source_line_; }
60 static const std::string& last_message() { return last_message_; }
61 static bool not_called_condition_was_called() {
62 return not_called_condition_was_called_;
63 }
64
65 private:
66 // Note: We record calls even if |log_level| is below |minimum_log_level_|
67 // (since the macros should mostly avoid this, and we want to be able to check
68 // that they do).
69 static void MockLogMessage(MojoLogLevel log_level,
70 const char* source_file,
71 uint32_t source_line,
72 const char* message) {
73 log_message_was_called_ = true;
74 last_log_level_ = log_level;
75 last_source_file_ = source_file;
76 last_source_line_ = source_line;
77 last_message_ = message;
78 }
79
80 static MojoLogLevel MockGetMinimumLogLevel() { return minimum_log_level_; }
81
82 static void MockSetMinimumLogLevel(MojoLogLevel minimum_log_level) {
83 minimum_log_level_ = minimum_log_level;
84 }
85
86 Environment environment_;
87
88 static const MojoLogger kMockLogger;
89 static MojoLogLevel minimum_log_level_;
90 static bool log_message_was_called_;
91 static MojoLogLevel last_log_level_;
92 static std::string last_source_file_;
93 static uint32_t last_source_line_;
94 static std::string last_message_;
95 static bool not_called_condition_was_called_;
96
97 DISALLOW_COPY_AND_ASSIGN(LoggingTest);
98 };
99
100 // static
101 const MojoLogger LoggingTest::kMockLogger = {
102 &LoggingTest::MockLogMessage,
103 &LoggingTest::MockGetMinimumLogLevel,
104 &LoggingTest::MockSetMinimumLogLevel};
105
106 // static
107 MojoLogLevel LoggingTest::minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
108
109 // static
110 bool LoggingTest::log_message_was_called_ = MOJO_LOG_LEVEL_INFO;
111
112 // static
113 MojoLogLevel LoggingTest::last_log_level_ = MOJO_LOG_LEVEL_INFO;
114
115 // static
116 std::string LoggingTest::last_source_file_;
117
118 // static
119 uint32_t LoggingTest::last_source_line_ = 0;
120
121 // static
122 std::string LoggingTest::last_message_;
123
124 // static
125 bool LoggingTest::not_called_condition_was_called_ = false;
126
127 TEST_F(LoggingTest, InternalLogMessage) {
128 internal::LogMessage(MOJO_LOG_LEVEL_INFO, "foo.cc", 123).stream() << "hello "
129 << "world";
130 EXPECT_TRUE(log_message_was_called());
131 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
132 EXPECT_EQ("foo.cc", last_source_file());
133 EXPECT_EQ(123u, last_source_line());
134 EXPECT_EQ("hello world", last_message());
135
136 ResetMockLogger();
137
138 internal::LogMessage(MOJO_LOG_LEVEL_WARNING, "./path/to/foo.cc", 123).stream()
139 << "hello "
140 << "world";
141 EXPECT_TRUE(log_message_was_called());
142 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level());
143 EXPECT_EQ("foo.cc", last_source_file());
144 EXPECT_EQ(123u, last_source_line());
145 EXPECT_EQ("hello world", last_message());
146
147 ResetMockLogger();
148
149 internal::LogMessage(MOJO_LOG_LEVEL_ERROR, "/path/to/foo.cc", 123).stream()
150 << "hello "
151 << "world";
152 EXPECT_TRUE(log_message_was_called());
153 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
154 EXPECT_EQ("foo.cc", last_source_file());
155 EXPECT_EQ(123u, last_source_line());
156 EXPECT_EQ("hello world", last_message());
157
158 ResetMockLogger();
159
160 internal::LogMessage(MOJO_LOG_LEVEL_FATAL, "path/to/foo.cc", 123).stream()
161 << "hello "
162 << "world";
163 EXPECT_TRUE(log_message_was_called());
164 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
165 EXPECT_EQ("foo.cc", last_source_file());
166 EXPECT_EQ(123u, last_source_line());
167 EXPECT_EQ("hello world", last_message());
168
169 ResetMockLogger();
170
171 internal::LogMessage(MOJO_LOG_LEVEL_VERBOSE, ".\\xy\\foo.cc", 123).stream()
172 << "hello "
173 << "world";
174 EXPECT_TRUE(log_message_was_called());
175 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE, last_log_level());
176 EXPECT_EQ("foo.cc", last_source_file());
177 EXPECT_EQ(123u, last_source_line());
178 EXPECT_EQ("hello world", last_message());
179
180 ResetMockLogger();
181
182 internal::LogMessage(MOJO_LOG_LEVEL_VERBOSE - 1, "xy\\foo.cc", 123).stream()
183 << "hello "
184 << "world";
185 EXPECT_TRUE(log_message_was_called());
186 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE - 1, last_log_level());
187 EXPECT_EQ("foo.cc", last_source_file());
188 EXPECT_EQ(123u, last_source_line());
189 EXPECT_EQ("hello world", last_message());
190
191 ResetMockLogger();
192
193 internal::LogMessage(MOJO_LOG_LEVEL_VERBOSE - 9, "C:\\xy\\foo.cc", 123)
194 .stream()
195 << "hello "
196 << "world";
197 EXPECT_TRUE(log_message_was_called());
198 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE - 9, last_log_level());
199 EXPECT_EQ("foo.cc", last_source_file());
200 EXPECT_EQ(123u, last_source_line());
201 EXPECT_EQ("hello world", last_message());
202
203 ResetMockLogger();
204
205 internal::LogMessage(MOJO_LOG_LEVEL_INFO, __FILE__, 123).stream() << "hello "
206 << "world";
207 EXPECT_TRUE(log_message_was_called());
208 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
209 EXPECT_EQ(kOurFilename, last_source_file());
210 EXPECT_EQ(123u, last_source_line());
211 EXPECT_EQ("hello world", last_message());
212 }
213
214 TEST_F(LoggingTest, LogStream) {
215 MOJO_LOG_STREAM(INFO) << "hello";
216 EXPECT_TRUE(log_message_was_called());
217 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
218 EXPECT_EQ(kOurFilename, last_source_file());
219 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
220 EXPECT_EQ("hello", last_message());
221
222 ResetMockLogger();
223
224 MOJO_LOG_STREAM(ERROR) << "hi " << 123;
225 EXPECT_TRUE(log_message_was_called());
226 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
227 EXPECT_EQ(kOurFilename, last_source_file());
228 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
229 EXPECT_EQ("hi 123", last_message());
230 }
231
232 TEST_F(LoggingTest, LazyLogStream) {
233 MOJO_LAZY_LOG_STREAM(INFO, true) << "hello";
234 EXPECT_TRUE(log_message_was_called());
235 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
236 EXPECT_EQ(kOurFilename, last_source_file());
237 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
238 EXPECT_EQ("hello", last_message());
239
240 ResetMockLogger();
241
242 MOJO_LAZY_LOG_STREAM(ERROR, true) << "hi " << 123;
243 EXPECT_TRUE(log_message_was_called());
244 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
245 EXPECT_EQ(kOurFilename, last_source_file());
246 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
247 EXPECT_EQ("hi 123", last_message());
248
249 ResetMockLogger();
250
251 MOJO_LAZY_LOG_STREAM(INFO, false) << "hello";
252 EXPECT_FALSE(log_message_was_called());
253
254 ResetMockLogger();
255
256 MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello";
257 EXPECT_FALSE(log_message_was_called());
258
259 ResetMockLogger();
260
261 PtrToMemberHelper helper;
262 helper.member = 1;
263 int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member;
264
265 // This probably fails to compile if we forget to parenthesize the condition
266 // in the macro (.* has lower precedence than !, which can't apply to
267 // |helper|).
268 MOJO_LAZY_LOG_STREAM(ERROR, helper.*member_ptr == 1) << "hello";
269 EXPECT_TRUE(log_message_was_called());
270
271 ResetMockLogger();
272
273 MOJO_LAZY_LOG_STREAM(WARNING, helper.*member_ptr == 0) << "hello";
274 EXPECT_FALSE(log_message_was_called());
275 }
276
277 TEST_F(LoggingTest, ShouldLog) {
278 // We start at |MOJO_LOG_LEVEL_INFO|.
279 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE));
280 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO));
281 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING));
282 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
283 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
284
285 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
286 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE));
287 EXPECT_FALSE(MOJO_SHOULD_LOG(INFO));
288 EXPECT_FALSE(MOJO_SHOULD_LOG(WARNING));
289 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
290 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
291
292 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_VERBOSE -
293 1);
294 EXPECT_TRUE(MOJO_SHOULD_LOG(VERBOSE));
295 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO));
296 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING));
297 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
298 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
299 }
300
301 TEST_F(LoggingTest, Log) {
302 // We start at |MOJO_LOG_LEVEL_INFO|.
303 MOJO_LOG(VERBOSE) << "hello";
304 EXPECT_FALSE(log_message_was_called());
305
306 ResetMockLogger();
307
308 MOJO_LOG(INFO) << "hello";
309 EXPECT_TRUE(log_message_was_called());
310 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
311 EXPECT_EQ(kOurFilename, last_source_file());
312 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
313 EXPECT_EQ("hello", last_message());
314
315 ResetMockLogger();
316
317 MOJO_LOG(ERROR) << "hello";
318 EXPECT_TRUE(log_message_was_called());
319 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
320 EXPECT_EQ(kOurFilename, last_source_file());
321 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
322 EXPECT_EQ("hello", last_message());
323
324 ResetMockLogger();
325
326 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
327
328 MOJO_LOG(VERBOSE) << "hello";
329 EXPECT_FALSE(log_message_was_called());
330
331 ResetMockLogger();
332
333 MOJO_LOG(INFO) << "hello";
334 EXPECT_FALSE(log_message_was_called());
335
336 ResetMockLogger();
337
338 MOJO_LOG(ERROR) << "hello";
339 EXPECT_TRUE(log_message_was_called());
340 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
341 EXPECT_EQ(kOurFilename, last_source_file());
342 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
343 EXPECT_EQ("hello", last_message());
344 }
345
346 TEST_F(LoggingTest, LogIf) {
347 // We start at |MOJO_LOG_LEVEL_INFO|.
348 MOJO_LOG_IF(VERBOSE, true) << "hello";
349 EXPECT_FALSE(log_message_was_called());
350
351 ResetMockLogger();
352
353 MOJO_LOG_IF(VERBOSE, false) << "hello";
354 EXPECT_FALSE(log_message_was_called());
355
356 ResetMockLogger();
357 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
358
359 bool x = true;
360 // Also try to make sure that we parenthesize the condition properly.
361 MOJO_LOG_IF(INFO, false || x) << "hello";
362 EXPECT_FALSE(log_message_was_called());
363
364 ResetMockLogger();
365
366 MOJO_LOG_IF(INFO, 0 != 1) << "hello";
367 EXPECT_FALSE(log_message_was_called());
368
369 ResetMockLogger();
370
371 MOJO_LOG_IF(WARNING, 1 + 1 == 2) << "hello";
372 EXPECT_FALSE(log_message_was_called());
373
374 ResetMockLogger();
375
376 MOJO_LOG_IF(ERROR, 1 * 2 == 2) << "hello";
377 EXPECT_TRUE(log_message_was_called());
378 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
379 EXPECT_EQ(kOurFilename, last_source_file());
380 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
381 EXPECT_EQ("hello", last_message());
382
383 ResetMockLogger();
384
385 MOJO_LOG_IF(FATAL, 1 * 2 == 3) << "hello";
386 EXPECT_FALSE(log_message_was_called());
387
388 ResetMockLogger();
389
390 // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the
391 // minimum.
392 MOJO_LOG_IF(INFO, NotCalledCondition()) << "hello";
393 EXPECT_FALSE(not_called_condition_was_called());
394 EXPECT_FALSE(log_message_was_called());
395 }
396
397 TEST_F(LoggingTest, Check) {
398 MOJO_CHECK(true) << "hello";
399 EXPECT_FALSE(log_message_was_called());
400
401 ResetMockLogger();
402
403 PtrToMemberHelper helper;
404 helper.member = 0;
405 int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member;
406
407 // Also try to make sure that we parenthesize the condition properly.
408 MOJO_CHECK(helper.*member_ptr == 1) << "hello";
409 EXPECT_TRUE(log_message_was_called());
410 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
411 EXPECT_EQ(kOurFilename, last_source_file());
412 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
413 EXPECT_EQ("Check failed: helper.*member_ptr == 1. hello", last_message());
414
415 ResetMockLogger();
416
417 // Also test a "naked" |MOJO_CHECK()|s.
418 MOJO_CHECK(1 + 2 == 3);
419 EXPECT_FALSE(log_message_was_called());
420 }
421
422 TEST_F(LoggingTest, Dlog) {
423 // We start at |MOJO_LOG_LEVEL_INFO|.
424 MOJO_DLOG(VERBOSE) << "hello";
425 EXPECT_FALSE(log_message_was_called());
426
427 ResetMockLogger();
428
429 MOJO_DLOG(INFO) << "hello";
430 #ifdef NDEBUG
431 EXPECT_FALSE(log_message_was_called());
432 #else
433 EXPECT_TRUE(log_message_was_called());
434 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
435 EXPECT_EQ(kOurFilename, last_source_file());
436 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 7), last_source_line());
437 EXPECT_EQ("hello", last_message());
438 #endif
439 }
440
441 TEST_F(LoggingTest, DlogIf) {
442 // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in
443 // this case.
444 MOJO_DLOG_IF(VERBOSE, NotCalledCondition()) << "hello";
445 EXPECT_FALSE(not_called_condition_was_called());
446 EXPECT_FALSE(log_message_was_called());
447
448 ResetMockLogger();
449
450 MOJO_DLOG_IF(INFO, 1 == 0) << "hello";
451 EXPECT_FALSE(log_message_was_called());
452
453 ResetMockLogger();
454
455 MOJO_DLOG_IF(INFO, 1 == 1) << "hello";
456 #ifdef NDEBUG
457 EXPECT_FALSE(log_message_was_called());
458 #else
459 EXPECT_TRUE(log_message_was_called());
460 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
461 EXPECT_EQ(kOurFilename, last_source_file());
462 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 7), last_source_line());
463 EXPECT_EQ("hello", last_message());
464 #endif
465
466 ResetMockLogger();
467
468 // |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds.
469 #ifndef NDEBUG
470 bool debug_only = true;
471 #endif
472 MOJO_DLOG_IF(WARNING, debug_only) << "hello";
473 #ifdef NDEBUG
474 EXPECT_FALSE(log_message_was_called());
475 #else
476 EXPECT_TRUE(log_message_was_called());
477 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level());
478 EXPECT_EQ(kOurFilename, last_source_file());
479 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 7), last_source_line());
480 EXPECT_EQ("hello", last_message());
481 #endif
482 }
483
484 TEST_F(LoggingTest, Dcheck) {
485 MOJO_DCHECK(true);
486 EXPECT_FALSE(log_message_was_called());
487
488 ResetMockLogger();
489
490 MOJO_DCHECK(true) << "hello";
491 EXPECT_FALSE(log_message_was_called());
492
493 ResetMockLogger();
494
495 // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for
496 // non-debug builds. (Hopefully, we'll get an unused variable error if it
497 // fails to compile the condition.)
498 bool was_called = false;
499 MOJO_DCHECK(DcheckTestHelper(&was_called)) << "hello";
500 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
501 EXPECT_FALSE(was_called);
502 EXPECT_FALSE(log_message_was_called());
503 #else
504 EXPECT_TRUE(was_called);
505 EXPECT_TRUE(log_message_was_called());
506 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
507 EXPECT_EQ(kOurFilename, last_source_file());
508 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 9), last_source_line());
509 EXPECT_EQ("Check failed: DcheckTestHelper(&was_called). hello",
510 last_message());
511 #endif
512
513 ResetMockLogger();
514
515 // Also try to make sure that we parenthesize the condition properly.
516 bool x = true;
517 MOJO_DCHECK(false || x) << "hello";
518 EXPECT_FALSE(log_message_was_called());
519 }
520
521 } // namespace
522 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/environment/tests/logger_unittest.cc ('k') | mojo/public/cpp/system/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698