1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/TestHarness.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,284 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Test harness for XPCOM objects, providing a scoped XPCOM initializer, 1.11 + * nsCOMPtr, nsRefPtr, do_CreateInstance, do_GetService, ns(Auto|C|)String, 1.12 + * and stdio.h/stdlib.h. 1.13 + */ 1.14 + 1.15 +#ifndef TestHarness_h__ 1.16 +#define TestHarness_h__ 1.17 + 1.18 +#if defined(_MSC_VER) && defined(MOZ_STATIC_JS) 1.19 +/* 1.20 + * Including js/OldDebugAPI.h may cause build break with --disable-shared-js 1.21 + * This is a workaround for bug 673616. 1.22 + */ 1.23 +#define STATIC_JS_API 1.24 +#endif 1.25 + 1.26 +#include "mozilla/ArrayUtils.h" 1.27 + 1.28 +#include "prenv.h" 1.29 +#include "nsComponentManagerUtils.h" 1.30 +#include "nsServiceManagerUtils.h" 1.31 +#include "nsCOMPtr.h" 1.32 +#include "nsAutoPtr.h" 1.33 +#include "nsStringGlue.h" 1.34 +#include "nsAppDirectoryServiceDefs.h" 1.35 +#include "nsDirectoryServiceDefs.h" 1.36 +#include "nsDirectoryServiceUtils.h" 1.37 +#include "nsIDirectoryService.h" 1.38 +#include "nsIFile.h" 1.39 +#include "nsIProperties.h" 1.40 +#include "nsIObserverService.h" 1.41 +#include "nsXULAppAPI.h" 1.42 +#include <stdio.h> 1.43 +#include <stdlib.h> 1.44 +#include <stdarg.h> 1.45 + 1.46 +static uint32_t gFailCount = 0; 1.47 + 1.48 +/** 1.49 + * Prints the given failure message and arguments using printf, prepending 1.50 + * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and 1.51 + * appending "\n" to eliminate having to type it at each call site. 1.52 + */ 1.53 +void fail(const char* msg, ...) 1.54 +{ 1.55 + va_list ap; 1.56 + 1.57 + printf("TEST-UNEXPECTED-FAIL | "); 1.58 + 1.59 + va_start(ap, msg); 1.60 + vprintf(msg, ap); 1.61 + va_end(ap); 1.62 + 1.63 + putchar('\n'); 1.64 + ++gFailCount; 1.65 +} 1.66 + 1.67 +/** 1.68 + * Prints the given success message and arguments using printf, prepending 1.69 + * "TEST-PASS " for the benefit of the test harness and 1.70 + * appending "\n" to eliminate having to type it at each call site. 1.71 + */ 1.72 +void passed(const char* msg, ...) 1.73 +{ 1.74 + va_list ap; 1.75 + 1.76 + printf("TEST-PASS | "); 1.77 + 1.78 + va_start(ap, msg); 1.79 + vprintf(msg, ap); 1.80 + va_end(ap); 1.81 + 1.82 + putchar('\n'); 1.83 +} 1.84 + 1.85 +//----------------------------------------------------------------------------- 1.86 + 1.87 +class ScopedLogging 1.88 +{ 1.89 +public: 1.90 + ScopedLogging() 1.91 + { 1.92 + NS_LogInit(); 1.93 + } 1.94 + 1.95 + ~ScopedLogging() 1.96 + { 1.97 + NS_LogTerm(); 1.98 + } 1.99 +}; 1.100 + 1.101 +class ScopedXPCOM : public nsIDirectoryServiceProvider2 1.102 +{ 1.103 + public: 1.104 + NS_DECL_ISUPPORTS 1.105 + 1.106 + ScopedXPCOM(const char* testName, 1.107 + nsIDirectoryServiceProvider *dirSvcProvider = nullptr) 1.108 + : mDirSvcProvider(dirSvcProvider) 1.109 + { 1.110 + mTestName = testName; 1.111 + printf("Running %s tests...\n", mTestName); 1.112 + 1.113 + nsresult rv = NS_InitXPCOM2(&mServMgr, nullptr, this); 1.114 + if (NS_FAILED(rv)) 1.115 + { 1.116 + fail("NS_InitXPCOM2 returned failure code 0x%x", rv); 1.117 + mServMgr = nullptr; 1.118 + return; 1.119 + } 1.120 + } 1.121 + 1.122 + ~ScopedXPCOM() 1.123 + { 1.124 + // If we created a profile directory, we need to remove it. 1.125 + if (mProfD) { 1.126 + nsCOMPtr<nsIObserverService> os = 1.127 + do_GetService(NS_OBSERVERSERVICE_CONTRACTID); 1.128 + MOZ_ASSERT(os); 1.129 + if (os) { 1.130 + MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-change-net-teardown", nullptr))); 1.131 + MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-change-teardown", nullptr))); 1.132 + MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-before-change", nullptr))); 1.133 + MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-before-change2", nullptr))); 1.134 + } 1.135 + 1.136 + if (NS_FAILED(mProfD->Remove(true))) { 1.137 + NS_WARNING("Problem removing profile directory"); 1.138 + } 1.139 + 1.140 + mProfD = nullptr; 1.141 + } 1.142 + 1.143 + if (mServMgr) 1.144 + { 1.145 + NS_RELEASE(mServMgr); 1.146 + nsresult rv = NS_ShutdownXPCOM(nullptr); 1.147 + if (NS_FAILED(rv)) 1.148 + { 1.149 + fail("XPCOM shutdown failed with code 0x%x", rv); 1.150 + exit(1); 1.151 + } 1.152 + } 1.153 + 1.154 + printf("Finished running %s tests.\n", mTestName); 1.155 + } 1.156 + 1.157 + bool failed() 1.158 + { 1.159 + return mServMgr == nullptr; 1.160 + } 1.161 + 1.162 + already_AddRefed<nsIFile> GetProfileDirectory() 1.163 + { 1.164 + if (mProfD) { 1.165 + nsCOMPtr<nsIFile> copy = mProfD; 1.166 + return copy.forget(); 1.167 + } 1.168 + 1.169 + // Create a unique temporary folder to use for this test. 1.170 + // Note that runcppunittests.py will run tests with a temp 1.171 + // directory as the cwd, so just put something under that. 1.172 + nsCOMPtr<nsIFile> profD; 1.173 + nsresult rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR, 1.174 + getter_AddRefs(profD)); 1.175 + NS_ENSURE_SUCCESS(rv, nullptr); 1.176 + 1.177 + rv = profD->Append(NS_LITERAL_STRING("cpp-unit-profd")); 1.178 + NS_ENSURE_SUCCESS(rv, nullptr); 1.179 + 1.180 + rv = profD->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755); 1.181 + NS_ENSURE_SUCCESS(rv, nullptr); 1.182 + 1.183 + mProfD = profD; 1.184 + return profD.forget(); 1.185 + } 1.186 + 1.187 + already_AddRefed<nsIFile> GetGREDirectory() 1.188 + { 1.189 + if (mGRED) { 1.190 + nsCOMPtr<nsIFile> copy = mGRED; 1.191 + return copy.forget(); 1.192 + } 1.193 + 1.194 + char* env = PR_GetEnv("MOZ_XRE_DIR"); 1.195 + nsCOMPtr<nsIFile> greD; 1.196 + if (env) { 1.197 + NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), false, 1.198 + getter_AddRefs(greD)); 1.199 + } 1.200 + 1.201 + mGRED = greD; 1.202 + return greD.forget(); 1.203 + } 1.204 + 1.205 + //////////////////////////////////////////////////////////////////////////// 1.206 + //// nsIDirectoryServiceProvider 1.207 + 1.208 + NS_IMETHODIMP GetFile(const char *aProperty, bool *_persistent, 1.209 + nsIFile **_result) 1.210 + { 1.211 + // If we were supplied a directory service provider, ask it first. 1.212 + if (mDirSvcProvider && 1.213 + NS_SUCCEEDED(mDirSvcProvider->GetFile(aProperty, _persistent, 1.214 + _result))) { 1.215 + return NS_OK; 1.216 + } 1.217 + 1.218 + // Otherwise, the test harness provides some directories automatically. 1.219 + if (0 == strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR) || 1.220 + 0 == strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR) || 1.221 + 0 == strcmp(aProperty, NS_APP_PROFILE_LOCAL_DIR_STARTUP)) { 1.222 + nsCOMPtr<nsIFile> profD = GetProfileDirectory(); 1.223 + NS_ENSURE_TRUE(profD, NS_ERROR_FAILURE); 1.224 + 1.225 + nsCOMPtr<nsIFile> clone; 1.226 + nsresult rv = profD->Clone(getter_AddRefs(clone)); 1.227 + NS_ENSURE_SUCCESS(rv, rv); 1.228 + 1.229 + *_persistent = true; 1.230 + clone.forget(_result); 1.231 + return NS_OK; 1.232 + } 1.233 + 1.234 + if (0 == strcmp(aProperty, NS_GRE_DIR)) { 1.235 + nsCOMPtr<nsIFile> greD = GetGREDirectory(); 1.236 + NS_ENSURE_TRUE(greD, NS_ERROR_FAILURE); 1.237 + 1.238 + *_persistent = true; 1.239 + greD.forget(_result); 1.240 + return NS_OK; 1.241 + } 1.242 + 1.243 + return NS_ERROR_FAILURE; 1.244 + } 1.245 + 1.246 + //////////////////////////////////////////////////////////////////////////// 1.247 + //// nsIDirectoryServiceProvider2 1.248 + 1.249 + NS_IMETHODIMP GetFiles(const char *aProperty, nsISimpleEnumerator **_enum) 1.250 + { 1.251 + // If we were supplied a directory service provider, ask it first. 1.252 + nsCOMPtr<nsIDirectoryServiceProvider2> provider = 1.253 + do_QueryInterface(mDirSvcProvider); 1.254 + if (provider && NS_SUCCEEDED(provider->GetFiles(aProperty, _enum))) { 1.255 + return NS_OK; 1.256 + } 1.257 + 1.258 + return NS_ERROR_FAILURE; 1.259 + } 1.260 + 1.261 + private: 1.262 + const char* mTestName; 1.263 + nsIServiceManager* mServMgr; 1.264 + nsCOMPtr<nsIDirectoryServiceProvider> mDirSvcProvider; 1.265 + nsCOMPtr<nsIFile> mProfD; 1.266 + nsCOMPtr<nsIFile> mGRED; 1.267 +}; 1.268 + 1.269 +NS_IMPL_QUERY_INTERFACE( 1.270 + ScopedXPCOM, 1.271 + nsIDirectoryServiceProvider, 1.272 + nsIDirectoryServiceProvider2 1.273 +) 1.274 + 1.275 +NS_IMETHODIMP_(MozExternalRefCountType) 1.276 +ScopedXPCOM::AddRef() 1.277 +{ 1.278 + return 2; 1.279 +} 1.280 + 1.281 +NS_IMETHODIMP_(MozExternalRefCountType) 1.282 +ScopedXPCOM::Release() 1.283 +{ 1.284 + return 1; 1.285 +} 1.286 + 1.287 +#endif // TestHarness_h__