dom/plugins/test/mochitest/cocoa_focus.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <html>
michael@0 2 <head>
michael@0 3 <title>NPCocoaEventFocusChanged Tests</title>
michael@0 4 </head>
michael@0 5 <body>
michael@0 6 <embed id="plugin1" type="application/x-test" width="100" height="100"></embed>
michael@0 7 <embed id="plugin2" type="application/x-test" width="100" height="100"></embed>
michael@0 8 <script type="application/javascript">
michael@0 9 function is(aLeft, aRight, aMessage) {
michael@0 10 window.opener.SimpleTest.is(aLeft, aRight, aMessage);
michael@0 11 }
michael@0 12
michael@0 13 function ok(aValue, aMessage) {
michael@0 14 window.opener.SimpleTest.ok(aValue, aMessage);
michael@0 15 }
michael@0 16
michael@0 17 function runTests() {
michael@0 18 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
michael@0 19 var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
michael@0 20 getInterface(Components.interfaces.nsIDOMWindowUtils);
michael@0 21
michael@0 22 var plugin1 = document.getElementById("plugin1"); // What we're testing.
michael@0 23 var plugin2 = document.getElementById("plugin2"); // Dummy.
michael@0 24
michael@0 25 var plugin1Bounds = plugin1.getBoundingClientRect();
michael@0 26 var plugin2Bounds = plugin2.getBoundingClientRect();
michael@0 27
michael@0 28 var plugin1X = (window.mozInnerScreenX + plugin1Bounds.left + 10);
michael@0 29 var plugin1Y = (window.mozInnerScreenY + plugin1Bounds.top + 10);
michael@0 30 var plugin2X = (window.mozInnerScreenX + plugin2Bounds.left + 10);
michael@0 31 var plugin2Y = (window.mozInnerScreenY + plugin2Bounds.top + 10);
michael@0 32
michael@0 33 const NSLeftMouseDown = 1,
michael@0 34 NSLeftMouseUp = 2;
michael@0 35
michael@0 36 if (plugin1.getEventModel() != 1) {
michael@0 37 window.opener.todo(false, "Skipping this test when not testing the Cocoa event model");
michael@0 38 window.opener.testsFinished();
michael@0 39 return;
michael@0 40 }
michael@0 41
michael@0 42 // Initialize to 0 since there is no initial state event,
michael@0 43 // plugins should assume they do not initially have focus.
michael@0 44 var expectedEventCount = 0;
michael@0 45
michael@0 46 // Make sure initial event count is correct.
michael@0 47 is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);
michael@0 48
michael@0 49 // Make sure initial focus state is unknown (assumed false).
michael@0 50 var initialStateUnknown = false;
michael@0 51 try {
michael@0 52 plugin1.getFocusState();
michael@0 53 } catch (e) {
michael@0 54 initialStateUnknown = true;
michael@0 55 }
michael@0 56 is(initialStateUnknown, true, "Initial state should be unknown, assumed false.");
michael@0 57
michael@0 58 // Give the plugin focus (the window is already focused).
michael@0 59 utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseDown, 0, plugin1);
michael@0 60 utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseUp, 0, plugin1);
michael@0 61 expectedEventCount++;
michael@0 62
michael@0 63 is(plugin1.getFocusState(), true, "Plugin should have focus.");
michael@0 64 is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);
michael@0 65
michael@0 66 // Make sure window activation state changes don't spontaneously
michael@0 67 // change plugin focus.
michael@0 68
michael@0 69 // Blur the window.
michael@0 70 window.blur();
michael@0 71
michael@0 72 is(plugin1.getFocusState(), true, "Plugin should still have focus.");
michael@0 73 is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);
michael@0 74
michael@0 75 // Focus the window.
michael@0 76 window.focus();
michael@0 77
michael@0 78 is(plugin1.getFocusState(), true, "Plugin should still have focus.");
michael@0 79 is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);
michael@0 80
michael@0 81 // Take focus from the plugin.
michael@0 82 utils.sendNativeMouseEvent(plugin2X, plugin2Y, NSLeftMouseDown, 0, plugin2);
michael@0 83 utils.sendNativeMouseEvent(plugin2X, plugin2Y, NSLeftMouseUp, 0, plugin2);
michael@0 84 expectedEventCount++;
michael@0 85
michael@0 86 is(plugin1.getFocusState(), false, "Plugin should not have focus.");
michael@0 87 is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);
michael@0 88
michael@0 89 // Make sure window activation causes the plugin to be informed of focus
michael@0 90 // changes that took place while the window was inactive.
michael@0 91
michael@0 92 // Give the plugin focus (the window is already focused).
michael@0 93 utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseDown, 0, plugin1);
michael@0 94 utils.sendNativeMouseEvent(plugin1X, plugin1Y, NSLeftMouseUp, 0, plugin1);
michael@0 95 expectedEventCount++;
michael@0 96
michael@0 97 // Blur the window.
michael@0 98 window.blur();
michael@0 99
michael@0 100 // Take focus from the plugin while the window is blurred.
michael@0 101 plugin2.focus();
michael@0 102
michael@0 103 is(plugin1.getFocusState(), true, "Plugin should still have focus.");
michael@0 104 is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);
michael@0 105
michael@0 106 // Focus the window.
michael@0 107 window.focus();
michael@0 108 expectedEventCount++;
michael@0 109
michael@0 110 is(plugin1.getFocusState(), false, "Plugin should not have focus.");
michael@0 111 is(plugin1.getFocusEventCount(), expectedEventCount, "Focus event count should be " + expectedEventCount);
michael@0 112
michael@0 113 window.opener.testsFinished();
michael@0 114 }
michael@0 115
michael@0 116 // Onload hander doesn't work for these tests -- no events arrive at the plugin.
michael@0 117 window.opener.SimpleTest.waitForFocus(runTests, window);
michael@0 118
michael@0 119 </script>
michael@0 120 </body>
michael@0 121 </html>

mercurial