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