tor-browser

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

test_quicksuggest_migrateBlockedDigests.js (3595B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // Tests migration of the `quicksuggest.blockedDigests` pref to dismissal keys
      6 // in the Rust component.
      7 
      8 "use strict";
      9 
     10 const PREF = "browser.urlbar.quicksuggest.blockedDigests";
     11 
     12 add_setup(async function () {
     13  await QuickSuggestTestUtils.ensureQuickSuggestInit();
     14 
     15  Assert.ok(
     16    !Services.prefs.prefHasDefaultValue(PREF),
     17    "Sanity check: Pref should not have a default-branch value"
     18  );
     19 });
     20 
     21 add_task(async function notDefined() {
     22  await doTest(null, []);
     23 });
     24 
     25 add_task(async function emptyString() {
     26  await doTest("", []);
     27 });
     28 
     29 add_task(async function invalidJson1() {
     30  await doTest("bogus", []);
     31 });
     32 
     33 add_task(async function invalidJson2() {
     34  await doTest("{", []);
     35 });
     36 
     37 add_task(async function falsey() {
     38  await doTest(JSON.stringify(null), []);
     39 });
     40 
     41 add_task(async function notAnArray1() {
     42  await doTest(JSON.stringify(123), []);
     43 });
     44 
     45 add_task(async function notAnArray2() {
     46  await doTest(JSON.stringify({}), []);
     47 });
     48 
     49 add_task(async function notAnArrayOfStrings() {
     50  await doTest(JSON.stringify([123, 456]), []);
     51 });
     52 
     53 add_task(async function oneKey() {
     54  // The string values in the array don't actually need to be digests, so don't
     55  // bother generating actual digests.
     56  let keys = ["aaa"];
     57  await doTest(JSON.stringify(keys), keys);
     58 });
     59 
     60 add_task(async function manyKeys() {
     61  let keys = ["aaa", "bbb", "ccc", "ddd", "eee"];
     62  await doTest(JSON.stringify(keys), keys);
     63 });
     64 
     65 add_task(async function manyKeysSomeInvalid() {
     66  await doTest(JSON.stringify(["aaa", 123, "bbb", 456, "ccc"]), [
     67    "aaa",
     68    "bbb",
     69    "ccc",
     70  ]);
     71 });
     72 
     73 add_task(async function keysAlreadyPresent() {
     74  await QuickSuggest.rustBackend.dismissByKey("yyy");
     75  await QuickSuggest.rustBackend.dismissByKey("zzz");
     76  await doTest(JSON.stringify(["aaa", "bbb", "ccc"]), [
     77    "aaa",
     78    "bbb",
     79    "ccc",
     80    "yyy",
     81    "zzz",
     82  ]);
     83 });
     84 
     85 async function doTest(prefValue, expectedDismissalKeys) {
     86  let { rustBackend } = QuickSuggest;
     87 
     88  // Disable the backend.
     89  rustBackend.enable(false);
     90 
     91  // Set the pref.
     92  if (prefValue === null) {
     93    Services.prefs.clearUserPref(PREF);
     94    Assert.ok(
     95      !Services.prefs.prefHasUserValue(PREF),
     96      "Sanity check: Pref should not have user value after clearing it"
     97    );
     98  } else {
     99    Services.prefs.setCharPref(PREF, prefValue);
    100    Assert.ok(
    101      Services.prefs.prefHasUserValue(PREF),
    102      "Sanity check: Pref should have user value after setting it"
    103    );
    104  }
    105 
    106  // Enable the backend, which will trigger migration.
    107  rustBackend.enable(true);
    108 
    109  // This test doesn't ingest anything but make sure the backend isn't doing any
    110  // async activity past this point.
    111  await rustBackend._test_ingest();
    112 
    113  await TestUtils.waitForCondition(
    114    () => !Services.prefs.prefHasUserValue(PREF),
    115    "Waiting for the Rust backend to complete the dismissals migration"
    116  );
    117  if (Services.prefs.prefHasUserValue(PREF)) {
    118    Assert.ok(false, "The backend should have completed the migration");
    119    return;
    120  }
    121 
    122  // Check the newly recorded dismissal keys.
    123  Assert.equal(
    124    await rustBackend.anyDismissedSuggestions(),
    125    !!expectedDismissalKeys.length,
    126    "anyDismissedSuggestions should match expectedDismissalKeys"
    127  );
    128  for (let key of expectedDismissalKeys) {
    129    Assert.ok(
    130      await rustBackend.isDismissedByKey(key),
    131      "Dismissal key should be recorded: " + key
    132    );
    133  }
    134 
    135  await rustBackend.clearDismissedSuggestions();
    136 }