<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=885996
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1234567</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test that update() modifies the element value such as done() when it is
   * not called as a concellation.
   */

  SimpleTest.waitForExplicitFinish();

  var MockColorPicker = SpecialPowers.MockColorPicker;

  var test = runTest();

  SimpleTest.waitForFocus(function() {
    test.next();
  });

  function* runTest() {
    MockColorPicker.init(window);
    var element = null;

    MockColorPicker.showCallback = function(picker, update) {
      is(picker.initialColor, element.value);

      var inputEvent = false;
      var changeEvent = false;
      element.oninput = function() {
        inputEvent = true;
      };
      element.onchange = function() {
        changeEvent = true;
      };

      if (element.dataset.type == 'update') {
        update('#f00ba4');

        is(inputEvent, true, 'input event should have been received');
        is(changeEvent, false, 'change event should not have been received');

        inputEvent = changeEvent = false;

        is(element.value, '#f00ba4');

        MockColorPicker.returnColor = '#f00ba7';
        isnot(element.value, MockColorPicker.returnColor);
      } else if (element.dataset.type == 'cancel') {
        MockColorPicker.returnColor = '#bababa';
        isnot(element.value, MockColorPicker.returnColor);
      } else if (element.dataset.type == 'done') {
        MockColorPicker.returnColor = '#098766';
        isnot(element.value, MockColorPicker.returnColor);
      } else if (element.dataset.type == 'noop-done') {
        MockColorPicker.returnColor = element.value;
        is(element.value, MockColorPicker.returnColor);
      }

      SimpleTest.executeSoon(function() {
        if (element.dataset.type == 'cancel') {
          isnot(element.value, MockColorPicker.returnColor);
          is(inputEvent, false, 'no input event should have been sent');
          is(changeEvent, false, 'no change event should have been sent');
        } else if (element.dataset.type == 'noop-done') {
          is(element.value, MockColorPicker.returnColor);
          is(inputEvent, false, 'no input event should have been sent');
          is(changeEvent, false, 'no change event should have been sent');
        } else {
          is(element.value, MockColorPicker.returnColor);
          is(inputEvent, true, 'input event should have been sent');
          is(changeEvent, true, 'change event should have been sent');
        }

        changeEvent = false;
        element.blur();

        setTimeout(function() {
          is(changeEvent, false, "change event should not be fired on blur");
          test.next();
        });
      });

      return element.dataset.type == 'cancel' ? "" : MockColorPicker.returnColor;
    };

    for (var i = 0; i < document.getElementsByTagName('input').length; ++i) {
      element = document.getElementsByTagName('input')[i];
      element.focus();
      synthesizeMouseAtCenter(element, {});
      yield undefined;
    };

    MockColorPicker.cleanup();
    SimpleTest.finish();
  }

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=885996">Mozilla Bug 885996</a>
<p id="display"></p>
<div id="content">
  <input type='color' data-type='update'>
  <input type='color' data-type='cancel'>
  <input type='color' data-type='done'>
  <input type='color' data-type='noop-done'>
</div>
<pre id="test">
</pre>
</body>
</html>
