1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/cpp/places_test_harness_tail.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsWidgetsCID.h" 1.11 +#include "nsIComponentRegistrar.h" 1.12 +#ifdef MOZ_CRASHREPORTER 1.13 +#include "nsICrashReporter.h" 1.14 +#endif 1.15 + 1.16 +#ifndef TEST_NAME 1.17 +#error "Must #define TEST_NAME before including places_test_harness_tail.h" 1.18 +#endif 1.19 + 1.20 +#ifndef TEST_FILE 1.21 +#error "Must #define TEST_FILE before include places_test_harness_tail.h" 1.22 +#endif 1.23 + 1.24 +int gTestsIndex = 0; 1.25 + 1.26 +#define TEST_INFO_STR "TEST-INFO | (%s) | " 1.27 + 1.28 +class RunNextTest : public nsRunnable 1.29 +{ 1.30 +public: 1.31 + NS_IMETHOD Run() 1.32 + { 1.33 + NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?"); 1.34 + if (gTestsIndex < int(mozilla::ArrayLength(gTests))) { 1.35 + do_test_pending(); 1.36 + Test &test = gTests[gTestsIndex++]; 1.37 + (void)fprintf(stderr, TEST_INFO_STR "Running %s.\n", TEST_FILE, 1.38 + test.name); 1.39 + test.func(); 1.40 + } 1.41 + 1.42 + do_test_finished(); 1.43 + return NS_OK; 1.44 + } 1.45 +}; 1.46 + 1.47 +void 1.48 +run_next_test() 1.49 +{ 1.50 + nsCOMPtr<nsIRunnable> event = new RunNextTest(); 1.51 + do_check_success(NS_DispatchToCurrentThread(event)); 1.52 +} 1.53 + 1.54 +int gPendingTests = 0; 1.55 + 1.56 +void 1.57 +do_test_pending() 1.58 +{ 1.59 + NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?"); 1.60 + gPendingTests++; 1.61 +} 1.62 + 1.63 +void 1.64 +do_test_finished() 1.65 +{ 1.66 + NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?"); 1.67 + NS_ASSERTION(gPendingTests > 0, "Invalid pending test count!"); 1.68 + gPendingTests--; 1.69 +} 1.70 + 1.71 +void 1.72 +disable_idle_service() 1.73 +{ 1.74 + (void)fprintf(stderr, TEST_INFO_STR "Disabling Idle Service.\n", TEST_FILE); 1.75 + static NS_DEFINE_IID(kIdleCID, NS_IDLE_SERVICE_CID); 1.76 + nsresult rv; 1.77 + nsCOMPtr<nsIFactory> idleFactory = do_GetClassObject(kIdleCID, &rv); 1.78 + do_check_success(rv); 1.79 + nsCOMPtr<nsIComponentRegistrar> registrar; 1.80 + rv = NS_GetComponentRegistrar(getter_AddRefs(registrar)); 1.81 + do_check_success(rv); 1.82 + rv = registrar->UnregisterFactory(kIdleCID, idleFactory); 1.83 + do_check_success(rv); 1.84 +} 1.85 + 1.86 +int 1.87 +main(int aArgc, 1.88 + char** aArgv) 1.89 +{ 1.90 + ScopedXPCOM xpcom(TEST_NAME); 1.91 + if (xpcom.failed()) 1.92 + return -1; 1.93 + // Initialize a profile folder to ensure a clean shutdown. 1.94 + nsCOMPtr<nsIFile> profile = xpcom.GetProfileDirectory(); 1.95 + if (!profile) { 1.96 + fail("Couldn't get the profile directory."); 1.97 + return -1; 1.98 + } 1.99 + 1.100 +#ifdef MOZ_CRASHREPORTER 1.101 + char* enabled = PR_GetEnv("MOZ_CRASHREPORTER"); 1.102 + if (enabled && !strcmp(enabled, "1")) { 1.103 + // bug 787458: move this to an even-more-common location to use in all 1.104 + // C++ unittests 1.105 + nsCOMPtr<nsICrashReporter> crashreporter = 1.106 + do_GetService("@mozilla.org/toolkit/crash-reporter;1"); 1.107 + if (crashreporter) { 1.108 + fprintf(stderr, "Setting up crash reporting\n"); 1.109 + 1.110 + nsCOMPtr<nsIProperties> dirsvc = 1.111 + do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); 1.112 + if (!dirsvc) 1.113 + NS_RUNTIMEABORT("Couldn't get directory service"); 1.114 + nsCOMPtr<nsIFile> cwd; 1.115 + nsresult rv = dirsvc->Get(NS_OS_CURRENT_WORKING_DIR, 1.116 + NS_GET_IID(nsIFile), 1.117 + getter_AddRefs(cwd)); 1.118 + if (NS_FAILED(rv)) 1.119 + NS_RUNTIMEABORT("Couldn't get CWD"); 1.120 + crashreporter->SetEnabled(true); 1.121 + crashreporter->SetMinidumpPath(cwd); 1.122 + } 1.123 + } 1.124 +#endif 1.125 + 1.126 + nsRefPtr<WaitForConnectionClosed> spinClose = new WaitForConnectionClosed(); 1.127 + 1.128 + // Tinderboxes are constantly on idle. Since idle tasks can interact with 1.129 + // tests, causing random failures, disable the idle service. 1.130 + disable_idle_service(); 1.131 + 1.132 + do_test_pending(); 1.133 + run_next_test(); 1.134 + 1.135 + // Spin the event loop until we've run out of tests to run. 1.136 + while (gPendingTests) { 1.137 + (void)NS_ProcessNextEvent(); 1.138 + } 1.139 + 1.140 + // And let any other events finish before we quit. 1.141 + (void)NS_ProcessPendingEvents(nullptr); 1.142 + 1.143 + // Check that we have passed all of our tests, and output accordingly. 1.144 + if (gPassedTests == gTotalTests) { 1.145 + passed(TEST_FILE); 1.146 + } 1.147 + 1.148 + (void)fprintf(stderr, TEST_INFO_STR "%u of %u tests passed\n", 1.149 + TEST_FILE, unsigned(gPassedTests), unsigned(gTotalTests)); 1.150 + 1.151 + return gPassedTests == gTotalTests ? 0 : -1; 1.152 +}