summaryrefslogtreecommitdiffstats
path: root/widget/tests/test_ime_state_on_editable_state_change_in_parent.html
blob: a1b307a51f3952c2af0f95a133679b789b214e14 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for IME state management at changing editable state</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="file_ime_state_test_helper.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<div></div>
<script>
"use strict";

/* import-globals-from file_ime_state_test_helper.js */

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  const tipWrapper = new TIPWrapper(window);

  function waitForIMEContentObserverSendingNotifications() {
    return new Promise(
      resolve => requestAnimationFrame(
        () => requestAnimationFrame(resolve)
      )
    );
  }

  function resetIMEStateWithFocusMove() {
    const input = document.createElement("input");
    document.body.appendChild(input);
    input.focus();
    input.remove();
    return waitForIMEContentObserverSendingNotifications();
  }

  await (async function test_setting_contenteditable_of_focused_div() {
    const div = document.querySelector("div");
    div.setAttribute("tabindex", "0");
    div.focus();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      "test_setting_contenteditable_of_focused_div: IME should be disabled when non-editable <div> has focus"
    );
    div.setAttribute("contenteditable", "");
    await waitForIMEContentObserverSendingNotifications();
    // Sometimes, it's not enough waiting only 2 animation frames here to wait
    // for IME focus, perhaps, it may be related to HTMLEditor initialization.
    // Let's wait one more animation frame here.
    if (!tipWrapper.IMEHasFocus) {
      await new Promise(resolve => requestAnimationFrame(resolve));
    }
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
      "test_setting_contenteditable_of_focused_div: IME should be enabled when contenteditable of focused <div> is set"
    );
    ok(
      tipWrapper.IMEHasFocus,
      "test_setting_contenteditable_of_focused_div: IME should have focus when contenteditable of focused <div> is set"
    )
    div.removeAttribute("contenteditable");
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      "test_setting_contenteditable_of_focused_div: IME should be disabled when contenteditable of focused <div> is removed"
    );
    ok(
      !tipWrapper.IMEHasFocus,
      "test_setting_contenteditable_of_focused_div: IME should not have focus when contenteditable of focused <div> is removed"
    );
    div.removeAttribute("tabindex");
  })();

  await resetIMEStateWithFocusMove();

  await (async function test_removing_contenteditable_of_non_last_editable_div() {
    const div = document.querySelector("div");
    div.setAttribute("tabindex", "0");
    div.setAttribute("contenteditable", "");
    const anotherEditableDiv = document.createElement("div");
    anotherEditableDiv.setAttribute("contenteditable", "");
    div.parentElement.appendChild(anotherEditableDiv);
    div.focus();
    await waitForIMEContentObserverSendingNotifications();
    div.removeAttribute("contenteditable");
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      "test_removing_contenteditable_of_non_last_editable_div: IME should be disabled when contenteditable of focused <div> is removed"
    );
    ok(
      !tipWrapper.IMEHasFocus,
      "test_removing_contenteditable_of_non_last_editable_div: IME should not have focus when contenteditable of focused <div> is removed"
    );
    anotherEditableDiv.remove();
    div.removeAttribute("tabindex");
  })();

  await resetIMEStateWithFocusMove();

  await (async function test_setting_designMode() {
    window.focus();
    document.designMode = "on";
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
      'test_setting_designMode: IME should be enabled when designMode is set to "on"'
    );
    ok(
      tipWrapper.IMEHasFocus,
      'test_setting_designMode: IME should have focus when designMode is set to "on"'
    );
    document.designMode = "off";
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      'test_setting_designMode: IME should be disabled when designMode is set to "off"'
    );
    ok(
      !tipWrapper.IMEHasFocus,
      'test_setting_designMode: IME should not have focus when designMode is set to "off"'
    );
  })();

  await resetIMEStateWithFocusMove();

  async function test_setting_content_editable_of_body_when_shadow_DOM_has_focus(aMode) {
    const div = document.querySelector("div");
    const shadow = div.attachShadow({mode: aMode});
    const divInShadow = document.createElement("div");
    divInShadow.setAttribute("tabindex", "0");
    shadow.appendChild(divInShadow);
    divInShadow.focus();
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      `test_setting_content_editable_of_body_when_shadow_DOM_has_focus(${
        aMode
      }): IME should be disabled when non-editable <div> in a shadow DOM has focus`
    );
    document.body.setAttribute("contenteditable", "");
    await waitForIMEContentObserverSendingNotifications();
    // todo_is because of bug 1807597.  Gecko does not update focus when focused
    // element becomes an editable child.  Therefore, cannot initialize
    // HTMLEditor with the new editing host.
    todo_is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
      `test_setting_content_editable_of_body_when_shadow_DOM_has_focus(${
        aMode
      }): IME should be enabled when the <body> becomes editable`
    );
    todo(
      tipWrapper.IMEHasFocus,
      `test_setting_content_editable_of_body_when_shadow_DOM_has_focus(${
        aMode
      }): IME should have focus when the <body> becomes editable`
    );
    document.body.removeAttribute("contenteditable");
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      `test_setting_content_editable_of_body_when_shadow_DOM_has_focus)${
        aMode
      }): IME should be disabled when the <body> becomes not editable`
    );
    ok(
      !tipWrapper.IMEHasFocus,
      `test_setting_content_editable_of_body_when_shadow_DOM_has_focus)${
        aMode
      }): IME should not have focus when the <body> becomes not editable`
    );
    div.remove();
    document.body.appendChild(document.createElement("div"));
  };

  async function test_setting_designMode_when_shadow_DOM_has_focus(aMode) {
    const div = document.querySelector("div");
    const shadow = div.attachShadow({mode: aMode});
    const divInShadow = document.createElement("div");
    divInShadow.setAttribute("tabindex", "0");
    shadow.appendChild(divInShadow);
    divInShadow.focus();
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      `test_setting_designMode_when_shadow_DOM_has_focus(${
        aMode
      }): IME should be disabled when non-editable <div> in a shadow DOM has focus`
    );
    document.designMode = "on";
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      `test_setting_designMode_when_shadow_DOM_has_focus(${
        aMode
      }): IME should stay disabled when designMode is set`
    );
    ok(
      !tipWrapper.IMEHasFocus,
      `test_setting_designMode_when_shadow_DOM_has_focus(${
        aMode
      }): IME should not have focus when designMode is set`
    );
    divInShadow.setAttribute("contenteditable", "");
    await waitForIMEContentObserverSendingNotifications();
    // todo_is because of bug 1807597.  Gecko does not update focus when focused
    // document is into the design mode.  Therefore, cannot initialize
    // HTMLEditor with the document node properly.
    todo_is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
      `test_setting_designMode_when_shadow_DOM_has_focus(${
        aMode
      }): IME should be enabled when focused <div> in a shadow DOM becomes editable`
    );
    todo(
      tipWrapper.IMEHasFocus,
      `test_setting_designMode_when_shadow_DOM_has_focus(${
        aMode
      }): IME should have focus when focused <div> in a shadow DOM becomes editable`
    );
    document.designMode = "off";
    await waitForIMEContentObserverSendingNotifications();
    is(
      window.windowUtils.IMEStatus,
      Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      `test_setting_designMode_when_shadow_DOM_has_focus(${
        aMode
      }): IME should be disabled when designMode is unset`
    );
    ok(
      !tipWrapper.IMEHasFocus,
      `test_setting_designMode_when_shadow_DOM_has_focus(${
        aMode
      }): IME should not have focus when designMode is unset`
    );
    div.remove();
    document.body.appendChild(document.createElement("div"));
  }

  for (const mode of ["open", "closed"]) {
    await test_setting_content_editable_of_body_when_shadow_DOM_has_focus(mode);
    await resetIMEStateWithFocusMove();
    await test_setting_designMode_when_shadow_DOM_has_focus(mode);
    await resetIMEStateWithFocusMove();
  }

  SimpleTest.finish();
});
</script>
</body>
</html>