Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / netwerk / test / TestMCTransport.cpp
blob93d1a4c2f0f304b0aa6d918358c6352d17d8b6f4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Darin Fisher <darin@netscape.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include <stdio.h>
41 #include "nsIServiceManager.h"
42 #include "nsIEventQueueService.h"
43 #include "nsIOutputStream.h"
44 #include "nsIStreamListener.h"
45 #include "nsITransport.h"
46 #include "nsIInputStream.h"
47 #include "nsIOutputStream.h"
48 #include "nsCOMPtr.h"
49 #include "plstr.h"
50 #include "prprf.h"
52 #ifndef USE_CREATE_INSTANCE
53 #include "nsICacheService.h"
54 #include "nsICacheSession.h"
55 #include "nsICacheEntryDescriptor.h"
56 #include "nsNetCID.h"
57 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
58 static nsICacheSession *session = nsnull;
59 static nsICacheEntryDescriptor *desc = nsnull;
60 #endif
62 /**
63 * This test program exercises the memory cache's nsITransport implementation.
65 * This test program loads a file into the memory cache (using OpenOutputStream),
66 * and then reads the file back out (using AsyncRead). The data read from the
67 * memory cache is written to a new file (with .out added as a suffix to the file
68 * name).
71 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
72 static nsIEventQueue *gEventQ = nsnull;
74 class TestListener : public nsIStreamListener
76 public:
77 NS_DECL_ISUPPORTS
78 NS_DECL_NSISTREAMLISTENER
79 NS_DECL_NSIREQUESTOBSERVER
81 TestListener(char *);
82 virtual ~TestListener();
84 private:
85 char *mFilename;
86 FILE *mFile;
89 NS_IMPL_ISUPPORTS2(TestListener,
90 nsIStreamListener,
91 nsIRequestObserver)
93 TestListener::TestListener(char *filename)
94 : mFilename(filename)
95 , mFile(nsnull)
99 TestListener::~TestListener()
103 NS_IMETHODIMP
104 TestListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
106 printf("OnStartRequest\n");
108 mFile = fopen(mFilename, "w");
109 if (!mFile)
110 return NS_ERROR_FAILURE;
112 return NS_OK;
115 NS_IMETHODIMP
116 TestListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
118 printf("OnStopRequest: status=%x\n", status);
120 if (mFile)
121 fclose(mFile);
123 return NS_OK;
126 NS_IMETHODIMP
127 TestListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
128 nsIInputStream *is,
129 PRUint32 offset, PRUint32 count)
131 printf("OnDataAvailable: offset=%u count=%u\n", offset, count);
133 if (!mFile) return NS_ERROR_FAILURE;
135 char buf[128];
136 nsresult rv;
137 PRUint32 nread = 0;
139 while (count) {
140 PRUint32 amount = PR_MIN(count, sizeof(buf));
142 rv = is->Read(buf, amount, &nread);
143 if (NS_FAILED(rv)) return rv;
145 fwrite(buf, nread, 1, mFile);
146 count -= nread;
148 return NS_OK;
151 nsresult TestMCTransport(const char *filename)
153 nsresult rv = NS_OK;
154 nsCOMPtr<nsITransport> transport;
156 #ifdef USE_CREATE_INSTANCE
157 transport = do_CreateInstance(
158 "@mozilla.org/network/memory-cache-transport;1", &rv);
159 if (NS_FAILED(rv))
160 return rv;
161 #else
162 nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
163 if (NS_FAILED(rv)) return rv;
165 rv = serv->CreateSession("TestMCTransport",
166 nsICache::STORE_IN_MEMORY, PR_TRUE,
167 &session);
168 if (NS_FAILED(rv)) return rv;
170 rv = session->OpenCacheEntry(nsDependentCString(filename),
171 nsICache::ACCESS_READ_WRITE,
172 nsICache::BLOCKING,
173 &desc);
174 if (NS_FAILED(rv)) return rv;
176 rv = desc->MarkValid();
177 if (NS_FAILED(rv)) return rv;
179 rv = desc->GetTransport(getter_AddRefs(transport));
180 if (NS_FAILED(rv)) return rv;
181 #endif
183 nsCOMPtr<nsIOutputStream> os;
184 rv = transport->OpenOutputStream(0, (PRUint32) -1, 0, getter_AddRefs(os));
185 if (NS_FAILED(rv)) return rv;
187 char *out = PR_smprintf("%s.out", filename);
188 nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
189 if (!listener)
190 return NS_ERROR_OUT_OF_MEMORY;
192 nsCOMPtr<nsIRequest> req;
193 rv = transport->AsyncRead(listener, nsnull, 0, (PRUint32) -1, 0, getter_AddRefs(req));
194 if (NS_FAILED(rv)) return rv;
196 FILE *file = fopen(filename, "r");
197 if (!file)
198 return NS_ERROR_FILE_NOT_FOUND;
200 char buf[256];
201 PRUint32 count, total=0;
203 while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
204 printf("writing %u bytes\n", count);
205 total += count;
206 rv = os->Write(buf, count, &count);
207 if (NS_FAILED(rv)) return rv;
209 // process an event
210 PLEvent *event = nsnull;
211 gEventQ->GetEvent(&event);
212 if (event) gEventQ->HandleEvent(event);
215 printf("wrote %u bytes\n", total);
217 return rv;
220 int main(int argc, char **argv)
222 nsresult rv;
224 if (argc < 2) {
225 printf("usage: %s filename\n", argv[0]);
226 return -1;
229 nsCOMPtr<nsIEventQueueService> eqs =
230 do_GetService(kEventQueueServiceCID, &rv);
231 if (NS_FAILED(rv)) {
232 printf("failed to create event queue service: rv=%x\n", rv);
233 return -1;
236 rv = eqs->CreateMonitoredThreadEventQueue();
237 if (NS_FAILED(rv)) {
238 printf("failed to create monitored event queue: rv=%x\n", rv);
239 return -1;
242 rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
243 if (NS_FAILED(rv)) {
244 printf("failed to get thread event queue: %x\n", rv);
245 return -1;
248 rv = TestMCTransport(argv[1]);
249 printf("TestMCTransport returned %x\n", rv);
251 gEventQ->ProcessPendingEvents();
253 #ifndef USE_CREATE_INSTANCE
254 NS_IF_RELEASE(desc);
255 NS_IF_RELEASE(session);
256 #endif
257 return 0;