tor-browser

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

head.js (2598B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const MOCK_PASSWORD = "mckP@ss3x2 fake_password";
      7 
      8 /**
      9 * Creates a default backupServiceState object with common properties.
     10 * This is used in tests to avoid repetition of the defaultParent structure.
     11 *
     12 * @param {object} overrides - Properties to override or add to the default state
     13 * @returns {object} The complete backupServiceState object
     14 */
     15 function createBackupServiceState(overrides = {}) {
     16  const testDefaultName = "test-default-path";
     17  return {
     18    defaultParent: {
     19      path: PathUtils.join(PathUtils.tempDir, testDefaultName),
     20      fileName: testDefaultName,
     21    },
     22    archiveEnabledStatus: true,
     23    restoreEnabledStatus: true,
     24    ...overrides,
     25  };
     26 }
     27 
     28 /**
     29 * Dispatches a custom event "ValidPasswordsDetected" or "InvalidPasswordsDetected" from
     30 * the password-validation-inputs element within a parent element.
     31 * Pass "ValidPasswordsDetected" to simulate when a user meets password requirements
     32 * before submitting any changes. Otherwise, pass "InvalidPasswordsDetected" to simulate when a
     33 * user no longer satisfies password requirements.
     34 *
     35 * @param {HTMLElement} parentEl
     36 *  The parent element that listens for the custom event and contains the inputs element dispatching it.
     37 * @param {HTMLElement} passwordInputsEl
     38 *  The inputs element embedded within the parent element that dispatches the custom event.
     39 * @param {string} event
     40 *  The event to dispatch.
     41 * @returns {Promise<undefined>}
     42 */
     43 function createMockValidityPassEventPromise(parentEl, passwordInputsEl, event) {
     44  let promise = new Promise(resolve => {
     45    parentEl.addEventListener(event, resolve, {
     46      once: true,
     47    });
     48  });
     49  let detail = {};
     50 
     51  if (event === "ValidPasswordsDetected") {
     52    detail.password = MOCK_PASSWORD;
     53  }
     54 
     55  passwordInputsEl.dispatchEvent(
     56    new CustomEvent(event, {
     57      bubbles: true,
     58      composed: true,
     59      detail,
     60    })
     61  );
     62  return promise;
     63 }
     64 
     65 /**
     66 * Dispatches an input event for a password input field.
     67 *
     68 * @param {HTMLElement} inputEl
     69 *  the input element that will dispatch the input event
     70 * @param {string} mockPassword
     71 *  the password entered for the input element
     72 * @returns {Promise<undefined>}
     73 */
     74 function createMockPassInputEventPromise(inputEl, mockPassword) {
     75  let promise = new Promise(resolve => {
     76    inputEl.addEventListener("input", () => resolve(), {
     77      once: true,
     78    });
     79  });
     80  inputEl.focus();
     81  inputEl.value = mockPassword;
     82  inputEl.dispatchEvent(new Event("input"));
     83  return promise;
     84 }