~ubuntu-branches/ubuntu/gutsy/virtualbox-ose/gutsy

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/ipc/ipcd/extensions/dconnect/test/TestDConnect.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-09-08 16:44:58 UTC
  • Revision ID: james.westby@ubuntu.com-20070908164458-wao29470vqtr8ksy
Tags: upstream-1.5.0-dfsg2
ImportĀ upstreamĀ versionĀ 1.5.0-dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:set ts=2 sw=2 et cindent: */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is Mozilla IPC.
 
16
 *
 
17
 * The Initial Developer of the Original Code is IBM Corporation.
 
18
 * Portions created by the Initial Developer are Copyright (C) 2004
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *   Darin Fisher <darin@meer.net>
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
#include "ipcIService.h"
 
39
#include "ipcIDConnectService.h"
 
40
#include "ipcCID.h"
 
41
 
 
42
#include "nsIEventQueueService.h"
 
43
#include "nsIServiceManager.h"
 
44
#include "nsIComponentRegistrar.h"
 
45
 
 
46
#include "nsXPCOMCID.h"
 
47
#include "nsILocalFile.h"
 
48
 
 
49
#include "nsString.h"
 
50
#include "prmem.h"
 
51
 
 
52
#if defined( XP_WIN ) || defined( XP_OS2 )
 
53
#define TEST_PATH "c:"
 
54
#else
 
55
#define TEST_PATH "/tmp"
 
56
#endif
 
57
 
 
58
#define RETURN_IF_FAILED(rv, step) \
 
59
    PR_BEGIN_MACRO \
 
60
    if (NS_FAILED(rv)) { \
 
61
        printf("*** %s failed: rv=%x\n", step, rv); \
 
62
        return rv;\
 
63
    } \
 
64
    PR_END_MACRO
 
65
 
 
66
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
 
67
static nsIEventQueue* gEventQ = nsnull;
 
68
static PRBool gKeepRunning = PR_TRUE;
 
69
 
 
70
static ipcIService *gIpcServ = nsnull;
 
71
 
 
72
static nsresult DoTest()
 
73
{
 
74
  nsresult rv;
 
75
 
 
76
  nsCOMPtr<ipcIDConnectService> dcon = do_GetService("@mozilla.org/ipc/dconnect-service;1", &rv);
 
77
  RETURN_IF_FAILED(rv, "getting dconnect service");
 
78
 
 
79
  PRUint32 remoteClientID = 1;
 
80
 
 
81
  nsCOMPtr<nsIFile> file;
 
82
  rv = dcon->CreateInstanceByContractID(remoteClientID,
 
83
                                        NS_LOCAL_FILE_CONTRACTID,
 
84
                                        NS_GET_IID(nsIFile),
 
85
                                        getter_AddRefs(file)); 
 
86
  NS_ENSURE_SUCCESS(rv, rv);
 
87
 
 
88
  nsCOMPtr<nsISupports> sup = do_QueryInterface(file, &rv);
 
89
  NS_ENSURE_SUCCESS(rv, rv);
 
90
 
 
91
  printf("*** calling QueryInterface\n");
 
92
  nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(file, &rv);
 
93
  NS_ENSURE_SUCCESS(rv, rv);
 
94
 
 
95
  nsAutoString path;
 
96
  path.AssignLiteral(TEST_PATH);
 
97
 
 
98
  printf("*** calling InitWithNativePath\n");
 
99
  rv = localFile->InitWithPath(path);
 
100
  NS_ENSURE_SUCCESS(rv, rv);
 
101
 
 
102
  nsAutoString buf;
 
103
  rv = file->GetPath(buf);
 
104
  NS_ENSURE_SUCCESS(rv, rv);
 
105
 
 
106
  if (!buf.Equals(path))
 
107
  {
 
108
    NS_ConvertUTF16toUTF8 temp(buf);
 
109
    printf("*** GetPath erroneously returned [%s]\n", temp.get());
 
110
    return NS_ERROR_FAILURE;
 
111
  }
 
112
 
 
113
  PRBool exists;
 
114
  rv = file->Exists(&exists);
 
115
  if (NS_FAILED(rv))
 
116
  {
 
117
    printf("*** Exists test failed [rv=%x]\n", rv);
 
118
    return NS_ERROR_FAILURE;
 
119
  }
 
120
  printf("File exists? [%d]\n", exists);
 
121
 
 
122
  nsCOMPtr<nsIFile> clone;
 
123
  rv = file->Clone(getter_AddRefs(clone));
 
124
  if (NS_FAILED(rv))
 
125
  {
 
126
    printf("*** Clone test failed [rv=%x]\n", rv);
 
127
    return NS_ERROR_FAILURE;
 
128
  }
 
129
 
 
130
  nsAutoString node;
 
131
  node.AssignLiteral("hello.txt");
 
132
 
 
133
  rv = clone->Append(node);
 
134
  if (NS_FAILED(rv))
 
135
  {
 
136
    printf("*** Append test failed [rv=%x]\n", rv);
 
137
    return NS_ERROR_FAILURE;
 
138
  }
 
139
 
 
140
  PRBool match;
 
141
  rv = file->Equals(clone, &match);
 
142
  if (NS_FAILED(rv))
 
143
  {
 
144
    printf("*** Equals test failed [rv=%x]\n", rv);
 
145
    return NS_ERROR_FAILURE;
 
146
  }
 
147
  printf("Files are equals? [%d]\n", match);
 
148
 
 
149
  // now test passing null for interface pointer
 
150
 
 
151
  rv = clone->Exists(&exists);
 
152
  if (NS_FAILED(rv))
 
153
  {
 
154
    printf("*** Exists test failed [rv=%x]\n", rv);
 
155
    return NS_ERROR_FAILURE;
 
156
  }
 
157
  if (!exists)
 
158
  {
 
159
    rv = clone->Create(nsIFile::NORMAL_FILE_TYPE, 0600);
 
160
    if (NS_FAILED(rv))
 
161
    {
 
162
      printf("*** Create test failed [rv=%x]\n", rv);
 
163
      return NS_ERROR_FAILURE;
 
164
    }
 
165
  }
 
166
 
 
167
  rv = clone->MoveTo(nsnull, NS_LITERAL_STRING("helloworld.txt"));
 
168
  if (NS_FAILED(rv))
 
169
  {
 
170
    printf("*** MoveTo test failed [rv=%x]\n", rv);
 
171
    return NS_ERROR_FAILURE;
 
172
  }
 
173
 
 
174
  // now test passing local objects to a remote object
 
175
 
 
176
  nsCOMPtr<nsILocalFile> myLocalFile;
 
177
  rv = NS_NewLocalFile(path, PR_TRUE, getter_AddRefs(myLocalFile));
 
178
  if (NS_FAILED(rv))
 
179
  {
 
180
    printf("*** NS_NewLocalFile failed [rv=%x]\n", rv);
 
181
    return NS_ERROR_FAILURE;
 
182
  }
 
183
 
 
184
  rv = file->Equals(myLocalFile, &match);
 
185
  if (NS_FAILED(rv))
 
186
  {
 
187
    printf("*** second Equals test failed [rv=%x]\n", rv);
 
188
    return NS_ERROR_FAILURE;
 
189
  }
 
190
  printf("Files are equals? [%d]\n", match);
 
191
 
 
192
  printf("*** DoTest completed successfully :-)\n");
 
193
  return NS_OK;
 
194
}
 
195
 
 
196
int main(int argc, char **argv)
 
197
{
 
198
  nsresult rv;
 
199
 
 
200
  PRBool serverMode = PR_FALSE;
 
201
  if (argc > 1)
 
202
  {
 
203
    if (strcmp(argv[1], "-server") == 0)
 
204
    {
 
205
      serverMode = PR_TRUE;
 
206
    }
 
207
    else
 
208
    {
 
209
      printf("usage: %s [-server]\n", argv[0]);
 
210
      return -1;
 
211
    }
 
212
  }
 
213
 
 
214
  {
 
215
    nsCOMPtr<nsIServiceManager> servMan;
 
216
    NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
 
217
    nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
 
218
    NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
 
219
    if (registrar)
 
220
        registrar->AutoRegister(nsnull);
 
221
 
 
222
    // Create the Event Queue for this thread...
 
223
    nsCOMPtr<nsIEventQueueService> eqs =
 
224
             do_GetService(kEventQueueServiceCID, &rv);
 
225
    RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
 
226
 
 
227
    rv = eqs->CreateMonitoredThreadEventQueue();
 
228
    RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
 
229
 
 
230
    rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
 
231
    RETURN_IF_FAILED(rv, "GetThreadEventQueue");
 
232
 
 
233
    nsCOMPtr<ipcIService> ipcServ(do_GetService(IPC_SERVICE_CONTRACTID, &rv));
 
234
    RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
 
235
    NS_ADDREF(gIpcServ = ipcServ);
 
236
 
 
237
    if (!serverMode)
 
238
    {
 
239
      rv = DoTest();
 
240
      RETURN_IF_FAILED(rv, "DoTest()");
 
241
    }
 
242
    else
 
243
    {
 
244
      gIpcServ->AddName("DConnectServer");
 
245
    }
 
246
 
 
247
    PLEvent *ev;
 
248
    while (gKeepRunning)
 
249
    {
 
250
      gEventQ->WaitForEvent(&ev);
 
251
      gEventQ->HandleEvent(ev);
 
252
    }
 
253
 
 
254
    NS_RELEASE(gIpcServ);
 
255
 
 
256
    printf("*** processing remaining events\n");
 
257
 
 
258
    // process any remaining events
 
259
    while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
 
260
        gEventQ->HandleEvent(ev);
 
261
 
 
262
    printf("*** done\n");
 
263
  } // this scopes the nsCOMPtrs
 
264
 
 
265
  // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
 
266
  rv = NS_ShutdownXPCOM(nsnull);
 
267
  NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
 
268
}