summaryrefslogtreecommitdiffstats
path: root/widget/tests/browser/browser_test_InputContextURI.js
blob: 52f05d90f94b1b04211f46e06388afeef5acf007 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const gDOMWindowUtils = EventUtils._getDOMWindowUtils(window);

function promiseURLBarFocus() {
  const waitForFocusInURLBar = BrowserTestUtils.waitForEvent(gURLBar, "focus");
  gURLBar.blur();
  gURLBar.focus();
  return Promise.all([
    waitForFocusInURLBar,
    TestUtils.waitForCondition(
      () =>
        gDOMWindowUtils.IMEStatus === Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED &&
        gDOMWindowUtils.inputContextOrigin ===
          Ci.nsIDOMWindowUtils.INPUT_CONTEXT_ORIGIN_MAIN
    ),
  ]);
}

function promiseIMEStateEnabledByRemote() {
  return TestUtils.waitForCondition(
    () =>
      gDOMWindowUtils.IMEStatus === Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED &&
      gDOMWindowUtils.inputContextOrigin ===
        Ci.nsIDOMWindowUtils.INPUT_CONTEXT_ORIGIN_CONTENT
  );
}

async function test_url_bar_url(aDesc) {
  await promiseURLBarFocus();

  is(
    gDOMWindowUtils.inputContextURI,
    null,
    `When the search bar has focus, input context URI should be null because of in chrome document (${aDesc})`
  );
}

async function test_input_in_http_or_https(aIsHTTPS) {
  await promiseURLBarFocus();

  const scheme = aIsHTTPS ? "https" : "http";
  const url = `${scheme}://example.com/browser/toolkit/content/tests/browser/file_empty.html`;
  await BrowserTestUtils.withNewTab(url, async browser => {
    ok(browser.isRemoteBrowser, "This test passes only in e10s mode");

    await SpecialPowers.spawn(browser, [], async () => {
      content.document.body.innerHTML = "<input>";
      const input = content.document.querySelector("input");
      input.focus();

      // Wait for a tick for flushing IMEContentObserver's pending notifications.
      await new Promise(resolve =>
        content.requestAnimationFrame(() =>
          content.requestAnimationFrame(resolve)
        )
      );
    });

    await promiseIMEStateEnabledByRemote();
    if (!gDOMWindowUtils.inputContextURI) {
      ok(
        false,
        `Input context should have valid URI when the scheme of focused tab's URL is ${scheme}`
      );
      return;
    }
    is(
      gDOMWindowUtils.inputContextURI.spec,
      url,
      `Input context should have the document URI when the scheme of focused tab's URL is ${scheme}`
    );
  });
}

add_task(async () => {
  await test_url_bar_url("first check");
});
add_task(async () => {
  await test_input_in_http_or_https(true);
});
add_task(async () => {
  await test_url_bar_url("check after remote content sets the URI");
});
add_task(async () => {
  await test_input_in_http_or_https(false);
});

add_task(async function test_input_in_data() {
  await BrowserTestUtils.withNewTab("data:text/html,<input>", async browser => {
    ok(browser.isRemoteBrowser, "This test passes only in e10s mode");

    await SpecialPowers.spawn(browser, [], async () => {
      const input = content.document.querySelector("input");
      input.focus();

      // Wait for a tick for flushing IMEContentObserver's pending notifications.
      await new Promise(resolve =>
        content.requestAnimationFrame(() =>
          content.requestAnimationFrame(resolve)
        )
      );
    });

    await promiseIMEStateEnabledByRemote();
    is(
      gDOMWindowUtils.inputContextURI,
      null,
      "Input context should not have data URI"
    );
  });
});

add_task(async function test_omit_private_things_in_URL() {
  await SpecialPowers.pushPrefEnv({
    set: [["network.auth.confirmAuth.enabled", false]],
  });
  await promiseURLBarFocus();

  await BrowserTestUtils.withNewTab(
    "https://username:password@example.com/browser/toolkit/content/tests/browser/file_empty.html?query=some#ref",
    async browser => {
      ok(browser.isRemoteBrowser, "This test passes only in e10s mode");

      await SpecialPowers.spawn(browser, [], async () => {
        content.document.body.innerHTML = "<input>";
        const input = content.document.querySelector("input");
        input.focus();

        // Wait for a tick for flushing IMEContentObserver's pending notifications.
        await new Promise(resolve =>
          content.requestAnimationFrame(() =>
            content.requestAnimationFrame(resolve)
          )
        );
      });

      await promiseIMEStateEnabledByRemote();
      if (!gDOMWindowUtils.inputContextURI) {
        ok(
          false,
          `Input context should have valid URI even when the URL contains some private things`
        );
        return;
      }
      is(
        gDOMWindowUtils.inputContextURI.spec,
        "https://example.com/browser/toolkit/content/tests/browser/file_empty.html",
        `Input context should have the document URI which omit some private things in the URL`
      );
    }
  );
});