browser/components/places/tests/unit/test_browserGlue_prefs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 /**
     5  * Tests that nsBrowserGlue is correctly interpreting the preferences settable
     6  * by the user or by other components.
     7  */
     9 const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML";
    10 const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks";
    11 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
    12 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
    14 const TOPIC_BROWSERGLUE_TEST = "browser-glue-test";
    15 const TOPICDATA_FORCE_PLACES_INIT = "force-places-init";
    17 let bg = Cc["@mozilla.org/browser/browserglue;1"].
    18          getService(Ci.nsIBrowserGlue);
    20 function waitForImportAndSmartBookmarks(aCallback) {
    21   Services.obs.addObserver(function waitImport() {
    22     Services.obs.removeObserver(waitImport, "bookmarks-restore-success");
    23     // Delay to test eventual smart bookmarks creation.
    24     do_execute_soon(function () {
    25       promiseAsyncUpdates().then(aCallback);
    26     });
    27   }, "bookmarks-restore-success", false);
    28 }
    30 [
    32   // This test must be the first one.
    33   function test_checkPreferences() {
    34     // Initialize Places through the History Service and check that a new
    35     // database has been created.
    36     do_check_eq(PlacesUtils.history.databaseStatus,
    37                 PlacesUtils.history.DATABASE_STATUS_CREATE);
    39     // Wait for Places init notification.
    40     Services.obs.addObserver(function(aSubject, aTopic, aData) {
    41       Services.obs.removeObserver(arguments.callee,
    42                                   "places-browser-init-complete");
    43       do_execute_soon(function () {
    44         // Ensure preferences status.
    45         do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML));
    47         try {
    48           do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
    49           do_throw("importBookmarksHTML pref should not exist");
    50         }
    51         catch(ex) {}
    53         try {
    54           do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
    55           do_throw("importBookmarksHTML pref should not exist");
    56         }
    57         catch(ex) {}
    59         run_next_test();
    60       });
    61     }, "places-browser-init-complete", false);
    62   },
    64   function test_import()
    65   {
    66     do_log_info("Import from bookmarks.html if importBookmarksHTML is true.");
    68     remove_all_bookmarks();
    69     // Sanity check: we should not have any bookmark on the toolbar.
    70     let itemId =
    71       PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
    72     do_check_eq(itemId, -1);
    74     // Set preferences.
    75     Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
    77     waitForImportAndSmartBookmarks(function () {
    78       // Check bookmarks.html has been imported, and a smart bookmark has been
    79       // created.
    80       itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
    81                                                     SMART_BOOKMARKS_ON_TOOLBAR);
    82       do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
    83       // Check preferences have been reverted.
    84       do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
    86       run_next_test();
    87     });
    88     // Force nsBrowserGlue::_initPlaces().
    89     do_log_info("Simulate Places init");
    90     bg.QueryInterface(Ci.nsIObserver).observe(null,
    91                                               TOPIC_BROWSERGLUE_TEST,
    92                                               TOPICDATA_FORCE_PLACES_INIT);
    93   },
    95   function test_import_noSmartBookmarks()
    96   {
    97     do_log_info("import from bookmarks.html, but don't create smart bookmarks \
    98                  if they are disabled");
   100     remove_all_bookmarks();
   101     // Sanity check: we should not have any bookmark on the toolbar.
   102     let itemId =
   103       PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   104     do_check_eq(itemId, -1);
   106     // Set preferences.
   107     Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1);
   108     Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
   110     waitForImportAndSmartBookmarks(function () {
   111       // Check bookmarks.html has been imported, but smart bookmarks have not
   112       // been created.
   113       itemId =
   114         PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   115       do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
   116       // Check preferences have been reverted.
   117       do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
   119       run_next_test();
   120     });
   121     // Force nsBrowserGlue::_initPlaces().
   122     do_log_info("Simulate Places init");
   123     bg.QueryInterface(Ci.nsIObserver).observe(null,
   124                                               TOPIC_BROWSERGLUE_TEST,
   125                                               TOPICDATA_FORCE_PLACES_INIT);
   126   },
   128   function test_import_autoExport_updatedSmartBookmarks()
   129   {
   130     do_log_info("Import from bookmarks.html, but don't create smart bookmarks \
   131                  if autoExportHTML is true and they are at latest version");
   133     remove_all_bookmarks();
   134     // Sanity check: we should not have any bookmark on the toolbar.
   135     let itemId =
   136       PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   137     do_check_eq(itemId, -1);
   139     // Set preferences.
   140     Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999);
   141     Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
   142     Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
   144     waitForImportAndSmartBookmarks(function () {
   145       // Check bookmarks.html has been imported, but smart bookmarks have not
   146       // been created.
   147       itemId =
   148         PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   149       do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
   150       do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
   151       // Check preferences have been reverted.
   152       Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
   154       run_next_test();
   155     });
   156     // Force nsBrowserGlue::_initPlaces()
   157     do_log_info("Simulate Places init");
   158     bg.QueryInterface(Ci.nsIObserver).observe(null,
   159                                               TOPIC_BROWSERGLUE_TEST,
   160                                               TOPICDATA_FORCE_PLACES_INIT);
   161   },
   163   function test_import_autoExport_oldSmartBookmarks()
   164   {
   165     do_log_info("Import from bookmarks.html, and create smart bookmarks if \
   166                  autoExportHTML is true and they are not at latest version.");
   168     remove_all_bookmarks();
   169     // Sanity check: we should not have any bookmark on the toolbar.
   170     let itemId =
   171       PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   172     do_check_eq(itemId, -1);
   174     // Set preferences.
   175     Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
   176     Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
   177     Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
   179     waitForImportAndSmartBookmarks(function () {
   180       // Check bookmarks.html has been imported, but smart bookmarks have not
   181       // been created.
   182       itemId =
   183         PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
   184                                              SMART_BOOKMARKS_ON_TOOLBAR);
   185       do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
   186       do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
   187       // Check preferences have been reverted.
   188       Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
   190       run_next_test();
   191     });
   192     // Force nsBrowserGlue::_initPlaces()
   193     do_log_info("Simulate Places init");
   194     bg.QueryInterface(Ci.nsIObserver).observe(null,
   195                                               TOPIC_BROWSERGLUE_TEST,
   196                                               TOPICDATA_FORCE_PLACES_INIT);
   197   },
   199   function test_restore()
   200   {
   201     do_log_info("restore from default bookmarks.html if \
   202                  restore_default_bookmarks is true.");
   204     remove_all_bookmarks();
   205     // Sanity check: we should not have any bookmark on the toolbar.
   206     let itemId =
   207       PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   208     do_check_eq(itemId, -1);
   210     // Set preferences.
   211     Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
   213     waitForImportAndSmartBookmarks(function () {
   214       // Check bookmarks.html has been restored.
   215       itemId =
   216         PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
   217                                              SMART_BOOKMARKS_ON_TOOLBAR);
   218       do_check_true(itemId > 0);
   219       // Check preferences have been reverted.
   220       do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
   222       run_next_test();
   223     });
   224     // Force nsBrowserGlue::_initPlaces()
   225     do_log_info("Simulate Places init");
   226     bg.QueryInterface(Ci.nsIObserver).observe(null,
   227                                               TOPIC_BROWSERGLUE_TEST,
   228                                               TOPICDATA_FORCE_PLACES_INIT);
   230   },
   232   function test_restore_import()
   233   {
   234     do_log_info("setting both importBookmarksHTML and \
   235                  restore_default_bookmarks should restore defaults.");
   237     remove_all_bookmarks();
   238     // Sanity check: we should not have any bookmark on the toolbar.
   239     let itemId =
   240       PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   241     do_check_eq(itemId, -1);
   243     // Set preferences.
   244     Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
   245     Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
   247     waitForImportAndSmartBookmarks(function () {
   248       // Check bookmarks.html has been restored.
   249       itemId =
   250         PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
   251                                              SMART_BOOKMARKS_ON_TOOLBAR);
   252       do_check_true(itemId > 0);
   253       // Check preferences have been reverted.
   254       do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
   255       do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
   257       run_next_test();
   258     });
   259     // Force nsBrowserGlue::_initPlaces()
   260     do_log_info("Simulate Places init");
   261     bg.QueryInterface(Ci.nsIObserver).observe(null,
   262                                               TOPIC_BROWSERGLUE_TEST,
   263                                               TOPICDATA_FORCE_PLACES_INIT);
   264   }
   266 ].forEach(add_test);
   268 do_register_cleanup(function () {
   269   remove_all_bookmarks();
   270   remove_bookmarks_html();
   271   remove_all_JSON_backups();
   272 });
   274 function run_test()
   275 {
   276   // Create our bookmarks.html from bookmarks.glue.html.
   277   create_bookmarks_html("bookmarks.glue.html");
   278   remove_all_JSON_backups();
   279   // Create our JSON backup from bookmarks.glue.json.
   280   create_JSON_backup("bookmarks.glue.json");
   282   run_next_test();
   283 }

mercurial