dom/plugins/test/mochitest/cocoa_focus.html
author Peter Van der Beken <peterv@propagandism.org>
Sat, 15 Feb 2014 22:12:35 +0100
changeset 181843 cdd53387bf1a4e03c800483430567808662243e6
parent 109288 d6f8b964ca275847feed01c5a3f96e19773a8854
permissions -rw-r--r--
Fix js/xpconnect/tests/chrome/test_bug853571.xul and use SpecialPowers more. * * * Fix webspeech tests to work with Window on WebIDL bindings.

<html>
<head>
  <title>NPCocoaEventFocusChanged Tests</title>
</head>
<body>
  <embed id="plugin1" type="application/x-test" width="100" height="100"></embed>
  <embed id="plugin2" type="application/x-test" width="100" height="100"></embed>
  <script type="application/javascript">
    function is(aLeft, aRight, aMessage) {
      window.opener.SimpleTest.is(aLeft, aRight, aMessage);
    }

    function ok(aValue, aMessage) {
      window.opener.SimpleTest.ok(aValue, aMessage);
    }

    function runTests() {
      netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
      var utils = SpecialPowers.DOMWindowUtils;

      var plugin1 = document.getElementById("plugin1"); // What we're testing.
      var plugin2 = document.getElementById("plugin2"); // Dummy.

      var plugin1Bounds = plugin1.getBoundingClientRect();
      var plugin2Bounds = plugin2.getBoundingClientRect();

      var plugin1X = (window.mozInnerScreenX + plugin1Bounds.left + 10);
      var plugin1Y = (window.mozInnerScreenY + plugin1Bounds.top + 10);
      var plugin2X = (window.mozInnerScreenX + plugin2Bounds.left + 10);
      var plugin2Y = (window.mozInnerScreenY + plugin2Bounds.top + 10);

      const NSLeftMouseDown          = 1,
            NSLeftMouseUp            = 2;

      if (plugin1.getEventModel() != 1) {
        window.opener.todo(false, "Skipping this test when not testing the Cocoa event model");
        window.opener.testsFinished();
        return;
      }

      // Initialize to 0 since there is no initial state event,
      // plugins should assume they do not initially have focus.
      var expectedEventCount = 0;

      // Make sure initial event count is correct.
      is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);

      // Make sure initial focus state is unknown (assumed false).
      var initialStateUnknown = false;
      try {
        plugin1.getFocusState();
      } catch (e) {
        initialStateUnknown = true;
      }
      is(initialStateUnknown, true, "Initial state should be unknown, assumed false.");

      // Give the plugin focus (the window is already focused).
      utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseDown, 0, plugin1);
      utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseUp, 0, plugin1);
      expectedEventCount++;

      is(plugin1.getFocusState(), true, "Plugin should have focus.");
      is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);

      // Make sure window activation state changes don't spontaneously
      // change plugin focus.

      // Blur the window.
      window.blur();

      is(plugin1.getFocusState(), true, "Plugin should still have focus.");
      is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);

      // Focus the window.
      window.focus();

      is(plugin1.getFocusState(), true, "Plugin should still have focus.");
      is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);

      // Take focus from the plugin.
      utils.sendNativeMouseEvent(plugin2X, plugin2Y, NSLeftMouseDown, 0, plugin2);
      utils.sendNativeMouseEvent(plugin2X, plugin2Y, NSLeftMouseUp, 0, plugin2);
      expectedEventCount++;

      is(plugin1.getFocusState(), false, "Plugin should not have focus.");
      is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);

      // Make sure window activation causes the plugin to be informed of focus
      // changes that took place while the window was inactive.

      // Give the plugin focus (the window is already focused).
      utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseDown, 0, plugin1);
      utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseUp, 0, plugin1);
      expectedEventCount++;

      // Blur the window.
      window.blur();

      // Take focus from the plugin while the window is blurred.
      plugin2.focus();

      is(plugin1.getFocusState(), true, "Plugin should still have focus.");
      is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);

      // Focus the window.
      window.focus();
      expectedEventCount++;

      is(plugin1.getFocusState(), false, "Plugin should not have focus.");
      is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);

      window.opener.testsFinished();
    }

    // Onload hander doesn't work for these tests -- no events arrive at the plugin.
    window.opener.SimpleTest.waitForFocus(runTests, window);

  </script>
</body>
</html>