test_browserGlue_shutdown.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 // Initialize nsBrowserGlue.
45 let bg = Cc["@mozilla.org/browser/browserglue;1"].
46  getService(Ci.nsIBrowserGlue);
47 
48 // Initialize Places through Bookmarks Service.
49 let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
50  getService(Ci.nsINavBookmarksService);
51 
52 // Get other services.
53 let ps = Cc["@mozilla.org/preferences-service;1"].
54  getService(Ci.nsIPrefBranch);
55 let os = Cc["@mozilla.org/observer-service;1"].
56  getService(Ci.nsIObserverService);
57 
58 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
59 
60 const TOPIC_QUIT_APPLICATION_GRANTED = "quit-application-granted";
61 
62 let tests = [];
63 
64 //------------------------------------------------------------------------------
65 
66 tests.push({
67  description: "Export to bookmarks.html if autoExportHTML is true.",
68  exec: function() {
69  // Sanity check: we should have bookmarks on the toolbar.
70  do_check_true(bs.getIdForItemAt(bs.toolbarFolder, 0) > 0);
71  // Set preferences.
72  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
73  // Force nsBrowserGlue::_shutdownPlaces().
74  os.notifyObservers(null, TOPIC_QUIT_APPLICATION_GRANTED, null);
75 
76  // Check bookmarks.html has been created.
78  // Check JSON backup has been created.
80 
81  // Check preferences have not been reverted.
82  do_check_true(ps.getBoolPref(PREF_AUTO_EXPORT_HTML));
83  // Reset preferences.
84  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
85 
86  next_test();
87  }
88 });
89 
90 //------------------------------------------------------------------------------
91 
92 tests.push({
93  description: "Export to bookmarks.html if autoExportHTML is true and a bookmarks.html exists.",
94  exec: function() {
95  // Sanity check: we should have bookmarks on the toolbar.
96  do_check_true(bs.getIdForItemAt(bs.toolbarFolder, 0) > 0);
97  // Setpreferences.
98  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
99  // Create a bookmarks.html in the profile.
100  let profileBookmarksHTMLFile = create_bookmarks_html("bookmarks.glue.html");
101  // Get file lastModified and size.
102  let lastMod = profileBookmarksHTMLFile.lastModifiedTime;
103  let fileSize = profileBookmarksHTMLFile.fileSize;
104  // Force nsBrowserGlue::_shutdownPlaces().
105  os.notifyObservers(null, TOPIC_QUIT_APPLICATION_GRANTED, null);
106 
107  // Check a new bookmarks.html has been created.
108  let profileBookmarksHTMLFile = check_bookmarks_html();
109  //XXX not working on Linux unit boxes. Could be filestats caching issue.
110  //do_check_true(profileBookmarksHTMLFile.lastModifiedTime > lastMod);
111  do_check_neq(profileBookmarksHTMLFile.fileSize, fileSize);
112 
113  // Check preferences have not been reverted.
114  do_check_true(ps.getBoolPref(PREF_AUTO_EXPORT_HTML));
115  // Reset preferences.
116  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
117 
118  next_test();
119  }
120 });
121 
122 //------------------------------------------------------------------------------
123 
124 tests.push({
125  description: "Backup to JSON should be a no-op if a backup for today already exists.",
126  exec: function() {
127  // Sanity check: we should have bookmarks on the toolbar.
128  do_check_true(bs.getIdForItemAt(bs.toolbarFolder, 0) > 0);
129  // Create a JSON backup in the profile.
130  let profileBookmarksJSONFile = create_JSON_backup("bookmarks.glue.json");
131  // Get file lastModified and size.
132  let lastMod = profileBookmarksJSONFile.lastModifiedTime;
133  let fileSize = profileBookmarksJSONFile.fileSize;
134  // Force nsBrowserGlue::_shutdownPlaces().
135  os.notifyObservers(null, TOPIC_QUIT_APPLICATION_GRANTED, null);
136 
137  // Check a new JSON backup has not been created.
138  do_check_true(profileBookmarksJSONFile.exists());
139  do_check_eq(profileBookmarksJSONFile.lastModifiedTime, lastMod);
140  do_check_eq(profileBookmarksJSONFile.fileSize, fileSize);
141 
142  finish_test();
143  }
144 });
145 
146 //------------------------------------------------------------------------------
147 
148 function finish_test() {
149  do_test_finished();
150 }
151 
152 var testIndex = 0;
153 function next_test() {
154  // Remove bookmarks.html from profile.
156  // Remove JSON backups from profile.
158 
159  // Execute next test.
160  let test = tests.shift();
161  dump("\nTEST " + (++testIndex) + ": " + test.description);
162  test.exec();
163 }
164 
165 function run_test() {
166  // Clean up bookmarks.
168 
169  // Create some bookmarks.
170  bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://mozilla.org/"),
171  bs.DEFAULT_INDEX, "bookmark-on-menu");
172  bs.insertBookmark(bs.toolbarFolder, uri("http://mozilla.org/"),
173  bs.DEFAULT_INDEX, "bookmark-on-toolbar");
174 
175  // Kick-off tests.
176  do_test_pending();
177  next_test();
178 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
const TOPIC_QUIT_APPLICATION_GRANTED
const PREF_AUTO_EXPORT_HTML
function create_bookmarks_html(aFilename)
function remove_all_bookmarks()
getService(Ci.sbIFaceplateManager)
function finish_test()
return null
Definition: FeedWriter.js:1143
function next_test()
function check_JSON_backup()
function remove_all_JSON_backups()
var uri
Definition: FeedWriter.js:1135
function create_JSON_backup(aFilename)
function run_test()
function remove_bookmarks_html()
const Ci
function check_bookmarks_html()