Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / embedding / qa / testembed / nsiHistory.cpp
blobac55c2e92f0641cad451e150d6c383bbc00bfec3
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....
41 //
42 // Test cases for the nsiHistory Interface
44 #include "stdafx.h"
45 #include "TestEmbed.h"
46 #include "BrowserImpl.h"
47 #include "BrowserFrm.h"
48 #include "UrlDialog.h"
49 #include "ProfileMgr.h"
50 #include "ProfilesDlg.h"
51 #include "QaUtils.h"
52 #include "nsihistory.h"
53 #include <stdio.h>
56 /////////////////////////////////////////////////////////////////////////////
57 // CNsIHistory
59 CNsIHistory::CNsIHistory(nsIWebNavigation *mWebNav)
61 //qaTests = mTests;
62 qaWebNav = mWebNav ;
66 CNsIHistory::~CNsIHistory()
72 /////////////////////////////////////////////////////////////////////////////
73 // CNsIHistory message handlers
74 // ***********************************************************************
75 // ***********************************************************************
76 // nsIHistory iface
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;
88 PRInt32 theIndex;
89 PRInt32 theMaxLength = 100;
91 CString shString;
93 nsCOMPtr<nsISHistory> theSessionHistory;
94 nsCOMPtr<nsIHistoryEntry> theHistoryEntry;
95 nsCOMPtr<nsISimpleEnumerator> theSimpleEnum;
98 //nsCOMPtr<nsIURI> theURI;
99 // do_QueryInterface
100 // NS_HISTORYENTRY_CONTRACTID
102 // get Session History through web nav iface
103 if (qaWebNav) {
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);
112 return;
114 else {
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);
122 else
123 QAOutput("We have the History Entry object!", 1);
127 switch(nMenuID)
129 case ID_INTERFACES_NSISHISTORY_RUNALLTESTS :
130 QAOutput("Begin nsISHistory tests.", 1);
131 RunAllTests();
132 QAOutput("End nsISHistory tests.", 1);
133 break ;
134 case ID_INTERFACES_NSISHISTORY_GETCOUNT :
135 GetCountTest(theSessionHistory, &numEntries, 2);
136 break ;
137 case ID_INTERFACES_NSISHISTORY_GETINDEX :
138 GetIndexTest(theSessionHistory, &theIndex, 2);
139 break ;
140 case ID_INTERFACES_NSISHISTORY_GETMAXLENGTH :
141 GetMaxLengthTest(theSessionHistory, &theMaxLength, 2);
142 break ;
143 case ID_INTERFACES_NSISHISTORY_SETMAXLENGTH :
144 SetMaxLengthTest(theSessionHistory, theMaxLength, 2);
145 break ;
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);
158 return;
160 GetURIHistTest(theHistoryEntry, 2);
161 GetTitleHistTest(theHistoryEntry, 2);
162 GetIsSubFrameTest(theHistoryEntry, 2);
163 } // end for loop
165 break ;
166 case ID_INTERFACES_NSISHISTORY_PURGEHISTORY :
167 PurgeHistoryTest(theSessionHistory, numEntries, 2);
168 break ;
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);
178 return;
180 SimpleEnumTest(theSimpleEnum, 2);
181 break ;
183 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_RUNALLTESTS :
184 RunAllHistoryEntryTests(2);
185 break ;
186 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETURI :
187 GetURIHistTest(theHistoryEntry, 2);
188 break ;
189 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETTITLE :
190 GetTitleHistTest(theHistoryEntry, 2);
191 break ;
192 case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETISSUBFRAME :
193 GetIsSubFrameTest(theHistoryEntry, 2);
194 break ;
199 void CNsIHistory::RunAllTests()
201 //nsresult rv;
203 PRInt32 numEntries = 5;
204 PRInt32 theIndex;
205 PRInt32 theMaxLength = 100;
207 CString shString;
209 nsCOMPtr<nsISHistory> theSessionHistory;
210 nsCOMPtr<nsIHistoryEntry> theHistoryEntry;
212 // get Session History through web nav iface
213 if (qaWebNav)
214 qaWebNav->GetSessionHistory( getter_AddRefs(theSessionHistory));
216 if (!theSessionHistory)
218 QAOutput("theSessionHistory object wasn't created. No session history tests performed.", 2);
219 return;
221 else {
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);
245 else
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);
266 return;
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);
279 } // end for loop
280 } // end outer else
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");
292 if (!theSimpleEnum)
293 QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
294 else
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,
306 PRInt16 displayMode)
308 rv = theSessionHistory->GetCount(numEntries);
309 if (*numEntries < 0)
310 QAOutput("numEntries for GetCount() < 0. Test failed.", 1);
311 else {
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,
319 PRInt16 displayMode)
321 rv = theSessionHistory->GetIndex(theIndex);
322 if (*theIndex <0)
323 QAOutput("theIndex for GetIndex() < 0. Test failed.", 1);
324 else {
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,
332 PRInt16 displayMode)
334 rv = theSessionHistory->SetMaxLength(theMaxLength);
335 if (theMaxLength < 0)
336 QAOutput("theMaxLength for SetMaxLength() < 0. Test failed.", 1);
337 else {
338 RvTestResult(rv, "SetMaxLength() (MaxLength attribute) test", displayMode);
339 RvTestResultDlg(rv, "SetMaxLength() (MaxLength attribute) test");
343 void CNsIHistory::GetMaxLengthTest(nsISHistory *theSessionHistory, PRInt32 *theMaxLength,
344 PRInt16 displayMode)
346 rv = theSessionHistory->GetMaxLength(theMaxLength);
347 if (*theMaxLength<0)
348 QAOutput("theMaxLength for GetMaxLength() < 0. Test failed.", 1);
349 else {
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,
359 PRInt32 theIndex)
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,
376 PRInt16 displayMode)
378 rv = theHistoryEntry->GetURI(getter_AddRefs(theURI));
379 RvTestResult(rv, "GetURI() (URI attribute) test", 1);
380 RvTestResultDlg(rv, "GetURI() (URI attribute) test");
381 if (!theURI)
382 QAOutput("theURI for GetURI() invalid. Test failed.", 1);
383 else
385 nsCAutoString uriString;
386 rv = theURI->GetSpec(uriString);
387 if (NS_FAILED(rv))
388 QAOutput("We didn't get the uriString.", 1);
389 else
390 FormatAndPrintOutput("The SH Url = ", uriString, displayMode);
394 void CNsIHistory::GetTitleHistTest(nsIHistoryEntry* theHistoryEntry,
395 PRInt16 displayMode)
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");
402 if (!theTitle) {
403 QAOutput("theTitle for GetTitle() is blank. Test failed.", 1);
404 return;
407 FormatAndPrintOutput("The title = ",
408 NS_ConvertUTF16toUTF8(theTitle).get(), displayMode);
411 void CNsIHistory::GetIsSubFrameTest(nsIHistoryEntry* theHistoryEntry,
412 PRInt16 displayMode)
414 PRBool isSubFrame;
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));
428 if (!theSimpleEnum)
429 QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
430 else
431 RvTestResult(rv, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test", 2);
435 void CNsIHistory::SimpleEnumTest(nsISimpleEnumerator *theSimpleEnum,
436 PRInt16 displayMode)
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));
445 if (!nextObj)
446 continue;
447 nextHistoryEntry = do_QueryInterface(nextObj);
448 if (!nextHistoryEntry)
449 continue;
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);
455 if (NS_FAILED(rv))
456 QAOutput("uriString for GetSpec() invalid. Test failed.", 1);
457 else
458 FormatAndPrintOutput("The SimpleEnum URL = ", uriString, displayMode);
462 void CNsIHistory::PurgeHistoryTest(nsISHistory* theSessionHistory, PRInt32 numEntries,
463 PRInt16 displayMode)
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;
474 PRInt32 theIndex;
475 PRInt32 theMaxLength = 100;
477 CString shString;
479 nsCOMPtr<nsISHistory> theSessionHistory;
480 nsCOMPtr<nsIHistoryEntry> theHistoryEntry;
482 // get Session History through web nav iface
483 if (qaWebNav)
484 qaWebNav->GetSessionHistory( getter_AddRefs(theSessionHistory));
486 if (!theSessionHistory)
488 QAOutput("theSessionHistory object wasn't created. No session history tests performed.", displayMode);
489 return;
491 else
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);
497 else
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);
518 return;
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);
531 } // end for loop
532 } // end outer else