Bug 1474179 - Part 4: Adds mochitest for the "copy raw" menuitem. r?valentin draft
authorZhang Junzhi <zjz@zjz.name>
Fri, 13 Jul 2018 00:02:01 +0800
changeset 817383 cecc1939c91537c8434f7e1579e5627d39029f71
parent 817382 67555daed546460cae7c1eed4745c3652006cc43
push id116050
push userbmo:zjz@zjz.name
push dateThu, 12 Jul 2018 16:06:02 +0000
reviewersvalentin
bugs1474179
milestone63.0a1
Bug 1474179 - Part 4: Adds mochitest for the "copy raw" menuitem. r?valentin MozReview-Commit-ID: GGlOjUgJCTO
browser/base/content/test/urlbar/browser.ini
browser/base/content/test/urlbar/browser_copyRaw.js
--- a/browser/base/content/test/urlbar/browser.ini
+++ b/browser/base/content/test/urlbar/browser.ini
@@ -33,16 +33,17 @@ skip-if = (verify && debug && (os == 'wi
 subsuite = clipboard
 [browser_bug562649.js]
 support-files = file_bug562649.html
 [browser_bug623155.js]
 support-files =
   redirect_bug623155.sjs
 [browser_bug783614.js]
 [browser_canonizeURL.js]
+[browser_copyRaw.js]
 [browser_dragdropURL.js]
 [browser_locationBarCommand.js]
 [browser_locationBarExternalLoad.js]
 skip-if = true # Bug 1315887
 [browser_moz_action_link.js]
 [browser_new_tab_urlbar_reset.js]
 [browser_page_action_menu.js]
 [browser_page_action_menu_add_search_engine.js]
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/urlbar/browser_copyRaw.js
@@ -0,0 +1,151 @@
+"use strict";
+
+// Test for an unencoded URL(the "copy raw" menuitem should never be displayed
+// for an unencoded URL)
+add_task(async () => {
+  const kUrl = "http://example.com/foo";
+
+  await BrowserTestUtils.withNewTab(kUrl, async (browser) => {
+    gURLBar.focus();
+    gURLBar.select();
+
+    // Popup the URL bar menu, and then check whether the "copy raw" menuitem is
+    // displayed.
+    // The menuitem is expected to be hidden even if the entire URL is selected,
+    // because the URL isn't encoded.
+    let textBox = document.getAnonymousElementByAttribute(gURLBar,
+      "anonid", "textbox-input-box");
+    let cxmenu = document.getAnonymousElementByAttribute(textBox,
+      "anonid", "input-box-contextmenu");
+    let cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popupshown");
+    EventUtils.synthesizeMouseAtCenter(gURLBar, {type: "contextmenu", button: 2});
+    await cxmenuPromise;
+    let menuItemCopyRaw = document.getAnonymousElementByAttribute(textBox,
+      "anonid", "copy-raw");
+    ok(menuItemCopyRaw.getAttribute("hidden") === "true",
+         "\"Copy raw\" menuitem should be hidden for an unencoded URL.");
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popuphidden");
+    cxmenu.hidePopup();
+    await cxmenuPromise;
+  });
+});
+
+// Test for an encoded URL(the "copy raw" menuitem should be displayed if the
+// entire URL is selected)
+add_task(async () => {
+  const kUrls = [
+    {
+      // This is a URL with an ASCII domain name but a Unicode URL path.
+      rawDomain: "example.com",
+      domain: "example.com",
+      path: "\u6E2C",
+    },
+    {
+      // This is a URL with a Unicode domain name but an ASCII URL path.
+      rawDomain: "xn--hxajbheg2az3al.xn--jxalpdlp",
+      domain: "\u03C0\u03B1\u03C1\u03AC\u03B4\u03B5\u03B9\u03B3\u03BC\u03B1.\u03B4\u03BF\u03BA\u03B9\u03BC\u03AE",
+      path: "test",
+    },
+  ];
+  for (let i = 0; i < kUrls.length; i++) {
+    await testEncodedUrl(kUrls[i]);
+  }
+});
+
+async function testEncodedUrl(url) {
+  const kScheme = "http://";
+  const kEncodedUrl = kScheme + url.rawDomain + "/" + encodeURIComponent(url.path);
+
+  await BrowserTestUtils.withNewTab(kEncodedUrl, async (browser) => {
+    gURLBar.focus();
+
+    // Summary:
+    // Popup the URL bar menu, and then check whether the "copy raw" menuitem is
+    // displayed.
+    // The menuitem is expected to be shown only if the entire URL is selected
+    // and hasn't been edited after page load; otherwise, it is expected to be
+    // hidden.
+    // We also test the menuitem's copying functionality when it's shown.
+
+    // First, load a page with an encoded URL, and popup the menu.
+    // The "copy raw" menuitem is expected to be hidden, because the URL is not
+    // selected.
+    let textBox = document.getAnonymousElementByAttribute(gURLBar,
+      "anonid", "textbox-input-box");
+    let cxmenu = document.getAnonymousElementByAttribute(textBox,
+      "anonid", "input-box-contextmenu");
+    let cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popupshown");
+    EventUtils.synthesizeMouseAtCenter(gURLBar, {type: "contextmenu", button: 2});
+    await cxmenuPromise;
+    let menuItemCopyRaw = document.getAnonymousElementByAttribute(textBox,
+      "anonid", "copy-raw");
+    ok(menuItemCopyRaw.getAttribute("hidden") === "true",
+         "\"Copy raw\" menuitem should be hidden if the decoded URL isn't selected.");
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popuphidden");
+    cxmenu.hidePopup();
+    await cxmenuPromise;
+
+    // Second, we select the entire URL, and then popup the menu again.
+    // This time, the "copy raw" menuitem is expected to be shown. We also test
+    // the menuitem's copying functionality in this step, the menuitem should
+    // copy the raw URL(I.e. the encoded URL).
+    gURLBar.select();
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popupshown");
+    EventUtils.synthesizeMouseAtCenter(gURLBar, {type: "contextmenu", button: 2});
+    await cxmenuPromise;
+    ok(!menuItemCopyRaw.getAttribute("hidden"),
+         "\"Copy raw\" menuitem should be shown if the entire decoded URL is selected.");
+    // Test the copying functionality.
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popuphidden");
+    await new Promise((resolve, reject) => {
+            waitForClipboard(kEncodedUrl, () => {
+              EventUtils.synthesizeMouseAtCenter(menuItemCopyRaw, {});
+            }, resolve, reject);
+          });
+    await cxmenuPromise;
+
+    // Third, we only select the domain prefix, and then popup the menu again.
+    // The "copy raw" menuitem is expected to be hidden, because the URL is
+    // not entirely selected.
+    gURLBar.selectionStart = 0;
+    gURLBar.selectionEnd = url.domain.length;
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popupshown");
+    EventUtils.synthesizeMouseAtCenter(gURLBar, {type: "contextmenu", button: 2});
+    await cxmenuPromise;
+    ok(menuItemCopyRaw.getAttribute("hidden") === "true",
+         "\"Copy raw\" menuitem should be hidden if only the beginning part of the decoded URL is selected.");
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popuphidden");
+    cxmenu.hidePopup();
+    await cxmenuPromise;
+
+    // Fourth, we only select the URI path, and then popup the menu again.
+    // The "copy raw" menuitem is expected to be hidden, because the URL is
+    // not entirely selected.
+    gURLBar.selectionStart = url.domain.length;
+    gURLBar.selectionEnd = gURLBar.textValue.length;
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popupshown");
+    EventUtils.synthesizeMouseAtCenter(gURLBar, {type: "contextmenu", button: 2});
+    await cxmenuPromise;
+    ok(menuItemCopyRaw.getAttribute("hidden") === "true",
+         "\"Copy raw\" menuitem should be hidden if only the trailing part of the decoded URL is selected.");
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popuphidden");
+    cxmenu.hidePopup();
+    await cxmenuPromise;
+
+    // Fifth, we edited the URL in the bar, and then popup the menu again.
+    // The "copy raw" menuitem is expected to be hidden, because the URL is
+    // not intact.
+    const val = gURLBar.textValue;
+    gURLBar.textValue += "a";
+    gURLBar.textValue = val;
+    gURLBar.select();
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popupshown");
+    EventUtils.synthesizeMouseAtCenter(gURLBar, {type: "contextmenu", button: 2});
+    await cxmenuPromise;
+    ok(menuItemCopyRaw.getAttribute("hidden") === "true",
+         "\"Copy raw\" menuitem should be hidden after the URL has been edited, even if the entire URL is selected.");
+    cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popuphidden");
+    cxmenu.hidePopup();
+    await cxmenuPromise;
+  });
+}