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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial