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 nsiHistory Interface
45 #include "TestEmbed.h"
46 #include "BrowserImpl.h"
47 #include "BrowserFrm.h"
48 #include "UrlDialog.h"
49 #include "ProfileMgr.h"
50 #include "ProfilesDlg.h"
52 #include "nsihistory.h"
56 /////////////////////////////////////////////////////////////////////////////
59 CNsIHistory::CNsIHistory(nsIWebNavigation
*mWebNav
)
66 CNsIHistory::~CNsIHistory()
72 /////////////////////////////////////////////////////////////////////////////
73 // CNsIHistory message handlers
74 // ***********************************************************************
75 // ***********************************************************************
78 // ***********************************************************************
79 // ***********************************************************************
80 // nsISHistory & nsIHistoryEntry ifaces:
82 void CNsIHistory::OnStartTests(UINT nMenuID
)
85 // Calls all or indivdual test cases on the basis of the
86 // option selected from menu.
87 PRInt32 numEntries
= 5;
89 PRInt32 theMaxLength
= 100;
93 nsCOMPtr
<nsISHistory
> theSessionHistory
;
94 nsCOMPtr
<nsIHistoryEntry
> theHistoryEntry
;
95 nsCOMPtr
<nsISimpleEnumerator
> theSimpleEnum
;
98 //nsCOMPtr<nsIURI> theURI;
100 // NS_HISTORYENTRY_CONTRACTID
102 // get Session History through web nav iface
104 rv
= qaWebNav
->GetSessionHistory(getter_AddRefs(theSessionHistory
));
105 RvTestResult(rv
, "GetSessionHistory() object", 1);
106 RvTestResultDlg(rv
, "GetSessionHistory() object", true);
109 if (!theSessionHistory
)
111 QAOutput("theSessionHistory object wasn't created. No session history tests performed.", 2);
115 QAOutput("theSessionHistory object was created.", 1);
116 theSessionHistory
->GetCount(&numEntries
);
119 theSessionHistory
->GetEntryAtIndex(0, PR_FALSE
, getter_AddRefs(theHistoryEntry
));
120 if (!theHistoryEntry
)
121 QAOutput("We didn't get the History Entry object.", 1);
123 QAOutput("We have the History Entry object!", 1);
129 case ID_INTERFACES_NSISHISTORY_RUNALLTESTS
:
130 QAOutput("Begin nsISHistory tests.", 1);
132 QAOutput("End nsISHistory tests.", 1);
134 case ID_INTERFACES_NSISHISTORY_GETCOUNT
:
135 GetCountTest(theSessionHistory
, &numEntries
, 2);
137 case ID_INTERFACES_NSISHISTORY_GETINDEX
:
138 GetIndexTest(theSessionHistory
, &theIndex
, 2);
140 case ID_INTERFACES_NSISHISTORY_GETMAXLENGTH
:
141 GetMaxLengthTest(theSessionHistory
, &theMaxLength
, 2);
143 case ID_INTERFACES_NSISHISTORY_SETMAXLENGTH
:
144 SetMaxLengthTest(theSessionHistory
, theMaxLength
, 2);
146 case ID_INTERFACES_NSISHISTORY_GETENTRYATINDEX
:
147 //GetEntryAtIndex(0, PR_FALSE, getter_AddRefs(theHistoryEntry));
148 for (theIndex
= 0; theIndex
< numEntries
; theIndex
++)
150 FormatAndPrintOutput("the index = ", theIndex
, 1);
152 rv
= theSessionHistory
->GetEntryAtIndex(theIndex
, PR_FALSE
, getter_AddRefs(theHistoryEntry
));
153 RvTestResult(rv
, "GetEntryAtIndex() test", 1);
154 RvTestResultDlg(rv
, "GetEntryAtIndex() test");
155 if (!theHistoryEntry
)
157 QAOutput("We didn't get the History Entry object. No more tests performed.", 1);
160 GetURIHistTest(theHistoryEntry
, 2);
161 GetTitleHistTest(theHistoryEntry
, 2);
162 GetIsSubFrameTest(theHistoryEntry
, 2);
166 case ID_INTERFACES_NSISHISTORY_PURGEHISTORY
:
167 PurgeHistoryTest(theSessionHistory
, numEntries
, 2);
169 case ID_INTERFACES_NSISHISTORY_GETSHISTORYENUMERATOR
:
170 //GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum));
172 rv
= theSessionHistory
->GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum
));
173 RvTestResult(rv
, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test", 1);
174 RvTestResultDlg(rv
, "GetSHistoryEnumerator() test");
176 if (!theSimpleEnum
) {
177 QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
180 SimpleEnumTest(theSimpleEnum
, 2);
183 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_RUNALLTESTS
:
184 RunAllHistoryEntryTests(2);
186 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETURI
:
187 GetURIHistTest(theHistoryEntry
, 2);
189 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETTITLE
:
190 GetTitleHistTest(theHistoryEntry
, 2);
192 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETISSUBFRAME
:
193 GetIsSubFrameTest(theHistoryEntry
, 2);
199 void CNsIHistory::RunAllTests()
203 PRInt32 numEntries
= 5;
205 PRInt32 theMaxLength
= 100;
209 nsCOMPtr
<nsISHistory
> theSessionHistory
;
210 nsCOMPtr
<nsIHistoryEntry
> theHistoryEntry
;
212 // get Session History through web nav iface
214 qaWebNav
->GetSessionHistory( getter_AddRefs(theSessionHistory
));
216 if (!theSessionHistory
)
218 QAOutput("theSessionHistory object wasn't created. No session history tests performed.", 2);
222 QAOutput("theSessionHistory object was created.", 1);
223 theSessionHistory
->GetCount(&numEntries
);
226 // test count attribute in nsISHistory.idl
227 GetCountTest(theSessionHistory
, &numEntries
, 1);
229 // test index attribute in nsISHistory.idl
230 GetIndexTest(theSessionHistory
, &theIndex
, 1);
232 // test maxLength attribute in nsISHistory.idl
233 SetMaxLengthTest(theSessionHistory
, theMaxLength
, 1);
234 GetMaxLengthTest(theSessionHistory
, &theMaxLength
, 1);
236 QAOutput("Start nsiHistoryEntry tests.", 1);
238 // get theHistoryEntry object
239 rv
= theSessionHistory
->GetEntryAtIndex(0, PR_FALSE
, getter_AddRefs(theHistoryEntry
));
240 RvTestResult(rv
, "GetEntryAtIndex() test", 1);
241 RvTestResultDlg(rv
, "GetEntryAtIndex() test");
243 if (!theHistoryEntry
)
244 QAOutput("We didn't get the History Entry object.", 1);
247 QAOutput("We have the History Entry object!", 1);
249 // getEntryAtIndex() tests
250 if (theMaxLength
< numEntries
)
252 QAOutput("Setting number of entries to maximum length!", 1);
253 numEntries
= theMaxLength
;
256 for (theIndex
= 0; theIndex
< numEntries
; theIndex
++)
258 FormatAndPrintOutput("the index = ", theIndex
, 1);
260 //GetEntryAtIndexTest(theSessionHistory,theHistoryEntry, theIndex);
262 theSessionHistory
->GetEntryAtIndex(theIndex
, PR_FALSE
, getter_AddRefs(theHistoryEntry
));
263 if (!theHistoryEntry
)
265 QAOutput("We didn't get the History Entry object. No more tests performed.", 1);
268 // nsiHistoryEntry.idl tests
270 // test URI attribute in nsIHistoryEntry.idl
271 GetURIHistTest(theHistoryEntry
, 1);
273 // test title attribute in nsIHistoryEntry.idl
274 GetTitleHistTest(theHistoryEntry
, 1);
276 // test isSubFrame attribute in nsIHistoryEntry.idl
277 GetIsSubFrameTest(theHistoryEntry
, 1);
283 // test SHistoryEnumerator attribute in nsISHistory.idl
284 nsCOMPtr
<nsISimpleEnumerator
> theSimpleEnum
;
286 // GetSHEnumTest(theSessionHistory, theSimpleEnum);
288 rv
= theSessionHistory
->GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum
));
289 RvTestResult(rv
, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test", 1);
290 RvTestResultDlg(rv
, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test");
293 QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
295 SimpleEnumTest(theSimpleEnum
, 1);
297 // PurgeHistory() test
299 PurgeHistoryTest(theSessionHistory
, numEntries
, 1);
302 // ***********************************************************************
303 // Individual nsISHistory tests
305 void CNsIHistory::GetCountTest(nsISHistory
*theSessionHistory
, PRInt32
*numEntries
,
308 rv
= theSessionHistory
->GetCount(numEntries
);
310 QAOutput("numEntries for GetCount() < 0. Test failed.", 1);
312 FormatAndPrintOutput("GetCount():number of entries = ", *numEntries
, displayMode
);
313 RvTestResult(rv
, "GetCount() (count attribute) test", 1);
314 RvTestResultDlg(rv
, "GetCount() (count attribute) test");
318 void CNsIHistory::GetIndexTest(nsISHistory
*theSessionHistory
, PRInt32
*theIndex
,
321 rv
= theSessionHistory
->GetIndex(theIndex
);
323 QAOutput("theIndex for GetIndex() < 0. Test failed.", 1);
325 FormatAndPrintOutput("GetIndex():the index = ", *theIndex
, displayMode
);
326 RvTestResult(rv
, "GetIndex() (index attribute) test", 1);
327 RvTestResultDlg(rv
, "GetIndex() (index attribute) test");
331 void CNsIHistory::SetMaxLengthTest(nsISHistory
*theSessionHistory
, PRInt32 theMaxLength
,
334 rv
= theSessionHistory
->SetMaxLength(theMaxLength
);
335 if (theMaxLength
< 0)
336 QAOutput("theMaxLength for SetMaxLength() < 0. Test failed.", 1);
338 RvTestResult(rv
, "SetMaxLength() (MaxLength attribute) test", displayMode
);
339 RvTestResultDlg(rv
, "SetMaxLength() (MaxLength attribute) test");
343 void CNsIHistory::GetMaxLengthTest(nsISHistory
*theSessionHistory
, PRInt32
*theMaxLength
,
346 rv
= theSessionHistory
->GetMaxLength(theMaxLength
);
348 QAOutput("theMaxLength for GetMaxLength() < 0. Test failed.", 1);
350 FormatAndPrintOutput("GetMaxLength():theMaxLength = ", *theMaxLength
, displayMode
);
351 RvTestResult(rv
, "GetMaxLength() (MaxLength attribute) test", 1);
352 RvTestResultDlg(rv
, "GetMaxLength() (MaxLength attribute) test");
357 /*void CNsIHistory::GetEntryAtIndexTest(nsISHistory *theSessionHistory,
358 nsIHistoryEntry *theHistoryEntry,
361 theSessionHistory->GetEntryAtIndex(theIndex, PR_FALSE,&theHistoryEntry);
363 // test URI attribute in nsIHistoryEntry.idl
364 //GetURIHistTest(theHistoryEntry);
366 // test title attribute in nsIHistoryEntry.idl
367 //GetTitleHistTest(theHistoryEntry);
369 // test isSubFrame attribute in nsIHistoryEntry.idl
370 //GetIsSubFrameTest(theHistoryEntry);
375 void CNsIHistory::GetURIHistTest(nsIHistoryEntry
* theHistoryEntry
,
378 rv
= theHistoryEntry
->GetURI(getter_AddRefs(theURI
));
379 RvTestResult(rv
, "GetURI() (URI attribute) test", 1);
380 RvTestResultDlg(rv
, "GetURI() (URI attribute) test");
382 QAOutput("theURI for GetURI() invalid. Test failed.", 1);
385 nsCAutoString uriString
;
386 rv
= theURI
->GetSpec(uriString
);
388 QAOutput("We didn't get the uriString.", 1);
390 FormatAndPrintOutput("The SH Url = ", uriString
, displayMode
);
394 void CNsIHistory::GetTitleHistTest(nsIHistoryEntry
* theHistoryEntry
,
397 nsXPIDLString theTitle
;
399 rv
= theHistoryEntry
->GetTitle(getter_Copies(theTitle
));
400 RvTestResult(rv
, "GetTitle() (title attribute) test", 1);
401 RvTestResultDlg(rv
, "GetTitle() (title attribute) test");
403 QAOutput("theTitle for GetTitle() is blank. Test failed.", 1);
407 FormatAndPrintOutput("The title = ",
408 NS_ConvertUTF16toUTF8(theTitle
).get(), displayMode
);
411 void CNsIHistory::GetIsSubFrameTest(nsIHistoryEntry
* theHistoryEntry
,
416 rv
= theHistoryEntry
->GetIsSubFrame(&isSubFrame
);
418 RvTestResult(rv
, "GetIsSubFrame() (isSubFrame attribute) test", 1);
419 RvTestResultDlg(rv
, "GetIsSubFrame() (isSubFrame attribute) test");
420 FormatAndPrintOutput("The subFrame boolean value = ", isSubFrame
, displayMode
);
424 void CNsIHistory::GetSHEnumTest(nsISHistory *theSessionHistory,
425 nsISimpleEnumerator *theSimpleEnum)
427 rv = theSessionHistory->GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum));
429 QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
431 RvTestResult(rv, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test", 2);
435 void CNsIHistory::SimpleEnumTest(nsISimpleEnumerator
*theSimpleEnum
,
438 PRBool bMore
= PR_FALSE
;
439 nsCOMPtr
<nsISupports
> nextObj
;
440 nsCOMPtr
<nsIHistoryEntry
> nextHistoryEntry
;
442 while (NS_SUCCEEDED(theSimpleEnum
->HasMoreElements(&bMore
)) && bMore
)
444 theSimpleEnum
->GetNext(getter_AddRefs(nextObj
));
447 nextHistoryEntry
= do_QueryInterface(nextObj
);
448 if (!nextHistoryEntry
)
450 rv
= nextHistoryEntry
->GetURI(getter_AddRefs(theURI
));
451 RvTestResult(rv
, "theSimpleEnum nsIHistoryEntry->GetURI() test", 1);
452 RvTestResultDlg(rv
, "theSimpleEnum nsIHistoryEntry->GetURI() test");
453 nsCAutoString uriString
;
454 rv
= theURI
->GetSpec(uriString
);
456 QAOutput("uriString for GetSpec() invalid. Test failed.", 1);
458 FormatAndPrintOutput("The SimpleEnum URL = ", uriString
, displayMode
);
462 void CNsIHistory::PurgeHistoryTest(nsISHistory
* theSessionHistory
, PRInt32 numEntries
,
465 rv
= theSessionHistory
->PurgeHistory(numEntries
);
466 RvTestResult(rv
, "PurgeHistory() test", 1);
467 RvTestResultDlg(rv
, "PurgeHistory() test");
468 FormatAndPrintOutput("PurgeHistory(): num requested entries for removal = ", numEntries
, 1);
471 void CNsIHistory::RunAllHistoryEntryTests(PRInt16 displayMode
)
473 PRInt32 numEntries
= 5;
475 PRInt32 theMaxLength
= 100;
479 nsCOMPtr
<nsISHistory
> theSessionHistory
;
480 nsCOMPtr
<nsIHistoryEntry
> theHistoryEntry
;
482 // get Session History through web nav iface
484 qaWebNav
->GetSessionHistory( getter_AddRefs(theSessionHistory
));
486 if (!theSessionHistory
)
488 QAOutput("theSessionHistory object wasn't created. No session history tests performed.", displayMode
);
492 QAOutput("theSessionHistory object was created.", 1);
494 theSessionHistory
->GetEntryAtIndex(0, PR_FALSE
, getter_AddRefs(theHistoryEntry
));
495 if (!theHistoryEntry
)
496 QAOutput("We didn't get the History Entry object.", 1);
499 QAOutput("We have the History Entry object!", 1);
501 // getEntryAtIndex() tests
502 if (theMaxLength
< numEntries
)
504 QAOutput("Setting number of entries to maximum length!", 1);
505 numEntries
= theMaxLength
;
508 for (theIndex
= 0; theIndex
< numEntries
; theIndex
++)
510 FormatAndPrintOutput("the index = ", theIndex
, displayMode
);
512 //GetEntryAtIndexTest(theSessionHistory,theHistoryEntry, theIndex);
514 theSessionHistory
->GetEntryAtIndex(theIndex
, PR_FALSE
, getter_AddRefs(theHistoryEntry
));
515 if (!theHistoryEntry
)
517 QAOutput("We didn't get the History Entry object. No more tests performed.", 1);
520 // nsiHistoryEntry.idl tests
522 // test URI attribute in nsIHistoryEntry.idl
523 GetURIHistTest(theHistoryEntry
, displayMode
);
525 // test title attribute in nsIHistoryEntry.idl
526 GetTitleHistTest(theHistoryEntry
, displayMode
);
528 // test isSubFrame attribute in nsIHistoryEntry.idl
529 GetIsSubFrameTest(theHistoryEntry
, displayMode
);