1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_bug678392.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,192 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +let HTTPROOT = "http://example.com/browser/browser/base/content/test/general/"; 1.9 + 1.10 +function maxSnapshotOverride() { 1.11 + return 5; 1.12 +} 1.13 + 1.14 +function test() { 1.15 + waitForExplicitFinish(); 1.16 + 1.17 + BrowserOpenTab(); 1.18 + let tab = gBrowser.selectedTab; 1.19 + registerCleanupFunction(function () { gBrowser.removeTab(tab); }); 1.20 + 1.21 + ok(gHistorySwipeAnimation, "gHistorySwipeAnimation exists."); 1.22 + 1.23 + if (!gHistorySwipeAnimation._isSupported()) { 1.24 + is(gHistorySwipeAnimation.active, false, "History swipe animation is not " + 1.25 + "active when not supported by the platform."); 1.26 + finish(); 1.27 + return; 1.28 + } 1.29 + 1.30 + gHistorySwipeAnimation._getMaxSnapshots = maxSnapshotOverride; 1.31 + gHistorySwipeAnimation.init(); 1.32 + 1.33 + is(gHistorySwipeAnimation.active, true, "History swipe animation support " + 1.34 + "was successfully initialized when supported."); 1.35 + 1.36 + cleanupArray(); 1.37 + load(gBrowser.selectedTab, HTTPROOT + "browser_bug678392-2.html", test0); 1.38 +} 1.39 + 1.40 +function load(aTab, aUrl, aCallback) { 1.41 + aTab.linkedBrowser.addEventListener("load", function onload(aEvent) { 1.42 + aEvent.currentTarget.removeEventListener("load", onload, true); 1.43 + waitForFocus(aCallback, content); 1.44 + }, true); 1.45 + aTab.linkedBrowser.loadURI(aUrl); 1.46 +} 1.47 + 1.48 +function cleanupArray() { 1.49 + let arr = gHistorySwipeAnimation._trackedSnapshots; 1.50 + while (arr.length > 0) { 1.51 + delete arr[0].browser.snapshots[arr[0].index]; // delete actual snapshot 1.52 + arr.splice(0, 1); 1.53 + } 1.54 +} 1.55 + 1.56 +function testArrayCleanup() { 1.57 + // Test cleanup of array of tracked snapshots. 1.58 + let arr = gHistorySwipeAnimation._trackedSnapshots; 1.59 + is(arr.length, 0, "Snapshots were removed correctly from the array of " + 1.60 + "tracked snapshots."); 1.61 +} 1.62 + 1.63 +function test0() { 1.64 + // Test growing of array of tracked snapshots. 1.65 + let tab = gBrowser.selectedTab; 1.66 + 1.67 + load(tab, HTTPROOT + "browser_bug678392-1.html", function() { 1.68 + ok(gHistorySwipeAnimation._trackedSnapshots, "Array for snapshot " + 1.69 + "tracking is initialized."); 1.70 + is(gHistorySwipeAnimation._trackedSnapshots.length, 1, "Snapshot array " + 1.71 + "has correct length of 1 after loading one page."); 1.72 + load(tab, HTTPROOT + "browser_bug678392-2.html", function() { 1.73 + is(gHistorySwipeAnimation._trackedSnapshots.length, 2, "Snapshot array " + 1.74 + " has correct length of 2 after loading two pages."); 1.75 + load(tab, HTTPROOT + "browser_bug678392-1.html", function() { 1.76 + is(gHistorySwipeAnimation._trackedSnapshots.length, 3, "Snapshot " + 1.77 + "array has correct length of 3 after loading three pages."); 1.78 + load(tab, HTTPROOT + "browser_bug678392-2.html", function() { 1.79 + is(gHistorySwipeAnimation._trackedSnapshots.length, 4, "Snapshot " + 1.80 + "array has correct length of 4 after loading four pages."); 1.81 + cleanupArray(); 1.82 + testArrayCleanup(); 1.83 + test1(); 1.84 + }); 1.85 + }); 1.86 + }); 1.87 + }); 1.88 +} 1.89 + 1.90 +function verifyRefRemoved(aIndex, aBrowser) { 1.91 + let wasFound = false; 1.92 + let arr = gHistorySwipeAnimation._trackedSnapshots; 1.93 + for (let i = 0; i < arr.length; i++) { 1.94 + if (arr[i].index == aIndex && arr[i].browser == aBrowser) 1.95 + wasFound = true; 1.96 + } 1.97 + is(wasFound, false, "The reference that was previously removed was " + 1.98 + "still found in the array of tracked snapshots."); 1.99 +} 1.100 + 1.101 +function test1() { 1.102 + // Test presence of snpashots in per-tab array of snapshots and removal of 1.103 + // individual snapshots (and corresponding references in the array of 1.104 + // tracked snapshots). 1.105 + let tab = gBrowser.selectedTab; 1.106 + 1.107 + load(tab, HTTPROOT + "browser_bug678392-1.html", function() { 1.108 + var historyIndex = gBrowser.webNavigation.sessionHistory.index - 1; 1.109 + load(tab, HTTPROOT + "browser_bug678392-2.html", function() { 1.110 + load(tab, HTTPROOT + "browser_bug678392-1.html", function() { 1.111 + load(tab, HTTPROOT + "browser_bug678392-2.html", function() { 1.112 + let browser = gBrowser.selectedBrowser; 1.113 + ok(browser.snapshots, "Array of snapshots exists in browser."); 1.114 + ok(browser.snapshots[historyIndex], "First page exists in snapshot " + 1.115 + "array."); 1.116 + ok(browser.snapshots[historyIndex + 1], "Second page exists in " + 1.117 + "snapshot array."); 1.118 + ok(browser.snapshots[historyIndex + 2], "Third page exists in " + 1.119 + "snapshot array."); 1.120 + ok(browser.snapshots[historyIndex + 3], "Fourth page exists in " + 1.121 + "snapshot array."); 1.122 + is(gHistorySwipeAnimation._trackedSnapshots.length, 4, "Length of " + 1.123 + "array of tracked snapshots is equal to 4 after loading four " + 1.124 + "pages."); 1.125 + 1.126 + // Test removal of reference in the middle of the array. 1.127 + gHistorySwipeAnimation._removeTrackedSnapshot(historyIndex + 1, 1.128 + browser); 1.129 + verifyRefRemoved(historyIndex + 1, browser); 1.130 + is(gHistorySwipeAnimation._trackedSnapshots.length, 3, "Length of " + 1.131 + "array of tracked snapshots is equal to 3 after removing one" + 1.132 + "reference from the array with length 4."); 1.133 + 1.134 + // Test removal of reference at end of array. 1.135 + gHistorySwipeAnimation._removeTrackedSnapshot(historyIndex + 3, 1.136 + browser); 1.137 + verifyRefRemoved(historyIndex + 3, browser); 1.138 + is(gHistorySwipeAnimation._trackedSnapshots.length, 2, "Length of " + 1.139 + "array of tracked snapshots is equal to 2 after removing two" + 1.140 + "references from the array with length 4."); 1.141 + 1.142 + // Test removal of reference at head of array. 1.143 + gHistorySwipeAnimation._removeTrackedSnapshot(historyIndex, 1.144 + browser); 1.145 + verifyRefRemoved(historyIndex, browser); 1.146 + is(gHistorySwipeAnimation._trackedSnapshots.length, 1, "Length of " + 1.147 + "array of tracked snapshots is equal to 1 after removing three" + 1.148 + "references from the array with length 4."); 1.149 + 1.150 + cleanupArray(); 1.151 + test2(); 1.152 + }); 1.153 + }); 1.154 + }); 1.155 + }); 1.156 +} 1.157 + 1.158 +function test2() { 1.159 + // Test growing of snapshot array across tabs. 1.160 + let tab = gBrowser.selectedTab; 1.161 + 1.162 + load(tab, HTTPROOT + "browser_bug678392-1.html", function() { 1.163 + var historyIndex = gBrowser.webNavigation.sessionHistory.index - 1; 1.164 + load(tab, HTTPROOT + "browser_bug678392-2.html", function() { 1.165 + is(gHistorySwipeAnimation._trackedSnapshots.length, 2, "Length of " + 1.166 + "snapshot array is equal to 2 after loading two pages"); 1.167 + let prevTab = tab; 1.168 + tab = gBrowser.addTab("about:newtab"); 1.169 + gBrowser.selectedTab = tab; 1.170 + load(tab, HTTPROOT + "browser_bug678392-2.html" /* initial page */, 1.171 + function() { 1.172 + load(tab, HTTPROOT + "browser_bug678392-1.html", function() { 1.173 + load(tab, HTTPROOT + "browser_bug678392-2.html", function() { 1.174 + is(gHistorySwipeAnimation._trackedSnapshots.length, 4, "Length " + 1.175 + "of snapshot array is equal to 4 after loading two pages in " + 1.176 + "two tabs each."); 1.177 + gBrowser.removeCurrentTab(); 1.178 + gBrowser.selectedTab = prevTab; 1.179 + cleanupArray(); 1.180 + test3(); 1.181 + }); 1.182 + }); 1.183 + }); 1.184 + }); 1.185 + }); 1.186 +} 1.187 + 1.188 +function test3() { 1.189 + // Test uninit of gHistorySwipeAnimation. 1.190 + // This test MUST be the last one to execute. 1.191 + gHistorySwipeAnimation.uninit(); 1.192 + is(gHistorySwipeAnimation.active, false, "History swipe animation support " + 1.193 + "was successfully uninitialized"); 1.194 + finish(); 1.195 +}