test_browserGlue_corrupt.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Places Unit Test code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2009
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Marco Bonardo <mak77@bonardo.net>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
44 function run_test() {
45  // Create our bookmarks.html copying bookmarks.glue.html to the profile
46  // folder. It will be ignored.
47  create_bookmarks_html("bookmarks.glue.html");
48 
49  // Create our JSON backup copying bookmarks.glue.json to the profile
50  // folder. It will be ignored.
51  create_JSON_backup("bookmarks.glue.json");
52 
53  // Remove current database file.
54  let db = gProfD.clone();
55  db.append("places.sqlite");
56  if (db.exists()) {
57  db.remove(false);
58  do_check_false(db.exists());
59  }
60  // Create a corrupt database.
61  corruptDB = gTestDir.clone();
62  corruptDB.append("corruptDB.sqlite");
63  corruptDB.copyTo(gProfD, "places.sqlite");
64  do_check_true(db.exists());
65 
66  // Initialize nsBrowserGlue before Places.
67  Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue);
68 
69  // Initialize Places through the History Service.
70  let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
71  getService(Ci.nsINavHistoryService);
72  // Check the database was corrupt.
73  // nsBrowserGlue uses databaseStatus to manage initialization.
74  do_check_eq(hs.databaseStatus, hs.DATABASE_STATUS_CORRUPT);
75 
76  // Restore could take some time, usually less than 1s.
77  // We will poll later in continue_test to be sure restore has finished.
78  do_test_pending();
79  do_timeout(1000, "continue_test();");
80 }
81 
82 function continue_test() {
83  let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
84  getService(Ci.nsINavBookmarksService);
85 
86  if (bs.getIdForItemAt(bs.toolbarFolder, 0) == -1) {
87  // Not enough time to complete restore, poll again later.
88  do_timeout(1000, "continue_test();");
89  return;
90  }
91 
92  // Check that JSON backup has been restored.
93  let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR);
94  do_check_eq(bs.getItemTitle(itemId), "examplejson");
95 
96  do_test_finished();
97 }
function run_test()
const Cc
do_check_eq(typeof PlacesUtils,"object")
function create_bookmarks_html(aFilename)
function continue_test()
const SMART_BOOKMARKS_ON_TOOLBAR
getService(Ci.sbIFaceplateManager)
let gTestDir
function create_JSON_backup(aFilename)
const Ci
var gProfD