1 // Copyright 2024 The Chromium Authors
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 "base/logging.h"
6 #include "base/test/logging/test_rust_logger_consumer.rs.h"
7 #include "base/test/mock_log.h"
8
9 using testing::_;
10
11 namespace logging {
12 namespace {
13
14 class RustLogIntegrationTest : public testing::Test {
15 public:
SetUp()16 void SetUp() override { log_.StartCapturingLogs(); }
17
TearDown()18 void TearDown() override { log_.StopCapturingLogs(); }
19
20 base::test::MockLog log_;
21 };
22
23 // TODO(crbug.com/374023535): Logging does not work in component builds.
24 #if defined(COMPONENT_BUILD)
25 #define MAYBE_CheckAllSeverity DISABLED_CheckAllSeverity
26 #else
27 #define MAYBE_CheckAllSeverity CheckAllSeverity
28 #endif
TEST_F(RustLogIntegrationTest,MAYBE_CheckAllSeverity)29 TEST_F(RustLogIntegrationTest, MAYBE_CheckAllSeverity) {
30 #if DCHECK_IS_ON()
31 // Debug and Trace logs from Rust are discarded when DCHECK_IS_ON() is false;
32 // otherwise, they are logged as info.
33 EXPECT_CALL(log_,
34 Log(LOGGING_INFO, _, _, _, testing::HasSubstr("test trace log")))
35 .WillOnce(testing::Return(true));
36
37 EXPECT_CALL(log_,
38 Log(LOGGING_INFO, _, _, _, testing::HasSubstr("test debug log")))
39 .WillOnce(testing::Return(true));
40 #endif
41
42 EXPECT_CALL(log_,
43 Log(LOGGING_INFO, _, _, _, testing::HasSubstr("test info log")))
44 .WillOnce(testing::Return(true));
45
46 EXPECT_CALL(log_, Log(LOGGING_WARNING, _, _, _,
47 testing::HasSubstr("test warning log")))
48 .WillOnce(testing::Return(true));
49
50 EXPECT_CALL(log_,
51 Log(LOGGING_ERROR, _, _, _, testing::HasSubstr("test error log")))
52 .WillOnce(testing::Return(true));
53
54 base::test::print_test_trace_log();
55 base::test::print_test_debug_log();
56 base::test::print_test_info_log();
57 base::test::print_test_warning_log();
58 base::test::print_test_error_log();
59 }
60
61 // TODO(crbug.com/374023535): Logging does not work in component builds.
62 #if defined(COMPONENT_BUILD)
63 #define MAYBE_Placeholders DISABLED_Placeholders
64 #else
65 #define MAYBE_Placeholders Placeholders
66 #endif
TEST_F(RustLogIntegrationTest,MAYBE_Placeholders)67 TEST_F(RustLogIntegrationTest, MAYBE_Placeholders) {
68 EXPECT_CALL(log_, Log(LOGGING_ERROR, _, _, _,
69 testing::HasSubstr("test log with placeholder 2")))
70 .WillOnce(testing::Return(true));
71
72 base::test::print_test_error_log_with_placeholder(2);
73 }
74
75 } // namespace
76 } // namespace logging
77