adding nsISHistoryTestLib.js test library and nsISHistoryTestCase1.html test script.

This commit is contained in:
depstein%netscape.com 2002-03-27 01:47:15 +00:00
parent d4045452e6
commit e6a4b04c27
2 changed files with 295 additions and 0 deletions

View File

@ -0,0 +1,76 @@
<html>
<head>
<script TYPE="text/javascript" SRC="nsISHistoryTestLib.js"></script>
<script TYPE="text/javascript">
function nsISHistoryTest()
{
// Test case for nsISessionHistory
// Calls the init() function of nsISHistory
// alert("In nsISHistoryTest constructor\n");
var interfaceName = "nsISHistory";
var sHistory = init();
var count = 0;
var index = 0;
var maxLength = 100;
// begins OnTestSessionHistory();
if (!sHistory)
{
alert("Unable to get session history");
return;
}
alert("Testing nsISHistory interfaces\n");
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
count = testHistoryCount(sHistory); // GetCount()
alert("global count: " + count);
index = testHistoryIndex(sHistory); // GetIndex()
alert ("global index: " + index);
maxLength = testMaxLength(sHistory); // GetMaxLength()
alert ("global maxLength: " + maxLength);
// Test GetEntryAtIndex()
for(i=0; i<count; i++) {
entry = testGetEntryAtIndex(sHistory, i, false);
if (entry)
alert("GetEntryAtIndex() Valid, cnt = " + i);
else
alert("GetEntryAtIndex() Invalid, cnt = " + i);
} // for
// Test the enumerator
var simpleEnum = testSimpleEnum(sHistory); // Get simple enumerator
alert("global simpleEnum: " + simpleEnum);
if (simpleEnum) {
var enumCnt = 0;
while (simpleEnum.hasMoreElements())
{
nextObj = simpleEnum.getNext();
alert("global nextObj: " + nextObj);
if (!nextObj)
continue;
nextHE = nextObj.QueryInterface(Components.interfaces.nsIHistoryEntry);
alert("global nextHE: " + nextHE);
if (!nextHE)
continue;
testHistoryEntry(nextHE, enumCnt);
enumCnt++;
} // while
} // if simplenum
}
</script>
</head>
<body>
<script TYPE="text/javascript">
nsISHistoryTest();
alert("finished script execution.");
</script>
</body>
</html>

View File

@ -0,0 +1,219 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Radha Kulkarni <radha@netscape.com>
* David Epstein <depstein@netscape.com>
*/
// nsISHistoryTestLib.js is a JavaScript library containing methods for
// testing of nsISHistory (session history) and nsIHistoryEntry interfaces.
// See nsISHistoryTestCase1.html (located in the same folder) for examples
// of how to use these methods.
// ***************************************************************************
// init() creates and returns the session history object.
function init()
{
//alert("In nsISHistoryTest::Init method");
var sHistory = null;
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserAccess");
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var ifaceReq = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
if (!ifaceReq) {
alert("Unable to get interface requestor object");
return false;
}
var webNav = ifaceReq.getInterface(Components.interfaces.nsIWebNavigation);
if(!webNav) {
alert("Unable to get WebNavigation");
return false;
}
sHistory = webNav.sessionHistory;
if (!sHistory) {
alert("Didn't get SessionHistory object");
return false;
}
return sHistory;
}
catch(e)
{
alert("Could not find Session History component" + e);
return false;
}
}
// *************************************************************************
// testHistoryCount() returns the number of entries in the session history.
// It accepts the session history object as the input parameter.
function testHistoryCount(sHistory)
{
if (!sHistory) {
alert("Didn't get SessionHistory object");
return false;
}
var cnt = sHistory.count; // GetCount()
return cnt;
}
// *************************************************************************
// testHistoryIndex() returns the current session history index.
// It accepts the session history object as the input parameter.
// In your test script,
function testHistoryIndex(sHistory)
{
if (!sHistory) {
alert("Didn't get SessionHistory object");
return false;
}
var shIndex = sHistory.index; // GetIndex()
return shIndex;
}
// *************************************************************************
// testMaxLength() returns the maximum number of session history entries.
// It accepts the session history object as the input parameter.
function testMaxLength(sHistory)
{
if (!sHistory) {
alert("Didn't get SessionHistory object");
return false;
}
var maxLen = sHistory.maxLength; // GetMaxLength
return maxLen;
}
// *************************************************************************
// testGetEntryAtIndex() returns a referenced entry of the session history.
// index. It accepts the session history object as the 1st input parameter,
// a counter to track entries for the 2nd parameter, and a modify index
// parameter for the 3rd one (which if 'true' allows the index entry to be
// modified).
function testGetEntryAtIndex(sHistory, cnt, modIndex)
{
if (!sHistory) {
alert("Didn't get SessionHistory object");
return false;
}
var entry = sHistory.getEntryAtIndex(cnt, modIndex);
return entry;
}
// *************************************************************************
function testSimpleEnum(sHistory)
// testSimpleEnum() returns an enumerated list of all the session history
// entries. It accepts the session history object as the input parameter
{
if (!sHistory) {
alert("Didn't get SessionHistory object");
return false;
}
var simpleEnum = sHistory.SHistoryEnumerator;
return simpleEnum;
}
// *************************************************************************
// testHistoryEntry() obtains the URI string, title, and sub-Frame status
// of the web page (i.e. whether the web page is in a frame). It accepts
// the history entry object as the 1st parameter, and the session history
// index as the 2nd one.
function testHistoryEntry(entry, index)
{
if (!entry)
alert("Didn't get history entry object");
// Get URI for the next Entry
Uri = entry.URI;
uriSpec = Uri.spec;
alert("The URI = " + uriSpec);
// Get Title for the nextEntry
title = entry.title;
alert("The page title = " + title);
// Get SubFrame Status for the nextEntry
frameStatus = entry.isSubFrame;
alert("The subframe value = " + frameStatus);
}
// *************************************************************************
// testHistoryEntryUri() returns the URI string for a given index. It accepts
// the history entry object as the 1st parameter, and the session history
// index as the 2nd one.
function testHistoryEntryUri(nextHE, index)
{
if (!nextHE) {
alert("Didn't get history entry object");
return false;
}
// Get URI for the next Entry
Uri = entry.URI;
uriSpec = Uri.spec;
return uriSpec;
}
// *************************************************************************
// testHistoryEntryTitle() returns the web page title for a given index. It accepts
// the history entry object as the 1st parameter, and the session history
// index as the 2nd one.
function testHistoryEntryTitle(nextHE, index)
{
if (!nextHE) {
alert("Didn't get history entry object");
return false;
}
// Get Title for the nextEntry
title = entry.title;
return title;
}
// *************************************************************************
// testHistoryEntryFrame() returns the sub-frame status for a given index. It accepts
// the history entry object as the 1st parameter, and the session history
// index as the 2nd one.
function testHistoryEntryFrame(nextHE, index)
{
if (!nextHE) {
alert("Didn't get history entry object");
return false;
}
// Get SubFrame Status for the nextEntry
frameStatus = entry.isSubFrame;
return frameStatus;
}
// *************************************************************************
// purgeEntries() purges entries in the session history. It accepts the session
// history object as the 1st parameter. The 2nd parameter is for the number of
// entries to purge (it will purge the earliest entries of the session history).
function purgeEntries(sHistory, numEntries)
{
if (!sHistory) {
alert("Didn't get SessionHistory object");
}
sHistory.PurgeHistory(numEntries);
}