tor-browser

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

test_BookmarksFileMigrator.js (3796B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { BookmarksFileMigrator } = ChromeUtils.importESModule(
      7  "resource:///modules/FileMigrators.sys.mjs"
      8 );
      9 
     10 const { MigrationWizardConstants } = ChromeUtils.importESModule(
     11  "chrome://browser/content/migration/migration-wizard-constants.mjs"
     12 );
     13 
     14 /**
     15 * Tests that the BookmarksFileMigrator properly subclasses FileMigratorBase
     16 * and delegates to BookmarkHTMLUtils or BookmarkJSONUtils.
     17 *
     18 * Normally, we'd override the BookmarkHTMLUtils and BookmarkJSONUtils methods
     19 * in our test here so that we just ensure that they're called with the
     20 * right arguments, rather than testing their behaviour. Unfortunately, both
     21 * BookmarkHTMLUtils and BookmarkJSONUtils are frozen with Object.freeze, which
     22 * prevents sinon from stubbing out any of their methods. Rather than unfreezing
     23 * those objects just for testing, we test the whole flow end-to-end, including
     24 * the import to Places.
     25 */
     26 
     27 add_setup(() => {
     28  Services.prefs.setBoolPref("browser.migrate.bookmarks-file.enabled", true);
     29  registerCleanupFunction(async () => {
     30    await PlacesUtils.bookmarks.eraseEverything();
     31    Services.prefs.clearUserPref("browser.migrate.bookmarks-file.enabled");
     32  });
     33 });
     34 
     35 /**
     36 * First check that the BookmarksFileMigrator implements the required parts
     37 * of the parent class.
     38 */
     39 add_task(async function test_BookmarksFileMigrator_members() {
     40  let migrator = new BookmarksFileMigrator();
     41  Assert.ok(
     42    migrator.constructor.key,
     43    `BookmarksFileMigrator implements static getter 'key'`
     44  );
     45 
     46  Assert.ok(
     47    migrator.constructor.displayNameL10nID,
     48    `BookmarksFileMigrator implements static getter 'displayNameL10nID'`
     49  );
     50 
     51  Assert.ok(
     52    migrator.constructor.brandImage,
     53    `BookmarksFileMigrator implements static getter 'brandImage'`
     54  );
     55 
     56  Assert.ok(
     57    migrator.progressHeaderL10nID,
     58    `BookmarksFileMigrator implements getter 'progressHeaderL10nID'`
     59  );
     60 
     61  Assert.ok(
     62    migrator.successHeaderL10nID,
     63    `BookmarksFileMigrator implements getter 'successHeaderL10nID'`
     64  );
     65 
     66  Assert.ok(
     67    await migrator.getFilePickerConfig(),
     68    `BookmarksFileMigrator implements method 'getFilePickerConfig'`
     69  );
     70 
     71  Assert.ok(
     72    migrator.displayedResourceTypes,
     73    `BookmarksFileMigrator implements getter 'displayedResourceTypes'`
     74  );
     75 
     76  Assert.ok(migrator.enabled, `BookmarksFileMigrator is enabled`);
     77 });
     78 
     79 add_task(async function test_BookmarksFileMigrator_HTML() {
     80  let migrator = new BookmarksFileMigrator();
     81  const EXPECTED_SUCCESS_STATE = {
     82    [MigrationWizardConstants.DISPLAYED_FILE_RESOURCE_TYPES
     83      .BOOKMARKS_FROM_FILE]: "8 bookmarks",
     84  };
     85 
     86  const BOOKMARKS_PATH = PathUtils.join(
     87    do_get_cwd().path,
     88    "bookmarks.exported.html"
     89  );
     90 
     91  let result = await migrator.migrate(BOOKMARKS_PATH);
     92 
     93  Assert.deepEqual(
     94    result,
     95    EXPECTED_SUCCESS_STATE,
     96    "Returned the expected success state."
     97  );
     98 });
     99 
    100 add_task(async function test_BookmarksFileMigrator_JSON() {
    101  let migrator = new BookmarksFileMigrator();
    102 
    103  const EXPECTED_SUCCESS_STATE = {
    104    [MigrationWizardConstants.DISPLAYED_FILE_RESOURCE_TYPES
    105      .BOOKMARKS_FROM_FILE]: "10 bookmarks",
    106  };
    107 
    108  const BOOKMARKS_PATH = PathUtils.join(
    109    do_get_cwd().path,
    110    "bookmarks.exported.json"
    111  );
    112 
    113  let result = await migrator.migrate(BOOKMARKS_PATH);
    114 
    115  Assert.deepEqual(
    116    result,
    117    EXPECTED_SUCCESS_STATE,
    118    "Returned the expected success state."
    119  );
    120 });
    121 
    122 add_task(async function test_BookmarksFileMigrator_invalid() {
    123  let migrator = new BookmarksFileMigrator();
    124 
    125  const INVALID_FILE_PATH = PathUtils.join(
    126    do_get_cwd().path,
    127    "bookmarks.invalid.html"
    128  );
    129 
    130  await Assert.rejects(
    131    migrator.migrate(INVALID_FILE_PATH),
    132    /Pick another file/
    133  );
    134 });