Bug 1550093 - Add automated tests for CopyToClipboardButton. r=MattN draft
authorpulselistener
Sat, 18 May 2019 06:39:51 +0000
changeset 2008507 e5593c84eb403a15de6dc3a5f82fd5fb32a24ad1
parent 2008506 446dfe5411e2081778fa58e9357b47b8e429d350
child 2008508 9a1bc6e7a1b7ceb5a468a21cac285718219d8253
push id363919
push userreviewbot
push dateSat, 18 May 2019 06:40:24 +0000
treeherdertry@6d5ea5dbe638 [default view] [failures only]
reviewersMattN
bugs1550093
milestone68.0a1
Bug 1550093 - Add automated tests for CopyToClipboardButton. r=MattN Differential Revision: https://phabricator.services.mozilla.com/D31664 Differential Diff: PHID-DIFF-fqpjkkqqeubhphkexyck
browser/components/aboutlogins/tests/browser/browser.ini
browser/components/aboutlogins/tests/browser/browser_copyToClipboardButton.js
--- a/browser/components/aboutlogins/tests/browser/browser.ini
+++ b/browser/components/aboutlogins/tests/browser/browser.ini
@@ -1,8 +1,9 @@
 [DEFAULT]
 prefs =
   signon.management.page.enabled=true
 
+[browser_copyToClipboardButton.js]
 [browser_deleteLogin.js]
 [browser_loginListChanges.js]
 [browser_openSite.js]
 [browser_updateLogin.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/aboutlogins/tests/browser/browser_copyToClipboardButton.js
@@ -0,0 +1,58 @@
+/* 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",
+      hostname: "https://www.example.com",
+    };
+
+    await ContentTask.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.
+      let copyButton = loginItem.shadowRoot.querySelector(".copy-username-button");
+      Object.defineProperty(copyButton.constructor, "BUTTON_RESET_TIMEOUT", {
+          configurable: true,
+          writable: true,
+          value: 1000,
+      });
+    });
+
+    for (let testCase of [
+      [TEST_LOGIN.username, ".copy-username-button"],
+      [TEST_LOGIN.password, ".copy-password-button"],
+    ]) {
+      let testObj = {
+        expectedValue: testCase[0],
+        copyButtonSelector: testCase[1],
+      };
+      await SimpleTest.promiseClipboardChange(testObj.expectedValue, async () => {
+        await ContentTask.spawn(browser, testObj, async function(aTestObj) {
+          let loginItem = content.document.querySelector("login-item");
+          let copyButton = loginItem.shadowRoot.querySelector(aTestObj.copyButtonSelector);
+          let innerButton = copyButton.shadowRoot.querySelector("button");
+          info("Clicking 'copy' button");
+          innerButton.click();
+        });
+      });
+      ok(true, "Username is on clipboard now");
+
+      await ContentTask.spawn(browser, testObj, async function(aTestObj) {
+        let loginItem = content.document.querySelector("login-item");
+        let copyButton = loginItem.shadowRoot.querySelector(aTestObj.copyButtonSelector);
+        ok(copyButton.hasAttribute("copied"), "Success message should be shown");
+        await ContentTaskUtils.waitForCondition(() => !copyButton.hasAttribute("copied"),
+          "'copied' attribute should be removed after a timeout");
+      });
+    }
+  });
+});