]> git.donarmstrong.com Git - dactyl.git/blob - common/tests/functional/shared-modules/places.js
Initial import of 1.0~b6
[dactyl.git] / common / tests / functional / shared-modules / places.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) 2009
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  *   Henrik Skupin <hskupin@mozilla.com>
22  *   Geo Mealer <gmealer@mozilla.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 ModalDialogAPI adds support for handling modal dialogs. It
41  * has to be used e.g. for alert boxes and other commonDialog instances.
42  *
43  * @version 1.0.2
44  */
45
46 // Include required modules
47 var utils = require("utils");
48
49 const gTimeout = 5000;
50
51 // Default bookmarks.html file lives in omni.jar, get via resource URI
52 const BOOKMARKS_RESOURCE = "resource:///defaults/profile/bookmarks.html";
53
54 // Bookmarks can take up to ten seconds to restore
55 const BOOKMARKS_TIMEOUT = 10000;
56
57 // Observer topics we need to watch to know whether we're finished
58 const TOPIC_BOOKMARKS_RESTORE_SUCCESS = "bookmarks-restore-success";
59
60 /**
61  * Instance of the bookmark service to gain access to the bookmark API.
62  *
63  * @see http://mxr.mozilla.org/mozilla-central (nsINavBookmarksService.idl)
64  */
65 var bookmarksService = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
66                        getService(Ci.nsINavBookmarksService);
67
68 /**
69  * Instance of the history service to gain access to the history API.
70  *
71  * @see http://mxr.mozilla.org/mozilla-central (nsINavHistoryService.idl)
72  */
73 var historyService = Cc["@mozilla.org/browser/nav-history-service;1"].
74                      getService(Ci.nsINavHistoryService);
75
76 /**
77  * Instance of the livemark service to gain access to the livemark API
78  *
79  * @see http://mxr.mozilla.org/mozilla-central (nsILivemarkService.idl)
80  */
81 var livemarkService = Cc["@mozilla.org/browser/livemark-service;2"].
82                       getService(Ci.nsILivemarkService);
83
84 /**
85  * Instance of the browser history interface to gain access to
86  * browser-specific history API
87  *
88  * @see http://mxr.mozilla.org/mozilla-central (nsIBrowserHistory.idl)
89  */
90 var browserHistory = Cc["@mozilla.org/browser/nav-history-service;1"].
91                      getService(Ci.nsIBrowserHistory);
92
93 /**
94  * Instance of the observer service to gain access to the observer API
95  *
96  * @see http://mxr.mozilla.org/mozilla-central (nsIObserverService.idl)
97  */
98 var observerService = Cc["@mozilla.org/observer-service;1"].
99                       getService(Ci.nsIObserverService);
100
101 /**
102  * Check if an URI is bookmarked within the specified folder
103  *
104  * @param (nsIURI) uri
105  *        URI of the bookmark
106  * @param {String} folderId
107  *        Folder in which the search has to take place
108  * @return Returns if the URI exists in the given folder
109  * @type Boolean
110  */
111 function isBookmarkInFolder(uri, folderId)
112 {
113   var ids = bookmarksService.getBookmarkIdsForURI(uri, {});
114   for (let i = 0; i < ids.length; i++) {
115     if (bookmarksService.getFolderIdForItem(ids[i]) == folderId)
116       return true;
117   }
118
119   return false;
120 }
121
122 /**
123  * Restore the default bookmarks for the current profile
124  */
125 function restoreDefaultBookmarks() {
126   // Set up the observer -- we're only checking for success here, so we'll simply
127   // time out and throw on failure. It makes the code much clearer than handling
128   // finished state and success state separately.
129   var importSuccessful = false;
130   var importObserver = {
131     observe: function (aSubject, aTopic, aData) {
132       if (aTopic == TOPIC_BOOKMARKS_RESTORE_SUCCESS) {
133         importSuccessful = true;
134       }
135     }
136   }
137   observerService.addObserver(importObserver, TOPIC_BOOKMARKS_RESTORE_SUCCESS, false);
138
139   try {
140     // Fire off the import
141     var bookmarksURI = utils.createURI(BOOKMARKS_RESOURCE);
142     var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].
143                    getService(Ci.nsIPlacesImportExportService);
144     importer.importHTMLFromURI(bookmarksURI, true);
145
146     // Wait for it to be finished--the observer above will flip this flag
147     mozmill.utils.waitFor(function () {
148       return importSuccessful;
149     }, "Default bookmarks have finished importing", BOOKMARKS_TIMEOUT);
150   }
151   finally {
152     // Whatever happens, remove the observer afterwards
153     observerService.removeObserver(importObserver, TOPIC_BOOKMARKS_RESTORE_SUCCESS);
154   }
155 }
156
157 /**
158  * Synchronous wrapper around browserHistory.removeAllPages()
159  * Removes history and blocks until done
160  */
161 function removeAllHistory() {
162   const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
163
164   // Create flag visible to both the eval and the observer object
165   var finishedFlag = {
166     state: false
167   }
168
169   // Set up an observer so we get notified when remove completes
170   let observer = {
171     observe: function(aSubject, aTopic, aData) {
172       observerService.removeObserver(this, TOPIC_EXPIRATION_FINISHED);
173       finishedFlag.state = true;
174     }
175   }
176   observerService.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
177
178   // Remove the pages, then block until we're done or until timeout is reached
179   browserHistory.removeAllPages();
180   mozmill.controller.waitForEval("subject.state == true", gTimeout, 100, finishedFlag);
181 }
182
183 // Export of variables
184 exports.bookmarksService = bookmarksService;
185 exports.historyService = historyService;
186 exports.livemarkService = livemarkService;
187 exports.browserHistory = browserHistory;
188
189 // Export of functions
190 exports.isBookmarkInFolder = isBookmarkInFolder;
191 exports.restoreDefaultBookmarks = restoreDefaultBookmarks;
192 exports.removeAllHistory = removeAllHistory;