1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/gtest/mozilla/GTestRunner.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "GTestRunner.h" 1.10 +#include "gtest/gtest.h" 1.11 +#include "mozilla/Attributes.h" 1.12 +#include "mozilla/NullPtr.h" 1.13 +#ifdef MOZ_CRASHREPORTER 1.14 +#include "nsICrashReporter.h" 1.15 +#endif 1.16 +#include "testing/TestHarness.h" 1.17 +#include "prenv.h" 1.18 + 1.19 +using ::testing::EmptyTestEventListener; 1.20 +using ::testing::InitGoogleTest; 1.21 +using ::testing::Test; 1.22 +using ::testing::TestCase; 1.23 +using ::testing::TestEventListeners; 1.24 +using ::testing::TestInfo; 1.25 +using ::testing::TestPartResult; 1.26 +using ::testing::UnitTest; 1.27 + 1.28 +namespace mozilla { 1.29 + 1.30 +// See gtest.h for method documentation 1.31 +class MozillaPrinter : public EmptyTestEventListener 1.32 +{ 1.33 +public: 1.34 + virtual void OnTestProgramStart(const UnitTest& /* aUnitTest */) MOZ_OVERRIDE { 1.35 + printf("TEST-INFO | GTest unit test starting\n"); 1.36 + } 1.37 + virtual void OnTestProgramEnd(const UnitTest& aUnitTest) MOZ_OVERRIDE { 1.38 + printf("TEST-%s | GTest unit test: %s\n", 1.39 + aUnitTest.Passed() ? "PASS" : "UNEXPECTED-FAIL", 1.40 + aUnitTest.Passed() ? "passed" : "failed"); 1.41 + } 1.42 + virtual void OnTestStart(const TestInfo& aTestInfo) MOZ_OVERRIDE { 1.43 + mTestInfo = &aTestInfo; 1.44 + printf("TEST-START | %s.%s\n", 1.45 + mTestInfo->test_case_name(), mTestInfo->name()); 1.46 + } 1.47 + virtual void OnTestPartResult(const TestPartResult& aTestPartResult) MOZ_OVERRIDE { 1.48 + printf("TEST-%s | %s.%s | %s @ %s:%i\n", 1.49 + !aTestPartResult.failed() ? "PASS" : "UNEXPECTED-FAIL", 1.50 + mTestInfo ? mTestInfo->test_case_name() : "?", mTestInfo ? mTestInfo->name() : "?", 1.51 + aTestPartResult.summary(), 1.52 + aTestPartResult.file_name(), aTestPartResult.line_number()); 1.53 + } 1.54 + virtual void OnTestEnd(const TestInfo& aTestInfo) MOZ_OVERRIDE { 1.55 + printf("TEST-%s | %s.%s | test completed (time: %llims)\n", 1.56 + aTestInfo.result()->Passed() ? "PASS": "UNEXPECTED-FAIL", 1.57 + aTestInfo.test_case_name(), aTestInfo.name(), 1.58 + aTestInfo.result()->elapsed_time()); 1.59 + MOZ_ASSERT(&aTestInfo == mTestInfo); 1.60 + mTestInfo = nullptr; 1.61 + } 1.62 + 1.63 + const TestInfo* mTestInfo; 1.64 +}; 1.65 + 1.66 +static void ReplaceGTestLogger() 1.67 +{ 1.68 + // Replace the GTest logger so that it can be passed 1.69 + // by the mozilla test parsers. 1.70 + // Code is based on: http://googletest.googlecode.com/svn/trunk/samples/sample9_unittest.cc 1.71 + UnitTest& unitTest = *UnitTest::GetInstance(); 1.72 + TestEventListeners& listeners = unitTest.listeners(); 1.73 + delete listeners.Release(listeners.default_result_printer()); 1.74 + 1.75 + listeners.Append(new MozillaPrinter); 1.76 +} 1.77 + 1.78 +int RunGTestFunc() 1.79 +{ 1.80 + int c = 0; 1.81 + InitGoogleTest(&c, static_cast<char**>(nullptr)); 1.82 + 1.83 + if (getenv("MOZ_TBPL_PARSER")) { 1.84 + ReplaceGTestLogger(); 1.85 + } 1.86 + 1.87 + PR_SetEnv("XPCOM_DEBUG_BREAK=stack-and-abort"); 1.88 + 1.89 + ScopedXPCOM xpcom("AsyncPanZoomController"); 1.90 + 1.91 +#ifdef MOZ_CRASHREPORTER 1.92 + nsCOMPtr<nsICrashReporter> crashreporter; 1.93 + char *crashreporterStr = PR_GetEnv("MOZ_CRASHREPORTER"); 1.94 + if (crashreporterStr && !strcmp(crashreporterStr, "1")) { 1.95 + //TODO: move this to an even-more-common location to use in all 1.96 + // C++ unittests 1.97 + crashreporter = do_GetService("@mozilla.org/toolkit/crash-reporter;1"); 1.98 + if (crashreporter) { 1.99 + std::cerr << "Setting up crash reporting" << std::endl; 1.100 + 1.101 + nsCOMPtr<nsIProperties> dirsvc = 1.102 + do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); 1.103 + nsCOMPtr<nsIFile> cwd; 1.104 + nsresult rv = dirsvc->Get(NS_OS_CURRENT_WORKING_DIR, 1.105 + NS_GET_IID(nsIFile), 1.106 + getter_AddRefs(cwd)); 1.107 + MOZ_ASSERT(NS_SUCCEEDED(rv)); 1.108 + crashreporter->SetEnabled(true); 1.109 + crashreporter->SetMinidumpPath(cwd); 1.110 + } 1.111 + } 1.112 +#endif 1.113 + 1.114 + return RUN_ALL_TESTS(); 1.115 +} 1.116 + 1.117 +// We use a static var 'RunGTest' defined in nsAppRunner.cpp. 1.118 +// RunGTest is initialized to nullptr but if GTest (this file) 1.119 +// is linked in then RunGTest will be set here indicating 1.120 +// GTest is supported. 1.121 +class _InitRunGTest { 1.122 +public: 1.123 + _InitRunGTest() { 1.124 + RunGTest = RunGTestFunc; 1.125 + } 1.126 +} InitRunGTest; 1.127 + 1.128 +}