Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * A Test application that exercises the sample moudule. This is intented |
michael@0 | 9 | * to be a sample application for using xpcom standalone. |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #include <stdio.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "nsXPCOMGlue.h" |
michael@0 | 15 | #include "nsXPCOM.h" |
michael@0 | 16 | #include "nsCOMPtr.h" |
michael@0 | 17 | #include "nsISample.h" |
michael@0 | 18 | #include "nsIServiceManager.h" |
michael@0 | 19 | #include "nsIComponentManager.h" |
michael@0 | 20 | #include "nsIComponentRegistrar.h" |
michael@0 | 21 | |
michael@0 | 22 | #define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1" |
michael@0 | 23 | |
michael@0 | 24 | int |
michael@0 | 25 | main(void) |
michael@0 | 26 | { |
michael@0 | 27 | nsresult rv; |
michael@0 | 28 | |
michael@0 | 29 | XPCOMGlueStartup(nullptr); |
michael@0 | 30 | |
michael@0 | 31 | // Initialize XPCOM |
michael@0 | 32 | nsCOMPtr<nsIServiceManager> servMan; |
michael@0 | 33 | rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
michael@0 | 34 | if (NS_FAILED(rv)) |
michael@0 | 35 | { |
michael@0 | 36 | printf("ERROR: XPCOM intialization error [%x].\n", |
michael@0 | 37 | static_cast<uint32_t>(rv)); |
michael@0 | 38 | return -1; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | nsCOMPtr<nsIComponentManager> manager = do_QueryInterface(servMan); |
michael@0 | 42 | |
michael@0 | 43 | // Create an instance of our component |
michael@0 | 44 | nsCOMPtr<nsISample> mysample; |
michael@0 | 45 | rv = manager->CreateInstanceByContractID(NS_SAMPLE_CONTRACTID, |
michael@0 | 46 | nullptr, |
michael@0 | 47 | NS_GET_IID(nsISample), |
michael@0 | 48 | getter_AddRefs(mysample)); |
michael@0 | 49 | if (NS_FAILED(rv)) |
michael@0 | 50 | { |
michael@0 | 51 | printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n" |
michael@0 | 52 | "Debugging hint:\n" |
michael@0 | 53 | "\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n" |
michael@0 | 54 | "\tsetenv NSPR_LOG_FILE xpcom.log\n" |
michael@0 | 55 | "\t./nsTestSample\n" |
michael@0 | 56 | "\t<check the contents for xpcom.log for possible cause of error>.\n", |
michael@0 | 57 | static_cast<uint32_t>(rv)); |
michael@0 | 58 | return -2; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | // Call methods on our sample to test it out. |
michael@0 | 62 | rv = mysample->WriteValue("Inital print:"); |
michael@0 | 63 | if (NS_FAILED(rv)) |
michael@0 | 64 | { |
michael@0 | 65 | printf("ERROR: Calling nsISample::WriteValue() [%x]\n", |
michael@0 | 66 | static_cast<uint32_t>(rv)); |
michael@0 | 67 | return -3; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | const char *testValue = "XPCOM defies gravity"; |
michael@0 | 71 | rv = mysample->SetValue(testValue); |
michael@0 | 72 | if (NS_FAILED(rv)) |
michael@0 | 73 | { |
michael@0 | 74 | printf("ERROR: Calling nsISample::SetValue() [%x]\n", |
michael@0 | 75 | static_cast<uint32_t>(rv)); |
michael@0 | 76 | return -3; |
michael@0 | 77 | } |
michael@0 | 78 | printf("Set value to: %s\n", testValue); |
michael@0 | 79 | char *str; |
michael@0 | 80 | rv = mysample->GetValue(&str); |
michael@0 | 81 | |
michael@0 | 82 | if (NS_FAILED(rv)) |
michael@0 | 83 | { |
michael@0 | 84 | printf("ERROR: Calling nsISample::GetValue() [%x]\n", |
michael@0 | 85 | static_cast<uint32_t>(rv)); |
michael@0 | 86 | return -3; |
michael@0 | 87 | } |
michael@0 | 88 | if (strcmp(str, testValue)) |
michael@0 | 89 | { |
michael@0 | 90 | printf("Test FAILED.\n"); |
michael@0 | 91 | return -4; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | NS_Free(str); |
michael@0 | 95 | |
michael@0 | 96 | rv = mysample->WriteValue("Final print :"); |
michael@0 | 97 | printf("Test passed.\n"); |
michael@0 | 98 | |
michael@0 | 99 | // All nsCOMPtr's must be deleted prior to calling shutdown XPCOM |
michael@0 | 100 | // as we should not hold references passed XPCOM Shutdown. |
michael@0 | 101 | servMan = 0; |
michael@0 | 102 | manager = 0; |
michael@0 | 103 | mysample = 0; |
michael@0 | 104 | |
michael@0 | 105 | // Shutdown XPCOM |
michael@0 | 106 | NS_ShutdownXPCOM(nullptr); |
michael@0 | 107 | |
michael@0 | 108 | return 0; |
michael@0 | 109 | } |