From 20fcca4e9559f116a45c195f77b8c78ac318e066 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 29 May 2025 09:59:48 -0700 Subject: [PATCH] Add a basic test for the logger writer PiperOrigin-RevId: 764774015 --- internal/platform/BUILD | 2 + .../platform/implementation/apple/Tests/BUILD | 16 ++++ .../apple/Tests/nearby_logger_writer_test.mm | 81 +++++++++++++++++++ internal/platform/logging.h | 11 +-- 4 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 internal/platform/implementation/apple/Tests/nearby_logger_writer_test.mm diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 5ccab0a2..3eaea91c 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -28,6 +28,7 @@ cc_library( "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", "@com_google_absl//absl/log:globals", + "@com_google_absl//absl/log:log_sink_registry", ], ) @@ -287,6 +288,7 @@ cc_library( "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", "@com_google_absl//absl/log:globals", + "@com_google_absl//absl/log:log_sink_registry", "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", diff --git a/internal/platform/implementation/apple/Tests/BUILD b/internal/platform/implementation/apple/Tests/BUILD index d1d6cafa..4dadf2e8 100644 --- a/internal/platform/implementation/apple/Tests/BUILD +++ b/internal/platform/implementation/apple/Tests/BUILD @@ -70,11 +70,27 @@ objc_library( ], ) +objc_library( + name = "LoggerWriterTestslib", + testonly = True, + srcs = [ + "nearby_logger_writer_test.mm", + ], + deps = [ + "//internal/platform:logging", + "//internal/platform/implementation/apple:logger_writer", + "//third_party/apple_frameworks:Foundation", + "//third_party/apple_frameworks:XCTest", + "//third_party/objective_c/google_toolbox_for_mac:GTM_Logger", + ], +) + ios_unit_test( name = "PlatformTests", minimum_os_version = IOS_MINIMUM_OS, runner = IOS_LATEST_TEST_RUNNER, deps = [ + ":LoggerWriterTestslib", ":PlatformTestslib", ], ) diff --git a/internal/platform/implementation/apple/Tests/nearby_logger_writer_test.mm b/internal/platform/implementation/apple/Tests/nearby_logger_writer_test.mm new file mode 100644 index 00000000..1b976aa1 --- /dev/null +++ b/internal/platform/implementation/apple/Tests/nearby_logger_writer_test.mm @@ -0,0 +1,81 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import +#import "GoogleToolboxForMac/GTMLogger.h" + +#include "internal/platform/implementation/apple/nearby_logger_writer.h" + +#include +#include + +#include "internal/platform/logging.h" + +class AbslFileLogSink : public absl::LogSink { + public: + AbslFileLogSink() = default; + ~AbslFileLogSink() override = default; + void Send(const absl::LogEntry& entry) override { + logMessage_ = std::string(entry.text_message()); + severity_ = entry.log_severity(); + } + + std::string GetLogMessage() const { return logMessage_; } + absl::LogSeverity GetSeverity() const { return severity_; } + + private: + std::string logMessage_; + absl::LogSeverity severity_ = absl::LogSeverity::kInfo; +}; + +@interface NearbyLoggerWriterTest : XCTestCase +@end + +@implementation NearbyLoggerWriterTest { + std::unique_ptr _logSink; +} + +- (void)setUp { + [super setUp]; + _logSink = std::make_unique(); + absl::SetGlobalVLogLevel(1); + absl::AddLogSink(_logSink.get()); + nearby::apple::EnableNearbyLoggerWriter(); +} + +- (void)tearDown { + absl::RemoveLogSink(_logSink.get()); + _logSink.reset(); + [super tearDown]; +} + +- (void)testInfoLogMessage { + GTMLoggerInfo(@"Hello, world!"); + XCTAssertEqual(_logSink->GetLogMessage(), "Hello, world!"); + XCTAssertEqual(_logSink->GetSeverity(), absl::LogSeverity::kInfo); +} + +- (void)testErrorLogMessage { + GTMLoggerError(@"Error!"); + XCTAssertEqual(_logSink->GetLogMessage(), "Error!"); + XCTAssertEqual(_logSink->GetSeverity(), absl::LogSeverity::kError); +} + +- (void)testVerboseLogMessage { + GTMLoggerDebug(@"Verbose!"); + XCTAssertEqual(_logSink->GetLogMessage(), "Verbose!"); + XCTAssertEqual(_logSink->GetSeverity(), absl::LogSeverity::kInfo); +} + +@end diff --git a/internal/platform/logging.h b/internal/platform/logging.h index f4960d53..cbefb3eb 100644 --- a/internal/platform/logging.h +++ b/internal/platform/logging.h @@ -20,13 +20,14 @@ #include "base/check.h" #include "base/check_op.h" #include "base/logging.h" -#else // defined(NEARBY_CHROMIUM) +#else // defined(NEARBY_CHROMIUM) // IWYU pragma: begin_exports -#include "absl/log/check.h" // nogncheck -#include "absl/log/globals.h" // nogncheck -#include "absl/log/log.h" // nogncheck +#include "absl/log/check.h" // nogncheck +#include "absl/log/globals.h" // nogncheck +#include "absl/log/log.h" // nogncheck +#include "absl/log/log_sink_registry.h" // nogncheck // IWYU pragma: end_exports -#endif // defined(NEARBY_CHROMIUM) +#endif // defined(NEARBY_CHROMIUM) // Public APIs // The stream statement must come last, or it won't compile.