Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / embedding / qa / testembed / nsichanneltests.cpp
blobfdc79e5267b5c312e09b9116d10c6df7a427cb2e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * David Epstein <depstein@netscape.com>
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 // nsIChannelTests.h : implementation file for nsIChannel test cases
41 // nsIChannelTests.cpp : implementation file
44 #include "stdafx.h"
45 #include "testembed.h"
46 #include "nsIChannelTests.h"
47 #include "BrowserImpl.h"
48 #include "BrowserFrm.h"
49 #include "UrlDialog.h"
50 #include "ProfileMgr.h"
51 #include "ProfilesDlg.h"
52 #include "QaUtils.h"
53 #include "Tests.h"
55 #ifdef _DEBUG
56 #define new DEBUG_NEW
57 #undef THIS_FILE
58 static char THIS_FILE[] = __FILE__;
59 #endif
61 /////////////////////////////////////////////////////////////////////////////
62 // nsIChannelTests
64 CnsIChannelTests::CnsIChannelTests(nsIWebBrowser *mWebBrowser,
65 CBrowserImpl *mpBrowserImpl)
67 qaWebBrowser = mWebBrowser;
68 qaBrowserImpl = mpBrowserImpl;
71 CnsIChannelTests::~CnsIChannelTests()
75 ChannelRow ChannelTable[] = {
76 {"http://www.netscape.com/", "text/plain"},
77 {"https://www.sun.com/", "text/html"},
78 {"ftp://ftp.mozilla.org/", "image/gif"},
79 {"gopher://gopher.tc.umn.edu/", "application/vnd.mozilla.xul+xml"},
80 {"http://www.mozilla.org/projects/embedding", "text/plain"},
81 {"file://C|/Program Files", "image/jpeg"},
82 {"about:mozilla", ""},
83 {"javascript:document.write('hi')", ""},
84 {"data:text/plain;charset=iso-8859-7,%be%fg%be", ""},
85 {"jar:resource:///chrome/toolkit.jar!/content/global/plugins.html", ""},
86 };
88 /////////////////////////////////////////////////////////////////////////////
89 // nsIChannelTests message handlers
91 nsIChannel * CnsIChannelTests::GetChannelObject(nsCAutoString theSpec)
93 theURI = GetURIObject(theSpec);
94 if (!theURI)
96 QAOutput("Didn't get URI object. Test failed.", 2);
97 return nsnull;
99 rv = NS_NewChannel(getter_AddRefs(theChannel), theURI, nsnull, nsnull);
100 RvTestResult(rv, "NS_NewChannel", 1);
101 if (!theChannel)
103 QAOutput("Didn't get Channel object. GetChannelObject Test failed.", 2);
104 return nsnull;
106 return theChannel;
109 nsIURI * CnsIChannelTests::GetURIObject(nsCAutoString theSpec)
111 rv = NS_NewURI(getter_AddRefs(theURI), theSpec);
112 RvTestResult(rv, "NS_NewURI", 1);
113 if (!theURI)
115 QAOutput("Didn't get URI object. GetURIObject Test failed.", 2);
116 return nsnull;
118 // nsIURI *retVal = theURI;
119 // NS_ADDREF(retVal);
120 // return retVal;
121 return theURI;
124 void CnsIChannelTests::SetOriginalURITest(nsIChannel *theChannel, nsCAutoString theSpec,
125 PRInt16 displayMode)
127 if (!theChannel)
129 QAOutput("Didn't get Channel object. SetOriginalURITest failed.", 2);
130 return;
132 theURI = GetURIObject(theSpec);
133 rv = theChannel->SetOriginalURI(theURI);
134 RvTestResult(rv, "SetOriginalURITest", displayMode);
135 if (displayMode == 1)
136 RvTestResultDlg(rv, "SetOriginalURITest", true);
137 if (!theURI)
138 QAOutput("Didn't get URI object. SetOriginalURITest failed.", displayMode);
141 void CnsIChannelTests::GetOriginalURITest(nsIChannel *theChannel, PRInt16 displayMode)
143 if (!theChannel)
145 QAOutput("Didn't get Channel object. GetOriginalURITest failed.", 2);
146 return;
148 rv = theChannel->GetOriginalURI(getter_AddRefs(theURI));
149 RvTestResult(rv, "GetOriginalURITest", displayMode);
150 if (displayMode == 1)
151 RvTestResultDlg(rv, "GetOriginalURITest");
152 if (!theURI)
154 QAOutput("Didn't get URI object. GetOriginalURITest failed.", displayMode);
155 return;
157 GetTheURI(theURI);
160 void CnsIChannelTests::GetURITest(nsIChannel *theChannel, PRInt16 displayMode)
162 if (!theChannel)
164 QAOutput("Didn't get Channel object. GetURITest failed.", 2);
165 return;
167 rv = theChannel->GetURI(getter_AddRefs(theURI));
168 RvTestResult(rv, "GetURITest", displayMode);
169 if (displayMode == 1)
170 RvTestResultDlg(rv, "GetURITest");
171 if (!theURI)
173 QAOutput("Didn't get URI object. GetURITest failed.", displayMode);
174 return;
176 GetTheURI(theURI);
179 void CnsIChannelTests::SetOwnerTest(nsIChannel *theChannel, PRInt16 displayMode)
181 if (!theChannel)
183 QAOutput("Didn't get Channel object. SetOwnerTest failed.", 2);
184 return;
186 theSupports = do_QueryInterface(qaWebBrowser);
187 rv = theChannel->SetOwner(theChannel);
188 RvTestResult(rv, "SetOwner", displayMode);
189 if (displayMode == 1)
190 RvTestResultDlg(rv, "SetOwner");
191 if (!theSupports)
192 QAOutput("Didn't get nsISupports object. SetOwnerTest failed.", displayMode);
195 void CnsIChannelTests::GetOwnerTest(nsIChannel *theChannel, PRInt16 displayMode)
197 if (!theChannel)
199 QAOutput("Didn't get Channel object. GetOwnerTest failed.", 2);
200 return;
202 rv = theChannel->GetOwner(getter_AddRefs(theSupports));
203 RvTestResult(rv, "GetOwner", displayMode);
204 if (displayMode == 1)
205 RvTestResultDlg(rv, "GetOwner");
206 if (!theSupports)
207 QAOutput("Didn't get nsISupports object. GetOwnerTest failed.", displayMode);
210 void CnsIChannelTests::SetNotificationsTest(nsIChannel *theChannel, PRInt16 displayMode)
212 if (!theChannel)
214 QAOutput("Didn't get Channel object. SetNotificationsTest failed.", 2);
215 return;
217 if (!qaWebBrowser)
219 QAOutput("Didn't get nsIWebBrowser object. SetNotificationsTest failed.", displayMode);
220 return;
222 theIRequestor = do_QueryInterface(qaWebBrowser);
223 rv = theChannel->SetNotificationCallbacks(theIRequestor);
224 RvTestResult(rv, "SetNotificationCallbacks", displayMode);
225 if (displayMode == 1)
226 RvTestResultDlg(rv, "SetNotificationCallbacks");
227 if (!theIRequestor)
228 QAOutput("Didn't get nsIInterfaceRequestor object. SetNotificationsTest failed.", displayMode);
231 void CnsIChannelTests::GetNotificationsTest(nsIChannel *theChannel, PRInt16 displayMode)
233 if (!theChannel)
235 QAOutput("Didn't get Channel object. GetNotificationsTest failed.", 2);
236 return;
238 rv = theChannel->GetNotificationCallbacks(getter_AddRefs(theIRequestor));
239 RvTestResult(rv, "GetNotificationCallbacks", displayMode);
240 if (displayMode == 1)
241 RvTestResultDlg(rv, "GetNotificationCallbacks");
242 if(!theIRequestor)
243 QAOutput("Didn't get nsIInterfaceRequestor object. GetNotificationsTest failed.", displayMode);
246 void CnsIChannelTests::GetSecurityInfoTest(nsIChannel *theChannel, PRInt16 displayMode)
248 if (!theChannel)
250 QAOutput("Didn't get nsIChannel object. GetSecurityInfoTest failed.", displayMode);
251 return;
253 theSupports = do_QueryInterface(qaWebBrowser);
254 rv = theChannel->GetSecurityInfo(getter_AddRefs(theSupports));
255 RvTestResult(rv, "GetSecurityInfo", displayMode);
256 if (!theSupports)
257 QAOutput("Didn't get nsISupports object for GetSecurityInfoTest.", displayMode);
260 void CnsIChannelTests::SetContentTypeTest(nsIChannel *theChannel, PRInt16 displayMode)
262 if (!theChannel)
264 QAOutput("Didn't get nsIChannel object. SetContentTypeTest failed.", displayMode);
265 return;
267 rv = theChannel->SetContentType(NS_LITERAL_CSTRING("text/plain"));
268 RvTestResult(rv, "SetContentType", displayMode);
271 void CnsIChannelTests::GetContentTypeTest(nsIChannel *theChannel, PRInt16 displayMode)
273 nsCAutoString contentType;
275 if (!theChannel)
277 QAOutput("Didn't get nsIChannel object. GetContentTypeTest failed.", displayMode);
278 return;
280 rv = theChannel->GetContentType(contentType);
281 RvTestResult(rv, "GetContentType", displayMode);
282 FormatAndPrintOutput("the content type = ", contentType, displayMode);
285 void CnsIChannelTests::SetContentCharsetTest(nsIChannel *theChannel, PRInt16 displayMode)
287 nsCAutoString charsetType;
289 if (!theChannel)
291 QAOutput("Didn't get nsIChannel object. SetContentCharsetTest failed.", displayMode);
292 return;
294 rv = theChannel->SetContentCharset(NS_LITERAL_CSTRING("ISO-8859-1"));
295 RvTestResult(rv, "SetContentCharset", displayMode);
298 void CnsIChannelTests::GetContentCharsetTest(nsIChannel *theChannel, PRInt16 displayMode)
300 nsCAutoString charsetType;
302 if (!theChannel)
304 QAOutput("Didn't get nsIChannel object. GetContentCharsetTest failed.", displayMode);
305 return;
307 rv = theChannel->GetContentCharset(charsetType);
308 RvTestResult(rv, "GetContentCharset", displayMode);
309 FormatAndPrintOutput("the charset type = ", charsetType, displayMode);
312 void CnsIChannelTests::SetContentLengthTest(nsIChannel *theChannel, PRInt16 displayMode)
314 PRInt32 contentLength;
316 contentLength = 10000;
318 if (!theChannel)
320 QAOutput("Didn't get nsIChannel object. SetContentLengthTest failed.", displayMode);
321 return;
323 rv = theChannel->SetContentLength(contentLength);
324 RvTestResult(rv, "SetContentLength", displayMode);
327 void CnsIChannelTests::GetContentLengthTest(nsIChannel *theChannel, PRInt16 displayMode)
329 PRInt32 contentLength;
331 if (!theChannel)
333 QAOutput("Didn't get nsIChannel object. GetContentLengthTest failed.", displayMode);
334 return;
336 rv = theChannel->GetContentLength(&contentLength);
337 RvTestResult(rv, "GetContentLength", displayMode);
338 FormatAndPrintOutput("the content length = ", contentLength, displayMode);
341 void CnsIChannelTests::OpenTest(nsIChannel *theChannel, PRInt16 displayMode)
343 if (!theChannel)
345 QAOutput("Didn't get nsIChannel object. OpenTest failed.", displayMode);
346 return;
348 rv = theChannel->Open(getter_AddRefs(theInputStream));
349 RvTestResult(rv, "OpenTest", displayMode);
350 if (!theInputStream)
351 QAOutput("Didn't get theInputStream object. OpenTest failed.", displayMode);
354 void CnsIChannelTests::AsyncOpenTest(nsIChannel *theChannel, PRInt16 displayMode)
356 nsCOMPtr<nsIStreamListener> listener(static_cast<nsIStreamListener*>(qaBrowserImpl));
357 nsCOMPtr<nsIWeakReference> thisListener(do_GetWeakReference(listener));
358 qaWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsIStreamListener));
359 if (!theChannel)
361 QAOutput("Didn't get nsIChannel object. AsyncOpenTest failed.", displayMode);
362 return;
364 if (!listener) {
365 QAOutput("Didn't get the stream listener object. AsyncOpenTest failed.", displayMode);
366 return;
368 // this calls nsIStreamListener::OnDataAvailable()
369 theSupports = do_QueryInterface(theChannel);
370 if (!theSupports)
371 QAOutput("Didn't get the nsISupports object. AsyncOpen() failed.", displayMode);
373 SaveObject(theSupports);
375 rv = theChannel->AsyncOpen(listener, theSupports);
376 RvTestResult(rv, "AsyncOpen()", displayMode);
377 if (displayMode == 1)
378 RvTestResultDlg(rv, "AsyncOpen()");
381 void CnsIChannelTests::PostAsyncOpenTests(nsIChannel *theChannel, PRInt16 displayMode)
383 // These tests are run after the channel is opened (using AsyncOpen())
384 // To run them in TestEmbed, select Tests > asyncOpen menu and enter complete URL with protocol
385 GetOriginalURITest(theChannel, displayMode);
386 GetURITest(theChannel, displayMode);
387 SetOwnerTest(theChannel, displayMode);
388 GetOwnerTest(theChannel, displayMode);
389 SetNotificationsTest(theChannel, displayMode);
390 GetNotificationsTest(theChannel, displayMode);
391 GetSecurityInfoTest(theChannel, displayMode);
392 SetContentTypeTest(theChannel, displayMode);
393 GetContentTypeTest(theChannel, displayMode);
394 SetContentCharsetTest(theChannel, displayMode);
395 GetContentCharsetTest(theChannel, displayMode);
396 SetContentLengthTest(theChannel, displayMode);
397 GetContentLengthTest(theChannel, displayMode);
400 void CnsIChannelTests::OnStartTests(UINT nMenuID)
402 theSpec = "http://www.netscape.com";
403 theChannel = GetChannelObject(theSpec);
404 if (!theChannel)
406 QAOutput("Didn't get nsIChannel object. Test not run.", 1);
407 return;
409 switch(nMenuID)
411 case ID_INTERFACES_NSICHANNEL_RUNALLTESTS :
412 RunAllTests();
413 break ;
414 case ID_INTERFACES_NSICHANNEL_SETORIGINALURI :
415 SetOriginalURITest(theChannel, theSpec, 2);
416 break ;
417 case ID_INTERFACES_NSICHANNEL_GETORIGINALURI :
418 GetOriginalURITest(theChannel, 2);
419 break ;
420 case ID_INTERFACES_NSICHANNEL_GETURI :
421 GetURITest(theChannel, 2);
422 break ;
423 case ID_INTERFACES_NSICHANNEL_SETOWNER :
424 SetOwnerTest(theChannel, 2);
425 break ;
426 case ID_INTERFACES_NSICHANNEL_GETOWNER :
427 GetOwnerTest(theChannel, 2);
428 break ;
429 case ID_INTERFACES_NSICHANNEL_SETNOTIFICATIONS :
430 SetNotificationsTest(theChannel, 2);
431 break ;
432 case ID_INTERFACES_NSICHANNEL_GETNOTIFICATIONS :
433 GetNotificationsTest(theChannel, 2);
434 break ;
435 case ID_INTERFACES_NSICHANNEL_GETSECURITYINFO :
436 AsyncOpenTest(theChannel, 1);
437 GetSecurityInfoTest(theChannel, 2);
438 break ;
439 case ID_INTERFACES_NSICHANNEL_SETCONTENTTYPE :
440 AsyncOpenTest(theChannel, 1);
441 SetContentTypeTest(theChannel, 2);
442 break ;
443 case ID_INTERFACES_NSICHANNEL_GETCONTENTTYPE :
444 AsyncOpenTest(theChannel, 1);
445 GetContentTypeTest(theChannel, 2);
446 break ;
447 case ID_INTERFACES_NSICHANNEL_SETCONTENTCHARSET :
448 AsyncOpenTest(theChannel, 1);
449 SetContentCharsetTest(theChannel, 2);
450 break ;
451 case ID_INTERFACES_NSICHANNEL_GETCONTENTCHARSET :
452 AsyncOpenTest(theChannel, 1);
453 GetContentCharsetTest(theChannel, 2);
454 break ;
455 case ID_INTERFACES_NSICHANNEL_SETCONTENTLENGTH :
456 AsyncOpenTest(theChannel, 1);
457 SetContentLengthTest(theChannel, 2);
458 break ;
459 case ID_INTERFACES_NSICHANNEL_GETCONTENTLENGTH :
460 AsyncOpenTest(theChannel, 1);
461 GetContentLengthTest(theChannel, 2);
462 break ;
463 case ID_INTERFACES_NSICHANNEL_OPEN :
464 OpenTest(theChannel, 2);
465 break ;
466 case ID_INTERFACES_NSICHANNEL_ASYNCOPEN :
467 AsyncOpenTest(theChannel, 2);
468 break ;
472 void CnsIChannelTests::RunAllTests()
474 int i;
476 for (i=0; i<10; i++)
478 theSpec = ChannelTable[i].theURL;
479 theChannel = GetChannelObject(theSpec);
480 if (!theChannel)
482 QAOutput("Didn't get nsIChannel object. RunAllTests not run.", 2);
483 return;
485 QAOutput("\nStart nsIChannel Tests: ");
486 SetOriginalURITest(theChannel, theSpec, 1);
487 GetOriginalURITest(theChannel, 1);
488 GetURITest(theChannel, 1);
489 SetOwnerTest(theChannel, 1);
490 GetOwnerTest(theChannel, 1);
491 SetNotificationsTest(theChannel, 1);
492 GetNotificationsTest(theChannel, 1);
493 AsyncOpenTest(theChannel, 1);
494 // PostAsyncOpenTests() called from nsIRequestObserver::OnStartRequest (in BrowserImpl.cpp)
495 QAOutput("\n");