Bug 1539482 - Write tests for nsISHEntry::CreateLoadInfo, r=peterv

Differential Revision: https://phabricator.services.mozilla.com/D26813

--HG--
extra : rebase_source : d3bf408e74dfd33fd6e9b3c8f69dbfa91028c647
extra : source : 88d429a1f9e4dc7cfae21d3690680a30a92f879f
extra : histedit_source : b8710737afc396011d74345dd5513c6d42f296ba
This commit is contained in:
Anny Gakhokidze 2019-04-08 18:39:40 -04:00
Родитель ac22fc9f39
Коммит e77474ca47
4 изменённых файлов: 189 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<body onpageshow="opener.bodyOnLoad()">
<a id="link1" href="#1">Link 1</a>
<a name="1">link 1</a>
</body>
</html>

Просмотреть файл

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<body onpageshow="opener.bodyOnLoad()">
<a id="link1" href="#1">Link 1</a>
<a id="link2" href="#2">Link 2</a>
<a name="1">link 1</a>
<a name="2">link 2</a>
</body>
</html>

Просмотреть файл

@ -42,6 +42,8 @@ support-files =
file_triggeringprincipal_iframe_iframe_window_open_frame_a.html
file_triggeringprincipal_iframe_iframe_window_open_frame_b.html
file_triggeringprincipal_iframe_iframe_window_open_frame_a_nav.html
file_load_history_entry_page_with_one_link.html
file_load_history_entry_page_with_two_links.html
file_bug1300461.html
file_bug1300461_redirect.html
file_bug1300461_redirect.html^headers^
@ -76,6 +78,7 @@ support-files = file_bug1536471.html
skip-if = fission # Times out.
[test_grandchild.html]
fail-if = fission
[test_load_history_entry.html]
[test_not-opener.html]
skip-if = fission # Times out.
[test_opener.html]

Просмотреть файл

@ -0,0 +1,170 @@
<!DOCTYPE HTML>
<html>
<head>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="application/javascript" src="/tests/SimpleTest/SpecialPowers.js"></script>
<script type="application/javascript">
/*
* Perform the following steps.
* 1) Go to file_load_history_entry_page_with_two_links.html, which contains two links, 'link1' and 'link2'
* 2) Click on 'link1' to be taken to file_load_history_entry_page_with_two_links.html#1
* 3) Click on 'link2' to be taken to file_load_history_entry_page_with_two_links.html#2
* 4) Go to file_load_history_entry_page_with_one_link.html
* 5) Push state to go to file_load_history_entry_page_with_one_link.html#1
*
* After each step
* - Check the number of session history entries
* - Reload the document and do the above again
* - Navigate back and check the correct history index
* - Navigate forward and check the correct history index and location
*/
async function test() {
let testWin;
var promise;
var previousLocation;
var numSHEntries = 0;
// Step 1. Open a new tab and load a document with two links inside
// Now we are at file_load_history_entry_page_with_two_links.html
numSHEntries++;
promise = waitForLoad();
testWin = window.open("file_load_history_entry_page_with_two_links.html");
await promise;
let shistory = SpecialPowers.wrap(testWin)
.docShell
.QueryInterface(SpecialPowers.Ci.nsIWebNavigation)
.sessionHistory;
// Step 2. Navigate the document by clicking on the 1st link
// Now we are at file_load_history_entry_page_with_two_links.html#1
numSHEntries++;
previousLocation = testWin.location.href;
await clickLink(testWin, "link1");
await doAfterEachTest(testWin, shistory, numSHEntries, previousLocation);
// Step 3. Navigate the document by clicking the 2nd link
// Now we are file_load_history_entry_page_with_two_links.html#2
numSHEntries++;
previousLocation = testWin.location.href;
await clickLink(testWin, "link2");
await doAfterEachTest(testWin, shistory, numSHEntries, previousLocation);
// Step 4. Navigate the document to a different page
// Now we are at file_load_history_entry_page_with_one_link.html
numSHEntries++;
previousLocation = testWin.location.href;
promise = waitForLoad();
testWin.location = "file_load_history_entry_page_with_one_link.html";
await promise;
await doAfterEachTest(testWin, shistory, numSHEntries, previousLocation, true);
// Step 5. Push some state
// Now we are at file_load_history_entry_page_with_one_link.html#1
numSHEntries++;
previousLocation = testWin.location.href;
testWin.history.pushState({foo: "bar"}, "", "#1");
is(testWin.history.length, numSHEntries, "Session history's length is correct after pushing state");
is(shistory.index, numSHEntries - 1 /* we haven't switched to new history entry yet*/,
"Session history's index is correct after pushing state");
await doAfterEachTest(testWin, shistory, numSHEntries, previousLocation);
// We are done with the test
testWin.close();
SimpleTest.finish();
}
/*
* @prevLocation
* if undefined, it is because there is no page to go back to
*
* @isCrossDocumentLoad
* did we just open a different document
*
*/
async function doAfterEachTest(testWin, shistory, expectedNumSHEntries, prevLocation, isCrossDocumentLoad = false) {
var initialLocation = testWin.location.href;
var initialSHIndex = shistory.index;
var promise;
is(testWin.history.length, expectedNumSHEntries, "Session history's length is correct");
// Reload the document
promise = waitForLoad();
testWin.location.reload(true);
await promise;
is(testWin.history.length, expectedNumSHEntries, "Session history's length is correct after reloading");
if (prevLocation == undefined) {
return;
}
// Navigate backwards
if (isCrossDocumentLoad) {
// Current page must have been a cross document load, so we just need to wait for
// document load to complete after we navigate the history back
// because popstate event will not be fired in this case
promise = waitForLoad();
} else {
promise = waitForPopstate(testWin);
}
testWin.history.back();
await promise;
is(testWin.location.href, prevLocation, "Window location is correct after navigating back in history");
is(shistory.index, initialSHIndex - 1, "Session history's index is correct after navigating back in history");
// Navigate forwards
if (isCrossDocumentLoad) {
promise = waitForLoad();
} else {
promise = waitForPopstate(testWin);
}
testWin.history.forward();
await promise;
is(testWin.location.href, initialLocation, "Window location is correct after navigating forward in history");
is(shistory.index, initialSHIndex, "Session history's index is correct after navigating forward in history");
}
async function waitForLoad() {
return new Promise(resolve => {
window.bodyOnLoad = function() {
setTimeout(resolve, 0);
window.bodyOnLoad = undefined;
};
});
}
async function waitForPopstate(win) {
return new Promise(resolve => {
win.addEventListener("popstate", (e) => {
setTimeout(resolve, 0);
}, {once: true});
});
}
async function clickLink(win, id) {
var link = win.document.getElementById(id);
let clickPromise = new Promise(resolve => {
win.addEventListener("hashchange", resolve, {once: true});
});
link.click();
await clickPromise;
}
</script>
</head>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1539482">Bug 1539482</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="text/javascript">
SimpleTest.waitForExplicitFinish();
</script>
</pre>
<body onload="test()">
</body>
</html>