Back to index

lightning-sunbird  0.9+nobinonly
TestDConnect.cpp
Go to the documentation of this file.
00001 /* vim:set ts=2 sw=2 et cindent: */
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 IPC.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@meer.net>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "ipcIService.h"
00039 #include "ipcIDConnectService.h"
00040 #include "ipcCID.h"
00041 
00042 #include "nsIEventQueueService.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsIComponentRegistrar.h"
00045 
00046 #include "nsXPCOMCID.h"
00047 #include "nsILocalFile.h"
00048 
00049 #include "nsString.h"
00050 #include "prmem.h"
00051 
00052 #if defined( XP_WIN ) || defined( XP_OS2 )
00053 #define TEST_PATH "c:"
00054 #else
00055 #define TEST_PATH "/tmp"
00056 #endif
00057 
00058 #define RETURN_IF_FAILED(rv, step) \
00059     PR_BEGIN_MACRO \
00060     if (NS_FAILED(rv)) { \
00061         printf("*** %s failed: rv=%x\n", step, rv); \
00062         return rv;\
00063     } \
00064     PR_END_MACRO
00065 
00066 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00067 static nsIEventQueue* gEventQ = nsnull;
00068 static PRBool gKeepRunning = PR_TRUE;
00069 
00070 static ipcIService *gIpcServ = nsnull;
00071 
00072 static nsresult DoTest()
00073 {
00074   nsresult rv;
00075 
00076   nsCOMPtr<ipcIDConnectService> dcon = do_GetService("@mozilla.org/ipc/dconnect-service;1", &rv);
00077   RETURN_IF_FAILED(rv, "getting dconnect service");
00078 
00079   PRUint32 remoteClientID = 1;
00080 
00081   nsCOMPtr<nsIFile> file;
00082   rv = dcon->CreateInstanceByContractID(remoteClientID,
00083                                         NS_LOCAL_FILE_CONTRACTID,
00084                                         NS_GET_IID(nsIFile),
00085                                         getter_AddRefs(file)); 
00086   NS_ENSURE_SUCCESS(rv, rv);
00087 
00088   nsCOMPtr<nsISupports> sup = do_QueryInterface(file, &rv);
00089   NS_ENSURE_SUCCESS(rv, rv);
00090 
00091   printf("*** calling QueryInterface\n");
00092   nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(file, &rv);
00093   NS_ENSURE_SUCCESS(rv, rv);
00094 
00095   nsAutoString path;
00096   path.AssignLiteral(TEST_PATH);
00097 
00098   printf("*** calling InitWithNativePath\n");
00099   rv = localFile->InitWithPath(path);
00100   NS_ENSURE_SUCCESS(rv, rv);
00101 
00102   nsAutoString buf;
00103   rv = file->GetPath(buf);
00104   NS_ENSURE_SUCCESS(rv, rv);
00105 
00106   if (!buf.Equals(path))
00107   {
00108     NS_ConvertUTF16toUTF8 temp(buf);
00109     printf("*** GetPath erroneously returned [%s]\n", temp.get());
00110     return NS_ERROR_FAILURE;
00111   }
00112 
00113   PRBool exists;
00114   rv = file->Exists(&exists);
00115   if (NS_FAILED(rv))
00116   {
00117     printf("*** Exists test failed [rv=%x]\n", rv);
00118     return NS_ERROR_FAILURE;
00119   }
00120   printf("File exists? [%d]\n", exists);
00121 
00122   nsCOMPtr<nsIFile> clone;
00123   rv = file->Clone(getter_AddRefs(clone));
00124   if (NS_FAILED(rv))
00125   {
00126     printf("*** Clone test failed [rv=%x]\n", rv);
00127     return NS_ERROR_FAILURE;
00128   }
00129 
00130   nsAutoString node;
00131   node.AssignLiteral("hello.txt");
00132 
00133   rv = clone->Append(node);
00134   if (NS_FAILED(rv))
00135   {
00136     printf("*** Append test failed [rv=%x]\n", rv);
00137     return NS_ERROR_FAILURE;
00138   }
00139 
00140   PRBool match;
00141   rv = file->Equals(clone, &match);
00142   if (NS_FAILED(rv))
00143   {
00144     printf("*** Equals test failed [rv=%x]\n", rv);
00145     return NS_ERROR_FAILURE;
00146   }
00147   printf("Files are equals? [%d]\n", match);
00148 
00149   // now test passing null for interface pointer
00150 
00151   rv = clone->Exists(&exists);
00152   if (NS_FAILED(rv))
00153   {
00154     printf("*** Exists test failed [rv=%x]\n", rv);
00155     return NS_ERROR_FAILURE;
00156   }
00157   if (!exists)
00158   {
00159     rv = clone->Create(nsIFile::NORMAL_FILE_TYPE, 0600);
00160     if (NS_FAILED(rv))
00161     {
00162       printf("*** Create test failed [rv=%x]\n", rv);
00163       return NS_ERROR_FAILURE;
00164     }
00165   }
00166 
00167   rv = clone->MoveTo(nsnull, NS_LITERAL_STRING("helloworld.txt"));
00168   if (NS_FAILED(rv))
00169   {
00170     printf("*** MoveTo test failed [rv=%x]\n", rv);
00171     return NS_ERROR_FAILURE;
00172   }
00173 
00174   // now test passing local objects to a remote object
00175 
00176   nsCOMPtr<nsILocalFile> myLocalFile;
00177   rv = NS_NewLocalFile(path, PR_TRUE, getter_AddRefs(myLocalFile));
00178   if (NS_FAILED(rv))
00179   {
00180     printf("*** NS_NewLocalFile failed [rv=%x]\n", rv);
00181     return NS_ERROR_FAILURE;
00182   }
00183 
00184   rv = file->Equals(myLocalFile, &match);
00185   if (NS_FAILED(rv))
00186   {
00187     printf("*** second Equals test failed [rv=%x]\n", rv);
00188     return NS_ERROR_FAILURE;
00189   }
00190   printf("Files are equals? [%d]\n", match);
00191 
00192   printf("*** DoTest completed successfully :-)\n");
00193   return NS_OK;
00194 }
00195 
00196 int main(int argc, char **argv)
00197 {
00198   nsresult rv;
00199 
00200   PRBool serverMode = PR_FALSE;
00201   if (argc > 1)
00202   {
00203     if (strcmp(argv[1], "-server") == 0)
00204     {
00205       serverMode = PR_TRUE;
00206     }
00207     else
00208     {
00209       printf("usage: %s [-server]\n", argv[0]);
00210       return -1;
00211     }
00212   }
00213 
00214   {
00215     nsCOMPtr<nsIServiceManager> servMan;
00216     NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00217     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00218     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00219     if (registrar)
00220         registrar->AutoRegister(nsnull);
00221 
00222     // Create the Event Queue for this thread...
00223     nsCOMPtr<nsIEventQueueService> eqs =
00224              do_GetService(kEventQueueServiceCID, &rv);
00225     RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
00226 
00227     rv = eqs->CreateMonitoredThreadEventQueue();
00228     RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
00229 
00230     rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00231     RETURN_IF_FAILED(rv, "GetThreadEventQueue");
00232 
00233     nsCOMPtr<ipcIService> ipcServ(do_GetService(IPC_SERVICE_CONTRACTID, &rv));
00234     RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
00235     NS_ADDREF(gIpcServ = ipcServ);
00236 
00237     if (!serverMode)
00238     {
00239       rv = DoTest();
00240       RETURN_IF_FAILED(rv, "DoTest()");
00241     }
00242     else
00243     {
00244       gIpcServ->AddName("DConnectServer");
00245     }
00246 
00247     PLEvent *ev;
00248     while (gKeepRunning)
00249     {
00250       gEventQ->WaitForEvent(&ev);
00251       gEventQ->HandleEvent(ev);
00252     }
00253 
00254     NS_RELEASE(gIpcServ);
00255 
00256     printf("*** processing remaining events\n");
00257 
00258     // process any remaining events
00259     while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
00260         gEventQ->HandleEvent(ev);
00261 
00262     printf("*** done\n");
00263   } // this scopes the nsCOMPtrs
00264 
00265   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00266   rv = NS_ShutdownXPCOM(nsnull);
00267   NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00268 }