Bug 1550093 - Add automated tests for CopyToClipboardButton. r=MattN
authorJared Wein <jwein@mozilla.com>
Fri, 17 May 2019 21:20:54 +0000
changeset 474422 cfa094f6fd3ae066f3278cf5e4817d753f7a135e
parent 474421 c5515eb04c11787046da2a1cbf2a805a204404b2
child 474423 9d057f563cdadc8d3e111eb17133e901d99e3f7c
push id113152
push userdluca@mozilla.com
push dateSat, 18 May 2019 10:33:03 +0000
treeherdermozilla-inbound@9b2f851979cb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1550093
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1550093 - Add automated tests for CopyToClipboardButton. r=MattN Differential Revision: https://phabricator.services.mozilla.com/D31664
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");
+      });
+    }
+  });
+});