Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions fml/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion fml/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
62 changes: 34 additions & 28 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4243,34 +4243,40 @@ TEST_F(ShellTest, PrintsErrorWhenPlatformMessageSentFromWrongThread) {
task_runner);
auto shell = CreateShell(settings, task_runners);

auto stream = std::make_shared<std::ostringstream>();
fml::CaptureNextLog(stream.get());

// The next call will result in a thread checker violation.
fml::ThreadChecker::DisableNextThreadCheckFailure();
SendPlatformMessage(shell.get(), std::make_unique<PlatformMessage>(
"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<std::ostringstream>();
fml::CaptureNextLog(stream.get());

// The next call will result in a thread checker violation.
fml::ThreadChecker::DisableNextThreadCheckFailure();
SendPlatformMessage(shell.get(), std::make_unique<PlatformMessage>(
"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<PlatformMessage>(
"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<PlatformMessage>(
"com.test.plugin", nullptr));

EXPECT_EQ(log_capture.str(), "");
}

DestroyShell(std::move(shell), task_runners);
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
}

} // namespace testing
Expand Down