summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/xulbrowser_plugin_visibility.xul
blob: d0873052005fb9cf57c89d051a6b406db8bcfe1d (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
	orient="vertical">

  <tabbox id="tabbox" flex="1">
    <tabs>
      <tab id="tab1" label="Tab 1" />
      <tab id="tab2" label="Tab 2" />
    </tabs>
    <tabpanels flex="1">
      <browser id="browser1" type="content-primary" flex="1" src="about:blank"/>
      <browser id="browser2" type="content-primary" flex="1" src="about:blank"/>
    </tabpanels>
  </tabbox>
  <script type="application/javascript" src="plugin-utils.js"/>
  <script type="application/javascript"><![CDATA[
    const ok = window.opener.wrappedJSObject.ok;
    const is = window.opener.wrappedJSObject.is;
    const done = window.opener.wrappedJSObject.done;
    const SimpleTest = window.opener.wrappedJSObject.SimpleTest;

    const nsIWebProgress = Components.interfaces.nsIWebProgress;
    const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;

    const kURI = 'http://mochi.test:8888/chrome/dom/plugins/test/mochitest/plugin_visibility_loader.html';

    function ProgressListener() {
    }
    ProgressListener.prototype.onStateChange =
      function(progress, req, flags, status) {
        if ((flags & nsIWebProgressListener.STATE_IS_WINDOW) &&
            (flags & nsIWebProgressListener.STATE_STOP))
          browserLoaded();
      };
    ProgressListener.prototype.QueryInterface = function(iid) {
      if (iid.equals(nsIWebProgressListener) ||
          iid.equals(Components.interfaces.nsISupportsWeakReference))
        return this;
      throw Components.results.NS_ERROR_NO_INTERFACE;
    };

    var loadCount = 0;
    function browserLoaded() {
      ++loadCount;
      if (2 == loadCount)
        startTest();
    }

    var tabbox = document.getElementById('tabbox');
    var browser1 = document.getElementById('browser1');
    var browser2 = document.getElementById('browser2');

    var progressListener1, progressListener2;

    function setup() {
      progressListener1 = new ProgressListener();
      browser1.addProgressListener(progressListener1, nsIWebProgress.NOTIFY_STATE_WINDOW);
      browser1.loadURI(kURI, null, null);
      progressListener2 = new ProgressListener();
      browser2.addProgressListener(progressListener2, nsIWebProgress.NOTIFY_STATE_WINDOW);
      browser2.loadURI(kURI, null, null);
    }

    window.addEventListener("load", setup, false);

    var plugin1, plugin2;

    const kTimeout = 5000; // 5 seconds
    var paintGiveUp;
    var paintInterval;

    function startTest() {
      plugin1 = browser1.contentDocument.getElementById('p').wrappedJSObject;
      plugin2 = browser2.contentDocument.getElementById('p').wrappedJSObject;

      paintGiveUp = Date.now() + kTimeout;
      paintInterval = setInterval(waitForPaint, 100);
    }

    function waitForPaint() {
      if (!plugin1.isVisible()) {
        if (Date.now() < paintGiveUp)
          return;

        ok(false, "Plugin in tab 1 never became visible.");
        done();
        return;
      }

      clearInterval(paintInterval);

      ok(true, "Plugin in tab 1 should be visible.");
      paintCountIs(plugin1, 1, "Plugin in tab 1 should have painted once.");

      ok(!plugin2.isVisible(), "Plugin in tab 2 should not be visible.");
      paintCountIs(plugin2, 0, "Plugin in tab 2 should not have painted.");

      tabbox.selectedIndex = 1;
      paintGiveUp = Date.now() + kTimeout;
      paintInterval = setInterval(part2, 100);
    }

    function part2() {
      if (!plugin2.isVisible()) {
        if (Date.now() < paintGiveUp)
          return;

        ok(false, "Plugin in tab 2 never became visible.");
        done();
        return;
      }

      clearInterval(paintInterval);

      ok(true, "Plugin in tab 2 became visible.");
      paintCountIs(plugin2, 1, "Plugin in tab 2 should have painted once.");

      ok(!plugin1.isVisible(), "Plugin in tab 1 should have become invisible.");
      paintCountIs(plugin1, 1, "Plugin in tab 1 should have painted once.");

      // Setcolor invalidates
      plugin1.setColor('FF00FF00');
      plugin2.setColor('FF00FF00');

      setTimeout(part3, 500);
    }

    function part3() {
      paintCountIs(plugin1, 1,
                   "Plugin in tab 1 should not have repainted after invalidate.");
      paintCountIs(plugin2, 2,
                   "Plugin in tab 2 should have repainted after invalidate.");
      done();
    }
  ]]></script>

</window>