1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/sample/program/nsTestSample.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + * A Test application that exercises the sample moudule. This is intented 1.12 + * to be a sample application for using xpcom standalone. 1.13 + */ 1.14 + 1.15 +#include <stdio.h> 1.16 + 1.17 +#include "nsXPCOMGlue.h" 1.18 +#include "nsXPCOM.h" 1.19 +#include "nsCOMPtr.h" 1.20 +#include "nsISample.h" 1.21 +#include "nsIServiceManager.h" 1.22 +#include "nsIComponentManager.h" 1.23 +#include "nsIComponentRegistrar.h" 1.24 + 1.25 +#define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1" 1.26 + 1.27 +int 1.28 +main(void) 1.29 +{ 1.30 + nsresult rv; 1.31 + 1.32 + XPCOMGlueStartup(nullptr); 1.33 + 1.34 + // Initialize XPCOM 1.35 + nsCOMPtr<nsIServiceManager> servMan; 1.36 + rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); 1.37 + if (NS_FAILED(rv)) 1.38 + { 1.39 + printf("ERROR: XPCOM intialization error [%x].\n", 1.40 + static_cast<uint32_t>(rv)); 1.41 + return -1; 1.42 + } 1.43 + 1.44 + nsCOMPtr<nsIComponentManager> manager = do_QueryInterface(servMan); 1.45 + 1.46 + // Create an instance of our component 1.47 + nsCOMPtr<nsISample> mysample; 1.48 + rv = manager->CreateInstanceByContractID(NS_SAMPLE_CONTRACTID, 1.49 + nullptr, 1.50 + NS_GET_IID(nsISample), 1.51 + getter_AddRefs(mysample)); 1.52 + if (NS_FAILED(rv)) 1.53 + { 1.54 + printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n" 1.55 + "Debugging hint:\n" 1.56 + "\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n" 1.57 + "\tsetenv NSPR_LOG_FILE xpcom.log\n" 1.58 + "\t./nsTestSample\n" 1.59 + "\t<check the contents for xpcom.log for possible cause of error>.\n", 1.60 + static_cast<uint32_t>(rv)); 1.61 + return -2; 1.62 + } 1.63 + 1.64 + // Call methods on our sample to test it out. 1.65 + rv = mysample->WriteValue("Inital print:"); 1.66 + if (NS_FAILED(rv)) 1.67 + { 1.68 + printf("ERROR: Calling nsISample::WriteValue() [%x]\n", 1.69 + static_cast<uint32_t>(rv)); 1.70 + return -3; 1.71 + } 1.72 + 1.73 + const char *testValue = "XPCOM defies gravity"; 1.74 + rv = mysample->SetValue(testValue); 1.75 + if (NS_FAILED(rv)) 1.76 + { 1.77 + printf("ERROR: Calling nsISample::SetValue() [%x]\n", 1.78 + static_cast<uint32_t>(rv)); 1.79 + return -3; 1.80 + } 1.81 + printf("Set value to: %s\n", testValue); 1.82 + char *str; 1.83 + rv = mysample->GetValue(&str); 1.84 + 1.85 + if (NS_FAILED(rv)) 1.86 + { 1.87 + printf("ERROR: Calling nsISample::GetValue() [%x]\n", 1.88 + static_cast<uint32_t>(rv)); 1.89 + return -3; 1.90 + } 1.91 + if (strcmp(str, testValue)) 1.92 + { 1.93 + printf("Test FAILED.\n"); 1.94 + return -4; 1.95 + } 1.96 + 1.97 + NS_Free(str); 1.98 + 1.99 + rv = mysample->WriteValue("Final print :"); 1.100 + printf("Test passed.\n"); 1.101 + 1.102 + // All nsCOMPtr's must be deleted prior to calling shutdown XPCOM 1.103 + // as we should not hold references passed XPCOM Shutdown. 1.104 + servMan = 0; 1.105 + manager = 0; 1.106 + mysample = 0; 1.107 + 1.108 + // Shutdown XPCOM 1.109 + NS_ShutdownXPCOM(nullptr); 1.110 + 1.111 + return 0; 1.112 +}