tor-browser

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

browser_privacy_passwordGenerationAndAutofill.js (12888B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 add_task(async function initialState() {
      5  await SpecialPowers.pushPrefEnv({
      6    set: [["browser.settings-redesign.enabled", false]],
      7  });
      8  // check pref permutations to verify the UI opens in the correct state
      9  const prefTests = [
     10    {
     11      initialPrefs: [
     12        ["signon.rememberSignons", true],
     13        ["signon.generation.available", true],
     14        ["signon.generation.enabled", true],
     15        ["signon.autofillForms", true],
     16      ],
     17      expected: "checked",
     18    },
     19    {
     20      initialPrefs: [
     21        ["signon.rememberSignons", true],
     22        ["signon.generation.available", true],
     23        ["signon.generation.enabled", false],
     24        ["signon.autofillForms", false],
     25      ],
     26      expected: "unchecked",
     27    },
     28    {
     29      initialPrefs: [
     30        ["signon.rememberSignons", true],
     31        ["signon.generation.available", false],
     32        ["signon.generation.enabled", false],
     33      ],
     34      expected: "hidden",
     35    },
     36    {
     37      initialPrefs: [
     38        ["signon.rememberSignons", false],
     39        ["signon.generation.available", true],
     40        ["signon.generation.enabled", true],
     41        ["signon.autofillForms", true],
     42      ],
     43      expected: "disabled",
     44    },
     45  ];
     46  for (let test of prefTests) {
     47    // set initial pref values
     48    info("initialState, testing with: " + JSON.stringify(test));
     49    await SpecialPowers.pushPrefEnv({ set: test.initialPrefs });
     50 
     51    // open about:privacy in a tab
     52    // verify expected conditions
     53    await BrowserTestUtils.withNewTab(
     54      {
     55        gBrowser,
     56        url: "about:preferences#privacy",
     57      },
     58      async function (browser) {
     59        let doc = browser.contentDocument;
     60        let generatePasswordsCheckbox = doc.getElementById("generatePasswords");
     61        let autofillFormsCheckbox = doc.getElementById(
     62          "passwordAutofillCheckbox"
     63        );
     64        doc.getElementById("passwordSettings").scrollIntoView();
     65 
     66        info("initialState, assert on expected state:" + test.expected);
     67        switch (test.expected) {
     68          case "hidden":
     69            is_element_hidden(
     70              generatePasswordsCheckbox,
     71              "#generatePasswords checkbox is hidden"
     72            );
     73            break;
     74          case "checked":
     75            is_element_visible(
     76              generatePasswordsCheckbox,
     77              "#generatePasswords checkbox is visible"
     78            );
     79            ok(
     80              generatePasswordsCheckbox.checked,
     81              "#generatePasswords checkbox is checked"
     82            );
     83            ok(
     84              autofillFormsCheckbox.checked,
     85              "#passwordAutofillCheckbox is checked"
     86            );
     87            break;
     88          case "unchecked":
     89            ok(
     90              !generatePasswordsCheckbox.checked,
     91              "#generatePasswords checkbox is un-checked"
     92            );
     93            ok(
     94              !autofillFormsCheckbox.checked,
     95              "#passwordAutofillCheckbox is un-checked"
     96            );
     97            break;
     98          case "disabled":
     99            ok(
    100              generatePasswordsCheckbox.disabled,
    101              "#generatePasswords checkbox is disabled"
    102            );
    103            ok(
    104              autofillFormsCheckbox.disabled,
    105              "#passwordAutofillCheckbox is disabled"
    106            );
    107            break;
    108          default:
    109            ok(false, "Unknown expected state: " + test.expected);
    110        }
    111      }
    112    );
    113    await SpecialPowers.popPrefEnv();
    114  }
    115 });
    116 
    117 add_task(async function toggleGenerationEnabled() {
    118  await SpecialPowers.pushPrefEnv({
    119    set: [["browser.settings-redesign.enabled", false]],
    120  });
    121  // clicking the checkbox should toggle the pref
    122  SpecialPowers.pushPrefEnv({
    123    set: [
    124      ["signon.generation.available", true],
    125      ["signon.generation.enabled", false],
    126      ["signon.rememberSignons", true],
    127    ],
    128  });
    129 
    130  await BrowserTestUtils.withNewTab(
    131    {
    132      gBrowser,
    133      url: "about:preferences#privacy",
    134    },
    135    async function (browser) {
    136      let doc = browser.contentDocument;
    137      let checkbox = doc.getElementById("generatePasswords");
    138 
    139      info("waiting for the browser to have focus");
    140      await SimpleTest.promiseFocus(browser);
    141      let prefChanged = TestUtils.waitForPrefChange(
    142        "signon.generation.enabled"
    143      );
    144 
    145      // the preferences "Search" bar obscures the checkbox if we scrollIntoView and try to click on it
    146      // so use keyboard events instead
    147      checkbox.focus();
    148      is(doc.activeElement, checkbox, "checkbox is focused");
    149      EventUtils.synthesizeKey(" ");
    150 
    151      info("waiting for pref to change");
    152      await prefChanged;
    153      ok(checkbox.checked, "#generatePasswords checkbox is checked");
    154      ok(
    155        Services.prefs.getBoolPref("signon.generation.enabled"),
    156        "enabled pref is now true"
    157      );
    158    }
    159  );
    160  await SpecialPowers.popPrefEnv();
    161 });
    162 
    163 add_task(async function toggleRememberSignon() {
    164  await SpecialPowers.pushPrefEnv({
    165    set: [["browser.settings-redesign.enabled", false]],
    166  });
    167  // toggling rememberSignons checkbox should make generation checkbox disabled
    168  SpecialPowers.pushPrefEnv({
    169    set: [
    170      ["signon.generation.available", true],
    171      ["signon.generation.enabled", true],
    172      ["signon.rememberSignons", true],
    173    ],
    174  });
    175 
    176  await BrowserTestUtils.withNewTab(
    177    {
    178      gBrowser,
    179      url: "about:preferences#privacy",
    180    },
    181    async function (browser) {
    182      let doc = browser.contentDocument;
    183      let checkbox = doc.getElementById("savePasswords");
    184      let generationCheckbox = doc.getElementById("generatePasswords");
    185 
    186      ok(
    187        !generationCheckbox.disabled,
    188        "generation checkbox is not initially disabled"
    189      );
    190 
    191      info("waiting for the browser to have focus");
    192      await SimpleTest.promiseFocus(browser);
    193      let prefChanged = TestUtils.waitForPrefChange("signon.rememberSignons");
    194 
    195      // the preferences "Search" bar obscures the checkbox if we scrollIntoView and try to click on it
    196      // so use keyboard events instead
    197      checkbox.focus();
    198      is(doc.activeElement, checkbox, "checkbox is focused");
    199      EventUtils.synthesizeKey(" ");
    200 
    201      info("waiting for pref to change");
    202      await prefChanged;
    203      ok(!checkbox.checked, "#savePasswords checkbox is un-checked");
    204      ok(generationCheckbox.disabled, "generation checkbox becomes disabled");
    205    }
    206  );
    207  await SpecialPowers.popPrefEnv();
    208 });
    209 
    210 // Settings redesign tests
    211 
    212 add_task(async function initialStateNew() {
    213  await SpecialPowers.pushPrefEnv({
    214    set: [["browser.settings-redesign.enabled", true]],
    215  });
    216  // check pref permutations to verify the UI opens in the correct state
    217  const prefTests = [
    218    {
    219      initialPrefs: [
    220        ["signon.rememberSignons", true],
    221        ["signon.generation.available", true],
    222        ["signon.generation.enabled", true],
    223        ["signon.autofillForms", true],
    224      ],
    225      expected: "checked",
    226    },
    227    {
    228      initialPrefs: [
    229        ["signon.rememberSignons", true],
    230        ["signon.generation.available", true],
    231        ["signon.generation.enabled", false],
    232        ["signon.autofillForms", false],
    233      ],
    234      expected: "unchecked",
    235    },
    236    {
    237      initialPrefs: [
    238        ["signon.rememberSignons", true],
    239        ["signon.generation.available", false],
    240        ["signon.generation.enabled", false],
    241      ],
    242      expected: "hidden",
    243    },
    244    {
    245      initialPrefs: [
    246        ["signon.rememberSignons", false],
    247        ["signon.generation.available", true],
    248        ["signon.generation.enabled", true],
    249        ["signon.autofillForms", true],
    250      ],
    251      expected: "disabled",
    252    },
    253  ];
    254  for (let test of prefTests) {
    255    // set initial pref values
    256    info("initialState, testing with: " + JSON.stringify(test));
    257    await SpecialPowers.pushPrefEnv({ set: test.initialPrefs });
    258 
    259    // open about:privacy in a tab
    260    // verify expected conditions
    261    await BrowserTestUtils.withNewTab(
    262      {
    263        gBrowser,
    264        url: "about:preferences#privacy",
    265      },
    266      async function (browser) {
    267        let doc = browser.contentDocument;
    268        let generatePasswordsCheckbox = doc
    269          .getElementById("suggestStrongPasswords")
    270          .shadowRoot.querySelector("input");
    271        let autofillFormsCheckbox = doc
    272          .getElementById("fillUsernameAndPasswords")
    273          .shadowRoot.querySelector("input");
    274        doc.getElementById("passwordsGroup").scrollIntoView();
    275 
    276        info("initialState, assert on expected state:" + test.expected);
    277        switch (test.expected) {
    278          case "hidden":
    279            is_element_hidden(
    280              generatePasswordsCheckbox,
    281              "#generatePasswords checkbox is hidden"
    282            );
    283            break;
    284          case "checked":
    285            is_element_visible(
    286              generatePasswordsCheckbox,
    287              "#generatePasswords checkbox is visible"
    288            );
    289            ok(
    290              generatePasswordsCheckbox.checked,
    291              "#generatePasswords checkbox is checked"
    292            );
    293            ok(
    294              autofillFormsCheckbox.checked,
    295              "#passwordAutofillCheckbox is checked"
    296            );
    297            break;
    298          case "unchecked":
    299            ok(
    300              !generatePasswordsCheckbox.checked,
    301              "#generatePasswords checkbox is un-checked"
    302            );
    303            ok(
    304              !autofillFormsCheckbox.checked,
    305              "#passwordAutofillCheckbox is un-checked"
    306            );
    307            break;
    308          case "disabled":
    309            ok(
    310              generatePasswordsCheckbox.disabled,
    311              "#generatePasswords checkbox is disabled"
    312            );
    313            ok(
    314              autofillFormsCheckbox.disabled,
    315              "#passwordAutofillCheckbox is disabled"
    316            );
    317            break;
    318          default:
    319            ok(false, "Unknown expected state: " + test.expected);
    320        }
    321      }
    322    );
    323    await SpecialPowers.popPrefEnv();
    324  }
    325 });
    326 
    327 add_task(async function toggleGenerationEnabledNew() {
    328  await SpecialPowers.pushPrefEnv({
    329    set: [["browser.settings-redesign.enabled", true]],
    330  });
    331  // clicking the checkbox should toggle the pref
    332  SpecialPowers.pushPrefEnv({
    333    set: [
    334      ["signon.generation.available", true],
    335      ["signon.generation.enabled", false],
    336      ["signon.rememberSignons", true],
    337    ],
    338  });
    339 
    340  await BrowserTestUtils.withNewTab(
    341    {
    342      gBrowser,
    343      url: "about:preferences#privacy",
    344    },
    345    async function (browser) {
    346      let doc = browser.contentDocument;
    347      let checkbox = doc.getElementById("suggestStrongPasswords");
    348 
    349      info("waiting for the browser to have focus");
    350      await SimpleTest.promiseFocus(browser);
    351      let prefChanged = TestUtils.waitForPrefChange(
    352        "signon.generation.enabled"
    353      );
    354 
    355      // the preferences "Search" bar obscures the checkbox if we scrollIntoView and try to click on it
    356      // so use keyboard events instead
    357      checkbox.focus();
    358      is(doc.activeElement, checkbox, "checkbox is focused");
    359      EventUtils.synthesizeKey(" ");
    360 
    361      info("waiting for pref to change");
    362      await prefChanged;
    363      ok(checkbox.checked, "#generatePasswords checkbox is checked");
    364      ok(
    365        Services.prefs.getBoolPref("signon.generation.enabled"),
    366        "enabled pref is now true"
    367      );
    368    }
    369  );
    370  await SpecialPowers.popPrefEnv();
    371 });
    372 
    373 add_task(async function toggleRememberSignonNew() {
    374  await SpecialPowers.pushPrefEnv({
    375    set: [["browser.settings-redesign.enabled", true]],
    376  });
    377  // toggling rememberSignons checkbox should make generation checkbox disabled
    378  SpecialPowers.pushPrefEnv({
    379    set: [
    380      ["signon.generation.available", true],
    381      ["signon.generation.enabled", true],
    382      ["signon.rememberSignons", true],
    383    ],
    384  });
    385 
    386  await BrowserTestUtils.withNewTab(
    387    {
    388      gBrowser,
    389      url: "about:preferences#privacy",
    390    },
    391    async function (browser) {
    392      let doc = browser.contentDocument;
    393      let checkbox = doc.getElementById("savePasswords");
    394      let generationCheckbox = doc
    395        .getElementById("suggestStrongPasswords")
    396        .shadowRoot.querySelector("input");
    397 
    398      ok(
    399        !generationCheckbox.disabled,
    400        "generation checkbox is not initially disabled"
    401      );
    402 
    403      info("waiting for the browser to have focus");
    404      await SimpleTest.promiseFocus(browser);
    405      let prefChanged = TestUtils.waitForPrefChange("signon.rememberSignons");
    406 
    407      checkbox.click();
    408 
    409      info("waiting for pref to change");
    410      await prefChanged;
    411      ok(!checkbox.checked, "#savePasswords checkbox is un-checked");
    412      await BrowserTestUtils.waitForCondition(
    413        () => generationCheckbox.disabled,
    414        "generation checkbox becomes disabled",
    415        200
    416      );
    417    }
    418  );
    419  await SpecialPowers.popPrefEnv();
    420 });