summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/tests/browser/browser_copyToClipboardButton.js
blob: f8ca37cc47a15ad1339f17cba62d1d69b1687930 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.events.testing.asyncClipboard", true]],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:logins" },
    async function (browser) {
      let TEST_LOGIN = {
        guid: "70a",
        username: "jared",
        password: "deraj",
        origin: "https://www.example.com",
      };

      await SpecialPowers.spawn(browser, [TEST_LOGIN], async function (login) {
        let loginItem = Cu.waiveXrays(
          content.document.querySelector("login-item")
        );

        // The login object needs to be cloned into the content global.
        loginItem.setLogin(Cu.cloneInto(login, content));

        // Lower the timeout for the test.
        Object.defineProperty(
          loginItem.constructor,
          "COPY_BUTTON_RESET_TIMEOUT",
          {
            configurable: true,
            writable: true,
            value: 1000,
          }
        );
      });

      let testCases = [[TEST_LOGIN.username, ".copy-username-button"]];
      if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin()) {
        testCases[1] = [TEST_LOGIN.password, ".copy-password-button"];
      }

      for (let testCase of testCases) {
        let testObj = {
          expectedValue: testCase[0],
          copyButtonSelector: testCase[1],
        };
        info(
          "waiting for " + testObj.expectedValue + " to be placed on clipboard"
        );
        let reauthObserved = true;
        if (testObj.copyButtonSelector.includes("password")) {
          reauthObserved = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(true);
        }

        await SimpleTest.promiseClipboardChange(
          testObj.expectedValue,
          async () => {
            await SpecialPowers.spawn(
              browser,
              [testObj],
              async function (aTestObj) {
                let loginItem = content.document.querySelector("login-item");
                let copyButton = loginItem.shadowRoot.querySelector(
                  aTestObj.copyButtonSelector
                );
                info("Clicking 'copy' button");
                copyButton.click();
              }
            );
          }
        );
        await reauthObserved;
        Assert.ok(true, testObj.expectedValue + " is on clipboard now");

        await SpecialPowers.spawn(
          browser,
          [testObj],
          async function (aTestObj) {
            let loginItem = Cu.waiveXrays(
              content.document.querySelector("login-item")
            );
            let copyButton = loginItem.shadowRoot.querySelector(
              aTestObj.copyButtonSelector
            );
            let otherCopyButton =
              copyButton == loginItem._copyUsernameButton
                ? loginItem._copyPasswordButton
                : loginItem._copyUsernameButton;
            Assert.ok(
              !otherCopyButton.dataset.copied,
              "The other copy button should have the 'copied' state removed"
            );
            Assert.ok(
              copyButton.dataset.copied,
              "Success message should be shown"
            );
          }
        );
      }

      // Wait for the 'copied' attribute to get removed from the copyPassword
      // button, which is the last button that is clicked in the above testcase.
      // Since another Copy button isn't clicked, the state won't get cleared
      // instantly. This test covers the built-in timeout of the visual display.
      await SpecialPowers.spawn(browser, [], async () => {
        let copyButton = Cu.waiveXrays(
          content.document.querySelector("login-item")
        )._copyPasswordButton;
        await ContentTaskUtils.waitForCondition(
          () => !copyButton.dataset.copied,
          "'copied' attribute should be removed after a timeout"
        );
      });
    }
  );
});