michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: /** michael@0: * Ensure that starting a load invalidates shistory. michael@0: */ michael@0: add_task(function test_load_start() { michael@0: // Create a new tab. michael@0: let tab = gBrowser.addTab("about:blank"); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Load a new URI but remove the tab before it has finished loading. michael@0: browser.loadURI("about:mozilla"); michael@0: yield promiseContentMessage(browser, "ss-test:onFrameTreeReset"); michael@0: gBrowser.removeTab(tab); michael@0: michael@0: // Undo close the tab. michael@0: tab = ss.undoCloseTab(window, 0); michael@0: browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Check that the correct URL was restored. michael@0: is(browser.currentURI.spec, "about:mozilla", "url is correct"); michael@0: michael@0: // Cleanup. michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: /** michael@0: * Ensure that purging shistory invalidates. michael@0: */ michael@0: add_task(function test_purge() { michael@0: // Create a new tab. michael@0: let tab = gBrowser.addTab("about:mozilla"); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Create a second shistory entry. michael@0: browser.loadURI("about:robots"); michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Check that we now have two shistory entries. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 2, "there are two shistory entries"); michael@0: michael@0: // Purge session history. michael@0: yield sendMessage(browser, "ss-test:purgeSessionHistory"); michael@0: michael@0: // Check that we are left with a single shistory entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 1, "there is one shistory entry"); michael@0: michael@0: // Cleanup. michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: /** michael@0: * Ensure that anchor navigation invalidates shistory. michael@0: */ michael@0: add_task(function test_hashchange() { michael@0: const URL = "data:text/html;charset=utf-8,clickme"; michael@0: michael@0: // Create a new tab. michael@0: let tab = gBrowser.addTab(URL); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Check that we start with a single shistory entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 1, "there is one shistory entry"); michael@0: michael@0: // Click the link and wait for a hashchange event. michael@0: browser.messageManager.sendAsyncMessage("ss-test:click", {id: "a"}); michael@0: yield promiseContentMessage(browser, "ss-test:hashchange"); michael@0: michael@0: // Check that we now have two shistory entries. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 2, "there are two shistory entries"); michael@0: michael@0: // Cleanup. michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: /** michael@0: * Ensure that loading pages from the bfcache invalidates shistory. michael@0: */ michael@0: add_task(function test_pageshow() { michael@0: const URL = "data:text/html;charset=utf-8,

first

"; michael@0: const URL2 = "data:text/html;charset=utf-8,

second

"; michael@0: michael@0: // Create a new tab. michael@0: let tab = gBrowser.addTab(URL); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Create a second shistory entry. michael@0: browser.loadURI(URL2); michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Go back to the previous url which is loaded from the bfcache. michael@0: browser.goBack(); michael@0: yield promiseContentMessage(browser, "ss-test:onFrameTreeCollected"); michael@0: is(browser.currentURI.spec, URL, "correct url after going back"); michael@0: michael@0: // Check that loading from bfcache did invalidate shistory. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {index} = JSON.parse(ss.getTabState(tab)); michael@0: is(index, 1, "first history entry is selected"); michael@0: michael@0: // Cleanup. michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: /** michael@0: * Ensure that subframe navigation invalidates shistory. michael@0: */ michael@0: add_task(function test_subframes() { michael@0: const URL = "data:text/html;charset=utf-8," + michael@0: "" + michael@0: "clickme" + michael@0: "clickme"; michael@0: michael@0: // Create a new tab. michael@0: let tab = gBrowser.addTab(URL); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Check that we have a single shistory entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 1, "there is one shistory entry"); michael@0: is(entries[0].children.length, 1, "the entry has one child"); michael@0: michael@0: // Navigate the subframe. michael@0: browser.messageManager.sendAsyncMessage("ss-test:click", {id: "a1"}); michael@0: yield promiseBrowserLoaded(browser, false /* don't ignore subframes */); michael@0: michael@0: // Check shistory. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 2, "there now are two shistory entries"); michael@0: is(entries[1].children.length, 1, "the second entry has one child"); michael@0: michael@0: // Go back in history. michael@0: browser.goBack(); michael@0: yield promiseBrowserLoaded(browser, false /* don't ignore subframes */); michael@0: michael@0: // Navigate the subframe again. michael@0: browser.messageManager.sendAsyncMessage("ss-test:click", {id: "a2"}); michael@0: yield promiseContentMessage(browser, "ss-test:hashchange"); michael@0: michael@0: // Check shistory. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 2, "there now are two shistory entries"); michael@0: is(entries[1].children.length, 1, "the second entry has one child"); michael@0: michael@0: // Cleanup. michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: /** michael@0: * Ensure that navigating from an about page invalidates shistory. michael@0: */ michael@0: add_task(function test_about_page_navigate() { michael@0: // Create a new tab. michael@0: let tab = gBrowser.addTab("about:blank"); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Check that we have a single shistory entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 1, "there is one shistory entry"); michael@0: is(entries[0].url, "about:blank", "url is correct"); michael@0: michael@0: browser.loadURI("about:robots"); michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Check that we have changed the history entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 1, "there is one shistory entry"); michael@0: is(entries[0].url, "about:robots", "url is correct"); michael@0: michael@0: // Cleanup. michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: /** michael@0: * Ensure that history.pushState and history.replaceState invalidate shistory. michael@0: */ michael@0: add_task(function test_pushstate_replacestate() { michael@0: // Create a new tab. michael@0: let tab = gBrowser.addTab("http://example.com/1"); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Check that we have a single shistory entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 1, "there is one shistory entry"); michael@0: is(entries[0].url, "http://example.com/1", "url is correct"); michael@0: michael@0: browser.messageManager. michael@0: sendAsyncMessage("ss-test:historyPushState", {url: 'test-entry/'}); michael@0: yield promiseContentMessage(browser, "ss-test:historyPushState"); michael@0: michael@0: // Check that we have added the history entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 2, "there is another shistory entry"); michael@0: is(entries[1].url, "http://example.com/test-entry/", "url is correct"); michael@0: michael@0: // Disabled until replaceState invalidation is supported. See Bug 967028. michael@0: browser.messageManager. michael@0: sendAsyncMessage("ss-test:historyReplaceState", {url: 'test-entry2/'}); michael@0: yield promiseContentMessage(browser, "ss-test:historyReplaceState"); michael@0: michael@0: // Check that we have modified the history entry. michael@0: SyncHandlers.get(browser).flush(); michael@0: let {entries} = JSON.parse(ss.getTabState(tab)); michael@0: is(entries.length, 2, "there is still two shistory entries"); michael@0: is(entries[1].url, "http://example.com/test-entry/test-entry2/", "url is correct"); michael@0: michael@0: // Cleanup. michael@0: gBrowser.removeTab(tab); michael@0: });