/* 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 = [
  '<div></div>',
  '<img class="data" src="data:image/png;base64,[encoded data, 10104 characters]" />',
  '<canvas class="canvas" width="600" height="600"></canvas>'
].join("\n");

add_task(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;
}
