browser/components/tabview/test/browser_tabview_bug595601.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
michael@0 5
michael@0 6 const TAB_STATE_NEEDS_RESTORE = 1;
michael@0 7 const TAB_STATE_RESTORING = 2;
michael@0 8
michael@0 9 let stateBackup = ss.getBrowserState();
michael@0 10
michael@0 11 let state = {windows:[{tabs:[
michael@0 12 // first group
michael@0 13 {entries:[{url:"http://example.com#1"}],extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#1\",\"groupID\":2}"}},
michael@0 14 {entries:[{url:"http://example.com#2"}],extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#2\",\"groupID\":2}"}},
michael@0 15 {entries:[{url:"http://example.com#3"}],extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#3\",\"groupID\":2}"}},
michael@0 16 {entries:[{url:"http://example.com#4"}],extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#4\",\"groupID\":2}"}},
michael@0 17
michael@0 18 // second group
michael@0 19 {entries:[{url:"http://example.com#5"}],hidden:true,extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#5\",\"groupID\":1}"}},
michael@0 20 {entries:[{url:"http://example.com#6"}],hidden:true,extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#6\",\"groupID\":1}"}},
michael@0 21 {entries:[{url:"http://example.com#7"}],hidden:true,extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#7\",\"groupID\":1}"}},
michael@0 22 {entries:[{url:"http://example.com#8"}],hidden:true,extData:{"tabview-tab":"{\"bounds\":{\"left\":20,\"top\":20,\"width\":20,\"height\":20},\"url\":\"http://example.com#8\",\"groupID\":1}"}}
michael@0 23 ],selected:4,extData:{
michael@0 24 "tabview-groups":"{\"nextID\":8,\"activeGroupId\":1}","tabview-group":"{\"1\":{\"bounds\":{\"left\":15,\"top\":10,\"width\":415,\"height\":367},\"userSize\":{\"x\":415,\"y\":367},\"title\":\"\",\"id\":1},\"2\":{\"bounds\":{\"left\":286,\"top\":488,\"width\":418,\"height\":313},\"title\":\"\",\"id\":2}}",
michael@0 25 "tabview-ui":"{\"pageBounds\":{\"left\":0,\"top\":0,\"width\":940,\"height\":1075}}"
michael@0 26 }}]};
michael@0 27
michael@0 28 function test() {
michael@0 29 waitForExplicitFinish();
michael@0 30
michael@0 31 Services.prefs.setBoolPref("browser.sessionstore.restore_hidden_tabs", false);
michael@0 32
michael@0 33 TabsProgressListener.init();
michael@0 34
michael@0 35 registerCleanupFunction(function () {
michael@0 36 TabsProgressListener.uninit();
michael@0 37
michael@0 38 Services.prefs.clearUserPref("browser.sessionstore.restore_hidden_tabs");
michael@0 39
michael@0 40 gBrowser.selectedTab = gBrowser.tabs[0];
michael@0 41 ss.setBrowserState(stateBackup);
michael@0 42 });
michael@0 43
michael@0 44 TabView._initFrame(function () {
michael@0 45 executeSoon(testRestoreWithHiddenTabs);
michael@0 46 });
michael@0 47 }
michael@0 48
michael@0 49 function testRestoreWithHiddenTabs() {
michael@0 50 TabsProgressListener.setCallback(function (needsRestore, isRestoring) {
michael@0 51 if (needsRestore <= 4) {
michael@0 52 TabsProgressListener.unsetCallback();
michael@0 53 is(needsRestore, 4, "4/8 tabs restored");
michael@0 54 }
michael@0 55 });
michael@0 56
michael@0 57 waitForBrowserState(state, 4, function () {
michael@0 58 is(gBrowser.tabs.length, 8, "there are now eight tabs");
michael@0 59 is(gBrowser.visibleTabs.length, 4, "four visible tabs");
michael@0 60
michael@0 61 let cw = TabView.getContentWindow();
michael@0 62 is(cw.GroupItems.groupItems.length, 2, "there are now two groupItems");
michael@0 63
michael@0 64 testSwitchToInactiveGroup();
michael@0 65 });
michael@0 66 }
michael@0 67
michael@0 68 function testSwitchToInactiveGroup() {
michael@0 69 let firstProgress = true;
michael@0 70
michael@0 71 TabsProgressListener.setCallback(function (needsRestore, isRestoring) {
michael@0 72 if (firstProgress) {
michael@0 73 firstProgress = false;
michael@0 74 is(isRestoring, 3, "restoring 3 tabs concurrently");
michael@0 75 } else {
michael@0 76 ok(isRestoring < 4, "restoring max. 3 tabs concurrently");
michael@0 77 }
michael@0 78
michael@0 79 if (needsRestore)
michael@0 80 return;
michael@0 81
michael@0 82 TabsProgressListener.unsetCallback();
michael@0 83
michael@0 84 is(gBrowser.visibleTabs.length, 4, "four visible tabs");
michael@0 85 waitForFocus(finish);
michael@0 86 });
michael@0 87
michael@0 88 gBrowser.selectedTab = gBrowser.tabs[4];
michael@0 89 }
michael@0 90
michael@0 91 function waitForBrowserState(state, numTabs, callback) {
michael@0 92 let tabContainer = gBrowser.tabContainer;
michael@0 93 tabContainer.addEventListener("SSTabRestored", function onRestored() {
michael@0 94 if (--numTabs <= 0) {
michael@0 95 tabContainer.removeEventListener("SSTabRestored", onRestored, true);
michael@0 96 executeSoon(callback);
michael@0 97 }
michael@0 98 }, true);
michael@0 99
michael@0 100 ss.setBrowserState(JSON.stringify(state));
michael@0 101 }
michael@0 102
michael@0 103 function countTabs() {
michael@0 104 let needsRestore = 0, isRestoring = 0;
michael@0 105 let windowsEnum = Services.wm.getEnumerator("navigator:browser");
michael@0 106
michael@0 107 while (windowsEnum.hasMoreElements()) {
michael@0 108 let window = windowsEnum.getNext();
michael@0 109 if (window.closed)
michael@0 110 continue;
michael@0 111
michael@0 112 for (let i = 0; i < window.gBrowser.tabs.length; i++) {
michael@0 113 let browser = window.gBrowser.tabs[i].linkedBrowser;
michael@0 114 if (browser.__SS_restoreState == TAB_STATE_RESTORING)
michael@0 115 isRestoring++;
michael@0 116 else if (browser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE)
michael@0 117 needsRestore++;
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 return [needsRestore, isRestoring];
michael@0 122 }
michael@0 123
michael@0 124 let TabsProgressListener = {
michael@0 125 init: function () {
michael@0 126 gBrowser.addTabsProgressListener(this);
michael@0 127 },
michael@0 128
michael@0 129 uninit: function () {
michael@0 130 this.unsetCallback();
michael@0 131 gBrowser.removeTabsProgressListener(this);
michael@0 132 },
michael@0 133
michael@0 134 setCallback: function (callback) {
michael@0 135 this.callback = callback;
michael@0 136 },
michael@0 137
michael@0 138 unsetCallback: function () {
michael@0 139 delete this.callback;
michael@0 140 },
michael@0 141
michael@0 142 onStateChange: function (aBrowser, aWebProgress, aRequest, aStateFlags, aStatus) {
michael@0 143 let isNetwork = aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK;
michael@0 144 let isWindow = aStateFlags & Ci.nsIWebProgressListener.STATE_IS_WINDOW;
michael@0 145
michael@0 146 if (!(this.callback && isNetwork && isWindow))
michael@0 147 return;
michael@0 148
michael@0 149 let self = this;
michael@0 150 let finalize = function () {
michael@0 151 if (wasRestoring)
michael@0 152 delete aBrowser.__wasRestoring;
michael@0 153
michael@0 154 self.callback.apply(null, countTabs());
michael@0 155 };
michael@0 156
michael@0 157 let isRestoring = aBrowser.__SS_restoreState == TAB_STATE_RESTORING;
michael@0 158 let wasRestoring = !aBrowser.__SS_restoreState && aBrowser.__wasRestoring;
michael@0 159 let hasStopped = aStateFlags & Ci.nsIWebProgressListener.STATE_STOP;
michael@0 160
michael@0 161 if (isRestoring && !hasStopped)
michael@0 162 aBrowser.__wasRestoring = true;
michael@0 163
michael@0 164 if (hasStopped && (isRestoring || wasRestoring))
michael@0 165 finalize();
michael@0 166 }
michael@0 167 }

mercurial