diff --git a/fml/logging.cc b/fml/logging.cc index bc5dd353f2120..0bffd08776f29 100644 --- a/fml/logging.cc +++ b/fml/logging.cc @@ -75,10 +75,22 @@ LogMessage::LogMessage(LogSeverity severity, // static thread_local std::ostringstream* LogMessage::capture_next_log_stream_ = nullptr; -void CaptureNextLog(std::ostringstream* stream) { - LogMessage::CaptureNextLog(stream); +namespace testing { + +LogCapture::LogCapture() { + fml::LogMessage::CaptureNextLog(&stream_); +} + +LogCapture::~LogCapture() { + fml::LogMessage::CaptureNextLog(nullptr); } +std::string LogCapture::str() const { + return stream_.str(); +} + +} // namespace testing + // static void LogMessage::CaptureNextLog(std::ostringstream* stream) { LogMessage::capture_next_log_stream_ = stream; diff --git a/fml/logging.h b/fml/logging.h index 21a5a5d82715d..39dac4c8e1568 100644 --- a/fml/logging.h +++ b/fml/logging.h @@ -12,7 +12,17 @@ namespace fml { -void CaptureNextLog(std::ostringstream* stream); +namespace testing { +struct LogCapture { + LogCapture(); + ~LogCapture(); + + std::string str() const; + + private: + std::ostringstream stream_; +}; +} // namespace testing class LogMessageVoidify { public: diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 16abef0dbc0f3..18dec36314c68 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -4243,34 +4243,40 @@ TEST_F(ShellTest, PrintsErrorWhenPlatformMessageSentFromWrongThread) { task_runner); auto shell = CreateShell(settings, task_runners); - auto stream = std::make_shared(); - fml::CaptureNextLog(stream.get()); - - // The next call will result in a thread checker violation. - fml::ThreadChecker::DisableNextThreadCheckFailure(); - SendPlatformMessage(shell.get(), std::make_unique( - "com.test.plugin", nullptr)); - - EXPECT_THAT(stream->str(), - ::testing::EndsWith( - "The 'com.test.plugin' channel sent a message from native to " - "Flutter on a non-platform thread. Platform channel messages " - "must be sent on the platform thread. Failure to do so may " - "result in data loss or crashes, and must be fixed in the " - "plugin or application code creating that channel.\nSee " - "https://docs.flutter.dev/platform-integration/" - "platform-channels#channels-and-platform-threading for more " - "information.\n")); - - stream = std::make_shared(); - fml::CaptureNextLog(stream.get()); - - // The next call will result in a thread checker violation. - fml::ThreadChecker::DisableNextThreadCheckFailure(); - SendPlatformMessage(shell.get(), std::make_unique( - "com.test.plugin", nullptr)); - - EXPECT_EQ(stream->str(), ""); + { + fml::testing::LogCapture log_capture; + + // The next call will result in a thread checker violation. + fml::ThreadChecker::DisableNextThreadCheckFailure(); + SendPlatformMessage(shell.get(), std::make_unique( + "com.test.plugin", nullptr)); + + EXPECT_THAT( + log_capture.str(), + ::testing::EndsWith( + "The 'com.test.plugin' channel sent a message from native to " + "Flutter on a non-platform thread. Platform channel messages " + "must be sent on the platform thread. Failure to do so may " + "result in data loss or crashes, and must be fixed in the " + "plugin or application code creating that channel.\nSee " + "https://docs.flutter.dev/platform-integration/" + "platform-channels#channels-and-platform-threading for more " + "information.\n")); + } + + { + fml::testing::LogCapture log_capture; + + // The next call will result in a thread checker violation. + fml::ThreadChecker::DisableNextThreadCheckFailure(); + SendPlatformMessage(shell.get(), std::make_unique( + "com.test.plugin", nullptr)); + + EXPECT_EQ(log_capture.str(), ""); + } + + DestroyShell(std::move(shell), task_runners); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } } // namespace testing