xpcom/sample/program/nsTestSample.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial