Bug 517604 - TestHarness.h should provide a directory provider for a profile directory.

Automagically provide a profile directory for native code tests.
r=bsmedberg
This commit is contained in:
Shawn Wilsher 2009-09-24 10:49:45 -07:00
parent 8f8c161f85
commit d646dc8afa
2 changed files with 119 additions and 12 deletions

View File

@ -49,6 +49,12 @@
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsStringGlue.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsIDirectoryService.h"
#include "nsIFile.h"
#include "nsIProperties.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@ -114,7 +120,7 @@ static PRBool _PlatformDeinitProfiler();
* @return PR_TRUE if profiling was available and successfully started.
* @see StopProfiling
*/
static PRBool
inline PRBool
StartProfiling(const char* profileName)
{
if (!gProfilerTriedInit) {
@ -143,7 +149,7 @@ StartProfiling(const char* profileName)
* @return PR_TRUE if profiling was successfully stopped.
* @see StartProfiling
*/
static PRBool
inline PRBool
StopProfiling()
{
NS_ASSERTION(gProfilerTriedInit, "tried to stop profile before starting one");
@ -151,7 +157,7 @@ StopProfiling()
return PR_FALSE;
NS_PRECONDITION(gCurrentProfile, "tried to stop profile before starting one");
const char* profileName = gCurrentProfile;
gCurrentProfile = 0;
return _PlatformStopProfile(profileName);
@ -229,20 +235,24 @@ _PlatformDeinitProfiler()
//-----------------------------------------------------------------------------
class ScopedXPCOM
class ScopedXPCOM : public nsIDirectoryServiceProvider2
{
public:
NS_DECL_ISUPPORTS
ScopedXPCOM(const char* testName,
nsIDirectoryServiceProvider *dirSvcProvider = NULL)
: mDirSvcProvider(dirSvcProvider)
{
mTestName = testName;
printf("Running %s tests...\n", mTestName);
nsresult rv = NS_InitXPCOM2(&mServMgr, NULL, dirSvcProvider);
nsresult rv = NS_InitXPCOM2(&mServMgr, NULL, this);
if (NS_FAILED(rv))
{
fail("NS_InitXPCOM2 returned failure code 0x%x", rv);
mServMgr = NULL;
return;
}
}
@ -252,6 +262,14 @@ class ScopedXPCOM
if (!_PlatformDeinitProfiler())
NS_WARNING("Problem shutting down profiler");
// If we created a profile directory, we need to remove it.
if (mProfD) {
if (NS_FAILED(mProfD->Remove(PR_TRUE)))
NS_WARNING("Problem removing profile direrctory");
mProfD = nsnull;
}
if (mServMgr)
{
NS_RELEASE(mServMgr);
@ -271,9 +289,98 @@ class ScopedXPCOM
return mServMgr == NULL;
}
already_AddRefed<nsIFile> GetProfileDirectory()
{
if (mProfD) {
NS_ADDREF(mProfD);
return mProfD.get();
}
// Create a unique temporary folder to use for this test.
nsCOMPtr<nsIFile> profD;
nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR,
getter_AddRefs(profD));
NS_ENSURE_SUCCESS(rv, nsnull);
rv = profD->Append(NS_LITERAL_STRING("cpp-unit-profd"));
NS_ENSURE_SUCCESS(rv, nsnull);
rv = profD->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755);
NS_ENSURE_SUCCESS(rv, nsnull);
mProfD = profD;
return profD.forget();
}
////////////////////////////////////////////////////////////////////////////
//// nsIDirectoryServiceProvider
NS_IMETHODIMP GetFile(const char *aProperty, PRBool *_persistent,
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) ||
0 == strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR)) {
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);
*_persistent = PR_TRUE;
clone.forget(_result);
return NS_OK;
}
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;
}
private:
const char* mTestName;
nsIServiceManager* mServMgr;
nsCOMPtr<nsIDirectoryServiceProvider> mDirSvcProvider;
nsCOMPtr<nsIFile> mProfD;
};
NS_IMPL_QUERY_INTERFACE2(
ScopedXPCOM,
nsIDirectoryServiceProvider,
nsIDirectoryServiceProvider2
)
NS_IMETHODIMP_(nsrefcnt)
ScopedXPCOM::AddRef()
{
return 2;
}
NS_IMETHODIMP_(nsrefcnt)
ScopedXPCOM::Release()
{
return 1;
}
#endif // TestHarness_h__

View File

@ -73,7 +73,7 @@ NS_DEFINE_CID(kExtServiceB_CID, EXT_SERVICE_B_CID);
#ifdef DEBUG_brade
static void
inline void
debugPrintPath(const char *aPrefix, nsIFile *aFile)
{
if (!aPrefix || !aFile)
@ -187,7 +187,7 @@ nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
#endif
if (NS_FAILED(rv))
{
fprintf(stderr, "%s FAILED - cannot create core service\n", aTestName);
fail("%s FAILED - cannot create core service\n", aTestName);
return rv;
}
@ -199,7 +199,7 @@ nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
#endif
if (NS_FAILED(rv))
{
fprintf(stderr, "%s FAILED - cannot create extension service\n", aTestName);
fail("%s FAILED - cannot create extension service\n", aTestName);
return rv;
}
@ -211,21 +211,21 @@ nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
nsCOMPtr<nsISupports> service = do_CreateInstance(aContractID, &rv);
#ifdef DEBUG_brade
if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
fprintf(stderr, "service: %p\n", service);
fprintf(stderr, "service: %p\n", service.get());
#endif
if (NS_FAILED(rv))
{
fprintf(stderr, "%s FAILED - cannot create service\n", aTestName);
fail("%s FAILED - cannot create service\n", aTestName);
return rv;
}
if (service != extService)
{
fprintf(stderr, "%s FAILED - wrong service registered\n", aTestName);
fail("%s FAILED - wrong service registered\n", aTestName);
return NS_ERROR_FAILURE;
}
fprintf(stderr, "%s PASSED!\n", aTestName);
passed(aTestName);
return NS_OK;
}