summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/test_instance_re-parent.html
blob: 1ba87adb5b4ced02a851af98b7f7ff158a646707 (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
<!DOCTYPE html>
<html>
<head>
  <title>Test re-parentinging an instance's DOM node</title>
  <script type="text/javascript" src="/MochiKit/packed.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="plugin-utils.js"></script>
</head>
<body onload="begin()">
  <script type="application/javascript;version=1.8">
  SimpleTest.waitForExplicitFinish();
  setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);

  const MAX_CHECK_PLUGIN_STOPPED_ATTEMPTS = 50;
  var numCheckPluginStoppedAttempts = 0;
  var exceptionThrown = false;
  var p = null;
  var d1 = null;
  var d2 = null;

  var destroyed = false;

  function begin() {
    runTests(function() {
      // Callback when finished - set plugin to windowed and repeat the tests

      info("Repeating tests with wmode=window");
      p.setAttribute("wmode", "window");
      d1.appendChild(p);

      // Forces the plugin to be respawned
      p.src = p.src;

      destroyed = false;
      exceptionThrown = false;
      runTests(function () {
        SimpleTest.finish();
      });
    });
  }

  function checkPluginStopped(callback, param) {
    if (numCheckPluginStoppedAttempts < MAX_CHECK_PLUGIN_STOPPED_ATTEMPTS &&
        !destroyed) {
      ++numCheckPluginStoppedAttempts;
      SimpleTest.executeSoon(function() {
          checkPluginStopped(callback, param);
      });
    } else {
      info("Number of check plugin stopped attempts: " +
           numCheckPluginStoppedAttempts);
      callback(param);
    }
  }

  function runTests(callback) {
    p = document.getElementById('plugin1');
    d1 = document.getElementById('div1');
    d2 = document.getElementById('div2');

    // First tests involve moving the instance from one div to another.
    p.startWatchingInstanceCount();
    p.callOnDestroy(function() {
      destroyed = true;
    });

    try {
      d1.removeChild(p);
    } catch (e) {
      exceptionThrown = true;
    }
    is(exceptionThrown, false, "Testing for exception after removeChild.");

    try {
      d2.appendChild(p);
    } catch (e) {
      exceptionThrown = true;
    }
    is(exceptionThrown, false, "Testing for exception after appendChild.");

    is(destroyed, false, "No instances should have been destroyed at this point.");
    is(p.getInstanceCount(), 0, "No new instances should have been created at this point.");

    // Wait for the event loop to spin and ensure the plugin still wasn't touched
    SimpleTest.executeSoon(function() {
      is(destroyed, false, "No instances should have been destroyed at this point.");
      is(p.getInstanceCount(), 0, "No new instances should have been created at this point.");

      d2.removeChild(p);
      checkPluginStopped(continueTestsAfterPluginDestruction, callback);
    });
  }

  function continueTestsAfterPluginDestruction(callback) {
    d2.appendChild(p);
    SimpleTest.executeSoon(function() {
      try {
        is(p.getInstanceCount(), 1, "One new instance should have been created at this point.");
      } catch (e) {
        exceptionThrown = true;
      }
      is(exceptionThrown, false, "Testing for exception getting instance count from plugin.");

      p.stopWatchingInstanceCount();
      callback.apply(null);
    });
  }
  </script>

  <p id="display"></p>

  <div id="div1">
    <!-- This embed has to have a "src" attribute. Part of this test involves seeing if we
         properly restart plugins that have been added back to a document without a change
         in URL. Not re-loading an object when the URL hasn't changed is a shortcut used for
         some object types. Without a URL, this won't be tested. -->
    <embed id="plugin1" src="loremipsum.txt" type="application/x-test" width="200" height="200"></embed>
  </div>
  <div id="div2">
  </div>
</body>
</html>