tor-browser

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

test_Chrome_bookmarks.js (6067B)


      1 "use strict";
      2 
      3 const { CustomizableUI } = ChromeUtils.importESModule(
      4  "moz-src:///browser/components/customizableui/CustomizableUI.sys.mjs"
      5 );
      6 
      7 const { PlacesUIUtils } = ChromeUtils.importESModule(
      8  "moz-src:///browser/components/places/PlacesUIUtils.sys.mjs"
      9 );
     10 
     11 let rootDir = do_get_file("chromefiles/", true);
     12 
     13 add_task(async function setup_fakePaths() {
     14  let pathId;
     15  if (AppConstants.platform == "macosx") {
     16    pathId = "ULibDir";
     17  } else if (AppConstants.platform == "win") {
     18    pathId = "LocalAppData";
     19  } else {
     20    pathId = "Home";
     21  }
     22  registerFakePath(pathId, rootDir);
     23 });
     24 
     25 add_task(async function setup_initialBookmarks() {
     26  let bookmarks = [];
     27  for (let i = 0; i < PlacesUIUtils.NUM_TOOLBAR_BOOKMARKS_TO_UNHIDE + 1; i++) {
     28    bookmarks.push({ url: "https://example.com/" + i, title: "" + i });
     29  }
     30 
     31  // Ensure we have enough items in both the menu and toolbar to trip creating a "from" folder.
     32  await PlacesUtils.bookmarks.insertTree({
     33    guid: PlacesUtils.bookmarks.toolbarGuid,
     34    children: bookmarks,
     35  });
     36  await PlacesUtils.bookmarks.insertTree({
     37    guid: PlacesUtils.bookmarks.menuGuid,
     38    children: bookmarks,
     39  });
     40 });
     41 
     42 async function testBookmarks(migratorKey, subDirs) {
     43  if (AppConstants.platform == "macosx") {
     44    subDirs.unshift("Application Support");
     45  } else if (AppConstants.platform == "win") {
     46    subDirs.push("User Data");
     47  } else {
     48    subDirs.unshift(".config");
     49  }
     50 
     51  let target = rootDir.clone();
     52 
     53  // Pretend this is the default profile
     54  while (subDirs.length) {
     55    target.append(subDirs.shift());
     56  }
     57 
     58  let localStatePath = PathUtils.join(target.path, "Local State");
     59  await IOUtils.writeJSON(localStatePath, []);
     60 
     61  target.append("Default");
     62 
     63  await IOUtils.makeDirectory(target.path, {
     64    createAncestor: true,
     65    ignoreExisting: true,
     66  });
     67 
     68  // Copy Favicons database into Default profile
     69  const sourcePath = do_get_file(
     70    "AppData/Local/Google/Chrome/User Data/Default/Favicons"
     71  ).path;
     72  await IOUtils.copy(sourcePath, target.path);
     73 
     74  // Get page url and the image data for each favicon
     75  let favicons = await MigrationUtils.getRowsFromDBWithoutLocks(
     76    sourcePath,
     77    "Chrome Bookmark Favicons",
     78    `SELECT page_url, image_data FROM icon_mapping
     79     INNER JOIN favicon_bitmaps ON (favicon_bitmaps.icon_id = icon_mapping.icon_id)
     80    `
     81  );
     82 
     83  target.append("Bookmarks");
     84  await IOUtils.remove(target.path, { ignoreAbsent: true });
     85 
     86  let bookmarksData = createChromeBookmarkStructure();
     87  await IOUtils.writeJSON(target.path, bookmarksData);
     88 
     89  let migrator = await MigrationUtils.getMigrator(migratorKey);
     90  Assert.ok(await migrator.hasPermissions(), "Has permissions");
     91  // Sanity check for the source.
     92  Assert.ok(await migrator.isSourceAvailable());
     93 
     94  let itemsSeen = { bookmarks: 0, folders: 0 };
     95  let listener = events => {
     96    for (let event of events) {
     97      itemsSeen[
     98        event.itemType == PlacesUtils.bookmarks.TYPE_FOLDER
     99          ? "folders"
    100          : "bookmarks"
    101      ]++;
    102    }
    103  };
    104 
    105  PlacesUtils.observers.addListener(["bookmark-added"], listener);
    106  const PROFILE = {
    107    id: "Default",
    108    name: "Default",
    109  };
    110  let observerNotified = false;
    111  Services.obs.addObserver((aSubject, aTopic, aData) => {
    112    let [toolbar, visibility] = JSON.parse(aData);
    113    Assert.equal(
    114      toolbar,
    115      CustomizableUI.AREA_BOOKMARKS,
    116      "Notification should be received for bookmarks toolbar"
    117    );
    118    Assert.equal(
    119      visibility,
    120      "true",
    121      "Notification should say to reveal the bookmarks toolbar"
    122    );
    123    observerNotified = true;
    124  }, "browser-set-toolbar-visibility");
    125  const initialToolbarCount = await getFolderItemCount(
    126    PlacesUtils.bookmarks.toolbarGuid
    127  );
    128  const initialUnfiledCount = await getFolderItemCount(
    129    PlacesUtils.bookmarks.unfiledGuid
    130  );
    131  const initialmenuCount = await getFolderItemCount(
    132    PlacesUtils.bookmarks.menuGuid
    133  );
    134 
    135  await promiseMigration(
    136    migrator,
    137    MigrationUtils.resourceTypes.BOOKMARKS,
    138    PROFILE
    139  );
    140  const postToolbarCount = await getFolderItemCount(
    141    PlacesUtils.bookmarks.toolbarGuid
    142  );
    143  const postUnfiledCount = await getFolderItemCount(
    144    PlacesUtils.bookmarks.unfiledGuid
    145  );
    146  const postmenuCount = await getFolderItemCount(
    147    PlacesUtils.bookmarks.menuGuid
    148  );
    149 
    150  Assert.equal(
    151    postUnfiledCount - initialUnfiledCount,
    152    210,
    153    "Should have seen 210 items in unsorted bookmarks"
    154  );
    155  Assert.equal(
    156    postToolbarCount - initialToolbarCount,
    157    105,
    158    "Should have seen 105 items in toolbar"
    159  );
    160  Assert.equal(
    161    postmenuCount - initialmenuCount,
    162    0,
    163    "Should have seen 0 items in menu toolbar"
    164  );
    165 
    166  PlacesUtils.observers.removeListener(["bookmark-added"], listener);
    167 
    168  Assert.equal(itemsSeen.bookmarks, 300, "Should have seen 300 bookmarks.");
    169  Assert.equal(itemsSeen.folders, 15, "Should have seen 15 folders.");
    170  Assert.equal(
    171    MigrationUtils._importQuantities.bookmarks,
    172    itemsSeen.bookmarks + itemsSeen.folders,
    173    "Telemetry reporting correct."
    174  );
    175  Assert.ok(observerNotified, "The observer should be notified upon migration");
    176 
    177  for (const favicon of favicons) {
    178    await assertFavicon(
    179      favicon.getResultByName("page_url"),
    180      favicon.getResultByName("image_data"),
    181      "image/png"
    182    );
    183  }
    184 }
    185 
    186 add_task(async function test_Chrome() {
    187  // Expire all favicons before the test to make sure favicons are imported
    188  PlacesUtils.favicons.expireAllFavicons();
    189  let subDirs =
    190    AppConstants.platform == "linux" ? ["google-chrome"] : ["Google", "Chrome"];
    191  await testBookmarks("chrome", subDirs);
    192 });
    193 
    194 add_task(async function test_ChromiumEdge() {
    195  PlacesUtils.favicons.expireAllFavicons();
    196  if (AppConstants.platform == "linux") {
    197    // Edge isn't available on Linux.
    198    return;
    199  }
    200  let subDirs =
    201    AppConstants.platform == "macosx"
    202      ? ["Microsoft Edge"]
    203      : ["Microsoft", "Edge"];
    204  await testBookmarks("chromium-edge", subDirs);
    205 });
    206 
    207 async function getFolderItemCount(guid) {
    208  let results = await PlacesUtils.promiseBookmarksTree(guid);
    209 
    210  return results.itemsCount;
    211 }