2005-06-30 06:06:16 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; 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/. */
|
2005-06-30 06:06:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Test application that exercises the sample moudule. This is intented
|
|
|
|
* to be a sample application for using xpcom standalone.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2005-07-01 16:24:07 +04:00
|
|
|
#include "nsXPCOMGlue.h"
|
2005-06-30 06:06:16 +04:00
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsISample.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIComponentRegistrar.h"
|
|
|
|
|
|
|
|
#define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1"
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
XPCOMGlueStartup(nullptr);
|
2005-06-30 06:06:16 +04:00
|
|
|
|
|
|
|
// Initialize XPCOM
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2005-06-30 06:06:16 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2012-10-16 07:52:17 +04:00
|
|
|
printf("ERROR: XPCOM intialization error [%x].\n", rv);
|
2005-06-30 06:06:16 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-06-10 22:11:11 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIComponentManager> manager = do_QueryInterface(servMan);
|
2012-10-16 07:52:17 +04:00
|
|
|
|
2005-06-30 06:06:16 +04:00
|
|
|
// Create an instance of our component
|
|
|
|
nsCOMPtr<nsISample> mysample;
|
|
|
|
rv = manager->CreateInstanceByContractID(NS_SAMPLE_CONTRACTID,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2005-06-30 06:06:16 +04:00
|
|
|
NS_GET_IID(nsISample),
|
|
|
|
getter_AddRefs(mysample));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n"
|
|
|
|
"Debugging hint:\n"
|
|
|
|
"\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n"
|
|
|
|
"\tsetenv NSPR_LOG_FILE xpcom.log\n"
|
|
|
|
"\t./nsTestSample\n"
|
|
|
|
"\t<check the contents for xpcom.log for possible cause of error>.\n",
|
2012-10-16 07:52:17 +04:00
|
|
|
rv);
|
2005-06-30 06:06:16 +04:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call methods on our sample to test it out.
|
|
|
|
rv = mysample->WriteValue("Inital print:");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2012-10-16 07:52:17 +04:00
|
|
|
printf("ERROR: Calling nsISample::WriteValue() [%x]\n", rv);
|
2005-06-30 06:06:16 +04:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *testValue = "XPCOM defies gravity";
|
|
|
|
rv = mysample->SetValue(testValue);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2012-10-16 07:52:17 +04:00
|
|
|
printf("ERROR: Calling nsISample::SetValue() [%x]\n", rv);
|
2005-06-30 06:06:16 +04:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
printf("Set value to: %s\n", testValue);
|
|
|
|
char *str;
|
|
|
|
rv = mysample->GetValue(&str);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2012-10-16 07:52:17 +04:00
|
|
|
printf("ERROR: Calling nsISample::GetValue() [%x]\n", rv);
|
2005-06-30 06:06:16 +04:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
if (strcmp(str, testValue))
|
|
|
|
{
|
|
|
|
printf("Test FAILED.\n");
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2005-07-01 16:24:07 +04:00
|
|
|
NS_Free(str);
|
|
|
|
|
2005-06-30 06:06:16 +04:00
|
|
|
rv = mysample->WriteValue("Final print :");
|
|
|
|
printf("Test passed.\n");
|
|
|
|
|
|
|
|
// All nsCOMPtr's must be deleted prior to calling shutdown XPCOM
|
|
|
|
// as we should not hold references passed XPCOM Shutdown.
|
|
|
|
servMan = 0;
|
|
|
|
manager = 0;
|
|
|
|
mysample = 0;
|
|
|
|
|
|
|
|
// Shutdown XPCOM
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ShutdownXPCOM(nullptr);
|
2005-06-30 06:06:16 +04:00
|
|
|
|
|
|
|
XPCOMGlueShutdown();
|
|
|
|
return 0;
|
|
|
|
}
|