tor-browser

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

test_Safari_history.js (2801B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const HISTORY_TEMPLATE_FILE_PATH = "Library/Safari/HistoryTemplate.db";
      7 const HISTORY_FILE_PATH = "Library/Safari/History.db";
      8 
      9 // We want this to be some recent time, so we'll always add some time to our
     10 // dates to keep them ~ five days ago.
     11 const MS_FROM_REFERENCE_TIME =
     12  new Date() - new Date("May 31, 2023 00:00:00 UTC");
     13 
     14 const TEST_URLS = [
     15  {
     16    url: "http://example.com/",
     17    title: "Example Domain",
     18    time: 706743588.04751,
     19    jsTime: 1685050788047 + MS_FROM_REFERENCE_TIME,
     20    visits: 1,
     21  },
     22  {
     23    url: "http://mozilla.org/",
     24    title: "",
     25    time: 706743581.133386,
     26    jsTime: 1685050781133 + MS_FROM_REFERENCE_TIME,
     27    visits: 1,
     28  },
     29  {
     30    url: "https://www.mozilla.org/en-CA/",
     31    title: "Internet for people, not profit - Mozilla",
     32    time: 706743581.133679,
     33    jsTime: 1685050781133 + MS_FROM_REFERENCE_TIME,
     34    visits: 1,
     35  },
     36 ];
     37 
     38 async function setupHistoryFile() {
     39  removeHistoryFile();
     40  let file = do_get_file(HISTORY_TEMPLATE_FILE_PATH);
     41  file.copyTo(file.parent, "History.db");
     42  await updateVisitTimes();
     43 }
     44 
     45 function removeHistoryFile() {
     46  let file = do_get_file(HISTORY_FILE_PATH, true);
     47  try {
     48    file.remove(false);
     49  } catch (ex) {
     50    // It is ok if this doesn't exist.
     51    if (ex.result != Cr.NS_ERROR_FILE_NOT_FOUND) {
     52      throw ex;
     53    }
     54  }
     55 }
     56 
     57 async function updateVisitTimes() {
     58  let cocoaDifference = MS_FROM_REFERENCE_TIME / 1000;
     59  let historyFile = do_get_file(HISTORY_FILE_PATH);
     60  let dbConn = await Sqlite.openConnection({ path: historyFile.path });
     61  await dbConn.execute(
     62    "UPDATE history_visits SET visit_time = visit_time + :difference;",
     63    { difference: cocoaDifference }
     64  );
     65  await dbConn.close();
     66 }
     67 
     68 add_setup(async function setup() {
     69  registerFakePath("ULibDir", do_get_file("Library/"));
     70  await setupHistoryFile();
     71  registerCleanupFunction(async () => {
     72    await PlacesUtils.history.clear();
     73    removeHistoryFile();
     74  });
     75 });
     76 
     77 add_task(async function testHistoryImport() {
     78  await PlacesUtils.history.clear();
     79 
     80  let migrator = await MigrationUtils.getMigrator("safari");
     81  await promiseMigration(migrator, MigrationUtils.resourceTypes.HISTORY);
     82 
     83  for (let urlInfo of TEST_URLS) {
     84    let entry = await PlacesUtils.history.fetch(urlInfo.url, {
     85      includeVisits: true,
     86    });
     87 
     88    Assert.equal(entry.url, urlInfo.url, "Should have the correct URL");
     89    Assert.equal(entry.title, urlInfo.title, "Should have the correct title");
     90    Assert.equal(
     91      entry.visits.length,
     92      urlInfo.visits,
     93      "Should have the correct number of visits"
     94    );
     95    Assert.equal(
     96      entry.visits[0].date.getTime(),
     97      urlInfo.jsTime,
     98      "Should have the correct date"
     99    );
    100  }
    101 });