tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_browserGlue_prefs.js (5076B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /**
      5 * Tests that nsBrowserGlue is correctly interpreting the preferences settable
      6 * by the user or by other components.
      7 */
      8 
      9 const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML";
     10 const PREF_RESTORE_DEFAULT_BOOKMARKS =
     11  "browser.bookmarks.restore_default_bookmarks";
     12 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
     13 
     14 // Make sure browser glue is initialized, so we can initialize places by
     15 // using the history service.
     16 Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver);
     17 
     18 add_task(async function setup() {
     19  // Create our bookmarks.html from bookmarks.glue.html.
     20  create_bookmarks_html("bookmarks.glue.html");
     21 
     22  remove_all_JSON_backups();
     23 
     24  // Create our JSON backup from bookmarks.glue.json.
     25  create_JSON_backup("bookmarks.glue.json");
     26 
     27  registerCleanupFunction(function () {
     28    remove_bookmarks_html();
     29    remove_all_JSON_backups();
     30 
     31    return PlacesUtils.bookmarks.eraseEverything();
     32  });
     33 });
     34 
     35 function simulatePlacesInit() {
     36  info("Simulate Places init");
     37  let { PlacesBrowserStartup } = ChromeUtils.importESModule(
     38    "moz-src:///browser/components/places/PlacesBrowserStartup.sys.mjs"
     39  );
     40  PlacesBrowserStartup._placesInitialized = false;
     41  PlacesBrowserStartup.initPlaces();
     42  return TestUtils.topicObserved("places-browser-init-complete");
     43 }
     44 
     45 add_task(async function test_checkPreferences() {
     46  // Initialize Places through the History Service and check that a new
     47  // database has been created.
     48  let promiseComplete = promiseTopicObserved("places-browser-init-complete");
     49  Assert.equal(
     50    PlacesUtils.history.databaseStatus,
     51    PlacesUtils.history.DATABASE_STATUS_CREATE
     52  );
     53  await promiseComplete;
     54 
     55  // Ensure preferences status.
     56  Assert.ok(!Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML));
     57 
     58  Assert.throws(
     59    () => Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML),
     60    /NS_ERROR_UNEXPECTED/
     61  );
     62  Assert.throws(
     63    () => Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS),
     64    /NS_ERROR_UNEXPECTED/
     65  );
     66 });
     67 
     68 add_task(async function test_import() {
     69  info("Import from bookmarks.html if importBookmarksHTML is true.");
     70 
     71  await PlacesUtils.bookmarks.eraseEverything();
     72 
     73  // Sanity check: we should not have any bookmark on the toolbar.
     74  Assert.ok(
     75    !(await PlacesUtils.bookmarks.fetch({
     76      parentGuid: PlacesUtils.bookmarks.toolbarGuid,
     77      index: 0,
     78    }))
     79  );
     80 
     81  // Set preferences.
     82  Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
     83 
     84  await simulatePlacesInit();
     85 
     86  // Check bookmarks.html has been imported.
     87  let bm = await PlacesUtils.bookmarks.fetch({
     88    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
     89    index: 0,
     90  });
     91  Assert.equal(bm.title, "example");
     92 
     93  // Check preferences have been reverted.
     94  Assert.ok(!Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
     95 });
     96 
     97 add_task(async function test_restore() {
     98  info(
     99    "restore from default bookmarks.html if " +
    100      "restore_default_bookmarks is true."
    101  );
    102 
    103  await PlacesUtils.bookmarks.eraseEverything();
    104 
    105  // Sanity check: we should not have any bookmark on the toolbar.
    106  Assert.ok(
    107    !(await PlacesUtils.bookmarks.fetch({
    108      parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    109      index: 0,
    110    }))
    111  );
    112 
    113  // Set preferences.
    114  Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
    115 
    116  await simulatePlacesInit();
    117 
    118  // Check default-bookmarks.html has been restored.
    119  let bm = await PlacesUtils.bookmarks.fetch({
    120    parentGuid: PlacesUtils.bookmarks.menuGuid,
    121    index: 0,
    122  });
    123 
    124  let chanTitle = AppConstants.NIGHTLY_BUILD
    125    ? "Firefox Nightly Resources"
    126    : "Mozilla Firefox";
    127  Assert.equal(bm.title, chanTitle, "Default bookmarks folder restored.");
    128 
    129  // Check preferences have been reverted.
    130  Assert.ok(!Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
    131 });
    132 
    133 add_task(async function test_restore_import() {
    134  info(
    135    "setting both importBookmarksHTML and " +
    136      "restore_default_bookmarks should restore defaults."
    137  );
    138 
    139  await PlacesUtils.bookmarks.eraseEverything();
    140 
    141  // Sanity check: we should not have any bookmark on the toolbar.
    142  Assert.ok(
    143    !(await PlacesUtils.bookmarks.fetch({
    144      parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    145      index: 0,
    146    }))
    147  );
    148 
    149  // Set preferences.
    150  Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
    151  Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
    152 
    153  await simulatePlacesInit();
    154 
    155  // Check default-bookmarks.html has been restored.
    156  let bm = await PlacesUtils.bookmarks.fetch({
    157    parentGuid: PlacesUtils.bookmarks.menuGuid,
    158    index: 0,
    159  });
    160 
    161  let chanTitle = AppConstants.NIGHTLY_BUILD
    162    ? "Firefox Nightly Resources"
    163    : "Mozilla Firefox";
    164  Assert.equal(bm.title, chanTitle, "Default bookmarks folder restored.");
    165 
    166  // Check preferences have been reverted.
    167  Assert.ok(!Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
    168  Assert.ok(!Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
    169 });