Skip to content

Commit

Permalink
Backout bug 681005 in order to fix bug 771504; a=akeybl
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan committed Sep 1, 2012
1 parent 12e7b75 commit 5cea6c9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 218 deletions.
147 changes: 42 additions & 105 deletions browser/components/sessionstore/src/SessionStore.jsm
Expand Up @@ -2823,108 +2823,6 @@ let SessionStoreInternal = {
this._sendRestoreCompletedNotifications();
},

/**
* Sets the tabs restoring order with the following priority:
* Selected tab, pinned tabs, visible tabs, unhidden tabs and hidden tabs.
* @param aTabBrowser
* Tab browser object
* @param aTabs
* Array of tab references
* @param aTabData
* Array of tab data
* @param aSelectedTab
* Index of selected tab
*/
_setTabsRestoringOrder : function ssi__setTabsRestoringOrder(
aTabBrowser, aTabs, aTabData, aSelectedTab) {
// Temporally store the pinned tabs before moving the hidden tabs and
// optimizing the visible tabs. In case the selected tab is a pinned tab,
// the index is also stored.
let pinnedTabs = aTabData.filter(function (aData) aData.pinned).length;
let pinnedTabsArray = [];
let pinnedTabsDataArray = [];
let pinnedSelectedTab = null;
if (pinnedTabs && aTabs.length > 1) {
for (let t = aTabs.length - 1; t >= 0; t--) {
if (aTabData[t].pinned) {
pinnedTabsArray.unshift(aTabs.splice(t, 1)[0]);
pinnedTabsDataArray.unshift(aTabData.splice(t, 1)[0]);
if (aSelectedTab) {
if (aSelectedTab > (t + 1))
--aSelectedTab;
else if (aSelectedTab == (t + 1)) {
aSelectedTab = null;
pinnedSelectedTab = 1;
}
} else if (pinnedSelectedTab)
++pinnedSelectedTab;
}
}
}

// Without the pinned tabs, we move the hidden tabs to the end of the list
// and optimize the visible tabs.
let unhiddenTabs = aTabData.filter(function (aData) !aData.hidden).length;
if (unhiddenTabs && aTabs.length > 1) {
// Load hidden tabs last, by pushing them to the end of the list.
for (let t = 0, tabsToReorder = aTabs.length - unhiddenTabs; tabsToReorder > 0; ) {
if (aTabData[t].hidden) {
aTabs = aTabs.concat(aTabs.splice(t, 1));
aTabData = aTabData.concat(aTabData.splice(t, 1));
if (aSelectedTab && aSelectedTab > t)
--aSelectedTab;
--tabsToReorder;
continue;
}
++t;
}

// Determine if we can optimize & load visible tabs first
let maxVisibleTabs = Math.ceil(aTabBrowser.tabContainer.mTabstrip.scrollClientSize /
aTabs[unhiddenTabs - 1].getBoundingClientRect().width);

// Make sure we restore visible tabs first, if there are enough
if (aSelectedTab && maxVisibleTabs < unhiddenTabs && aSelectedTab > 1) {
let firstVisibleTab = 0;
if (unhiddenTabs - maxVisibleTabs > aSelectedTab) {
// aSelectedTab is leftmost since we scroll to it when possible
firstVisibleTab = aSelectedTab - 1;
} else {
// aSelectedTab is rightmost or no more room to scroll right
firstVisibleTab = unhiddenTabs - maxVisibleTabs;
}
aTabs = aTabs.splice(firstVisibleTab, maxVisibleTabs).concat(aTabs);
aTabData = aTabData.splice(firstVisibleTab, maxVisibleTabs).concat(aTabData);
aSelectedTab -= firstVisibleTab;
}
}

// Load the pinned tabs at the beginning of the list and restore the
// selected tab index.
if (pinnedTabsArray) {
// Restore the selected tab index.
if (pinnedSelectedTab) {
aSelectedTab = pinnedSelectedTab;
} else {
aSelectedTab += pinnedTabsArray.length;
}
// Load the pinned tabs at the beginning of the list.
for (let t = pinnedTabsArray.length - 1; t >= 0; t--) {
aTabs.unshift(pinnedTabsArray.splice(t, 1)[0]);
aTabData.unshift(pinnedTabsDataArray.splice(t, 1)[0]);
}
}

// Load the selected tab to the first position.
if (aSelectedTab-- && aTabs[aSelectedTab]) {
aTabs.unshift(aTabs.splice(aSelectedTab, 1)[0]);
aTabData.unshift(aTabData.splice(aSelectedTab, 1)[0]);
aTabBrowser.selectedTab = aTabs[0];
}

return [aTabs, aTabData];
},

/**
* Manage history restoration for a window
* @param aWindow
Expand Down Expand Up @@ -2977,9 +2875,48 @@ let SessionStoreInternal = {
return;
}

// Sets the tabs restoring order.
[aTabs, aTabData] =
this._setTabsRestoringOrder(tabbrowser, aTabs, aTabData, aSelectTab);
let unhiddenTabs = aTabData.filter(function (aData) !aData.hidden).length;

if (unhiddenTabs && aTabs.length > 1) {
// Load hidden tabs last, by pushing them to the end of the list
for (let t = 0, tabsToReorder = aTabs.length - unhiddenTabs; tabsToReorder > 0; ) {
if (aTabData[t].hidden) {
aTabs = aTabs.concat(aTabs.splice(t, 1));
aTabData = aTabData.concat(aTabData.splice(t, 1));
if (aSelectTab > t)
--aSelectTab;
--tabsToReorder;
continue;
}
++t;
}

// Determine if we can optimize & load visible tabs first
let maxVisibleTabs = Math.ceil(tabbrowser.tabContainer.mTabstrip.scrollClientSize /
aTabs[unhiddenTabs - 1].getBoundingClientRect().width);

// make sure we restore visible tabs first, if there are enough
if (maxVisibleTabs < unhiddenTabs && aSelectTab > 1) {
let firstVisibleTab = 0;
if (unhiddenTabs - maxVisibleTabs > aSelectTab) {
// aSelectTab is leftmost since we scroll to it when possible
firstVisibleTab = aSelectTab - 1;
} else {
// aSelectTab is rightmost or no more room to scroll right
firstVisibleTab = unhiddenTabs - maxVisibleTabs;
}
aTabs = aTabs.splice(firstVisibleTab, maxVisibleTabs).concat(aTabs);
aTabData = aTabData.splice(firstVisibleTab, maxVisibleTabs).concat(aTabData);
aSelectTab -= firstVisibleTab;
}
}

// make sure to restore the selected tab first (if any)
if (aSelectTab-- && aTabs[aSelectTab]) {
aTabs.unshift(aTabs.splice(aSelectTab, 1)[0]);
aTabData.unshift(aTabData.splice(aSelectTab, 1)[0]);
tabbrowser.selectedTab = aTabs[0];
}

// Prepare the tabs so that they can be properly restored. We'll pin/unpin
// and show/hide tabs as necessary. We'll also set the labels, user typed
Expand Down
141 changes: 28 additions & 113 deletions browser/components/sessionstore/test/browser_480148.js
Expand Up @@ -8,152 +8,72 @@ function test() {
requestLongerTimeout(3);

// builds the tests state based on a few parameters
function buildTestState(num, selected, hidden, pinned) {
let state = { windows: [ { "tabs": [], "selected": selected + 1 } ] };
function buildTestState(num, selected, hidden) {
let state = { windows: [ { "tabs": [], "selected": selected } ] };
while (num--) {
state.windows[0].tabs.push({
entries: [
{ url: "http://example.com/?t=" + state.windows[0].tabs.length }
]
});
state.windows[0].tabs.push({entries: [{url: "http://example.com/"}]});
let i = state.windows[0].tabs.length - 1;
if (hidden.length > 0 && i == hidden[0]) {
state.windows[0].tabs[i].hidden = true;
hidden.splice(0, 1);
}
if (pinned.length > 0 && i == pinned[0]) {
state.windows[0].tabs[i].pinned = true;
pinned.splice(0, 1);
}
}
return state;
}

let tests = [
{ testNum: 1,
totalTabs: 13,
selectedTab: 0,
selectedTab: 1,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 2,
totalTabs: 13,
selectedTab: 12,
selectedTab: 13,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [12, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6]
},
{ testNum: 3,
totalTabs: 13,
selectedTab: 3,
selectedTab: 4,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [3, 4, 5, 6, 7, 8, 0, 1, 2, 9, 10, 11, 12]
},
{ testNum: 4,
totalTabs: 13,
selectedTab: 10,
selectedTab: 11,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [10, 7, 8, 9, 11, 12, 0, 1, 2, 3, 4, 5, 6]
},
{ testNum: 5,
totalTabs: 13,
selectedTab: 12,
selectedTab: 13,
shownTabs: 6,
hiddenTabs: [0, 4, 9],
pinnedTabs: [],
order: [12, 6, 7, 8, 10, 11, 1, 2, 3, 5, 0, 4, 9]
},
{ testNum: 6,
totalTabs: 13,
selectedTab: 3,
selectedTab: 4,
shownTabs: 6,
hiddenTabs: [1, 7, 12],
pinnedTabs: [],
order: [3, 4, 5, 6, 8, 9, 0, 2, 10, 11, 1, 7, 12]
},
{ testNum: 7,
totalTabs: 13,
selectedTab: 3,
selectedTab: 4,
shownTabs: 6,
hiddenTabs: [0, 1, 2],
pinnedTabs: [],
order: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 1, 2]
},
{ testNum: 8,
totalTabs: 13,
selectedTab: 0,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0],
order: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 9,
totalTabs: 13,
selectedTab: 1,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0],
order: [1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 10,
totalTabs: 13,
selectedTab: 3,
shownTabs: 6,
hiddenTabs: [2],
pinnedTabs: [0,1],
order: [3, 0, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2]
},
{ testNum: 11,
totalTabs: 13,
selectedTab: 12,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0,1,2],
order: [12, 0, 1, 2, 7, 8, 9, 10, 11, 3, 4, 5, 6]
},
{ testNum: 12,
totalTabs: 13,
selectedTab: 6,
shownTabs: 6,
hiddenTabs: [3,4,5],
pinnedTabs: [0,1,2],
order: [6, 0, 1, 2, 7, 8, 9, 10, 11, 12, 3, 4, 5]
},
{ testNum: 13,
totalTabs: 13,
selectedTab: 1,
shownTabs: 6,
hiddenTabs: [3,4,5],
pinnedTabs: [0,1,2],
order: [1, 0, 2, 6, 7, 8, 9, 10, 11, 12, 3, 4, 5]
},
{ testNum: 14,
totalTabs: 13,
selectedTab: 2,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0,1,2],
order: [2, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 15,
totalTabs: 13,
selectedTab: 3,
shownTabs: 6,
hiddenTabs: [1,4],
pinnedTabs: [0,1,2],
order: [3, 0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 4]
}
];

let tabMinWidth =
parseInt(getComputedStyle(gBrowser.selectedTab, null).minWidth);
let tabMinWidth = parseInt(getComputedStyle(gBrowser.selectedTab, null).minWidth);
let testIndex = 0;

function runNextTest() {
Expand All @@ -164,46 +84,41 @@ function test() {

info ("Starting test " + (++testIndex));
let test = tests.shift();
let state = buildTestState(test.totalTabs, test.selectedTab,
test.hiddenTabs, test.pinnedTabs);
let state = buildTestState(test.totalTabs, test.selectedTab, test.hiddenTabs);
let tabbarWidth = Math.floor((test.shownTabs - 0.5) * tabMinWidth);
let win = openDialog(location, "_blank", "chrome,all,dialog=no");
let tabsRestored = [];
let actualOrder = [];

win.addEventListener("SSTabRestoring", function onSSTabRestoring(aEvent) {
let tab = aEvent.originalTarget;
let tabLink = tab.linkedBrowser.currentURI.spec;
let tabIndex =
tabLink.substring(tabLink.indexOf("?t=") + 3, tabLink.length);

// we need to compare with the tab's restoring index, no with the
// position index, since the pinned tabs change the positions in the
// tabbar.
tabsRestored.push(tabIndex);
let currentIndex = Array.indexOf(win.gBrowser.tabs, tab);
actualOrder.push(currentIndex);

if (tabsRestored.length < state.windows[0].tabs.length)
if (actualOrder.length < state.windows[0].tabs.length)
return;

// all of the tabs should be restoring or restored by now
is(tabsRestored.length, state.windows[0].tabs.length,
is(actualOrder.length, state.windows[0].tabs.length,
"Test #" + testIndex + ": Number of restored tabs is as expected");

is(tabsRestored.join(" "), test.order.join(" "),
is(actualOrder.join(" "), test.order.join(" "),
"Test #" + testIndex + ": 'visible' tabs restored first");

// cleanup
// Cleanup.
win.removeEventListener("SSTabRestoring", onSSTabRestoring, false);
win.close();
executeSoon(runNextTest);
}, false);

whenWindowLoaded(win, function(aEvent) {
let extent =
win.outerWidth - win.gBrowser.tabContainer.mTabstrip.scrollClientSize;
let windowWidth = tabbarWidth + extent;
win.resizeTo(windowWidth, win.outerHeight);
ss.setWindowState(win, JSON.stringify(state), true);
});
win.addEventListener("load", function onLoad(aEvent) {
win.removeEventListener("load", onLoad, false);
executeSoon(function () {
let extent = win.outerWidth - win.gBrowser.tabContainer.mTabstrip.scrollClientSize;
let windowWidth = tabbarWidth + extent;
win.resizeTo(windowWidth, win.outerHeight);
ss.setWindowState(win, JSON.stringify(state), true);
});
}, false);
};

runNextTest();
Expand Down

0 comments on commit 5cea6c9

Please sign in to comment.