diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /testing/gtest/mozilla/MozHelpers.cpp | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/gtest/mozilla/MozHelpers.cpp')
-rw-r--r-- | testing/gtest/mozilla/MozHelpers.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/testing/gtest/mozilla/MozHelpers.cpp b/testing/gtest/mozilla/MozHelpers.cpp new file mode 100644 index 0000000000..78fe614964 --- /dev/null +++ b/testing/gtest/mozilla/MozHelpers.cpp @@ -0,0 +1,73 @@ +#include "MozHelpers.h" + +#include <iostream> + +#include "gtest/gtest-spi.h" +#include "mozilla/Mutex.h" +#include "nsDebug.h" + +namespace mozilla::gtest { + +void DisableCrashReporter() { + nsCOMPtr<nsICrashReporter> crashreporter = + do_GetService("@mozilla.org/toolkit/crash-reporter;1"); + if (crashreporter) { + crashreporter->SetEnabled(false); + } +} + +class ScopedTestResultReporterImpl + : public ScopedTestResultReporter, + public testing::ScopedFakeTestPartResultReporter { + public: + explicit ScopedTestResultReporterImpl(ExitMode aExitMode) + : testing::ScopedFakeTestPartResultReporter(INTERCEPT_ALL_THREADS, + nullptr), + mExitMode(aExitMode) {} + + ~ScopedTestResultReporterImpl() { + switch (mExitMode) { + case ExitMode::ExitOnDtor: + exit(ExitCode(Status())); + case ExitMode::NoExit: + break; + } + } + + void ReportTestPartResult(const testing::TestPartResult& aResult) override { + { + MutexAutoLock lock(mMutex); + if (aResult.nonfatally_failed() && + mStatus < TestResultStatus::NonFatalFailure) { + mStatus = TestResultStatus::NonFatalFailure; + } + + if (aResult.fatally_failed() && + mStatus < TestResultStatus::FatalFailure) { + mStatus = TestResultStatus::FatalFailure; + } + } + + std::ostringstream stream; + stream << aResult; + printf_stderr("%s\n", stream.str().c_str()); + } + + TestResultStatus Status() const override { + MutexAutoLock lock(mMutex); + return mStatus; + } + + private: + const ExitMode mExitMode; + + mutable Mutex mMutex{"ScopedTestResultReporterImpl::mMutex"}; + TestResultStatus mStatus MOZ_GUARDED_BY(mMutex) = TestResultStatus::Pass; +}; + +UniquePtr<ScopedTestResultReporter> ScopedTestResultReporter::Create( + ExitMode aExitMode) { + return MakeUnique<ScopedTestResultReporterImpl>(aExitMode); +} + +} // namespace mozilla::gtest |