browser_480148.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
16  * The Initial Developer of the Original Code is Mozilla Corporation.
17  * Portions created by the Initial Developer are Copyright (C) 2009
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul O’Shannessy <poshannessy@mozilla.com> (primary author)
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
36 
37 function test() {
40  // builds the tests state based on a few parameters
41  function buildTestState(num, selected) {
42  let state = { windows: [ { "tabs": [], "selected": selected } ] };
43  while (num--)
44  state.windows[0].tabs.push({entries: [{url: "http://example.com/"}]});
45  return state;
46  }
47 
48  // builds an array of the indexs we expect to see in the order they get loaded
49  function buildExpectedOrder(num, selected, shown) {
50  // assume selected is 1-based index
51  selected--;
52  let expected = [selected];
53  // fill left to selected if space
54  for (let i = selected - (shown - expected.length); i >= 0 && i < selected; i++)
55  expected.push(i);
56  // fill from left to right until right length or end
57  for (let i = selected + 1; expected.length < shown && i < num; i++)
58  expected.push(i);
59  // fill in the remaining
60  for (let i = 0; i < num; i++) {
61  if (expected.indexOf(i) == -1) {
62  expected.push(i);
63  }
64  }
65  return expected;
66  }
67 
68  // test setup
69  let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
71 
72  // the number of tests we're running
73  let numTests = 4;
74  let completedTests = 0;
75 
76  // access the pref service just once
77  let tabMinWidth = gPrefService.getIntPref("browser.tabs.tabMinWidth");
78 
79  function runTest(testNum, totalTabs, selectedTab, shownTabs, order) {
80  let test = {
81  QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMEventListener,
82  Ci.nsISupportsWeakReference]),
83 
84  state: buildTestState(totalTabs, selectedTab),
85  numTabsToShow: shownTabs,
86  expectedOrder: order,
87  actualOrder: [],
88  windowWidth: null,
89  callback: null,
90  window: null,
91 
92  handleSSTabRestoring: function (aEvent) {
93  let tab = aEvent.originalTarget;
94  let tabbrowser = this.window.gBrowser;
95  let currentIndex = Array.indexOf(tabbrowser.mTabs, tab);
96  this.actualOrder.push(currentIndex);
97 
98  if (this.actualOrder.length < this.state.windows[0].tabs.length)
99  return;
100 
101  // all of the tabs should be restoring or restored by now
102  is(this.actualOrder.length, this.state.windows[0].tabs.length,
103  "Test #" + testNum + ": Number of restored tabs is as expected");
104 
105  is(this.actualOrder.join(" "), this.expectedOrder.join(" "),
106  "Test #" + testNum + ": 'visible' tabs restored first");
107 
108  // cleanup
109  this.window.close();
110  // if we're all done, explicitly finish
111  if (++completedTests == numTests)
112  finish();
113  },
114 
115  handleLoad: function (aEvent) {
116  let _this = this;
117  executeSoon(function () {
118  _this.window.resizeTo(_this.windowWidth, _this.window.outerHeight);
119  ss.setWindowState(_this.window, JSON.stringify(_this.state), true);
120  });
121  },
122 
123  // Implement nsIDOMEventListener for handling various window and tab events
124  handleEvent: function (aEvent) {
125  switch (aEvent.type) {
126  case "load":
127  this.handleLoad(aEvent);
128  break;
129  case "SSTabRestoring":
130  this.handleSSTabRestoring(aEvent);
131  break;
132  }
133  },
134 
135  // setup and actually run the test
136  run: function () {
137  this.windowWidth = Math.floor((this.numTabsToShow - 0.5) * tabMinWidth);
138  this.window = openDialog(location, "_blank", "chrome,all,dialog=no");
139  this.window.addEventListener("SSTabRestoring", this, false);
140  this.window.addEventListener("load", this, false);
141  }
142  };
143  test.run();
144  }
145 
146  // actually create & run the tests
147  runTest(1, 13, 1, 6, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
148  runTest(2, 13, 13, 6, [12, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6]);
149  runTest(3, 13, 4, 6, [3, 4, 5, 6, 7, 8, 0, 1, 2, 9, 10, 11, 12]);
150  runTest(4, 13, 11, 6, [10, 7, 8, 9, 11, 12, 0, 1, 2, 3, 4, 5, 6]);
151 
152  // finish() is run by the last test to finish, so no cleanup down here
153 }
const Cc
var windows
sbOSDControlService prototype QueryInterface
var tab
let window
function test()
var tabbrowser
function num(elem, prop)
var _this
waitForExplicitFinish()
grep callback
return null
Definition: FeedWriter.js:1143
var expected
var gPrefService
Definition: overlay.js:34
function url(spec)
const Ci
var JSON
function runTest()
_getSelectedPageStyle s i