testing/gtest/mozilla/GTestRunner.cpp

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "GTestRunner.h"
michael@0 7 #include "gtest/gtest.h"
michael@0 8 #include "mozilla/Attributes.h"
michael@0 9 #include "mozilla/NullPtr.h"
michael@0 10 #ifdef MOZ_CRASHREPORTER
michael@0 11 #include "nsICrashReporter.h"
michael@0 12 #endif
michael@0 13 #include "testing/TestHarness.h"
michael@0 14 #include "prenv.h"
michael@0 15
michael@0 16 using ::testing::EmptyTestEventListener;
michael@0 17 using ::testing::InitGoogleTest;
michael@0 18 using ::testing::Test;
michael@0 19 using ::testing::TestCase;
michael@0 20 using ::testing::TestEventListeners;
michael@0 21 using ::testing::TestInfo;
michael@0 22 using ::testing::TestPartResult;
michael@0 23 using ::testing::UnitTest;
michael@0 24
michael@0 25 namespace mozilla {
michael@0 26
michael@0 27 // See gtest.h for method documentation
michael@0 28 class MozillaPrinter : public EmptyTestEventListener
michael@0 29 {
michael@0 30 public:
michael@0 31 virtual void OnTestProgramStart(const UnitTest& /* aUnitTest */) MOZ_OVERRIDE {
michael@0 32 printf("TEST-INFO | GTest unit test starting\n");
michael@0 33 }
michael@0 34 virtual void OnTestProgramEnd(const UnitTest& aUnitTest) MOZ_OVERRIDE {
michael@0 35 printf("TEST-%s | GTest unit test: %s\n",
michael@0 36 aUnitTest.Passed() ? "PASS" : "UNEXPECTED-FAIL",
michael@0 37 aUnitTest.Passed() ? "passed" : "failed");
michael@0 38 }
michael@0 39 virtual void OnTestStart(const TestInfo& aTestInfo) MOZ_OVERRIDE {
michael@0 40 mTestInfo = &aTestInfo;
michael@0 41 printf("TEST-START | %s.%s\n",
michael@0 42 mTestInfo->test_case_name(), mTestInfo->name());
michael@0 43 }
michael@0 44 virtual void OnTestPartResult(const TestPartResult& aTestPartResult) MOZ_OVERRIDE {
michael@0 45 printf("TEST-%s | %s.%s | %s @ %s:%i\n",
michael@0 46 !aTestPartResult.failed() ? "PASS" : "UNEXPECTED-FAIL",
michael@0 47 mTestInfo ? mTestInfo->test_case_name() : "?", mTestInfo ? mTestInfo->name() : "?",
michael@0 48 aTestPartResult.summary(),
michael@0 49 aTestPartResult.file_name(), aTestPartResult.line_number());
michael@0 50 }
michael@0 51 virtual void OnTestEnd(const TestInfo& aTestInfo) MOZ_OVERRIDE {
michael@0 52 printf("TEST-%s | %s.%s | test completed (time: %llims)\n",
michael@0 53 aTestInfo.result()->Passed() ? "PASS": "UNEXPECTED-FAIL",
michael@0 54 aTestInfo.test_case_name(), aTestInfo.name(),
michael@0 55 aTestInfo.result()->elapsed_time());
michael@0 56 MOZ_ASSERT(&aTestInfo == mTestInfo);
michael@0 57 mTestInfo = nullptr;
michael@0 58 }
michael@0 59
michael@0 60 const TestInfo* mTestInfo;
michael@0 61 };
michael@0 62
michael@0 63 static void ReplaceGTestLogger()
michael@0 64 {
michael@0 65 // Replace the GTest logger so that it can be passed
michael@0 66 // by the mozilla test parsers.
michael@0 67 // Code is based on: http://googletest.googlecode.com/svn/trunk/samples/sample9_unittest.cc
michael@0 68 UnitTest& unitTest = *UnitTest::GetInstance();
michael@0 69 TestEventListeners& listeners = unitTest.listeners();
michael@0 70 delete listeners.Release(listeners.default_result_printer());
michael@0 71
michael@0 72 listeners.Append(new MozillaPrinter);
michael@0 73 }
michael@0 74
michael@0 75 int RunGTestFunc()
michael@0 76 {
michael@0 77 int c = 0;
michael@0 78 InitGoogleTest(&c, static_cast<char**>(nullptr));
michael@0 79
michael@0 80 if (getenv("MOZ_TBPL_PARSER")) {
michael@0 81 ReplaceGTestLogger();
michael@0 82 }
michael@0 83
michael@0 84 PR_SetEnv("XPCOM_DEBUG_BREAK=stack-and-abort");
michael@0 85
michael@0 86 ScopedXPCOM xpcom("AsyncPanZoomController");
michael@0 87
michael@0 88 #ifdef MOZ_CRASHREPORTER
michael@0 89 nsCOMPtr<nsICrashReporter> crashreporter;
michael@0 90 char *crashreporterStr = PR_GetEnv("MOZ_CRASHREPORTER");
michael@0 91 if (crashreporterStr && !strcmp(crashreporterStr, "1")) {
michael@0 92 //TODO: move this to an even-more-common location to use in all
michael@0 93 // C++ unittests
michael@0 94 crashreporter = do_GetService("@mozilla.org/toolkit/crash-reporter;1");
michael@0 95 if (crashreporter) {
michael@0 96 std::cerr << "Setting up crash reporting" << std::endl;
michael@0 97
michael@0 98 nsCOMPtr<nsIProperties> dirsvc =
michael@0 99 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
michael@0 100 nsCOMPtr<nsIFile> cwd;
michael@0 101 nsresult rv = dirsvc->Get(NS_OS_CURRENT_WORKING_DIR,
michael@0 102 NS_GET_IID(nsIFile),
michael@0 103 getter_AddRefs(cwd));
michael@0 104 MOZ_ASSERT(NS_SUCCEEDED(rv));
michael@0 105 crashreporter->SetEnabled(true);
michael@0 106 crashreporter->SetMinidumpPath(cwd);
michael@0 107 }
michael@0 108 }
michael@0 109 #endif
michael@0 110
michael@0 111 return RUN_ALL_TESTS();
michael@0 112 }
michael@0 113
michael@0 114 // We use a static var 'RunGTest' defined in nsAppRunner.cpp.
michael@0 115 // RunGTest is initialized to nullptr but if GTest (this file)
michael@0 116 // is linked in then RunGTest will be set here indicating
michael@0 117 // GTest is supported.
michael@0 118 class _InitRunGTest {
michael@0 119 public:
michael@0 120 _InitRunGTest() {
michael@0 121 RunGTest = RunGTestFunc;
michael@0 122 }
michael@0 123 } InitRunGTest;
michael@0 124
michael@0 125 }

mercurial