toolkit/components/places/tests/cpp/places_test_harness_tail.h

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:915ed6fbf7b0
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "nsWidgetsCID.h"
8 #include "nsIComponentRegistrar.h"
9 #ifdef MOZ_CRASHREPORTER
10 #include "nsICrashReporter.h"
11 #endif
12
13 #ifndef TEST_NAME
14 #error "Must #define TEST_NAME before including places_test_harness_tail.h"
15 #endif
16
17 #ifndef TEST_FILE
18 #error "Must #define TEST_FILE before include places_test_harness_tail.h"
19 #endif
20
21 int gTestsIndex = 0;
22
23 #define TEST_INFO_STR "TEST-INFO | (%s) | "
24
25 class RunNextTest : public nsRunnable
26 {
27 public:
28 NS_IMETHOD Run()
29 {
30 NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
31 if (gTestsIndex < int(mozilla::ArrayLength(gTests))) {
32 do_test_pending();
33 Test &test = gTests[gTestsIndex++];
34 (void)fprintf(stderr, TEST_INFO_STR "Running %s.\n", TEST_FILE,
35 test.name);
36 test.func();
37 }
38
39 do_test_finished();
40 return NS_OK;
41 }
42 };
43
44 void
45 run_next_test()
46 {
47 nsCOMPtr<nsIRunnable> event = new RunNextTest();
48 do_check_success(NS_DispatchToCurrentThread(event));
49 }
50
51 int gPendingTests = 0;
52
53 void
54 do_test_pending()
55 {
56 NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
57 gPendingTests++;
58 }
59
60 void
61 do_test_finished()
62 {
63 NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
64 NS_ASSERTION(gPendingTests > 0, "Invalid pending test count!");
65 gPendingTests--;
66 }
67
68 void
69 disable_idle_service()
70 {
71 (void)fprintf(stderr, TEST_INFO_STR "Disabling Idle Service.\n", TEST_FILE);
72 static NS_DEFINE_IID(kIdleCID, NS_IDLE_SERVICE_CID);
73 nsresult rv;
74 nsCOMPtr<nsIFactory> idleFactory = do_GetClassObject(kIdleCID, &rv);
75 do_check_success(rv);
76 nsCOMPtr<nsIComponentRegistrar> registrar;
77 rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
78 do_check_success(rv);
79 rv = registrar->UnregisterFactory(kIdleCID, idleFactory);
80 do_check_success(rv);
81 }
82
83 int
84 main(int aArgc,
85 char** aArgv)
86 {
87 ScopedXPCOM xpcom(TEST_NAME);
88 if (xpcom.failed())
89 return -1;
90 // Initialize a profile folder to ensure a clean shutdown.
91 nsCOMPtr<nsIFile> profile = xpcom.GetProfileDirectory();
92 if (!profile) {
93 fail("Couldn't get the profile directory.");
94 return -1;
95 }
96
97 #ifdef MOZ_CRASHREPORTER
98 char* enabled = PR_GetEnv("MOZ_CRASHREPORTER");
99 if (enabled && !strcmp(enabled, "1")) {
100 // bug 787458: move this to an even-more-common location to use in all
101 // C++ unittests
102 nsCOMPtr<nsICrashReporter> crashreporter =
103 do_GetService("@mozilla.org/toolkit/crash-reporter;1");
104 if (crashreporter) {
105 fprintf(stderr, "Setting up crash reporting\n");
106
107 nsCOMPtr<nsIProperties> dirsvc =
108 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
109 if (!dirsvc)
110 NS_RUNTIMEABORT("Couldn't get directory service");
111 nsCOMPtr<nsIFile> cwd;
112 nsresult rv = dirsvc->Get(NS_OS_CURRENT_WORKING_DIR,
113 NS_GET_IID(nsIFile),
114 getter_AddRefs(cwd));
115 if (NS_FAILED(rv))
116 NS_RUNTIMEABORT("Couldn't get CWD");
117 crashreporter->SetEnabled(true);
118 crashreporter->SetMinidumpPath(cwd);
119 }
120 }
121 #endif
122
123 nsRefPtr<WaitForConnectionClosed> spinClose = new WaitForConnectionClosed();
124
125 // Tinderboxes are constantly on idle. Since idle tasks can interact with
126 // tests, causing random failures, disable the idle service.
127 disable_idle_service();
128
129 do_test_pending();
130 run_next_test();
131
132 // Spin the event loop until we've run out of tests to run.
133 while (gPendingTests) {
134 (void)NS_ProcessNextEvent();
135 }
136
137 // And let any other events finish before we quit.
138 (void)NS_ProcessPendingEvents(nullptr);
139
140 // Check that we have passed all of our tests, and output accordingly.
141 if (gPassedTests == gTotalTests) {
142 passed(TEST_FILE);
143 }
144
145 (void)fprintf(stderr, TEST_INFO_STR "%u of %u tests passed\n",
146 TEST_FILE, unsigned(gPassedTests), unsigned(gTotalTests));
147
148 return gPassedTests == gTotalTests ? 0 : -1;
149 }

mercurial