Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5db8065

Browse files
authored
[Impeller] Validation errors in tests cause GTest failures. (#53786)
Earlier, this used to take down the entire process. Now, the entire set of failures will be listed instead.
1 parent 786a9f2 commit 5db8065

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

impeller/base/validation.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ namespace impeller {
1212

1313
static std::atomic_int32_t sValidationLogsDisabledCount = 0;
1414
static std::atomic_int32_t sValidationLogsAreFatal = 0;
15+
static ValidationFailureCallback sValidationFailureCallback;
1516

1617
void ImpellerValidationErrorsSetFatal(bool fatal) {
1718
sValidationLogsAreFatal = fatal;
1819
}
1920

21+
void ImpellerValidationErrorsSetCallback(ValidationFailureCallback callback) {
22+
sValidationFailureCallback = std::move(callback);
23+
}
24+
2025
ScopedValidationDisable::ScopedValidationDisable() {
2126
sValidationLogsDisabledCount++;
2227
}
@@ -47,6 +52,10 @@ std::ostream& ValidationLog::GetStream() {
4752
}
4853

4954
void ImpellerValidationBreak(const char* message, const char* file, int line) {
55+
if (sValidationFailureCallback &&
56+
sValidationFailureCallback(message, file, line)) {
57+
return;
58+
}
5059
const auto severity =
5160
ImpellerValidationErrorsAreFatal() ? fml::LOG_FATAL : fml::LOG_ERROR;
5261
auto fml_log = fml::LogMessage{severity, file, line, nullptr};

impeller/base/validation.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_IMPELLER_BASE_VALIDATION_H_
66
#define FLUTTER_IMPELLER_BASE_VALIDATION_H_
77

8+
#include <functional>
89
#include <sstream>
910

1011
namespace impeller {
@@ -37,6 +38,21 @@ void ImpellerValidationErrorsSetFatal(bool fatal);
3738

3839
bool ImpellerValidationErrorsAreFatal();
3940

41+
using ValidationFailureCallback =
42+
std::function<bool(const char* message, const char* file, int line)>;
43+
44+
//------------------------------------------------------------------------------
45+
/// @brief Sets a callback that callers (usually tests) can set to
46+
/// intercept validation failures.
47+
///
48+
/// Returning true from the callback indicates that Impeller can
49+
/// continue and avoid any default behavior on tripping validation
50+
/// (which could include process termination).
51+
///
52+
/// @param[in] callback The callback
53+
///
54+
void ImpellerValidationErrorsSetCallback(ValidationFailureCallback callback);
55+
4056
struct ScopedValidationDisable {
4157
ScopedValidationDisable();
4258

impeller/playground/playground_test.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,26 @@
1111
namespace impeller {
1212

1313
PlaygroundTest::PlaygroundTest()
14-
: Playground(PlaygroundSwitches{flutter::testing::GetArgsForProcess()}) {}
14+
: Playground(PlaygroundSwitches{flutter::testing::GetArgsForProcess()}) {
15+
ImpellerValidationErrorsSetCallback(
16+
[](const char* message, const char* file, int line) -> bool {
17+
// GTEST_MESSAGE_AT_ can only be used in a function that returns void.
18+
// Hence the goofy lambda. The failure message and location will still
19+
// be correct however.
20+
//
21+
// https://google.github.io/googletest/advanced.html#assertion-placement
22+
[message, file, line]() -> void {
23+
GTEST_MESSAGE_AT_(file, line, "Impeller Validation Error",
24+
::testing::TestPartResult::kFatalFailure)
25+
<< message;
26+
}();
27+
return true;
28+
});
29+
}
1530

16-
PlaygroundTest::~PlaygroundTest() = default;
31+
PlaygroundTest::~PlaygroundTest() {
32+
ImpellerValidationErrorsSetCallback(nullptr);
33+
}
1734

1835
namespace {
1936
bool DoesSupportWideGamutTests() {
@@ -36,8 +53,6 @@ void PlaygroundTest::SetUp() {
3653
return;
3754
}
3855

39-
ImpellerValidationErrorsSetFatal(true);
40-
4156
// Test names that end with "WideGamut" will render with wide gamut support.
4257
std::string test_name = flutter::testing::GetCurrentTestName();
4358
PlaygroundSwitches switches = switches_;

0 commit comments

Comments
 (0)