summaryrefslogtreecommitdiffstats
path: root/image/test/browser/browser_offscreen_image_in_out_of_process_iframe.js
blob: 346413b6022f9738585d1390acdad6b75d5b35eb (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const DIRPATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content/",
  ""
);
const parentPATH = DIRPATH + "empty.html";
const iframePATH = DIRPATH + "empty.html";

const parentURL = `http://example.com/${parentPATH}`;
const iframeURL = `http://example.org/${iframePATH}`;

add_task(async function setup_pref() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // To avoid throttling requestAnimationFrame callbacks in invisible
      // iframes
      ["layout.throttled_frame_rate", 60],
    ],
  });
});

add_task(async function () {
  const win = await BrowserTestUtils.openNewBrowserWindow({
    fission: true,
  });

  try {
    const browser = win.gBrowser.selectedTab.linkedBrowser;

    BrowserTestUtils.startLoadingURIString(browser, parentURL);
    await BrowserTestUtils.browserLoaded(browser, false, parentURL);

    async function setup(url) {
      const scroller = content.document.createElement("div");
      scroller.style = "width: 300px; height: 300px; overflow: scroll;";
      scroller.setAttribute("id", "scroller");
      content.document.body.appendChild(scroller);

      // Make a space bigger than display port.
      const spacer = content.document.createElement("div");
      spacer.style = "width: 100%; height: 10000px;";
      scroller.appendChild(spacer);

      const iframe = content.document.createElement("iframe");
      scroller.appendChild(iframe);

      iframe.contentWindow.location = url;
      await new Promise(resolve => {
        iframe.addEventListener("load", resolve, { once: true });
      });

      return iframe.browsingContext;
    }

    async function setupImage() {
      const img = content.document.createElement("img");
      // This GIF is a 100ms interval animation.
      img.setAttribute("src", "animated.gif");
      img.setAttribute("id", "img");
      content.document.body.appendChild(img);

      const spacer = content.document.createElement("div");
      spacer.style = "width: 100%; height: 10000px;";
      content.document.body.appendChild(spacer);
      await new Promise(resolve => {
        img.addEventListener("load", resolve, { once: true });
      });
    }

    // Returns the count of frameUpdate during |time| (in ms) period.
    async function observeFrameUpdate(time) {
      function ImageDecoderObserverStub() {
        this.sizeAvailable = function sizeAvailable() {};
        this.frameComplete = function frameComplete() {};
        this.decodeComplete = function decodeComplete() {};
        this.loadComplete = function loadComplete() {};
        this.frameUpdate = function frameUpdate() {};
        this.discard = function discard() {};
        this.isAnimated = function isAnimated() {};
        this.hasTransparency = function hasTransparency() {};
      }

      // Start from the callback of setTimeout.
      await new Promise(resolve => content.window.setTimeout(resolve, 0));

      let frameCount = 0;
      const observer = new ImageDecoderObserverStub();
      observer.frameUpdate = () => {
        frameCount++;
      };
      observer.loadComplete = () => {
        // Ignore the frameUpdate callback along with loadComplete. It seems
        // a frameUpdate sometimes happens with a loadComplete when attatching
        // observer in fission world.
        frameCount--;
      };

      const gObserver = SpecialPowers.Cc["@mozilla.org/image/tools;1"]
        .getService(SpecialPowers.Ci.imgITools)
        .createScriptedObserver(observer);
      const img = content.document.getElementById("img");

      SpecialPowers.wrap(img).addObserver(gObserver);
      await new Promise(resolve => content.window.setTimeout(resolve, time));
      SpecialPowers.wrap(img).removeObserver(gObserver);

      return frameCount;
    }

    // Setup an iframe which is initially scrolled out.
    const iframe = await SpecialPowers.spawn(browser, [iframeURL], setup);

    // Setup a 100ms interval animated GIF image in the iframe.
    await SpecialPowers.spawn(iframe, [], setupImage);

    let frameCount = await SpecialPowers.spawn(
      iframe,
      [1000],
      observeFrameUpdate
    );
    // Bug 1577084.
    if (frameCount == 0) {
      is(frameCount, 0, "no frameupdates");
    } else {
      todo_is(frameCount, 0, "no frameupdates");
    }

    // Scroll the iframe into view.
    await SpecialPowers.spawn(browser, [], async () => {
      const scroller = content.document.getElementById("scroller");
      scroller.scrollTo({ left: 0, top: 9800, behavior: "smooth" });
      await new Promise(resolve => content.window.setTimeout(resolve, 1000));
    });

    await new Promise(resolve => requestAnimationFrame(resolve));

    frameCount = await SpecialPowers.spawn(iframe, [1000], observeFrameUpdate);
    Assert.greater(frameCount, 0, "There should be frameUpdate(s)");

    await new Promise(resolve => requestAnimationFrame(resolve));

    await SpecialPowers.spawn(iframe, [], async () => {
      const img = content.document.getElementById("img");
      // Move the image outside of the scroll port.  'position: absolute' causes
      // a relow on the image element.
      img.style = "position: absolute; top: 9000px;";
      await new Promise(resolve =>
        content.window.requestAnimationFrame(resolve)
      );
    });

    await new Promise(resolve => requestAnimationFrame(resolve));

    frameCount = await SpecialPowers.spawn(iframe, [1000], observeFrameUpdate);
    is(frameCount, 0, "No frameUpdate should happen");
  } finally {
    await BrowserTestUtils.closeWindow(win);
  }
});