1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2002
19 * the Initial Developer. All Rights Reserved.
22 * Darin Fisher <darin@netscape.com>
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.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
39 #include "nsIComponentRegistrar.h"
40 #include "nsIStreamTransportService.h"
41 #include "nsIAsyncInputStream.h"
42 #include "nsIProgressEventSink.h"
43 #include "nsIInterfaceRequestor.h"
44 #include "nsIInterfaceRequestorUtils.h"
45 #include "nsIProxyObjectManager.h"
46 #include "nsIRequest.h"
47 #include "nsIServiceManager.h"
48 #include "nsIComponentManager.h"
51 #include "nsStringAPI.h"
52 #include "nsIFileStreams.h"
53 #include "nsIStreamListener.h"
54 #include "nsILocalFile.h"
55 #include "nsNetUtil.h"
56 #include "nsAutoLock.h"
59 ////////////////////////////////////////////////////////////////////////////////
61 #if defined(PR_LOGGING)
63 // set NSPR_LOG_MODULES=Test:5
65 static PRLogModuleInfo
*gTestLog
= nsnull
;
66 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
71 ////////////////////////////////////////////////////////////////////////////////
73 static NS_DEFINE_CID(kSimpleURICID
, NS_SIMPLEURI_CID
);
75 ////////////////////////////////////////////////////////////////////////////////
77 class MyListener
: public nsIStreamListener
83 virtual ~MyListener() {}
85 NS_IMETHOD
OnStartRequest(nsIRequest
*req
, nsISupports
*ctx
)
87 LOG(("MyListener::OnStartRequest\n"));
91 NS_IMETHOD
OnDataAvailable(nsIRequest
*req
, nsISupports
*ctx
,
92 nsIInputStream
*stream
,
93 PRUint32 offset
, PRUint32 count
)
95 LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset
, count
));
101 PRUint32 n
, amt
= PR_MIN(count
, sizeof(buf
));
103 rv
= stream
->Read(buf
, amt
, &n
);
105 LOG((" read returned 0x%08x\n", rv
));
109 LOG((" read %u bytes\n", n
));
116 NS_IMETHOD
OnStopRequest(nsIRequest
*req
, nsISupports
*ctx
, nsresult status
)
118 LOG(("MyListener::OnStopRequest [status=%x]\n", status
));
124 NS_IMPL_ISUPPORTS2(MyListener
,
128 ////////////////////////////////////////////////////////////////////////////////
130 class MyCallbacks
: public nsIInterfaceRequestor
131 , public nsIProgressEventSink
137 virtual ~MyCallbacks() {}
139 NS_IMETHOD
GetInterface(const nsID
&iid
, void **result
)
141 return QueryInterface(iid
, result
);
144 NS_IMETHOD
OnStatus(nsIRequest
*req
, nsISupports
*ctx
, nsresult status
,
145 const PRUnichar
*statusArg
)
147 LOG(("MyCallbacks::OnStatus [status=%x]\n", status
));
151 NS_IMETHOD
OnProgress(nsIRequest
*req
, nsISupports
*ctx
,
152 PRUint64 progress
, PRUint64 progressMax
)
154 LOG(("MyCallbacks::OnProgress [progress=%llu/%llu]\n", progress
, progressMax
));
159 NS_IMPL_ISUPPORTS2(MyCallbacks
,
160 nsIInterfaceRequestor
,
161 nsIProgressEventSink
)
163 ////////////////////////////////////////////////////////////////////////////////
166 * asynchronously copy file.
169 RunTest(nsIFile
*file
)
175 nsCOMPtr
<nsIInputStream
> stream
;
176 rv
= NS_NewLocalFileInputStream(getter_AddRefs(stream
), file
);
177 if (NS_FAILED(rv
)) return rv
;
179 nsCOMPtr
<nsIURI
> uri
= do_CreateInstance(kSimpleURICID
);
181 uri
->SetSpec(NS_LITERAL_CSTRING("foo://bar"));
183 nsCOMPtr
<nsIChannel
> chan
;
184 rv
= NS_NewInputStreamChannel(getter_AddRefs(chan
), uri
, stream
);
185 if (NS_FAILED(rv
)) return rv
;
187 rv
= chan
->SetNotificationCallbacks(new MyCallbacks());
188 if (NS_FAILED(rv
)) return rv
;
190 rv
= chan
->AsyncOpen(new MyListener(), nsnull
);
191 if (NS_FAILED(rv
)) return rv
;
197 ////////////////////////////////////////////////////////////////////////////////
200 main(int argc
, char* argv
[])
202 if (test_common_init(&argc
, &argv
) != 0)
208 printf("usage: %s <file-to-read>\n", argv
[0]);
211 char* fileName
= argv
[1];
213 nsCOMPtr
<nsIServiceManager
> servMan
;
214 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
215 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
216 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
218 registrar
->AutoRegister(nsnull
);
220 #if defined(PR_LOGGING)
221 gTestLog
= PR_NewLogModule("Test");
224 nsCOMPtr
<nsILocalFile
> file
;
225 rv
= NS_NewNativeLocalFile(nsDependentCString(fileName
), PR_FALSE
, getter_AddRefs(file
));
226 if (NS_FAILED(rv
)) return rv
;
229 NS_ASSERTION(NS_SUCCEEDED(rv
), "RunTest failed");
231 // give background threads a chance to finish whatever work they may
233 PR_Sleep(PR_SecondsToInterval(1));
234 } // this scopes the nsCOMPtrs
235 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
236 rv
= NS_ShutdownXPCOM(nsnull
);
237 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");