2007-09-21 06:17:59 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-09-21 06:17:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test harness for XPCOM objects, providing a scoped XPCOM initializer,
|
2008-06-03 01:29:00 +00:00
|
|
|
* nsCOMPtr, nsRefPtr, do_CreateInstance, do_GetService, ns(Auto|C|)String,
|
|
|
|
* and stdio.h/stdlib.h.
|
2007-09-21 06:17:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TestHarness_h__
|
|
|
|
#define TestHarness_h__
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2012-08-30 19:20:38 +00:00
|
|
|
#include "prenv.h"
|
2007-09-21 07:59:09 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2008-06-03 01:29:00 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2007-09-21 06:17:59 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2008-06-03 01:29:00 +00:00
|
|
|
#include "nsStringGlue.h"
|
2009-09-24 17:49:45 +00:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIProperties.h"
|
2012-01-20 13:47:51 +00:00
|
|
|
#include "nsIObserverService.h"
|
2010-08-12 19:37:44 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2007-09-21 06:17:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-06-03 01:29:00 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static uint32_t gFailCount = 0;
|
2009-03-24 00:44:37 +00:00
|
|
|
|
2008-06-03 01:29:00 +00:00
|
|
|
/**
|
|
|
|
* Prints the given failure message and arguments using printf, prepending
|
2009-02-10 22:05:28 +00:00
|
|
|
* "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and
|
|
|
|
* appending "\n" to eliminate having to type it at each call site.
|
2008-06-03 01:29:00 +00:00
|
|
|
*/
|
|
|
|
void fail(const char* msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2009-02-10 22:05:28 +00:00
|
|
|
printf("TEST-UNEXPECTED-FAIL | ");
|
2008-06-03 01:29:00 +00:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vprintf(msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
putchar('\n');
|
2009-03-24 00:44:37 +00:00
|
|
|
++gFailCount;
|
2008-06-03 01:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-28 09:17:15 +00:00
|
|
|
* Prints the given success message and arguments using printf, prepending
|
|
|
|
* "TEST-PASS " for the benefit of the test harness and
|
|
|
|
* appending "\n" to eliminate having to type it at each call site.
|
2008-06-03 01:29:00 +00:00
|
|
|
*/
|
2012-02-28 09:17:15 +00:00
|
|
|
void passed(const char* msg, ...)
|
2008-06-03 01:29:00 +00:00
|
|
|
{
|
2012-02-28 09:17:15 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
printf("TEST-PASS | ");
|
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vprintf(msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
putchar('\n');
|
2008-06-03 01:29:00 +00:00
|
|
|
}
|
|
|
|
|
2009-04-24 21:47:45 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-09-21 06:17:59 +00:00
|
|
|
|
2010-06-25 12:19:48 +00:00
|
|
|
class ScopedLogging
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScopedLogging()
|
|
|
|
{
|
|
|
|
NS_LogInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedLogging()
|
|
|
|
{
|
|
|
|
NS_LogTerm();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-24 17:49:45 +00:00
|
|
|
class ScopedXPCOM : public nsIDirectoryServiceProvider2
|
2007-09-21 06:17:59 +00:00
|
|
|
{
|
|
|
|
public:
|
2009-09-24 17:49:45 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-08-05 13:36:59 +00:00
|
|
|
explicit ScopedXPCOM(const char* testName,
|
|
|
|
nsIDirectoryServiceProvider *dirSvcProvider = nullptr)
|
2009-09-24 17:49:45 +00:00
|
|
|
: mDirSvcProvider(dirSvcProvider)
|
2007-09-21 06:17:59 +00:00
|
|
|
{
|
|
|
|
mTestName = testName;
|
|
|
|
printf("Running %s tests...\n", mTestName);
|
|
|
|
|
2013-10-10 20:42:16 +00:00
|
|
|
nsresult rv = NS_InitXPCOM2(&mServMgr, nullptr, this);
|
2007-09-21 06:17:59 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2008-06-03 01:29:00 +00:00
|
|
|
fail("NS_InitXPCOM2 returned failure code 0x%x", rv);
|
2013-10-10 20:42:16 +00:00
|
|
|
mServMgr = nullptr;
|
2009-09-24 17:49:45 +00:00
|
|
|
return;
|
2007-09-21 06:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedXPCOM()
|
|
|
|
{
|
2009-09-24 17:49:45 +00:00
|
|
|
// If we created a profile directory, we need to remove it.
|
|
|
|
if (mProfD) {
|
2012-01-20 13:47:51 +00:00
|
|
|
nsCOMPtr<nsIObserverService> os =
|
|
|
|
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
|
|
|
|
MOZ_ASSERT(os);
|
|
|
|
if (os) {
|
2012-07-30 14:20:58 +00:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-change-net-teardown", nullptr)));
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-change-teardown", nullptr)));
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-before-change", nullptr)));
|
2013-03-01 20:11:21 +00:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-before-change2", nullptr)));
|
2012-01-20 13:47:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 00:01:38 +00:00
|
|
|
if (NS_FAILED(mProfD->Remove(true))) {
|
|
|
|
NS_WARNING("Problem removing profile directory");
|
|
|
|
}
|
2009-09-24 17:49:45 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mProfD = nullptr;
|
2009-09-24 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
2007-09-21 06:17:59 +00:00
|
|
|
if (mServMgr)
|
|
|
|
{
|
|
|
|
NS_RELEASE(mServMgr);
|
2013-10-10 20:42:16 +00:00
|
|
|
nsresult rv = NS_ShutdownXPCOM(nullptr);
|
2007-09-21 06:17:59 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2008-06-03 01:29:00 +00:00
|
|
|
fail("XPCOM shutdown failed with code 0x%x", rv);
|
2007-09-21 06:17:59 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Finished running %s tests.\n", mTestName);
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool failed()
|
2007-09-21 06:17:59 +00:00
|
|
|
{
|
2013-10-10 20:42:16 +00:00
|
|
|
return mServMgr == nullptr;
|
2007-09-21 06:17:59 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 17:49:45 +00:00
|
|
|
already_AddRefed<nsIFile> GetProfileDirectory()
|
|
|
|
{
|
|
|
|
if (mProfD) {
|
2011-09-16 20:22:44 +00:00
|
|
|
nsCOMPtr<nsIFile> copy = mProfD;
|
|
|
|
return copy.forget();
|
2009-09-24 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a unique temporary folder to use for this test.
|
2013-08-20 15:59:51 +00:00
|
|
|
// Note that runcppunittests.py will run tests with a temp
|
|
|
|
// directory as the cwd, so just put something under that.
|
2009-09-24 17:49:45 +00:00
|
|
|
nsCOMPtr<nsIFile> profD;
|
2013-08-20 15:59:51 +00:00
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR,
|
2009-09-24 17:49:45 +00:00
|
|
|
getter_AddRefs(profD));
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 17:49:45 +00:00
|
|
|
|
|
|
|
rv = profD->Append(NS_LITERAL_STRING("cpp-unit-profd"));
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 17:49:45 +00:00
|
|
|
|
|
|
|
rv = profD->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755);
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 17:49:45 +00:00
|
|
|
|
|
|
|
mProfD = profD;
|
|
|
|
return profD.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-30 19:20:38 +00:00
|
|
|
already_AddRefed<nsIFile> GetGREDirectory()
|
|
|
|
{
|
|
|
|
if (mGRED) {
|
|
|
|
nsCOMPtr<nsIFile> copy = mGRED;
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
char* env = PR_GetEnv("MOZ_XRE_DIR");
|
|
|
|
nsCOMPtr<nsIFile> greD;
|
|
|
|
if (env) {
|
|
|
|
NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), false,
|
|
|
|
getter_AddRefs(greD));
|
|
|
|
}
|
|
|
|
|
|
|
|
mGRED = greD;
|
|
|
|
return greD.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-10 19:06:57 +00:00
|
|
|
already_AddRefed<nsIFile> GetGREBinDirectory()
|
|
|
|
{
|
|
|
|
if (mGREBinD) {
|
|
|
|
nsCOMPtr<nsIFile> copy = mGREBinD;
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> greD = GetGREDirectory();
|
|
|
|
if (!greD) {
|
|
|
|
return greD.forget();
|
|
|
|
}
|
|
|
|
greD->Clone(getter_AddRefs(mGREBinD));
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
nsAutoCString leafName;
|
|
|
|
mGREBinD->GetNativeLeafName(leafName);
|
|
|
|
if (leafName.Equals("Resources")) {
|
|
|
|
mGREBinD->SetNativeLeafName(NS_LITERAL_CSTRING("MacOS"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> copy = mGREBinD;
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
2009-09-24 17:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIDirectoryServiceProvider
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP GetFile(const char *aProperty, bool *_persistent,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsIFile **_result) override
|
2009-09-24 17:49:45 +00:00
|
|
|
{
|
|
|
|
// If we were supplied a directory service provider, ask it first.
|
|
|
|
if (mDirSvcProvider &&
|
|
|
|
NS_SUCCEEDED(mDirSvcProvider->GetFile(aProperty, _persistent,
|
|
|
|
_result))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, the test harness provides some directories automatically.
|
|
|
|
if (0 == strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR) ||
|
2010-08-12 19:37:44 +00:00
|
|
|
0 == strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR) ||
|
|
|
|
0 == strcmp(aProperty, NS_APP_PROFILE_LOCAL_DIR_STARTUP)) {
|
2009-09-24 17:49:45 +00:00
|
|
|
nsCOMPtr<nsIFile> profD = GetProfileDirectory();
|
|
|
|
NS_ENSURE_TRUE(profD, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> clone;
|
|
|
|
nsresult rv = profD->Clone(getter_AddRefs(clone));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*_persistent = true;
|
2009-09-24 17:49:45 +00:00
|
|
|
clone.forget(_result);
|
|
|
|
return NS_OK;
|
2014-10-10 19:06:57 +00:00
|
|
|
} else if (0 == strcmp(aProperty, NS_GRE_DIR)) {
|
2012-08-30 19:20:38 +00:00
|
|
|
nsCOMPtr<nsIFile> greD = GetGREDirectory();
|
|
|
|
NS_ENSURE_TRUE(greD, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
*_persistent = true;
|
|
|
|
greD.forget(_result);
|
|
|
|
return NS_OK;
|
2014-10-10 19:06:57 +00:00
|
|
|
} else if (0 == strcmp(aProperty, NS_GRE_BIN_DIR)) {
|
|
|
|
nsCOMPtr<nsIFile> greBinD = GetGREBinDirectory();
|
|
|
|
NS_ENSURE_TRUE(greBinD, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
*_persistent = true;
|
|
|
|
greBinD.forget(_result);
|
|
|
|
return NS_OK;
|
2012-08-30 19:20:38 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 17:49:45 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIDirectoryServiceProvider2
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
NS_IMETHODIMP GetFiles(const char *aProperty, nsISimpleEnumerator **_enum) override
|
2009-09-24 17:49:45 +00:00
|
|
|
{
|
|
|
|
// If we were supplied a directory service provider, ask it first.
|
|
|
|
nsCOMPtr<nsIDirectoryServiceProvider2> provider =
|
|
|
|
do_QueryInterface(mDirSvcProvider);
|
|
|
|
if (provider && NS_SUCCEEDED(provider->GetFiles(aProperty, _enum))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2007-09-21 06:17:59 +00:00
|
|
|
private:
|
|
|
|
const char* mTestName;
|
|
|
|
nsIServiceManager* mServMgr;
|
2009-09-24 17:49:45 +00:00
|
|
|
nsCOMPtr<nsIDirectoryServiceProvider> mDirSvcProvider;
|
|
|
|
nsCOMPtr<nsIFile> mProfD;
|
2012-08-30 19:20:38 +00:00
|
|
|
nsCOMPtr<nsIFile> mGRED;
|
2014-10-10 19:06:57 +00:00
|
|
|
nsCOMPtr<nsIFile> mGREBinD;
|
2007-09-21 06:17:59 +00:00
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE(
|
2009-09-24 17:49:45 +00:00
|
|
|
ScopedXPCOM,
|
|
|
|
nsIDirectoryServiceProvider,
|
|
|
|
nsIDirectoryServiceProvider2
|
|
|
|
)
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2009-09-24 17:49:45 +00:00
|
|
|
ScopedXPCOM::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2009-09-24 17:49:45 +00:00
|
|
|
ScopedXPCOM::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-21 06:17:59 +00:00
|
|
|
#endif // TestHarness_h__
|