summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/cocoa_focus.html
blob: ca35fd7a1fc870b9d0b75b8c9f3f78f0a3d5dba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<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 synthesizeNativeMouseEvent(aX, aY, aNativeMsg, aModifiers, aElement, aCallback) {
      var observer = {
        observe: function(aSubject, aTopic, aData) {
          if (aCallback && aTopic == "mouseevent") {
            aCallback(aData);
          }
        }
      };
      SpecialPowers.DOMWindowUtils.sendNativeMouseEvent(aX, aY, aNativeMsg, aModifiers, aElement, observer);
      return true;
    }

    function* runTests() {
      var utils = SpecialPowers.DOMWindowUtils;
      var scale = utils.screenPixelsPerCSSPixel;

      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");
        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).
      yield synthesizeNativeMouseEvent(plugin1X * scale, plugin1Y * scale, NSLeftMouseDown, 0, plugin1, continueTest);
      yield synthesizeNativeMouseEvent(plugin1X * scale, plugin1Y * scale, NSLeftMouseUp, 0, plugin1, continueTest);
      expectedEventCount++;

      is(plugin1.getFocusState(), true, "(1) 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.
      SpecialPowers.focus(opener);

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

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

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

      // Take focus from the plugin.
      yield synthesizeNativeMouseEvent(plugin2X * scale, plugin2Y * scale, NSLeftMouseDown, 0, plugin2, continueTest);
      yield synthesizeNativeMouseEvent(plugin2X * scale, plugin2Y * scale, NSLeftMouseUp, 0, plugin2, continueTest);
      expectedEventCount++;

      is(plugin1.getFocusState(), false, "(4) 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).
      yield synthesizeNativeMouseEvent(plugin1X * scale, plugin1Y * scale, NSLeftMouseDown, 0, plugin1, continueTest);
      yield synthesizeNativeMouseEvent(plugin1X * scale, plugin1Y * scale, NSLeftMouseUp, 0, plugin1, continueTest);
      expectedEventCount++;

      // Blur the window.
      SpecialPowers.focus(opener);

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

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

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

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

    var gTestContinuation = null;
    function continueTest() {
      if (!gTestContinuation) {
        gTestContinuation = runTests();
      }
      var ret = gTestContinuation.next();
      if (ret.done) {
        window.opener.testsFinished();
      } else {
        is(ret.value, true, "Mouse event successfully synthesized");
      }
    }

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

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