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

"use strict";

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

class IMEStateOnReadonlyChangeTester {
  static #sTextControlTypes = [
    {
      description: "<input>",
      createElement: aDoc => aDoc.createElement("input"),
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
    {
      description: `<input type="text">`,
      createElement: aDoc => {
        const input = aDoc.createElement("input");
        input.setAttribute("type", "text");
        return input;
      },
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
    {
      description: `<input type="password">`,
      createElement: aDoc => {
        const input = aDoc.createElement("input");
        input.setAttribute("type", "password");
        return input;
      },
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_PASSWORD,
    },
    {
      description: `<input type="url">`,
      createElement: aDoc => {
        const input = aDoc.createElement("input");
        input.setAttribute("type", "url");
        return input;
      },
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
    {
      description: `<input type="email">`,
      createElement: aDoc => {
        const input = aDoc.createElement("input");
        input.setAttribute("type", "email");
        return input;
      },
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
    {
      description: `<input type="search">`,
      createElement: aDoc => {
        const input = aDoc.createElement("input");
        input.setAttribute("type", "search");
        return input;
      },
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
    {
      description: `<input type="tel">`,
      createElement: aDoc => {
        const input = aDoc.createElement("input");
        input.setAttribute("type", "tel");
        return input;
      },
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
    {
      description: `<input type="number">`,
      createElement: aDoc => {
        const input = aDoc.createElement("input");
        input.setAttribute("type", "number");
        return input;
      },
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
    {
      description: `<textarea></textarea>`,
      createElement: aDoc => aDoc.createElement("textarea"),
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED,
    },
  ];

  static get numberOfTextControlTypes() {
    return IMEStateOnReadonlyChangeTester.#sTextControlTypes.length;
  }

  // Only runner fields
  #mTextControl;
  #mTextControlElement;
  #mWindow;

  // Only checker fields
  #mWindowUtils;
  #mTIPWrapper;

  clear() {
    this.#mTextControlElement?.remove();
    this.#mTIPWrapper?.clearFocusBlurNotifications();
    this.#mTIPWrapper = null;
  }

  #flushPendingIMENotifications() {
    return new Promise(resolve =>
      this.#mWindow.requestAnimationFrame(() =>
        this.#mWindow.requestAnimationFrame(resolve)
      )
    );
  }

  /**
   * @param {number} aIndex The index of text control types.
   * @param {Window} aWindow The window running tests.
   * @param {Element} aContainer The element which should have new <input> or <textarea>.
   * @returns {object} Expected data before running tests.
   */
  async prepareToRun(aIndex, aWindow, aContainer) {
    this.#mWindow = aWindow;
    this.#mTextControl =
      IMEStateOnReadonlyChangeTester.#sTextControlTypes[aIndex];

    if (aContainer.ownerDocument.activeElement) {
      aContainer.ownerDocument.activeElement.blur();
      await this.#flushPendingIMENotifications();
    }
    if (this.#mTextControlElement?.isConnected) {
      this.#mTextControlElement.remove();
    }
    this.#mTextControlElement = this.#mTextControl.createElement(
      aContainer.ownerDocument
    );

    const waitForFocus = new Promise(resolve =>
      this.#mTextControlElement.addEventListener("focus", resolve, {
        once: true,
      })
    );
    aContainer.appendChild(this.#mTextControlElement);
    this.#mTextControlElement.focus();
    await waitForFocus;

    await this.#flushPendingIMENotifications();

    return {
      description: this.#mTextControl.description,
      expectedIMEState: this.#mTextControl.expectedIMEState,
      expectedIMEFocus: true,
    };
  }

  /**
   * @param {object} aExpectedData The expected data which was returned by prepareToRun().
   * @param {TIPWrapper} aTIPWrapper TIP wrapper in aWindow.
   * @param {Window} aWindow [optional] The window object of which you want to check IME state.
   */
  checkBeforeRun(aExpectedData, aTIPWrapper, aWindow = window) {
    this.#mWindowUtils = SpecialPowers.wrap(aWindow).windowUtils;
    this.#mTIPWrapper = aTIPWrapper;
    const description = `IMEStateOnReadonlyChangeTester(${aExpectedData.description})`;
    is(
      this.#mWindowUtils.IMEStatus,
      aExpectedData.expectedIMEState,
      `${description}: IME state should be set to expected value before setting readonly attribute`
    );
    is(
      this.#mTIPWrapper.IMEHasFocus,
      aExpectedData.expectedIMEFocus,
      `${description}: IME state should ${
        aExpectedData.expectedIMEFocus ? "" : "not"
      } have focus before setting readonly attribute`
    );
  }

  /**
   * @returns {object} Expected result.
   */
  async runToMakeTextControlReadonly() {
    this.#mTextControlElement.setAttribute("readonly", "");

    await this.#flushPendingIMENotifications();

    return {
      description: this.#mTextControl.description,
      expectedIMEState: SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      expectedIMEFocus: false,
    };
  }

  /**
   * @param {object} aExpectedData Expected result which is returned by runToMakeTextControlReadonly().
   */
  checkResultOfMakingTextControlReadonly(aExpectedData) {
    const description = `IMEStateOnReadonlyChangeTester(${aExpectedData.description})`;
    is(
      this.#mWindowUtils.IMEStatus,
      aExpectedData.expectedIMEState,
      `${description}: IME state should be set to expected value after setting readonly attribute`
    );
    is(
      this.#mTIPWrapper.IMEHasFocus,
      aExpectedData.expectedIMEFocus,
      `${description}: IME state should ${
        aExpectedData.expectedIMEFocus ? "" : "not"
      } have focus after setting readonly attribute`
    );
  }

  /**
   * @returns {object} Expected result.
   */
  async runToMakeTextControlEditable() {
    this.#mTextControlElement.removeAttribute("readonly");

    await this.#flushPendingIMENotifications();

    return {
      description: this.#mTextControl.description,
      expectedIMEState: this.#mTextControl.expectedIMEState,
      expectedIMEFocus: true,
    };
  }

  /**
   * @param {object} aExpectedData Expected result which is returned by runToMakeTextControlEditable().
   */
  checkResultOfMakingTextControlEditable(aExpectedData) {
    const description = `IMEStateOnReadonlyChangeTester(${aExpectedData.description})`;
    is(
      this.#mWindowUtils.IMEStatus,
      aExpectedData.expectedIMEState,
      `${description}: IME state should be set to expected value after removing readonly attribute`
    );
    is(
      this.#mTIPWrapper.IMEHasFocus,
      aExpectedData.expectedIMEFocus,
      `${description}: IME state should ${
        aExpectedData.expectedIMEFocus ? "" : "not"
      } have focus after removing readonly attribute`
    );
  }
}