michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsWidgetsCID.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: #ifdef MOZ_CRASHREPORTER michael@0: #include "nsICrashReporter.h" michael@0: #endif michael@0: michael@0: #ifndef TEST_NAME michael@0: #error "Must #define TEST_NAME before including places_test_harness_tail.h" michael@0: #endif michael@0: michael@0: #ifndef TEST_FILE michael@0: #error "Must #define TEST_FILE before include places_test_harness_tail.h" michael@0: #endif michael@0: michael@0: int gTestsIndex = 0; michael@0: michael@0: #define TEST_INFO_STR "TEST-INFO | (%s) | " michael@0: michael@0: class RunNextTest : public nsRunnable michael@0: { michael@0: public: michael@0: NS_IMETHOD Run() michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?"); michael@0: if (gTestsIndex < int(mozilla::ArrayLength(gTests))) { michael@0: do_test_pending(); michael@0: Test &test = gTests[gTestsIndex++]; michael@0: (void)fprintf(stderr, TEST_INFO_STR "Running %s.\n", TEST_FILE, michael@0: test.name); michael@0: test.func(); michael@0: } michael@0: michael@0: do_test_finished(); michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: void michael@0: run_next_test() michael@0: { michael@0: nsCOMPtr event = new RunNextTest(); michael@0: do_check_success(NS_DispatchToCurrentThread(event)); michael@0: } michael@0: michael@0: int gPendingTests = 0; michael@0: michael@0: void michael@0: do_test_pending() michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?"); michael@0: gPendingTests++; michael@0: } michael@0: michael@0: void michael@0: do_test_finished() michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?"); michael@0: NS_ASSERTION(gPendingTests > 0, "Invalid pending test count!"); michael@0: gPendingTests--; michael@0: } michael@0: michael@0: void michael@0: disable_idle_service() michael@0: { michael@0: (void)fprintf(stderr, TEST_INFO_STR "Disabling Idle Service.\n", TEST_FILE); michael@0: static NS_DEFINE_IID(kIdleCID, NS_IDLE_SERVICE_CID); michael@0: nsresult rv; michael@0: nsCOMPtr idleFactory = do_GetClassObject(kIdleCID, &rv); michael@0: do_check_success(rv); michael@0: nsCOMPtr registrar; michael@0: rv = NS_GetComponentRegistrar(getter_AddRefs(registrar)); michael@0: do_check_success(rv); michael@0: rv = registrar->UnregisterFactory(kIdleCID, idleFactory); michael@0: do_check_success(rv); michael@0: } michael@0: michael@0: int michael@0: main(int aArgc, michael@0: char** aArgv) michael@0: { michael@0: ScopedXPCOM xpcom(TEST_NAME); michael@0: if (xpcom.failed()) michael@0: return -1; michael@0: // Initialize a profile folder to ensure a clean shutdown. michael@0: nsCOMPtr profile = xpcom.GetProfileDirectory(); michael@0: if (!profile) { michael@0: fail("Couldn't get the profile directory."); michael@0: return -1; michael@0: } michael@0: michael@0: #ifdef MOZ_CRASHREPORTER michael@0: char* enabled = PR_GetEnv("MOZ_CRASHREPORTER"); michael@0: if (enabled && !strcmp(enabled, "1")) { michael@0: // bug 787458: move this to an even-more-common location to use in all michael@0: // C++ unittests michael@0: nsCOMPtr crashreporter = michael@0: do_GetService("@mozilla.org/toolkit/crash-reporter;1"); michael@0: if (crashreporter) { michael@0: fprintf(stderr, "Setting up crash reporting\n"); michael@0: michael@0: nsCOMPtr dirsvc = michael@0: do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); michael@0: if (!dirsvc) michael@0: NS_RUNTIMEABORT("Couldn't get directory service"); michael@0: nsCOMPtr cwd; michael@0: nsresult rv = dirsvc->Get(NS_OS_CURRENT_WORKING_DIR, michael@0: NS_GET_IID(nsIFile), michael@0: getter_AddRefs(cwd)); michael@0: if (NS_FAILED(rv)) michael@0: NS_RUNTIMEABORT("Couldn't get CWD"); michael@0: crashreporter->SetEnabled(true); michael@0: crashreporter->SetMinidumpPath(cwd); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: nsRefPtr spinClose = new WaitForConnectionClosed(); michael@0: michael@0: // Tinderboxes are constantly on idle. Since idle tasks can interact with michael@0: // tests, causing random failures, disable the idle service. michael@0: disable_idle_service(); michael@0: michael@0: do_test_pending(); michael@0: run_next_test(); michael@0: michael@0: // Spin the event loop until we've run out of tests to run. michael@0: while (gPendingTests) { michael@0: (void)NS_ProcessNextEvent(); michael@0: } michael@0: michael@0: // And let any other events finish before we quit. michael@0: (void)NS_ProcessPendingEvents(nullptr); michael@0: michael@0: // Check that we have passed all of our tests, and output accordingly. michael@0: if (gPassedTests == gTotalTests) { michael@0: passed(TEST_FILE); michael@0: } michael@0: michael@0: (void)fprintf(stderr, TEST_INFO_STR "%u of %u tests passed\n", michael@0: TEST_FILE, unsigned(gPassedTests), unsigned(gTotalTests)); michael@0: michael@0: return gPassedTests == gTotalTests ? 0 : -1; michael@0: }