Back to index

lightning-sunbird  0.9+nobinonly
nsTestSample.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Suresh Duddi <dp@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00045 #include <stdio.h>
00046 
00047 #include "nsXPCOMGlue.h"
00048 #include "nsXPCOM.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsISample.h"
00051 #include "nsIServiceManager.h"
00052 #include "nsIComponentManager.h"
00053 #include "nsIComponentRegistrar.h"
00054 
00055 #define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1"
00056 
00057 int
00058 main(void)
00059 {
00060     nsresult rv;
00061 
00062     XPCOMGlueStartup(nsnull);
00063 
00064     // Initialize XPCOM
00065     nsCOMPtr<nsIServiceManager> servMan;
00066     rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00067     if (NS_FAILED(rv))
00068     {
00069         printf("ERROR: XPCOM intialization error [%x].\n", rv);
00070         return -1;
00071     }
00072     // register all components in our default component directory
00073     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00074     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00075     registrar->AutoRegister(nsnull);
00076     
00077     nsCOMPtr<nsIComponentManager> manager = do_QueryInterface(registrar);
00078     NS_ASSERTION(registrar, "Null nsIComponentManager");
00079     
00080     // Create an instance of our component
00081     nsCOMPtr<nsISample> mysample;
00082     rv = manager->CreateInstanceByContractID(NS_SAMPLE_CONTRACTID,
00083                                              nsnull,
00084                                              NS_GET_IID(nsISample),
00085                                              getter_AddRefs(mysample));
00086     if (NS_FAILED(rv))
00087     {
00088         printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n"
00089                "Debugging hint:\n"
00090                "\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n"
00091                "\tsetenv NSPR_LOG_FILE xpcom.log\n"
00092                "\t./nsTestSample\n"
00093                "\t<check the contents for xpcom.log for possible cause of error>.\n",
00094                rv);
00095         return -2;
00096     }
00097 
00098     // Call methods on our sample to test it out.
00099     rv = mysample->WriteValue("Inital print:");
00100     if (NS_FAILED(rv))
00101     {
00102         printf("ERROR: Calling nsISample::WriteValue() [%x]\n", rv);
00103         return -3;
00104     }
00105 
00106     const char *testValue = "XPCOM defies gravity";
00107     rv = mysample->SetValue(testValue);
00108     if (NS_FAILED(rv))
00109     {
00110         printf("ERROR: Calling nsISample::SetValue() [%x]\n", rv);
00111         return -3;
00112     }
00113     printf("Set value to: %s\n", testValue);
00114     char *str;
00115     rv = mysample->GetValue(&str);
00116 
00117     if (NS_FAILED(rv))
00118     {
00119         printf("ERROR: Calling nsISample::GetValue() [%x]\n", rv);
00120         return -3;
00121     }
00122     if (strcmp(str, testValue))
00123     {
00124         printf("Test FAILED.\n");
00125         return -4;
00126     }
00127 
00128     NS_Free(str);
00129 
00130     rv = mysample->WriteValue("Final print :");
00131     printf("Test passed.\n");
00132     
00133     // All nsCOMPtr's must be deleted prior to calling shutdown XPCOM
00134     // as we should not hold references passed XPCOM Shutdown.
00135     servMan = 0;
00136     registrar = 0;
00137     manager = 0;
00138     mysample = 0;
00139     
00140     // Shutdown XPCOM
00141     NS_ShutdownXPCOM(nsnull);
00142 
00143     XPCOMGlueShutdown();
00144     return 0;
00145 }