michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * A Test application that exercises the sample moudule. This is intented michael@0: * to be a sample application for using xpcom standalone. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "nsXPCOMGlue.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsISample.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: michael@0: #define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1" michael@0: michael@0: int michael@0: main(void) michael@0: { michael@0: nsresult rv; michael@0: michael@0: XPCOMGlueStartup(nullptr); michael@0: michael@0: // Initialize XPCOM michael@0: nsCOMPtr servMan; michael@0: rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: printf("ERROR: XPCOM intialization error [%x].\n", michael@0: static_cast(rv)); michael@0: return -1; michael@0: } michael@0: michael@0: nsCOMPtr manager = do_QueryInterface(servMan); michael@0: michael@0: // Create an instance of our component michael@0: nsCOMPtr mysample; michael@0: rv = manager->CreateInstanceByContractID(NS_SAMPLE_CONTRACTID, michael@0: nullptr, michael@0: NS_GET_IID(nsISample), michael@0: getter_AddRefs(mysample)); michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n" michael@0: "Debugging hint:\n" michael@0: "\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n" michael@0: "\tsetenv NSPR_LOG_FILE xpcom.log\n" michael@0: "\t./nsTestSample\n" michael@0: "\t.\n", michael@0: static_cast(rv)); michael@0: return -2; michael@0: } michael@0: michael@0: // Call methods on our sample to test it out. michael@0: rv = mysample->WriteValue("Inital print:"); michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: printf("ERROR: Calling nsISample::WriteValue() [%x]\n", michael@0: static_cast(rv)); michael@0: return -3; michael@0: } michael@0: michael@0: const char *testValue = "XPCOM defies gravity"; michael@0: rv = mysample->SetValue(testValue); michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: printf("ERROR: Calling nsISample::SetValue() [%x]\n", michael@0: static_cast(rv)); michael@0: return -3; michael@0: } michael@0: printf("Set value to: %s\n", testValue); michael@0: char *str; michael@0: rv = mysample->GetValue(&str); michael@0: michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: printf("ERROR: Calling nsISample::GetValue() [%x]\n", michael@0: static_cast(rv)); michael@0: return -3; michael@0: } michael@0: if (strcmp(str, testValue)) michael@0: { michael@0: printf("Test FAILED.\n"); michael@0: return -4; michael@0: } michael@0: michael@0: NS_Free(str); michael@0: michael@0: rv = mysample->WriteValue("Final print :"); michael@0: printf("Test passed.\n"); michael@0: michael@0: // All nsCOMPtr's must be deleted prior to calling shutdown XPCOM michael@0: // as we should not hold references passed XPCOM Shutdown. michael@0: servMan = 0; michael@0: manager = 0; michael@0: mysample = 0; michael@0: michael@0: // Shutdown XPCOM michael@0: NS_ShutdownXPCOM(nullptr); michael@0: michael@0: return 0; michael@0: }