Bug 1550093 - Add automated tests for CopyToClipboardButton. r=MattN draft
authorpulselistener
Sat, 18 May 2019 06:49:05 +0000
changeset 2008569 675eba6f045453b9f8027d6c20c7f1ee5d7fcc2e
parent 2008568 1dd61c1a35b7dded605f4ff434abf8f7472ab454
child 2008570 4b660b1afcb53fd21005aecb145722671ad4b630
push id363923
push userreviewbot
push dateSat, 18 May 2019 06:49:52 +0000
treeherdertry@6eb4ec2cb49f [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");
+      });
+    }
+  });
+});