summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/test/AppUiTestDelegate.sys.mjs
blob: 8f880251ef256c07311f00f8db81276773040918 (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
/* 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 lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  EventDispatcher: "resource://gre/modules/Messaging.sys.mjs",
  GeckoViewTabBridge: "resource://gre/modules/GeckoViewTab.sys.mjs",
  mobileWindowTracker: "resource://gre/modules/GeckoViewWebExtension.sys.mjs",
});

const TEST_SUPPORT_EXTENSION_ID = "test-runner-support@tests.mozilla.org";

/**
 * The implementation of AppUiTestDelegate. All implementations need to be kept
 * in sync. For details, see:
 * testing/specialpowers/content/AppTestDelegateParent.sys.mjs
 *
 * This implementation mostly forwards calls to TestRunnerApiEngine in
 * mobile/android/test_runner/src/main/java/org/mozilla/geckoview/test_runner/TestRunnerApiEngine.java
 */
class Delegate {
  _sendMessageToApp(data) {
    // "GeckoView:WebExtension:Message" with the "nativeApp" property set is a
    // message usually emitted by the runtime.sendNativeMessage implementation.
    //
    // Although a dummy extension with ID TEST_SUPPORT_EXTENSION_ID is installed
    // by TestRunnerActivity, the sendNativeMessage API is not used directly.
    // Instead, we forge a message in the same (internal) format here.
    //
    // The message is ultimately received and handled by TestRunnerApiEngine at
    // mobile/android/test_runner/src/main/java/org/mozilla/geckoview/test_runner/TestRunnerApiEngine.java
    const message = {
      type: "GeckoView:WebExtension:Message",
      sender: {
        envType: "addon_child",
        url: "test-runner-support:///",
      },
      data,
      extensionId: TEST_SUPPORT_EXTENSION_ID,
      nativeApp: "test-runner-support",
    };

    return lazy.EventDispatcher.instance.sendRequestForResult(message);
  }

  clickPageAction(window, extensionId) {
    return this._sendMessageToApp({ type: "clickPageAction", extensionId });
  }

  clickBrowserAction(window, extensionId) {
    return this._sendMessageToApp({ type: "clickBrowserAction", extensionId });
  }

  closePageAction(window, extensionId) {
    return this._sendMessageToApp({ type: "closePageAction", extensionId });
  }

  closeBrowserAction(window, extensionId) {
    return this._sendMessageToApp({ type: "closeBrowserAction", extensionId });
  }

  awaitExtensionPanel(window, extensionId) {
    return this._sendMessageToApp({ type: "awaitExtensionPanel", extensionId });
  }

  async removeTab(tab) {
    const window = tab.browser.ownerGlobal;
    await lazy.GeckoViewTabBridge.closeTab({
      window,
      extensionId: TEST_SUPPORT_EXTENSION_ID,
    });
  }

  async openNewForegroundTab(window, url, waitForLoad = true) {
    const tab = await lazy.GeckoViewTabBridge.createNewTab({
      extensionId: TEST_SUPPORT_EXTENSION_ID,
      createProperties: {
        url,
        active: true,
      },
    });

    const { browser } = tab;
    const triggeringPrincipal =
      Services.scriptSecurityManager.createContentPrincipal(
        Services.io.newURI(url),
        {}
      );

    browser.fixupAndLoadURIString(url, {
      flags: Ci.nsIWebNavigation.LOAD_FLAGS_NONE,
      triggeringPrincipal,
    });

    const newWindow = browser.ownerGlobal;
    lazy.mobileWindowTracker.setTabActive(newWindow, true);

    if (!waitForLoad) {
      return tab;
    }

    return new Promise(resolve => {
      const listener = ev => {
        const { browsingContext, internalURL } = ev.detail;

        // Sometimes we arrive here without an internalURL. If that's the
        // case, just keep waiting until we get one.
        if (!internalURL || internalURL == "about:blank") {
          return;
        }

        // Ignore subframes
        if (browsingContext !== browsingContext.top) {
          return;
        }

        resolve(tab);
        browser.removeEventListener("AppTestDelegate:load", listener, true);
      };
      browser.addEventListener("AppTestDelegate:load", listener, true);
    });
  }
}

export var AppUiTestDelegate = new Delegate();