Coverage Report

Created: 2018-09-25 14:53

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