2007-09-20 23:17:59 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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-20 23:17:59 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test harness for XPCOM objects, providing a scoped XPCOM initializer,
|
2008-06-02 18:29:00 -07:00
|
|
|
* nsCOMPtr, nsRefPtr, do_CreateInstance, do_GetService, ns(Auto|C|)String,
|
|
|
|
* and stdio.h/stdlib.h.
|
2007-09-20 23:17:59 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TestHarness_h__
|
|
|
|
#define TestHarness_h__
|
|
|
|
|
2011-08-17 17:33:58 -07:00
|
|
|
#if defined(_MSC_VER) && defined(MOZ_STATIC_JS)
|
|
|
|
/*
|
|
|
|
* Including jsdbgapi.h may cause build break with --disable-shared-js
|
|
|
|
* This is a workaround for bug 673616.
|
|
|
|
*/
|
|
|
|
#define STATIC_JS_API
|
|
|
|
#endif
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2012-08-30 12:20:38 -07:00
|
|
|
#include "prenv.h"
|
2007-09-21 00:59:09 -07:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2008-06-02 18:29:00 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2007-09-20 23:17:59 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2008-06-02 18:29:00 -07:00
|
|
|
#include "nsStringGlue.h"
|
2009-09-24 10:49:45 -07:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIProperties.h"
|
2012-01-20 05:47:51 -08:00
|
|
|
#include "nsIObserverService.h"
|
2010-08-12 12:37:44 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2011-07-26 15:56:09 -07:00
|
|
|
#include "jsdbgapi.h"
|
2007-09-20 23:17:59 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-06-02 18:29:00 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t gFailCount = 0;
|
2009-03-23 17:44:37 -07:00
|
|
|
|
2008-06-02 18:29:00 -07:00
|
|
|
/**
|
|
|
|
* Prints the given failure message and arguments using printf, prepending
|
2009-02-10 14:05:28 -08: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-02 18:29:00 -07:00
|
|
|
*/
|
|
|
|
void fail(const char* msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2009-02-10 14:05:28 -08:00
|
|
|
printf("TEST-UNEXPECTED-FAIL | ");
|
2008-06-02 18:29:00 -07:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vprintf(msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
putchar('\n');
|
2009-03-23 17:44:37 -07:00
|
|
|
++gFailCount;
|
2008-06-02 18:29:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-28 01:17:15 -08: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-02 18:29:00 -07:00
|
|
|
*/
|
2012-02-28 01:17:15 -08:00
|
|
|
void passed(const char* msg, ...)
|
2008-06-02 18:29:00 -07:00
|
|
|
{
|
2012-02-28 01:17:15 -08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
printf("TEST-PASS | ");
|
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vprintf(msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
putchar('\n');
|
2008-06-02 18:29:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-24 14:47:45 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-09-20 23:17:59 -07:00
|
|
|
|
2010-06-25 05:19:48 -07:00
|
|
|
class ScopedLogging
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScopedLogging()
|
|
|
|
{
|
|
|
|
NS_LogInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedLogging()
|
|
|
|
{
|
|
|
|
NS_LogTerm();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-24 10:49:45 -07:00
|
|
|
class ScopedXPCOM : public nsIDirectoryServiceProvider2
|
2007-09-20 23:17:59 -07:00
|
|
|
{
|
|
|
|
public:
|
2009-09-24 10:49:45 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2008-03-31 07:40:10 -07:00
|
|
|
ScopedXPCOM(const char* testName,
|
|
|
|
nsIDirectoryServiceProvider *dirSvcProvider = NULL)
|
2009-09-24 10:49:45 -07:00
|
|
|
: mDirSvcProvider(dirSvcProvider)
|
2007-09-20 23:17:59 -07:00
|
|
|
{
|
|
|
|
mTestName = testName;
|
|
|
|
printf("Running %s tests...\n", mTestName);
|
|
|
|
|
2009-09-24 10:49:45 -07:00
|
|
|
nsresult rv = NS_InitXPCOM2(&mServMgr, NULL, this);
|
2007-09-20 23:17:59 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2008-06-02 18:29:00 -07:00
|
|
|
fail("NS_InitXPCOM2 returned failure code 0x%x", rv);
|
2007-09-20 23:17:59 -07:00
|
|
|
mServMgr = NULL;
|
2009-09-24 10:49:45 -07:00
|
|
|
return;
|
2007-09-20 23:17:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedXPCOM()
|
|
|
|
{
|
2009-09-24 10:49:45 -07:00
|
|
|
// If we created a profile directory, we need to remove it.
|
|
|
|
if (mProfD) {
|
2012-01-20 05:47:51 -08:00
|
|
|
nsCOMPtr<nsIObserverService> os =
|
|
|
|
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
|
|
|
|
MOZ_ASSERT(os);
|
|
|
|
if (os) {
|
2012-07-30 07:20:58 -07: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 12:11:21 -08:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(os->NotifyObservers(nullptr, "profile-before-change2", nullptr)));
|
2012-01-20 05:47:51 -08:00
|
|
|
}
|
|
|
|
|
2011-11-04 17:01:38 -07:00
|
|
|
if (NS_FAILED(mProfD->Remove(true))) {
|
|
|
|
NS_WARNING("Problem removing profile directory");
|
|
|
|
}
|
2009-09-24 10:49:45 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mProfD = nullptr;
|
2009-09-24 10:49:45 -07:00
|
|
|
}
|
|
|
|
|
2007-09-20 23:17:59 -07:00
|
|
|
if (mServMgr)
|
|
|
|
{
|
|
|
|
NS_RELEASE(mServMgr);
|
|
|
|
nsresult rv = NS_ShutdownXPCOM(NULL);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2008-06-02 18:29:00 -07:00
|
|
|
fail("XPCOM shutdown failed with code 0x%x", rv);
|
2007-09-20 23:17:59 -07:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Finished running %s tests.\n", mTestName);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool failed()
|
2007-09-20 23:17:59 -07:00
|
|
|
{
|
|
|
|
return mServMgr == NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-24 10:49:45 -07:00
|
|
|
already_AddRefed<nsIFile> GetProfileDirectory()
|
|
|
|
{
|
|
|
|
if (mProfD) {
|
2011-09-16 13:22:44 -07:00
|
|
|
nsCOMPtr<nsIFile> copy = mProfD;
|
|
|
|
return copy.forget();
|
2009-09-24 10:49:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a unique temporary folder to use for this test.
|
|
|
|
nsCOMPtr<nsIFile> profD;
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR,
|
|
|
|
getter_AddRefs(profD));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 10:49:45 -07:00
|
|
|
|
|
|
|
rv = profD->Append(NS_LITERAL_STRING("cpp-unit-profd"));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 10:49:45 -07:00
|
|
|
|
|
|
|
rv = profD->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 10:49:45 -07:00
|
|
|
|
|
|
|
mProfD = profD;
|
|
|
|
return profD.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-30 12:20:38 -07: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();
|
|
|
|
}
|
|
|
|
|
2009-09-24 10:49:45 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIDirectoryServiceProvider
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP GetFile(const char *aProperty, bool *_persistent,
|
2009-09-24 10:49:45 -07:00
|
|
|
nsIFile **_result)
|
|
|
|
{
|
|
|
|
// 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 12:37:44 -07:00
|
|
|
0 == strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR) ||
|
|
|
|
0 == strcmp(aProperty, NS_APP_PROFILE_LOCAL_DIR_STARTUP)) {
|
2009-09-24 10:49:45 -07: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 07:59:28 -07:00
|
|
|
*_persistent = true;
|
2009-09-24 10:49:45 -07:00
|
|
|
clone.forget(_result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-30 12:20:38 -07:00
|
|
|
if (0 == strcmp(aProperty, NS_GRE_DIR)) {
|
|
|
|
nsCOMPtr<nsIFile> greD = GetGREDirectory();
|
|
|
|
NS_ENSURE_TRUE(greD, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
*_persistent = true;
|
|
|
|
greD.forget(_result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-24 10:49:45 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIDirectoryServiceProvider2
|
|
|
|
|
|
|
|
NS_IMETHODIMP GetFiles(const char *aProperty, nsISimpleEnumerator **_enum)
|
|
|
|
{
|
|
|
|
// 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-20 23:17:59 -07:00
|
|
|
private:
|
|
|
|
const char* mTestName;
|
|
|
|
nsIServiceManager* mServMgr;
|
2009-09-24 10:49:45 -07:00
|
|
|
nsCOMPtr<nsIDirectoryServiceProvider> mDirSvcProvider;
|
|
|
|
nsCOMPtr<nsIFile> mProfD;
|
2012-08-30 12:20:38 -07:00
|
|
|
nsCOMPtr<nsIFile> mGRED;
|
2007-09-20 23:17:59 -07:00
|
|
|
};
|
|
|
|
|
2009-09-24 10:49:45 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE2(
|
|
|
|
ScopedXPCOM,
|
|
|
|
nsIDirectoryServiceProvider,
|
|
|
|
nsIDirectoryServiceProvider2
|
|
|
|
)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
ScopedXPCOM::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
ScopedXPCOM::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-20 23:17:59 -07:00
|
|
|
#endif // TestHarness_h__
|