tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DOMFullscreenTestUtils.sys.mjs (3016B)


      1 /* global content */
      2 const testContext = {
      3  scope: null,
      4  windowGlobal: null,
      5 };
      6 
      7 export var DOMFullscreenTestUtils = {
      8  /**
      9   * Running this init allows helpers to access test scope helpers, like Assert
     10   * and SimpleTest.
     11   * Tests should call init() before using the helpers which rely on properties assigned here.
     12   *
     13   * @param {object} scope The global scope where tests are being run.
     14   * @param {Window} win The DOM Window global
     15   */
     16  init(scope, win) {
     17    if (!scope) {
     18      throw new Error(
     19        "Must initialize DOMFullscreenTestUtils with a test scope"
     20      );
     21    }
     22    if (!win) {
     23      throw new Error(
     24        "Must initialize DOMFullscreenTestUtils with a windowGlobal"
     25      );
     26    }
     27    testContext.scope = scope;
     28    testContext.windowGlobal = win;
     29    testContext.scope.registerCleanupFunction(() => {
     30      delete testContext.scope;
     31      delete testContext.windowGlobal;
     32    });
     33  },
     34 
     35  waitForFullScreenState(browser, state, actionAfterFSEvent) {
     36    return new Promise(resolve => {
     37      let eventReceived = false;
     38 
     39      let observe = () => {
     40        if (!eventReceived) {
     41          return;
     42        }
     43        Services.obs.removeObserver(observe, "fullscreen-painted");
     44        resolve();
     45      };
     46      Services.obs.addObserver(observe, "fullscreen-painted");
     47 
     48      browser.ownerGlobal.addEventListener(
     49        `MozDOMFullscreen:${state ? "Entered" : "Exited"}`,
     50        () => {
     51          eventReceived = true;
     52          if (actionAfterFSEvent) {
     53            actionAfterFSEvent();
     54          }
     55        },
     56        { once: true }
     57      );
     58    });
     59  },
     60 
     61  /**
     62   * Spawns content task in browser to enter / leave fullscreen
     63   *
     64   * @param browser - Browser to use for JS fullscreen requests
     65   * @param {boolean} fullscreenState - true to enter fullscreen, false to leave
     66   * @returns {Promise} - Resolves once fullscreen change is applied
     67   */
     68  async changeFullscreen(browser, fullScreenState) {
     69    if (!testContext.scope) {
     70      throw new Error(
     71        "Must first initialize DOMFullscreenTestUtils with a test scope"
     72      );
     73    }
     74    await new Promise(resolve =>
     75      testContext.scope.SimpleTest.waitForFocus(resolve, browser.ownerGlobal)
     76    );
     77    let fullScreenChange = DOMFullscreenTestUtils.waitForFullScreenState(
     78      browser,
     79      fullScreenState
     80    );
     81    testContext.windowGlobal.SpecialPowers.spawn(
     82      browser,
     83      [fullScreenState],
     84      async state => {
     85        // Wait for document focus before requesting full-screen
     86        const { ContentTaskUtils } = ChromeUtils.importESModule(
     87          "resource://testing-common/ContentTaskUtils.sys.mjs"
     88        );
     89        await ContentTaskUtils.waitForCondition(
     90          () => content.browsingContext.isActive && content.document.hasFocus(),
     91          "Waiting for document focus"
     92        );
     93        if (state) {
     94          content.document.body.requestFullscreen();
     95        } else {
     96          content.document.exitFullscreen();
     97        }
     98      }
     99    );
    100    return fullScreenChange;
    101  },
    102 };