summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/androidTest/assets/web_extensions/test-support/TestSupportChild.jsm
blob: 58c60a4e6c8c101e5e3de448536cf80a25271373 (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
/* 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/. */

const { GeckoViewActorChild } = ChromeUtils.importESModule(
  "resource://gre/modules/GeckoViewActorChild.sys.mjs"
);

const EXPORTED_SYMBOLS = ["TestSupportChild"];

class TestSupportChild extends GeckoViewActorChild {
  receiveMessage(aMsg) {
    debug`receiveMessage: ${aMsg.name}`;

    switch (aMsg.name) {
      case "FlushApzRepaints":
        return new Promise(resolve => {
          const repaintDone = () => {
            debug`APZ flush done`;
            Services.obs.removeObserver(repaintDone, "apz-repaints-flushed");
            resolve();
          };
          Services.obs.addObserver(repaintDone, "apz-repaints-flushed");
          if (this.contentWindow.windowUtils.flushApzRepaints()) {
            debug`Flushed APZ repaints, waiting for callback...`;
          } else {
            debug`Flushing APZ repaints was a no-op, triggering callback directly...`;
            repaintDone();
          }
        });
      case "PromiseAllPaintsDone":
        return new Promise(resolve => {
          const window = this.contentWindow;
          const utils = window.windowUtils;

          function waitForPaints() {
            // Wait until paint suppression has ended
            if (utils.paintingSuppressed) {
              dump`waiting for paint suppression to end...`;
              window.setTimeout(waitForPaints, 0);
              return;
            }

            if (utils.isMozAfterPaintPending) {
              dump`waiting for paint...`;
              window.addEventListener("MozAfterPaint", waitForPaints, {
                once: true,
              });
              return;
            }
            resolve();
          }
          waitForPaints();
        });
      case "GetLinkColor": {
        const { selector } = aMsg.data;
        const element = this.document.querySelector(selector);
        if (!element) {
          throw new Error("No element for " + selector);
        }
        const color =
          this.contentWindow.windowUtils.getVisitedDependentComputedStyle(
            element,
            "",
            "color"
          );
        return color;
      }
      case "SetResolutionAndScaleTo": {
        return new Promise(resolve => {
          const window = this.contentWindow;
          const { resolution } = aMsg.data;
          window.visualViewport.addEventListener("resize", () => resolve(), {
            once: true,
          });
          window.windowUtils.setResolutionAndScaleTo(resolution);
        });
      }
    }
    return null;
  }
}
const { debug } = TestSupportChild.initLogging("GeckoViewTestSupport");