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;
+ });
+}