summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/cocoa_window_focus.html
blob: 8305b19a4bf64d35759b3c0302f3b52ab4f57a3a (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
<html>
<head>
  <title>NPCocoaEventWindowFocusChanged Tests</title>
</head>
<body onload="runTests()">
  <embed id="plugin1" type="application/x-test" width="400" height="400"></embed>
  <embed id="plugin2" type="application/x-test" width="400" height="400"></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 executeSoon(func) {
      window.opener.SimpleTest.executeSoon(func);
    }

    function waitForFocus(aCb, aTarget, aBlank) {
      window.opener.SimpleTest.waitForFocus(aCb, aTarget, aBlank);
    }

    function runTests() {
      var plugin1 = document.getElementById("plugin1");
      var plugin2 = document.getElementById("plugin2");

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

      // The first plugin will have in-page focus for these tests.
      plugin1.focus();

      // The expected event count which applies to all instances.
      // Initialize to 1 to account for the initial state event.
      var expectedEventCount = 1;

      // First make sure the plugins got an initial window focus event.
      // Since this window was just opened it should be in the front. If
      // the plugin has not been sent an initial window state then it will
      // be in an unknown state and it will throw an exception.
      try {
        is(plugin1.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
        is(plugin1.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

        is(plugin2.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
        is(plugin2.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);
      } catch (e) {
        ok(false, "Plugin does not know its initial top-level window activation state!");
      }

      var fm = SpecialPowers.Services.focus;

      waitForFocus(function() {
        // Make sure the plugin handled the focus event before checking.
        executeSoon(function() {
          expectedEventCount++;

          is(plugin1.getTopLevelWindowActivationState(), false, "Activation state should be: deactivated");
          is(plugin1.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

          is(plugin2.getTopLevelWindowActivationState(), false, "Activation state should be: deactivated");
          is(plugin2.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

          // Bring our window back to the front and make sure plugins were properly notified.
          fm.focusedWindow = window;

          waitForFocus(function() {
            // Make sure the plugin handled the focus event before checking.
            executeSoon(function() {
              expectedEventCount++;

              is(plugin1.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
              is(plugin1.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

              is(plugin2.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
              is(plugin2.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

              window.opener.testsFinished();
            });
          }, window);
        });
      }, window.opener);

      // Send our window to the back and make sure plugins were properly notified.
      // Calling window.blur() is not allowed.
      fm.focusedWindow = window.opener;
    }
  </script>
</body>
</html>