summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/tests/chrome/aboutlogins_common.js
blob: d24c962da0dd55ebff69eabe09d5355c24740a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict";

/* exported asyncElementRendered, importDependencies */

/**
 * A helper to await on while waiting for an asynchronous rendering of a Custom
 * Element.
 * @returns {Promise}
 */
function asyncElementRendered() {
  return Promise.resolve();
}

/**
 * Import the templates from the real page to avoid duplication in the tests.
 * @param {HTMLIFrameElement} templateFrame - Frame to copy the resources from
 * @param {HTMLElement} destinationEl - Where to append the copied resources
 */
function importDependencies(templateFrame, destinationEl) {
  let promises = [];
  for (let template of templateFrame.contentDocument.querySelectorAll(
    "template"
  )) {
    let imported = document.importNode(template, true);
    destinationEl.appendChild(imported);
    // Preload the styles in the actual page, to ensure they're loaded on time.
    for (let element of imported.content.querySelectorAll(
      "link[rel='stylesheet']"
    )) {
      let clone = element.cloneNode(true);
      promises.push(
        new Promise(resolve => {
          clone.onload = function () {
            resolve();
            clone.remove();
          };
        })
      );
      destinationEl.appendChild(clone);
    }
  }
  return Promise.all(promises);
}

Object.defineProperty(document, "l10n", {
  configurable: true,
  writable: true,
  value: {
    connectRoot() {},
    translateElements() {
      return Promise.resolve();
    },
    getAttributes(element) {
      return {
        id: element.getAttribute("data-l10n-id"),
        args: element.getAttribute("data-l10n-args")
          ? JSON.parse(element.getAttribute("data-l10n-args"))
          : {},
      };
    },
    setAttributes(element, id, args) {
      element.setAttribute("data-l10n-id", id);
      if (args) {
        element.setAttribute("data-l10n-args", JSON.stringify(args));
      } else {
        element.removeAttribute("data-l10n-args");
      }
    },
  },
});

Object.defineProperty(window, "AboutLoginsUtils", {
  configurable: true,
  writable: true,
  value: {
    getLoginOrigin(uriString) {
      return uriString;
    },
    setFocus(element) {
      return element.focus();
    },
    async promptForPrimaryPassword(resolve, messageId) {
      resolve(true);
    },
    doLoginsMatch(login1, login2) {
      return (
        login1.origin == login2.origin &&
        login1.username == login2.username &&
        login1.password == login2.password
      );
    },
    fileImportEnabled: SpecialPowers.getBoolPref(
      "signon.management.page.fileImport.enabled"
    ),
    primaryPasswordEnabled: false,
  },
});