]> git.donarmstrong.com Git - dactyl.git/blob - common/tests/functional/shared-modules/sessionstore.js
Initial import of 1.0~b6
[dactyl.git] / common / tests / functional / shared-modules / sessionstore.js
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 MozMill Test code.
15  *
16  * The Initial Developer of the Original Code is Mozilla Foundation.
17  * Portions created by the Initial Developer are Copyright (C) 2010
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  *   Henrik Skupin <hskupin@mozilla.com>
22  *   Aaron Train <aaron.train@gmail.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 /**
39  * @fileoverview
40  * The SessionStoreAPI adds support for accessing session related elements and features
41  *
42  * @version 1.0.0
43  */
44
45 // Include required modules
46 var prefs = require("prefs");
47 var utils = require("utils");
48 var widgets = require("widgets");
49
50 // Session Store service
51 var sessionStoreService = Cc["@mozilla.org/browser/sessionstore;1"]
52                              .getService(Ci.nsISessionStore);
53
54 // Preference for indicating the amount of restorable tabs
55 const SESSIONSTORE_MAXTABS_PREF = 'browser.sessionstore.max_tabs_undo';
56
57 const gTimeout = 5000;
58
59 /**
60  * Constructor
61  *
62  * @param {MozMillController} controller
63  *        MozMill controller of the browser window to operate on.
64  */
65 function aboutSessionRestore(controller)
66 {
67   this._controller = controller;
68 }
69
70 /**
71  * This class handles the about:sessionrestore page.
72  */
73 aboutSessionRestore.prototype = {
74   /**
75    * Returns the MozMill controller
76    *
77    * @returns Mozmill controller
78    * @type {MozMillController}
79    */
80   get controller() {
81     return this._controller;
82   },
83
84   /**
85    * Returns the tree which contains the windows and tabs
86    *
87    * @returns Tree with windows and tabs to restore
88    * @type {ElemBase}
89    */
90   get tabList() {
91     return this.getElement({type: "tabList"});
92   },
93
94   /**
95    * Gets all the needed external DTD urls as an array
96    *
97    * @returns Array of external DTD urls
98    * @type [string]
99    */
100   getDtds : function aboutSessionRestore_getDtds() {
101     var dtds = ["chrome://browser/locale/browser.dtd",
102                 "chrome://browser/locale/aboutSessionRestore.dtd"];
103     return dtds;
104   },
105
106   /**
107    * Retrieve an UI element based on the given spec
108    *
109    * @param {object} spec
110    *        Information of the UI element which should be retrieved
111    *        type: General type information
112    *        subtype: Specific element or property
113    *        value: Value of the element or property
114    * @returns Element which has been created
115    * @type {ElemBase}
116    */
117   getElement : function aboutSessionRestore_getElement(spec) {
118     var elem = null;
119
120     switch(spec.type) {
121       case "button_restoreSession":
122         elem = new elementslib.ID(this._controller.tabs.activeTab, "errorTryAgain");
123         break;
124       case "error_longDesc":
125         elem = new elementslib.ID(this._controller.tabs.activeTab, "errorLongDesc");
126         break;
127       case "error_pageContainer":
128         elem = new elementslib.ID(this._controller.tabs.activeTab, "errorPageContainer");
129         break;
130       case "error_shortDesc":
131         elem = new elementslib.ID(this._controller.tabs.activeTab, "errorShortDescText");
132         break;
133       case "error_title":
134         elem = new elementslib.ID(this._controller.tabs.activeTab, "errorTitleText");
135         break;
136       case "tabList":
137         elem = new elementslib.ID(this._controller.window.document, "tabList");
138         break;
139       default:
140         throw new Error(arguments.callee.name + ": Unknown element type - " + spec.type);
141     }
142
143     return elem;
144   },
145
146   /**
147    * Returns the current restore state of the given element
148    *
149    * @param {object} element
150    *        Element which restore state should be retrieved
151    * @returns True if the element should be restored
152    * @type {boolean}
153    *
154    */
155   getRestoreState : function aboutSessionRestore_getRestoreState(element) {
156     var tree = this.tabList.getNode();
157
158     return tree.view.getCellValue(element.listIndex, tree.columns.getColumnAt(0));
159   },
160
161   /**
162    * Get restorable tabs under the given window
163    *
164    * @param {object} window
165    *        Window inside the tree
166    * @returns List of tabs
167    * @type {array of object}
168    */
169   getTabs : function aboutSessionRestore_getTabs(window) {
170     var tabs = [ ];
171     var tree = this.tabList.getNode();
172
173     // Add entries when they are tabs (no container)
174     var ii = window.listIndex + 1;
175     while (ii < tree.view.rowCount && !tree.view.isContainer(ii)) {
176       tabs.push({
177                  index: tabs.length,
178                  listIndex : ii,
179                  restore: tree.view.getCellValue(ii, tree.columns.getColumnAt(0)),
180                  title: tree.view.getCellText(ii, tree.columns.getColumnAt(2))
181                 });
182       ii++;
183     }
184
185     return tabs;
186   },
187
188   /**
189    * Get restorable windows
190    *
191    * @returns List of windows
192    * @type {array of object}
193    */
194   getWindows : function aboutSessionRestore_getWindows() {
195     var windows = [ ];
196     var tree = this.tabList.getNode();
197
198     for (var ii = 0; ii < tree.view.rowCount; ii++) {
199       if (tree.view.isContainer(ii)) {
200         windows.push({
201                       index: windows.length,
202                       listIndex : ii,
203                       open: tree.view.isContainerOpen(ii),
204                       restore: tree.view.getCellValue(ii, tree.columns.getColumnAt(0)),
205                       title: tree.view.getCellText(ii, tree.columns.getColumnAt(2))
206                      });
207       }
208     }
209
210     return windows;
211   },
212
213   /**
214    * Toggles the restore state for the element
215    *
216    * @param {object} element
217    *        Specifies the element which restore state should be toggled
218    */
219   toggleRestoreState : function aboutSessionRestore_toggleRestoreState(element) {
220     var state = this.getRestoreState(element);
221
222     widgets.clickTreeCell(this._controller, this.tabList, element.listIndex, 0, {});
223     this._controller.sleep(0);
224
225     this._controller.assertJS("subject.newState != subject.oldState",
226                               {newState : this.getRestoreState(element), oldState : state});
227   }
228 }
229
230 /**
231  * Resets the list of recently closed tabs by setting and clearing the user preference
232  */
233 function resetRecentlyClosedTabs()
234 {
235   prefs.preferences.setPref(SESSIONSTORE_MAXTABS_PREF, 0);
236   prefs.preferences.clearUserPref(SESSIONSTORE_MAXTABS_PREF);
237 }
238
239 /**
240  * Returns the number of restorable tabs for a given window
241  *
242  * @param {MozMillController} controller
243  *        MozMillController of the window to operate on
244  * @returns The number of restorable tabs in the window
245  */
246 function getClosedTabCount(controller)
247 {
248   return sessionStoreService.getClosedTabCount(controller.window);
249 }
250
251 /**
252  * Restores the tab which has been recently closed
253  *
254  * @param {MozMillController} controller
255  *        MozMillController of the window to operate on
256  * @param {object} event
257  *        Specifies the event to use to execute the command
258  */
259 function undoClosedTab(controller, event)
260 {
261   var count = sessionStoreService.getClosedTabCount(controller.window);
262
263   switch (event.type) {
264     case "menu":
265       throw new Error("Menu gets build dynamically and cannot be accessed.");
266       break;
267     case "shortcut":
268       var cmdKey = utils.getEntity(this.getDtds(), "tabCmd.commandkey");
269       controller.keypress(null, cmdKey, {accelKey: true, shiftKey: true});
270       break;
271   }
272
273   if (count > 0)
274     controller.assertJS("subject.newTabCount < subject.oldTabCount",
275                         {
276                          newTabCount : sessionStoreService.getClosedTabCount(controller.window),
277                          oldTabCount : count
278                         });
279 }
280
281 /**
282  * Restores the window which has been recently closed
283  *
284  * @param {MozMillController} controller
285  *        MozMillController of the window to operate on
286  * @param {object} event
287  *        Specifies the event to use to execute the command
288  */
289 function undoClosedWindow(controller, event)
290 {
291   var count = sessionStoreService.getClosedWindowCount(controller.window);
292
293   switch (event.type) {
294     case "menu":
295       throw new Error("Menu gets build dynamically and cannot be accessed.");
296       break;
297     case "shortcut":
298       var cmdKey = utils.getEntity(this.getDtds(), "newNavigatorCmd.key");
299       controller.keypress(null, cmdKey, {accelKey: true, shiftKey: true});
300       break;
301   }
302
303   if (count > 0)
304     controller.assertJS("subject.newWindowCount < subject.oldWindowCount",
305                         {
306                          newWindowCount : sessionStoreService.getClosedWindowCount(controller.window),
307                          oldWindowCount : count
308                         });
309 }
310
311 // Export of functions
312 exports.getClosedTabCount = getClosedTabCount;
313 exports.resetRecentlyClosedTabs = resetRecentlyClosedTabs;
314 exports.undoClosedTab = undoClosedTab;
315 exports.undoClosedWindow = undoClosedWindow;
316
317 // Export of classes
318 exports.aboutSessionRestore = aboutSessionRestore;