head_bookmarks.js
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Places.
16  *
17  * The Initial Developer of the Original Code is
18  * Google Inc.
19  * Portions created by the Initial Developer are Copyright (C) 2005
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Brian Ryner <bryner@brianryner.com>
24  * Dietrich Ayala <dietrich@mozilla.com>
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 version(170);
41 
43 const NS_APP_PROFILE_DIR_STARTUP = "ProfDS";
44 const NS_APP_BOOKMARKS_50_FILE = "BMarks";
45 
46 var Ci = Components.interfaces;
47 var Cc = Components.classes;
48 var Cr = Components.results;
49 
50 function LOG(aMsg) {
51  aMsg = ("*** PLACES TESTS: " + aMsg);
52  Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
53  logStringMessage(aMsg);
54  print(aMsg);
55 }
56 
57 var gProfD = do_get_profile();
58 var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
59  getService(Ci.nsIProperties);
60 
61 var dirProvider = {
62  getFile: function(prop, persistent) {
63  persistent.value = true;
64  if (prop == NS_APP_BOOKMARKS_50_FILE) {
65  var bmarks = gProfD.clone();
66  bmarks.append("bookmarks.html");
67  return bmarks;
68  }
69  return null;
70  },
71  QueryInterface: function(iid) {
72  if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
73  iid.equals(Ci.nsISupports)) {
74  return this;
75  }
76  throw Cr.NS_ERROR_NO_INTERFACE;
77  }
78 };
79 dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(dirProvider);
80 
81 var XULAppInfo = {
82  vendor: "Mozilla",
83  name: "PlacesTest",
84  ID: "{230de50e-4cd1-11dc-8314-0800200c9a66}",
85  version: "1",
86  appBuildID: "2007010101",
87  platformVersion: "",
88  platformBuildID: "2007010101",
89  inSafeMode: false,
90  logConsoleErrors: true,
91  OS: "XPCShell",
92  XPCOMABI: "noarch-spidermonkey",
93 
94  QueryInterface: function QueryInterface(iid) {
95  if (iid.equals(Ci.nsIXULAppInfo) ||
96  iid.equals(Ci.nsIXULRuntime) ||
97  iid.equals(Ci.nsISupports))
98  return this;
99  throw Cr.NS_ERROR_NO_INTERFACE;
100  }
101 };
102 
104  createInstance: function (outer, iid) {
105  if (outer != null)
106  throw Cr.NS_ERROR_NO_AGGREGATION;
107  return XULAppInfo.QueryInterface(iid);
108  }
109 };
110 
111 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
112 registrar.registerFactory(Components.ID("{fbfae60b-64a4-44ef-a911-08ceb70b9f31}"),
113  "XULAppInfo", "@mozilla.org/xre/app-info;1",
115 
116 var iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
117 
118 function uri(spec) {
119  return iosvc.newURI(spec, null, null);
120 }
121 
122 /*
123  * Removes all bookmarks and checks for correct cleanup
124  */
126  var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
127  getService(Ci.nsINavBookmarksService);
128  // Clear all bookmarks
129  bs.removeFolderChildren(bs.bookmarksMenuFolder);
130  bs.removeFolderChildren(bs.toolbarFolder);
131  bs.removeFolderChildren(bs.unfiledBookmarksFolder);
132  // Check for correct cleanup
134 }
135 
136 /*
137  * Checks that we don't have any bookmark
138  */
139 function check_no_bookmarks() {
140  var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
141  getService(Ci.nsINavHistoryService);
142  var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
143  getService(Ci.nsINavBookmarksService);
144  var query = hs.getNewQuery();
145  query.setFolders([bs.toolbarFolder, bs.bookmarksMenuFolder, bs.unfiledBookmarksFolder], 3);
146  var options = hs.getNewQueryOptions();
147  options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
148  var result = hs.executeQuery(query, options);
149  var root = result.root;
150  root.containerOpen = true;
151  do_check_eq(root.childCount, 0);
152  root.containerOpen = false;
153 }
154 
155 let gTestDir = do_get_cwd();
156 const FILENAME_BOOKMARKS_HTML = "bookmarks.html";
157 let backup_date = new Date().toLocaleFormat("%Y-%m-%d");
158 const FILENAME_BOOKMARKS_JSON = "bookmarks-" + backup_date + ".json";
159 
160 // Smart bookmarks constants.
163 const SMART_BOOKMARKS_ON_MENU = 3; // Takes in count the additional separator.
164 
165 // Default bookmarks constants.
168 
178 function create_bookmarks_html(aFilename) {
179  if (!aFilename)
180  do_throw("you must pass a filename to create_bookmarks_html function");
182  let bookmarksHTMLFile = gTestDir.clone();
183  bookmarksHTMLFile.append(aFilename);
184  do_check_true(bookmarksHTMLFile.exists());
185  bookmarksHTMLFile.copyTo(gProfD, FILENAME_BOOKMARKS_HTML);
186  let profileBookmarksHTMLFile = gProfD.clone();
187  profileBookmarksHTMLFile.append(FILENAME_BOOKMARKS_HTML);
188  do_check_true(profileBookmarksHTMLFile.exists());
189  return profileBookmarksHTMLFile;
190 }
191 
196  let profileBookmarksHTMLFile = gProfD.clone();
197  profileBookmarksHTMLFile.append(FILENAME_BOOKMARKS_HTML);
198  if (profileBookmarksHTMLFile.exists()) {
199  profileBookmarksHTMLFile.remove(false);
200  do_check_false(profileBookmarksHTMLFile.exists());
201  }
202 }
203 
210  let profileBookmarksHTMLFile = gProfD.clone();
211  profileBookmarksHTMLFile.append(FILENAME_BOOKMARKS_HTML);
212  do_check_true(profileBookmarksHTMLFile.exists());
213  return profileBookmarksHTMLFile;
214 }
215 
225 function create_JSON_backup(aFilename) {
226  if (!aFilename)
227  do_throw("you must pass a filename to create_JSON_backup function");
229  let bookmarksBackupDir = gProfD.clone();
230  bookmarksBackupDir.append("bookmarkbackups");
231  if (!bookmarksBackupDir.exists()) {
232  bookmarksBackupDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0777);
233  do_check_true(bookmarksBackupDir.exists());
234  }
235  let bookmarksJSONFile = gTestDir.clone();
236  bookmarksJSONFile.append(aFilename);
237  do_check_true(bookmarksJSONFile.exists());
238  bookmarksJSONFile.copyTo(bookmarksBackupDir, FILENAME_BOOKMARKS_JSON);
239  let profileBookmarksJSONFile = bookmarksBackupDir.clone();
240  profileBookmarksJSONFile.append(FILENAME_BOOKMARKS_JSON);
241  do_check_true(profileBookmarksJSONFile.exists());
242  return profileBookmarksJSONFile;
243 }
244 
249  let bookmarksBackupDir = gProfD.clone();
250  bookmarksBackupDir.append("bookmarkbackups");
251  if (bookmarksBackupDir.exists()) {
252  bookmarksBackupDir.remove(true);
253  do_check_false(bookmarksBackupDir.exists());
254  }
255 }
256 
262 function check_JSON_backup() {
263  let profileBookmarksJSONFile = gProfD.clone();
264  profileBookmarksJSONFile.append("bookmarkbackups");
265  profileBookmarksJSONFile.append(FILENAME_BOOKMARKS_JSON);
266  do_check_true(profileBookmarksJSONFile.exists());
267  return profileBookmarksJSONFile;
268 }
269 
276 function dump_table(aName)
277 {
278  let db = Cc["@mozilla.org/browser/nav-history-service;1"].
279  getService(Ci.nsPIPlacesDatabase).
280  DBConnection;
281  let stmt = db.createStatement("SELECT * FROM " + aName);
282 
283  dump("\n*** Printing data from " + aName + ":\n");
284  let count = 0;
285  while (stmt.executeStep()) {
286  let columns = stmt.numEntries;
287 
288  if (count == 0) {
289  // print the column names
290  for (let i = 0; i < columns; i++)
291  dump(stmt.getColumnName(i) + "\t");
292  dump("\n");
293  }
294 
295  // print the row
296  for (let i = 0; i < columns; i++) {
297  switch (stmt.getTypeOfIndex(i)) {
298  case Ci.mozIStorageValueArray.VALUE_TYPE_NULL:
299  dump("NULL\t");
300  break;
301  case Ci.mozIStorageValueArray.VALUE_TYPE_INTEGER:
302  dump(stmt.getInt64(i) + "\t");
303  break;
304  case Ci.mozIStorageValueArray.VALUE_TYPE_FLOAT:
305  dump(stmt.getDouble(i) + "\t");
306  break;
307  case Ci.mozIStorageValueArray.VALUE_TYPE_TEXT:
308  dump(stmt.getString(i) + "\t");
309  break;
310  }
311  }
312  dump("\n");
313 
314  count++;
315  }
316  dump("*** There were a total of " + count + " rows of data.\n\n");
317 
318  stmt.reset();
319  stmt.finalize();
320  stmt = null;
321 }
322 
327 {
328  let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
329  while (tm.mainThread.hasPendingEvents())
330  tm.mainThread.processNextEvent(false);
331 }
const NS_APP_USER_PROFILE_50_DIR
do_check_eq(typeof PlacesUtils,"object")
var registrar
var dirSvc
const SMART_BOOKMARKS_VERSION
function check_no_bookmarks()
const FILENAME_BOOKMARKS_JSON
const DEFAULT_BOOKMARKS_ON_TOOLBAR
var XULAppInfo
var dirProvider
let backup_date
function create_bookmarks_html(aFilename)
const FILENAME_BOOKMARKS_HTML
sidebarFactory createInstance
Definition: nsSidebar.js:351
const DEFAULT_BOOKMARKS_ON_MENU
function remove_all_bookmarks()
version(170)
const NS_APP_BOOKMARKS_50_FILE
var Ci
const SMART_BOOKMARKS_ON_TOOLBAR
getService(Ci.sbIFaceplateManager)
function flush_main_thread_events()
function getFile(fileName)
function uri(spec)
let gTestDir
var count
Definition: test_bug7406.js:32
var Cr
var columns
dirSvc QueryInterface(Ci.nsIDirectoryService).registerProvider(dirProvider)
function dump_table(aName)
return null
Definition: FeedWriter.js:1143
_updateCookies aName
function check_JSON_backup()
function remove_all_JSON_backups()
var gProfD
function create_JSON_backup(aFilename)
const NS_APP_PROFILE_DIR_STARTUP
foldersync options
Definition: options.js:13
function remove_bookmarks_html()
const SMART_BOOKMARKS_ON_MENU
function LOG(aMsg)
var XULAppInfoFactory
var iosvc
function check_bookmarks_html()
_getSelectedPageStyle s i
var Cc