diff -r 000000000000 -r 6474c204b198 browser/devtools/markupview/test/browser_markupview_copy_image_data.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/browser/devtools/markupview/test/browser_markupview_copy_image_data.js Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,111 @@ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Test that image nodes have the "copy data-uri" contextual menu item enabled +// and that clicking it puts the image data into the clipboard + +const PAGE_CONTENT = [ + '
', + '', + '' +].join("\n"); + +let test = asyncTest(function*() { + yield addTab("data:text/html,markup view copy image as data-uri"); + createDocument(); + let doc = content.document; + + let {inspector} = yield openInspector(); + + yield selectNode("div", inspector); + yield assertCopyImageDataNotAvailable(inspector); + + yield selectNode("img", inspector); + yield assertCopyImageDataAvailable(inspector); + yield triggerCopyImageUrlAndWaitForClipboard(doc.querySelector("img").src, inspector); + + yield selectNode("canvas", inspector); + yield assertCopyImageDataAvailable(inspector); + let canvas = doc.querySelector(".canvas"); + yield triggerCopyImageUrlAndWaitForClipboard(canvas.toDataURL(), inspector); + + // Check again that the menu isn't available on the DIV (to make sure our + // menu updating mechanism works) + yield selectNode("div", inspector); + yield assertCopyImageDataNotAvailable(inspector); +}); + +function createDocument() { + let doc = content.document; + + doc.body.innerHTML = PAGE_CONTENT; + let context = doc.querySelector(".canvas").getContext("2d"); + context.beginPath(); + context.moveTo(300, 0); + context.lineTo(600, 600); + context.lineTo(0, 600); + context.closePath(); + context.fillStyle = "#ffc821"; + context.fill(); +} + +function assertCopyImageDataNotAvailable(inspector) { + return openNodeMenu(inspector).then(menu => { + let item = menu.getElementsByAttribute("id", "node-menu-copyimagedatauri")[0]; + ok(item, "The menu item was found in the contextual menu"); + is(item.getAttribute("disabled"), "true", "The menu item is disabled"); + }).then(() => closeNodeMenu(inspector)); +} + +function assertCopyImageDataAvailable(inspector) { + return openNodeMenu(inspector).then(menu => { + let item = menu.getElementsByAttribute("id", "node-menu-copyimagedatauri")[0]; + ok(item, "The menu item was found in the contextual menu"); + is(item.getAttribute("disabled"), "", "The menu item is enabled"); + }).then(() => closeNodeMenu(inspector)); +} + +function openNodeMenu(inspector) { + let def = promise.defer(); + + inspector.nodemenu.addEventListener("popupshown", function onOpen() { + inspector.nodemenu.removeEventListener("popupshown", onOpen, false); + def.resolve(inspector.nodemenu); + }, false); + inspector.nodemenu.hidden = false; + inspector.nodemenu.openPopup(); + + return def.promise; +} + +function closeNodeMenu(inspector) { + let def = promise.defer(); + + inspector.nodemenu.addEventListener("popuphidden", function onClose() { + inspector.nodemenu.removeEventListener("popuphidden", onClose, false); + def.resolve(inspector.nodemenu); + }, false); + inspector.nodemenu.hidden = true; + inspector.nodemenu.hidePopup(); + + return def.promise; +} + +function triggerCopyImageUrlAndWaitForClipboard(expected, inspector) { + let def = promise.defer(); + + SimpleTest.waitForClipboard(expected, () => { + inspector.markup.getContainer(inspector.selection.nodeFront).copyImageDataUri(); + }, () => { + ok(true, "The clipboard contains the expected value " + expected.substring(0, 50) + "..."); + def.resolve(); + }, () => { + ok(false, "The clipboard doesn't contain the expected value " + expected.substring(0, 50) + "..."); + def.resolve(); + }); + + return def.promise; +}