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
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.
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 ***** */
42 // Test cases for the nsiWebNavigation Interface
47 #include "testembed.h"
49 #include "nsiwebnav.h"
50 #include "UrlDialog.h"
55 static char THIS_FILE
[] = __FILE__
;
58 /////////////////////////////////////////////////////////////////////////////
61 CNsIWebNav::CNsIWebNav(nsIWebNavigation
*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
)
97 case ID_INTERFACES_NSIWEBNAV_RUNALLTESTS
:
100 case ID_INTERFACES_NSIWEBNAV_GETCANGOBACK
:
103 case ID_INTERFACES_NSIWEBNAV_GETCANGOFORWARD
:
106 case ID_INTERFACES_NSIWEBNAV_GOBACK
:
109 case ID_INTERFACES_NSIWEBNAV_GOFORWARD
:
112 case ID_INTERFACES_NSIWEBNAV_GOTOINDEX
:
115 case ID_INTERFACES_NSIWEBNAV_LOADURI
:
116 LoadURITest(nsnull
, nsnull
, 2, PR_FALSE
);
118 case ID_INTERFACES_NSIWEBNAV_RELOAD
:
119 ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE
, 2);
121 case ID_INTERFACES_NSIWEBNAV_STOP
:
122 StopURITest("file://C|/Program Files",
123 nsIWebNavigation::STOP_CONTENT
, 2);
125 case ID_INTERFACES_NSIWEBNAV_GETDOCUMENT
:
128 case ID_INTERFACES_NSIWEBNAV_GETCURRENTURI
:
129 GetCurrentURITest(2);
131 case ID_INTERFACES_NSIWEBNAV_GETREFERRINGURI
:
132 GetReferringURITest(2);
134 case ID_INTERFACES_NSIWEBNAV_GETSESSIONHISTORY
:
137 case ID_INTERFACES_NSIWEBNAV_SETSESSIONHISTORY
:
143 void CNsIWebNav::RunAllTests()
148 QAOutput("We have the web nav object.", 1);
150 QAOutput("We don't have the web nav object. No tests performed.", 2);
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
164 // canGoForward attribute test
173 // LoadURI() & reload tests
175 QAOutput("Run a few LoadURI() tests.", 1);
178 LoadUriandReload(11);
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);
190 GetCurrentURITest(1);
191 GetReferringURITest(1);
193 // session history test
198 void CNsIWebNav::LoadUriandReload(int URItotal
)
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
);
213 ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE
, 1);
216 ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE
, 1);
219 ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY
, 1);
221 // simulate shift-reload
223 ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE
|
224 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY
, 1);
227 ReloadTest(nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE
, 1);
230 ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE
, 1);
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
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);
302 case nsIWebNavigation::LOAD_FLAGS_NONE
:
303 strcpy(theFlagName
, "LOAD_FLAGS_NONE");
305 case nsIWebNavigation::LOAD_FLAGS_MASK
:
306 strcpy(theFlagName
, "LOAD_FLAGS_MASK");
308 case nsIWebNavigation::LOAD_FLAGS_IS_REFRESH
:
309 strcpy(theFlagName
, "LOAD_FLAGS_IS_REFRESH");
311 case nsIWebNavigation::LOAD_FLAGS_IS_LINK
:
312 strcpy(theFlagName
, "LOAD_FLAGS_IS_LINK");
314 case nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY
:
315 strcpy(theFlagName
, "LOAD_FLAGS_BYPASS_HISTORY");
317 case nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY
:
318 strcpy(theFlagName
, "LOAD_FLAGS_REPLACE_HISTORY");
320 case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE
:
321 strcpy(theFlagName
, "LOAD_FLAGS_BYPASS_CACHE");
323 case nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY
:
324 strcpy(theFlagName
, "LOAD_FLAGS_BYPASS_PROXY");
326 case nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE
:
327 strcpy(theFlagName
, "LOAD_FLAGS_CHARSET_CHANGE");
331 rv
= qaWebNav
->LoadURI(NS_ConvertASCIItoUTF16(theUrl
).get(),
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];
347 case nsIWebNavigation::LOAD_FLAGS_NONE
:
348 strcpy(theFlagName
, "LOAD_FLAGS_NONE");
350 case nsIWebNavigation::LOAD_FLAGS_MASK
:
351 strcpy(theFlagName
, "LOAD_FLAGS_MASK");
353 case nsIWebNavigation::LOAD_FLAGS_IS_REFRESH
:
354 strcpy(theFlagName
, "LOAD_FLAGS_IS_REFRESH");
356 case nsIWebNavigation::LOAD_FLAGS_IS_LINK
:
357 strcpy(theFlagName
, "LOAD_FLAGS_IS_LINK");
359 case nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY
:
360 strcpy(theFlagName
, "LOAD_FLAGS_BYPASS_HISTORY");
362 case nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY
:
363 strcpy(theFlagName
, "LOAD_FLAGS_REPLACE_HISTORY");
365 case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE
:
366 strcpy(theFlagName
, "LOAD_FLAGS_BYPASS_CACHE");
368 case nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY
:
369 strcpy(theFlagName
, "LOAD_FLAGS_BYPASS_PROXY");
371 case nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE
:
372 strcpy(theFlagName
, "LOAD_FLAGS_CHARSET_CHANGE");
374 case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE
| nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY
:
375 strcpy(theFlagName
, "cache & proxy");
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
,
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");
395 strcpy(flagString
, "STOP_CONTENT");
397 qaWebNav
->LoadURI(NS_ConvertASCIItoUTF16(theUrl
).get(),
398 nsIWebNavigation::LOAD_FLAGS_NONE
,
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
));
415 QAOutput("We didn't get the document. Test failed.", 2);
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
));
431 QAOutput("We didn't get the URI. Test failed.", 2);
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
;
449 if (myDialog
.DoModal() == IDOK
)
451 uriString
= myDialog
.m_urlfield
;
452 rv
= NS_NewURI(getter_AddRefs(theURI
), uriString
);
454 QAOutput("We GOT the URI.", 1);
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);
463 QAOutput("We didn't get the URI. Test failed.", 2);
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);
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
);
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
);