michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let HTTPROOT = "http://example.com/browser/browser/base/content/test/general/"; michael@0: michael@0: function maxSnapshotOverride() { michael@0: return 5; michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: BrowserOpenTab(); michael@0: let tab = gBrowser.selectedTab; michael@0: registerCleanupFunction(function () { gBrowser.removeTab(tab); }); michael@0: michael@0: ok(gHistorySwipeAnimation, "gHistorySwipeAnimation exists."); michael@0: michael@0: if (!gHistorySwipeAnimation._isSupported()) { michael@0: is(gHistorySwipeAnimation.active, false, "History swipe animation is not " + michael@0: "active when not supported by the platform."); michael@0: finish(); michael@0: return; michael@0: } michael@0: michael@0: gHistorySwipeAnimation._getMaxSnapshots = maxSnapshotOverride; michael@0: gHistorySwipeAnimation.init(); michael@0: michael@0: is(gHistorySwipeAnimation.active, true, "History swipe animation support " + michael@0: "was successfully initialized when supported."); michael@0: michael@0: cleanupArray(); michael@0: load(gBrowser.selectedTab, HTTPROOT + "browser_bug678392-2.html", test0); michael@0: } michael@0: michael@0: function load(aTab, aUrl, aCallback) { michael@0: aTab.linkedBrowser.addEventListener("load", function onload(aEvent) { michael@0: aEvent.currentTarget.removeEventListener("load", onload, true); michael@0: waitForFocus(aCallback, content); michael@0: }, true); michael@0: aTab.linkedBrowser.loadURI(aUrl); michael@0: } michael@0: michael@0: function cleanupArray() { michael@0: let arr = gHistorySwipeAnimation._trackedSnapshots; michael@0: while (arr.length > 0) { michael@0: delete arr[0].browser.snapshots[arr[0].index]; // delete actual snapshot michael@0: arr.splice(0, 1); michael@0: } michael@0: } michael@0: michael@0: function testArrayCleanup() { michael@0: // Test cleanup of array of tracked snapshots. michael@0: let arr = gHistorySwipeAnimation._trackedSnapshots; michael@0: is(arr.length, 0, "Snapshots were removed correctly from the array of " + michael@0: "tracked snapshots."); michael@0: } michael@0: michael@0: function test0() { michael@0: // Test growing of array of tracked snapshots. michael@0: let tab = gBrowser.selectedTab; michael@0: michael@0: load(tab, HTTPROOT + "browser_bug678392-1.html", function() { michael@0: ok(gHistorySwipeAnimation._trackedSnapshots, "Array for snapshot " + michael@0: "tracking is initialized."); michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 1, "Snapshot array " + michael@0: "has correct length of 1 after loading one page."); michael@0: load(tab, HTTPROOT + "browser_bug678392-2.html", function() { michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 2, "Snapshot array " + michael@0: " has correct length of 2 after loading two pages."); michael@0: load(tab, HTTPROOT + "browser_bug678392-1.html", function() { michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 3, "Snapshot " + michael@0: "array has correct length of 3 after loading three pages."); michael@0: load(tab, HTTPROOT + "browser_bug678392-2.html", function() { michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 4, "Snapshot " + michael@0: "array has correct length of 4 after loading four pages."); michael@0: cleanupArray(); michael@0: testArrayCleanup(); michael@0: test1(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function verifyRefRemoved(aIndex, aBrowser) { michael@0: let wasFound = false; michael@0: let arr = gHistorySwipeAnimation._trackedSnapshots; michael@0: for (let i = 0; i < arr.length; i++) { michael@0: if (arr[i].index == aIndex && arr[i].browser == aBrowser) michael@0: wasFound = true; michael@0: } michael@0: is(wasFound, false, "The reference that was previously removed was " + michael@0: "still found in the array of tracked snapshots."); michael@0: } michael@0: michael@0: function test1() { michael@0: // Test presence of snpashots in per-tab array of snapshots and removal of michael@0: // individual snapshots (and corresponding references in the array of michael@0: // tracked snapshots). michael@0: let tab = gBrowser.selectedTab; michael@0: michael@0: load(tab, HTTPROOT + "browser_bug678392-1.html", function() { michael@0: var historyIndex = gBrowser.webNavigation.sessionHistory.index - 1; michael@0: load(tab, HTTPROOT + "browser_bug678392-2.html", function() { michael@0: load(tab, HTTPROOT + "browser_bug678392-1.html", function() { michael@0: load(tab, HTTPROOT + "browser_bug678392-2.html", function() { michael@0: let browser = gBrowser.selectedBrowser; michael@0: ok(browser.snapshots, "Array of snapshots exists in browser."); michael@0: ok(browser.snapshots[historyIndex], "First page exists in snapshot " + michael@0: "array."); michael@0: ok(browser.snapshots[historyIndex + 1], "Second page exists in " + michael@0: "snapshot array."); michael@0: ok(browser.snapshots[historyIndex + 2], "Third page exists in " + michael@0: "snapshot array."); michael@0: ok(browser.snapshots[historyIndex + 3], "Fourth page exists in " + michael@0: "snapshot array."); michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 4, "Length of " + michael@0: "array of tracked snapshots is equal to 4 after loading four " + michael@0: "pages."); michael@0: michael@0: // Test removal of reference in the middle of the array. michael@0: gHistorySwipeAnimation._removeTrackedSnapshot(historyIndex + 1, michael@0: browser); michael@0: verifyRefRemoved(historyIndex + 1, browser); michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 3, "Length of " + michael@0: "array of tracked snapshots is equal to 3 after removing one" + michael@0: "reference from the array with length 4."); michael@0: michael@0: // Test removal of reference at end of array. michael@0: gHistorySwipeAnimation._removeTrackedSnapshot(historyIndex + 3, michael@0: browser); michael@0: verifyRefRemoved(historyIndex + 3, browser); michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 2, "Length of " + michael@0: "array of tracked snapshots is equal to 2 after removing two" + michael@0: "references from the array with length 4."); michael@0: michael@0: // Test removal of reference at head of array. michael@0: gHistorySwipeAnimation._removeTrackedSnapshot(historyIndex, michael@0: browser); michael@0: verifyRefRemoved(historyIndex, browser); michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 1, "Length of " + michael@0: "array of tracked snapshots is equal to 1 after removing three" + michael@0: "references from the array with length 4."); michael@0: michael@0: cleanupArray(); michael@0: test2(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function test2() { michael@0: // Test growing of snapshot array across tabs. michael@0: let tab = gBrowser.selectedTab; michael@0: michael@0: load(tab, HTTPROOT + "browser_bug678392-1.html", function() { michael@0: var historyIndex = gBrowser.webNavigation.sessionHistory.index - 1; michael@0: load(tab, HTTPROOT + "browser_bug678392-2.html", function() { michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 2, "Length of " + michael@0: "snapshot array is equal to 2 after loading two pages"); michael@0: let prevTab = tab; michael@0: tab = gBrowser.addTab("about:newtab"); michael@0: gBrowser.selectedTab = tab; michael@0: load(tab, HTTPROOT + "browser_bug678392-2.html" /* initial page */, michael@0: function() { michael@0: load(tab, HTTPROOT + "browser_bug678392-1.html", function() { michael@0: load(tab, HTTPROOT + "browser_bug678392-2.html", function() { michael@0: is(gHistorySwipeAnimation._trackedSnapshots.length, 4, "Length " + michael@0: "of snapshot array is equal to 4 after loading two pages in " + michael@0: "two tabs each."); michael@0: gBrowser.removeCurrentTab(); michael@0: gBrowser.selectedTab = prevTab; michael@0: cleanupArray(); michael@0: test3(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function test3() { michael@0: // Test uninit of gHistorySwipeAnimation. michael@0: // This test MUST be the last one to execute. michael@0: gHistorySwipeAnimation.uninit(); michael@0: is(gHistorySwipeAnimation.active, false, "History swipe animation support " + michael@0: "was successfully uninitialized"); michael@0: finish(); michael@0: }