Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / embedding / qa / testembed / nsIWebNav.cpp
blob39becef58740ebd8654c45f5aff7655e24c044cf
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>
24 * Ashish Bhatt <ashishbhatt@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 // File Overview....
42 // Test cases for the nsiWebNavigation Interface
46 #include "stdafx.h"
47 #include "testembed.h"
48 #include "qautils.h"
49 #include "nsiwebnav.h"
50 #include "UrlDialog.h"
52 #ifdef _DEBUG
53 #define new DEBUG_NEW
54 #undef THIS_FILE
55 static char THIS_FILE[] = __FILE__;
56 #endif
58 /////////////////////////////////////////////////////////////////////////////
59 // CNsIWebNav
61 CNsIWebNav::CNsIWebNav(nsIWebNavigation *mWebNav)
63 qaWebNav = mWebNav ;
66 CNsIWebNav::~CNsIWebNav()
71 /////////////////////////////////////////////////////////////////////////////
72 // CNsIWebNav message handlers
73 // ***********************************************************************
74 // nsIWebNavigation iface
75 // ***********************************************************************
77 // Url table for web navigation
78 NavElement UrlTable[] = {
79 {"http://www.intel.com/", nsIWebNavigation::LOAD_FLAGS_NONE},
80 {"http://www.yahoo.com/", nsIWebNavigation::LOAD_FLAGS_MASK},
81 {"http://www.oracle.com/", nsIWebNavigation::LOAD_FLAGS_IS_LINK},
82 {"http://www.sun.com/", nsIWebNavigation::LOAD_FLAGS_IS_REFRESH},
83 {"ftp://ftp.netscape.com", nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY},
84 {"ftp://ftp.mozilla.org/", nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY},
85 {"https://www.motorola.com/", nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE},
86 {"https://www.amazon.com", nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY},
87 {"about:plugins", nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE},
88 {"javascript: document.write('Test!')", nsIWebNavigation::LOAD_FLAGS_NONE},
89 {"file://C|/Program Files", nsIWebNavigation::LOAD_FLAGS_NONE}
93 void CNsIWebNav::OnStartTests(UINT nMenuID)
95 switch(nMenuID)
97 case ID_INTERFACES_NSIWEBNAV_RUNALLTESTS :
98 RunAllTests();
99 break ;
100 case ID_INTERFACES_NSIWEBNAV_GETCANGOBACK :
101 CanGoBackTest(2);
102 break ;
103 case ID_INTERFACES_NSIWEBNAV_GETCANGOFORWARD :
104 CanGoForwardTest(2);
105 break ;
106 case ID_INTERFACES_NSIWEBNAV_GOBACK :
107 GoBackTest(2);
108 break ;
109 case ID_INTERFACES_NSIWEBNAV_GOFORWARD :
110 GoForwardTest(2);
111 break ;
112 case ID_INTERFACES_NSIWEBNAV_GOTOINDEX :
113 GoToIndexTest(2);
114 break ;
115 case ID_INTERFACES_NSIWEBNAV_LOADURI :
116 LoadURITest(nsnull, nsnull, 2, PR_FALSE);
117 break ;
118 case ID_INTERFACES_NSIWEBNAV_RELOAD :
119 ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE, 2);
120 break ;
121 case ID_INTERFACES_NSIWEBNAV_STOP :
122 StopURITest("file://C|/Program Files",
123 nsIWebNavigation::STOP_CONTENT, 2);
124 break ;
125 case ID_INTERFACES_NSIWEBNAV_GETDOCUMENT :
126 GetDocumentTest(2);
127 break ;
128 case ID_INTERFACES_NSIWEBNAV_GETCURRENTURI :
129 GetCurrentURITest(2);
130 break ;
131 case ID_INTERFACES_NSIWEBNAV_GETREFERRINGURI:
132 GetReferringURITest(2);
133 break;
134 case ID_INTERFACES_NSIWEBNAV_GETSESSIONHISTORY :
135 GetSHTest(2);
136 break ;
137 case ID_INTERFACES_NSIWEBNAV_SETSESSIONHISTORY :
138 SetSHTest(2);
139 break ;
143 void CNsIWebNav::RunAllTests()
145 int i=0;
147 if (qaWebNav)
148 QAOutput("We have the web nav object.", 1);
149 else {
150 QAOutput("We don't have the web nav object. No tests performed.", 2);
151 return;
154 // load a couple of URLs to get things going
155 LoadURITest("http://www.cisco.com", nsIWebNavigation::LOAD_FLAGS_NONE, 2, PR_TRUE);
156 LoadURITest("www.google.com", nsIWebNavigation::LOAD_FLAGS_NONE, 2, PR_TRUE);
158 // canGoBack attribute test
159 CanGoBackTest(1);
161 // GoBack test
162 GoBackTest(2);
164 // canGoForward attribute test
165 CanGoForwardTest(1);
167 // GoForward test
168 GoForwardTest(2);
170 // GotoIndex test
171 GoToIndexTest(2);
173 // LoadURI() & reload tests
175 QAOutput("Run a few LoadURI() tests.", 1);
178 LoadUriandReload(11);
181 // Stop() tests
182 StopURITest("http://www.microsoft.com", nsIWebNavigation::STOP_ALL, 1);
183 StopURITest("https://www.microsoft.com/", nsIWebNavigation::STOP_NETWORK, 1);
184 StopURITest("ftp://ftp.microsoft.com/", nsIWebNavigation::STOP_CONTENT, 1);
186 // document test
187 GetDocumentTest(1);
189 // uri test
190 GetCurrentURITest(1);
191 GetReferringURITest(1);
193 // session history test
194 SetSHTest(1);
195 GetSHTest(1);
198 void CNsIWebNav::LoadUriandReload(int URItotal)
200 int i=0, j=0;
201 // LoadURI() & reload tests
203 QAOutput("Run a few LoadURI() and Reload() tests.", 1);
205 for (j=0; j < 9; j++)
207 for (i=0; i < URItotal; i++)
209 LoadURITest(UrlTable[i].theURI, UrlTable[j].theFlag, 2, PR_TRUE);
210 switch (i)
212 case 0:
213 ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE, 1);
214 break;
215 case 1:
216 ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE, 1);
217 break;
218 case 2:
219 ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY, 1);
220 break;
221 // simulate shift-reload
222 case 3:
223 ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE |
224 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY, 1);
225 break;
226 case 4:
227 ReloadTest(nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE, 1);
228 break;
229 case 5:
230 ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE, 1);
231 break;
237 // ***********************************************************************
238 // Individual nsIWebNavigation tests
241 void CNsIWebNav::CanGoBackTest(PRInt16 displayMode)
243 PRBool canGoBack = PR_FALSE;
244 rv = qaWebNav->GetCanGoBack(&canGoBack);
245 RvTestResult(rv, "GetCanGoBack() attribute test", displayMode);
246 FormatAndPrintOutput("canGoBack value = ", canGoBack, displayMode);
249 void CNsIWebNav::GoBackTest(PRInt16 displayMode)
251 rv = qaWebNav->GoBack();
252 RvTestResult(rv, "GoBack() test", displayMode);
255 void CNsIWebNav::CanGoForwardTest(PRInt16 displayMode)
257 PRBool canGoForward = PR_FALSE;
258 rv = qaWebNav->GetCanGoForward(&canGoForward);
259 RvTestResult(rv, "GetCanGoForward() attribute test", displayMode);
260 FormatAndPrintOutput("canGoForward value = ", canGoForward, displayMode);
263 void CNsIWebNav::GoForwardTest(PRInt16 displayMode)
265 rv = qaWebNav->GoForward();
266 RvTestResult(rv, "GoForward() test", displayMode);
269 void CNsIWebNav::GoToIndexTest(PRInt16 displayMode)
271 PRInt32 theIndex = 0;
273 rv = qaWebNav->GotoIndex(theIndex);
274 RvTestResult(rv, "GotoIndex() test", displayMode);
277 void CNsIWebNav::LoadURITest(char *theUrl, PRUint32 theFlag,
278 PRInt16 displayMode, PRBool runAllTests)
280 char theTotalString[500];
281 char theFlagName[200];
283 if (runAllTests == PR_FALSE) // load just one url from Url dialog
285 CUrlDialog myDialog;
286 if (myDialog.DoModal() == IDOK)
288 QAOutput("Begin Change URL test.", 1);
289 rv = qaWebNav->LoadURI(NS_ConvertASCIItoUTF16(myDialog.m_urlfield).get(),
290 myDialog.m_flagvalue, nsnull,nsnull, nsnull);
292 RvTestResult(rv, "rv LoadURI() test", 1);
293 FormatAndPrintOutput("The url = ", myDialog.m_urlfield, displayMode);
294 FormatAndPrintOutput("The flag = ", myDialog.m_flagvalue, displayMode);
295 QAOutput("End Change URL test.", 1);
297 return;
300 switch(theFlag)
302 case nsIWebNavigation::LOAD_FLAGS_NONE:
303 strcpy(theFlagName, "LOAD_FLAGS_NONE");
304 break;
305 case nsIWebNavigation::LOAD_FLAGS_MASK:
306 strcpy(theFlagName, "LOAD_FLAGS_MASK");
307 break;
308 case nsIWebNavigation::LOAD_FLAGS_IS_REFRESH:
309 strcpy(theFlagName, "LOAD_FLAGS_IS_REFRESH");
310 break;
311 case nsIWebNavigation::LOAD_FLAGS_IS_LINK:
312 strcpy(theFlagName, "LOAD_FLAGS_IS_LINK");
313 break;
314 case nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY:
315 strcpy(theFlagName, "LOAD_FLAGS_BYPASS_HISTORY");
316 break;
317 case nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY:
318 strcpy(theFlagName, "LOAD_FLAGS_REPLACE_HISTORY");
319 break;
320 case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE:
321 strcpy(theFlagName, "LOAD_FLAGS_BYPASS_CACHE");
322 break;
323 case nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY:
324 strcpy(theFlagName, "LOAD_FLAGS_BYPASS_PROXY");
325 break;
326 case nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE:
327 strcpy(theFlagName, "LOAD_FLAGS_CHARSET_CHANGE");
328 break;
331 rv = qaWebNav->LoadURI(NS_ConvertASCIItoUTF16(theUrl).get(),
332 theFlag,
333 nsnull,
334 nsnull,
335 nsnull);
336 sprintf(theTotalString, "%s%s%s%s%s", "LoadURI(): ", theUrl, " w/ ", theFlagName, " test");
337 RvTestResult(rv, theTotalString, displayMode);
340 void CNsIWebNav::ReloadTest(PRUint32 theFlag, PRInt16 displayMode)
342 char theTotalString[500];
343 char theFlagName[200];
345 switch(theFlag)
347 case nsIWebNavigation::LOAD_FLAGS_NONE:
348 strcpy(theFlagName, "LOAD_FLAGS_NONE");
349 break;
350 case nsIWebNavigation::LOAD_FLAGS_MASK:
351 strcpy(theFlagName, "LOAD_FLAGS_MASK");
352 break;
353 case nsIWebNavigation::LOAD_FLAGS_IS_REFRESH:
354 strcpy(theFlagName, "LOAD_FLAGS_IS_REFRESH");
355 break;
356 case nsIWebNavigation::LOAD_FLAGS_IS_LINK:
357 strcpy(theFlagName, "LOAD_FLAGS_IS_LINK");
358 break;
359 case nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY:
360 strcpy(theFlagName, "LOAD_FLAGS_BYPASS_HISTORY");
361 break;
362 case nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY:
363 strcpy(theFlagName, "LOAD_FLAGS_REPLACE_HISTORY");
364 break;
365 case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE:
366 strcpy(theFlagName, "LOAD_FLAGS_BYPASS_CACHE");
367 break;
368 case nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY:
369 strcpy(theFlagName, "LOAD_FLAGS_BYPASS_PROXY");
370 break;
371 case nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE:
372 strcpy(theFlagName, "LOAD_FLAGS_CHARSET_CHANGE");
373 break;
374 case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY:
375 strcpy(theFlagName, "cache & proxy");
376 break;
379 rv = qaWebNav->Reload(theFlag);
380 sprintf(theTotalString, "%s%s%s%s", "Reload(): ", " w/ ", theFlagName, " test");
381 RvTestResult(rv, theTotalString, displayMode);
384 void CNsIWebNav::StopURITest(char *theUrl, PRUint32 theFlag,
385 PRInt16 displayMode)
387 char theTotalString[200];
388 char flagString[100];
390 if (theFlag == nsIWebNavigation::STOP_ALL)
391 strcpy(flagString, "STOP_ALL");
392 else if (theFlag == nsIWebNavigation::STOP_NETWORK)
393 strcpy(flagString, "STOP_NETWORK");
394 else
395 strcpy(flagString, "STOP_CONTENT");
397 qaWebNav->LoadURI(NS_ConvertASCIItoUTF16(theUrl).get(),
398 nsIWebNavigation::LOAD_FLAGS_NONE,
399 nsnull,
400 nsnull,
401 nsnull);
403 rv = qaWebNav->Stop(theFlag);
404 sprintf(theTotalString, "%s%s%s%s", "Stop(): ", theUrl, " test: ", flagString);
405 RvTestResult(rv, theTotalString, displayMode);
408 void CNsIWebNav::GetDocumentTest(PRInt16 displayMode)
410 nsCOMPtr<nsIDOMDocument> theDocument;
411 nsCOMPtr<nsIDOMDocumentType> theDocType;
413 rv = qaWebNav->GetDocument(getter_AddRefs(theDocument));
414 if (!theDocument) {
415 QAOutput("We didn't get the document. Test failed.", 2);
416 return;
418 else
419 RvTestResult(rv, "GetDocument() test", displayMode);
421 rv = theDocument->GetDoctype(getter_AddRefs(theDocType));
422 RvTestResult(rv, "nsIDOMDocument::GetDoctype() for nsIWebNav test", displayMode);
425 void CNsIWebNav::GetCurrentURITest(PRInt16 displayMode)
427 nsCOMPtr<nsIURI> theURI;
429 rv = qaWebNav->GetCurrentURI(getter_AddRefs(theURI));
430 if (!theURI) {
431 QAOutput("We didn't get the URI. Test failed.", 2);
432 return;
434 else
435 RvTestResult(rv, "GetCurrentURI() test", displayMode);
437 nsCAutoString uriString;
438 rv = theURI->GetSpec(uriString);
439 RvTestResult(rv, "nsIURI::GetSpec() for nsIWebNav test", 1);
441 FormatAndPrintOutput("the nsIWebNav uri = ", uriString, displayMode);
444 void CNsIWebNav::GetReferringURITest(PRInt16 displayMode)
446 nsCOMPtr<nsIURI> theURI;
447 nsCAutoString uriString;
448 CUrlDialog myDialog;
449 if (myDialog.DoModal() == IDOK)
451 uriString = myDialog.m_urlfield;
452 rv = NS_NewURI(getter_AddRefs(theURI), uriString);
453 if (theURI)
454 QAOutput("We GOT the URI.", 1);
455 else
456 QAOutput("We DIDN'T GET the URI.", 1);
457 rv = qaWebNav->GetReferringURI(getter_AddRefs(theURI));
458 RvTestResult(rv, "GetReferringURI() test", displayMode);
459 // rv = qaWebNav->LoadURI(NS_ConvertASCIItoUTF16(myDialog.m_urlfield).get(),
460 // myDialog.m_flagvalue, theURI, nsnull, nsnull);
462 if (!theURI) {
463 QAOutput("We didn't get the URI. Test failed.", 2);
464 return;
467 rv = theURI->GetSpec(uriString);
468 RvTestResult(rv, "nsIURI::GetSpec() for nsIWebNav test", 1);
470 FormatAndPrintOutput("the nsIWebNav uri = ", uriString, displayMode);
473 void CNsIWebNav::GetSHTest(PRInt16 displayMode)
475 PRInt32 numOfElements;
477 nsCOMPtr<nsISHistory> theSessionHistory;
478 rv = qaWebNav->GetSessionHistory(getter_AddRefs(theSessionHistory));
479 if (!theSessionHistory) {
480 QAOutput("We didn't get the session history. Test failed.", 2);
481 return;
483 else
484 RvTestResult(rv, "GetSessionHistory() test", displayMode);
486 rv = theSessionHistory->GetCount(&numOfElements);
487 RvTestResult(rv, "nsISHistory::GetCount() for nsIWebNav test", 1);
489 FormatAndPrintOutput("the sHist entry count = ", numOfElements, displayMode);
492 void CNsIWebNav::SetSHTest(PRInt16 displayMode)
494 nsCOMPtr<nsISHistory> theSessionHistory, tempSHObject;
495 // we want to save the existing session history
496 rv = qaWebNav->GetSessionHistory(getter_AddRefs(theSessionHistory));
497 // this will create the test session history object
498 tempSHObject = do_CreateInstance(NS_SHISTORY_CONTRACTID);
499 rv = qaWebNav->SetSessionHistory(tempSHObject);
500 RvTestResult(rv, "SetSessionHistory() test", displayMode);
501 if (!tempSHObject)
502 QAOutput("We didn't get the session history test object. Test failed.", 2);
503 // we now reset the previous session history
504 rv = qaWebNav->SetSessionHistory(theSessionHistory);