2007-09-21 10:17:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04: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 10:17:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test harness for XPCOM objects, providing a scoped XPCOM initializer,
|
2008-06-03 05:29:00 +04:00
|
|
|
* nsCOMPtr, nsRefPtr, do_CreateInstance, do_GetService, ns(Auto|C|)String,
|
|
|
|
* and stdio.h/stdlib.h.
|
2007-09-21 10:17:59 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TestHarness_h__
|
|
|
|
#define TestHarness_h__
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2017-05-05 23:23:00 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2012-08-30 23:20:38 +04:00
|
|
|
#include "prenv.h"
|
2007-09-21 11:59:09 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2008-06-03 05:29:00 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2007-09-21 10:17:59 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.h"
|
2009-09-24 21:49:45 +04:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsIFile.h"
|
2012-01-20 17:47:51 +04:00
|
|
|
#include "nsIObserverService.h"
|
Bug 1600545 - Remove useless inclusions of header files generated from IDL files in modules/, netwerk/, parser/, security/, startupcache/, storage/, toolkit/, tools/, uriloader/, widget/, xpcom/ and xpfe/ r=Ehsan
The inclusions were removed with the following very crude script and the
resulting breakage was fixed up by hand. The manual fixups did either
revert the changes done by the script, replace a generic header with a more
specific one or replace a header with a forward declaration.
find . -name "*.idl" | grep -v web-platform | grep -v third_party | while read path; do
interfaces=$(grep "^\(class\|interface\).*:.*" "$path" | cut -d' ' -f2)
if [ -n "$interfaces" ]; then
if [[ "$interfaces" == *$'\n'* ]]; then
regexp="\("
for i in $interfaces; do regexp="$regexp$i\|"; done
regexp="${regexp%%\\\|}\)"
else
regexp="$interfaces"
fi
interface=$(basename "$path")
rg -l "#include.*${interface%%.idl}.h" . | while read path2; do
hits=$(grep -v "#include.*${interface%%.idl}.h" "$path2" | grep -c "$regexp" )
if [ $hits -eq 0 ]; then
echo "Removing ${interface} from ${path2}"
grep -v "#include.*${interface%%.idl}.h" "$path2" > "$path2".tmp
mv -f "$path2".tmp "$path2"
fi
done
fi
done
Differential Revision: https://phabricator.services.mozilla.com/D55444
--HG--
extra : moz-landing-system : lando
2019-12-06 12:17:57 +03:00
|
|
|
#include "nsIServiceManager.h"
|
2010-08-12 23:37:44 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2007-09-21 10:17:59 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-06-03 05:29:00 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t gFailCount = 0;
|
2009-03-24 03:44:37 +03:00
|
|
|
|
2008-06-03 05:29:00 +04:00
|
|
|
/**
|
|
|
|
* Prints the given failure message and arguments using printf, prepending
|
2009-02-11 01:05:28 +03: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 05:29:00 +04:00
|
|
|
*/
|
2017-05-05 23:23:00 +03:00
|
|
|
MOZ_FORMAT_PRINTF(1, 2) void fail(const char* msg, ...) {
|
2008-06-03 05:29:00 +04:00
|
|
|
va_list ap;
|
|
|
|
|
2009-02-11 01:05:28 +03:00
|
|
|
printf("TEST-UNEXPECTED-FAIL | ");
|
2008-06-03 05:29:00 +04:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vprintf(msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
putchar('\n');
|
2009-03-24 03:44:37 +03:00
|
|
|
++gFailCount;
|
2008-06-03 05:29:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-28 13:17:15 +04: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 05:29:00 +04:00
|
|
|
*/
|
2017-05-05 23:23:00 +03:00
|
|
|
MOZ_FORMAT_PRINTF(1, 2) void passed(const char* msg, ...) {
|
2012-02-28 13:17:15 +04:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
printf("TEST-PASS | ");
|
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vprintf(msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
putchar('\n');
|
2008-06-03 05:29:00 +04:00
|
|
|
}
|
|
|
|
|
2009-04-25 01:47:45 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-09-21 10:17:59 +04:00
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
class ScopedXPCOM : public nsIDirectoryServiceProvider2 {
|
2007-09-21 10:17:59 +04:00
|
|
|
public:
|
2009-09-24 21:49:45 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit ScopedXPCOM(const char* testName,
|
2018-04-13 16:01:28 +03:00
|
|
|
nsIDirectoryServiceProvider* dirSvcProvider = nullptr)
|
2018-06-15 14:41:20 +03:00
|
|
|
: mServMgr(nullptr), mDirSvcProvider(dirSvcProvider) {
|
2007-09-21 10:17:59 +04:00
|
|
|
mTestName = testName;
|
|
|
|
printf("Running %s tests...\n", mTestName);
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2019-03-15 09:38:09 +03:00
|
|
|
nsresult rv = NS_InitXPCOM(&mServMgr, nullptr, this);
|
2007-09-21 10:17:59 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2019-03-15 09:38:09 +03:00
|
|
|
fail("NS_InitXPCOM returned failure code 0x%" PRIx32,
|
2017-05-05 23:23:00 +03:00
|
|
|
static_cast<uint32_t>(rv));
|
2013-10-11 00:42:16 +04:00
|
|
|
mServMgr = nullptr;
|
2009-09-24 21:49:45 +04:00
|
|
|
return;
|
2007-09-21 10:17:59 +04:00
|
|
|
}
|
2018-12-14 21:10:35 +03:00
|
|
|
}
|
2007-09-21 10:17:59 +04:00
|
|
|
|
|
|
|
~ScopedXPCOM() {
|
2009-09-24 21:49:45 +04:00
|
|
|
// If we created a profile directory, we need to remove it.
|
|
|
|
if (mProfD) {
|
2012-01-20 17:47:51 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os =
|
|
|
|
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
|
2016-10-13 05:40:45 +03:00
|
|
|
MOZ_RELEASE_ASSERT(os);
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
os->NotifyObservers(nullptr, "profile-change-net-teardown", nullptr));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
os->NotifyObservers(nullptr, "profile-change-teardown", nullptr));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
os->NotifyObservers(nullptr, "profile-before-change", nullptr));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
os->NotifyObservers(nullptr, "profile-before-change-qm", nullptr));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(os->NotifyObservers(
|
|
|
|
nullptr, "profile-before-change-telemetry", nullptr));
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2011-11-05 04:01:38 +04:00
|
|
|
if (NS_FAILED(mProfD->Remove(true))) {
|
|
|
|
NS_WARNING("Problem removing profile directory");
|
2009-09-24 21:49:45 +04:00
|
|
|
}
|
|
|
|
|
2013-10-11 00:42:16 +04:00
|
|
|
mProfD = nullptr;
|
2007-09-21 10:17:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mServMgr) {
|
|
|
|
NS_RELEASE(mServMgr);
|
|
|
|
nsresult rv = NS_ShutdownXPCOM(nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
fail("XPCOM shutdown failed with code 0x%" PRIx32,
|
|
|
|
static_cast<uint32_t>(rv));
|
2018-12-14 21:10:35 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-09-21 10:17:59 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
printf("Finished running %s tests.\n", mTestName);
|
2018-12-14 21:10:35 +03:00
|
|
|
}
|
|
|
|
|
2013-10-11 00:42:16 +04:00
|
|
|
bool failed() { return mServMgr == nullptr; }
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
already_AddRefed<nsIFile> GetProfileDirectory() {
|
|
|
|
if (mProfD) {
|
2011-09-17 00:22:44 +04:00
|
|
|
nsCOMPtr<nsIFile> copy = mProfD;
|
|
|
|
return copy.forget();
|
2007-09-21 10:17:59 +04:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
// Create a unique temporary folder to use for this test.
|
|
|
|
// Note that runcppunittests.py will run tests with a temp
|
|
|
|
// directory as the cwd, so just put something under that.
|
2011-09-17 00:22:44 +04:00
|
|
|
nsCOMPtr<nsIFile> profD;
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR,
|
|
|
|
getter_AddRefs(profD));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 21:49:45 +04:00
|
|
|
|
2013-08-20 19:59:51 +04:00
|
|
|
rv = profD->Append(NS_LITERAL_STRING("cpp-unit-profd"));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 21:49:45 +04:00
|
|
|
|
|
|
|
rv = profD->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-09-24 21:49:45 +04:00
|
|
|
|
|
|
|
mProfD = profD;
|
|
|
|
return profD.forget();
|
2018-12-14 21:10:35 +03:00
|
|
|
}
|
2009-09-24 21:49:45 +04:00
|
|
|
|
2012-08-30 23:20:38 +04:00
|
|
|
already_AddRefed<nsIFile> GetGREDirectory() {
|
|
|
|
if (mGRED) {
|
2009-09-24 21:49:45 +04:00
|
|
|
nsCOMPtr<nsIFile> copy = mGRED;
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-30 23:20:38 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIFile> GetGREBinDirectory() {
|
2014-10-10 23:06:57 +04:00
|
|
|
if (mGREBinD) {
|
2012-08-30 23:20:38 +04:00
|
|
|
nsCOMPtr<nsIFile> copy = mGREBinD;
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-10 23:06:57 +04:00
|
|
|
nsCOMPtr<nsIFile> greD = GetGREDirectory();
|
|
|
|
if (!greD) {
|
|
|
|
return greD.forget();
|
|
|
|
}
|
|
|
|
greD->Clone(getter_AddRefs(mGREBinD));
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
nsAutoCString leafName;
|
|
|
|
mGREBinD->GetNativeLeafName(leafName);
|
2017-09-06 11:13:45 +03:00
|
|
|
if (leafName.EqualsLiteral("Resources")) {
|
2014-10-10 23:06:57 +04:00
|
|
|
mGREBinD->SetNativeLeafName(NS_LITERAL_CSTRING("MacOS"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> copy = mGREBinD;
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIDirectoryServiceProvider
|
|
|
|
|
|
|
|
NS_IMETHOD GetFile(const char* aProperty, bool* _persistent,
|
|
|
|
nsIFile** _result) override {
|
|
|
|
// If we were supplied a directory service provider, ask it first.
|
|
|
|
if (mDirSvcProvider && NS_SUCCEEDED(mDirSvcProvider->GetFile(
|
2014-10-10 23:06:57 +04:00
|
|
|
aProperty, _persistent, _result))) {
|
|
|
|
return NS_OK;
|
2012-08-30 23:20:38 +04:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
// Otherwise, the test harness provides some directories automatically.
|
|
|
|
if (0 == strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR) ||
|
2010-08-12 23:37:44 +04:00
|
|
|
0 == strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR) ||
|
2009-09-24 21:49:45 +04:00
|
|
|
0 == strcmp(aProperty, NS_APP_PROFILE_LOCAL_DIR_STARTUP)) {
|
|
|
|
nsCOMPtr<nsIFile> profD = GetProfileDirectory();
|
|
|
|
NS_ENSURE_TRUE(profD, NS_ERROR_FAILURE);
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
nsCOMPtr<nsIFile> clone;
|
|
|
|
nsresult rv = profD->Clone(getter_AddRefs(clone));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*_persistent = true;
|
2009-09-24 21:49:45 +04:00
|
|
|
clone.forget(_result);
|
|
|
|
return NS_OK;
|
2014-10-10 23:06:57 +04:00
|
|
|
} else if (0 == strcmp(aProperty, NS_GRE_DIR)) {
|
2012-08-30 23:20:38 +04:00
|
|
|
nsCOMPtr<nsIFile> greD = GetGREDirectory();
|
|
|
|
NS_ENSURE_TRUE(greD, NS_ERROR_FAILURE);
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*_persistent = true;
|
2012-08-30 23:20:38 +04:00
|
|
|
greD.forget(_result);
|
2009-09-24 21:49:45 +04:00
|
|
|
return NS_OK;
|
2014-10-10 23:06:57 +04:00
|
|
|
} else if (0 == strcmp(aProperty, NS_GRE_BIN_DIR)) {
|
|
|
|
nsCOMPtr<nsIFile> greBinD = GetGREBinDirectory();
|
|
|
|
NS_ENSURE_TRUE(greBinD, NS_ERROR_FAILURE);
|
2018-12-14 21:10:35 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*_persistent = true;
|
2014-10-10 23:06:57 +04:00
|
|
|
greBinD.forget(_result);
|
2009-09-24 21:49:45 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-12-14 21:10:35 +03:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIDirectoryServiceProvider2
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD GetFiles(const char* aProperty,
|
|
|
|
nsISimpleEnumerator** _enum) override {
|
2009-09-24 21:49:45 +04: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 10:17:59 +04:00
|
|
|
private:
|
|
|
|
const char* mTestName;
|
|
|
|
nsIServiceManager* mServMgr;
|
2009-09-24 21:49:45 +04:00
|
|
|
nsCOMPtr<nsIDirectoryServiceProvider> mDirSvcProvider;
|
|
|
|
nsCOMPtr<nsIFile> mProfD;
|
2012-08-30 23:20:38 +04:00
|
|
|
nsCOMPtr<nsIFile> mGRED;
|
2014-10-10 23:06:57 +04:00
|
|
|
nsCOMPtr<nsIFile> mGREBinD;
|
2007-09-21 10:17:59 +04:00
|
|
|
};
|
|
|
|
|
2009-09-24 21:49:45 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(ScopedXPCOM, nsIDirectoryServiceProvider,
|
|
|
|
nsIDirectoryServiceProvider2)
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2009-09-24 21:49:45 +04:00
|
|
|
ScopedXPCOM::AddRef() { return 2; }
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2009-09-24 21:49:45 +04:00
|
|
|
ScopedXPCOM::Release() { return 1; }
|
|
|
|
|
2007-09-21 10:17:59 +04:00
|
|
|
#endif // TestHarness_h__
|