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