summaryrefslogtreecommitdiffstats
path: root/testing/gtest/mozilla/MozHelpers.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /testing/gtest/mozilla/MozHelpers.cpp
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
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.cpp73
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