summaryrefslogtreecommitdiffstats
path: root/widget/tests/file_test_ime_state_in_text_control_on_reframe.js
blob: 719022b889778a8d3c9b2e96ef27c38c15791e2e (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
/* 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 */

// Bug 580388 and bug 808287
class IMEStateInTextControlOnReframeTester {
  static #sTextControls = [
    {
      tag: "input",
      type: "text",
    },
    {
      tag: "input",
      type: "password",
    },
    {
      tag: "textarea",
    },
  ];

  static get numberOfTextControlTypes() {
    return IMEStateInTextControlOnReframeTester.#sTextControls.length;
  }

  #createElement() {
    const textControl = this.#mDocument.createElement(this.#mTextControl.tag);
    if (this.#mTextControl.type !== undefined) {
      textControl.setAttribute("type", this.#mTextControl.type);
    }
    return textControl;
  }

  #getDescription() {
    return `<${this.#mTextControl.tag}${
      this.#mTextControl.type !== undefined
        ? ` type=${this.#mTextControl.type}`
        : ""
    }>`;
  }

  #getExpectedIMEState() {
    return this.#mTextControl.type == "password"
      ? SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_PASSWORD
      : SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED;
  }

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

  // Runner only fields.
  #mTextControl;
  #mTextControlElement;
  #mWindow;
  #mDocument;

  // Checker only fields.
  #mWindowUtils;
  #mTIPWrapper;

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

  /**
   * @param {number} aIndex Index of the test.
   * @param {Element} aDocument The document to run the test.
   * @param {Window} aWindow [optional] The DOM window for aDocument.
   * @returns {object} Expected result of initial state.
   */
  async prepareToRun(aIndex, aDocument, aWindow = window) {
    this.#mWindow = aWindow;
    this.#mDocument = aDocument;
    this.#mDocument.activeElement?.blur();
    this.#mTextControlElement?.remove();
    await this.#flushPendingIMENotifications();
    this.#mTextControl =
      IMEStateInTextControlOnReframeTester.#sTextControls[aIndex];
    this.#mTextControlElement = this.#createElement();
    this.#mDocument.body.appendChild(this.#mTextControlElement);
    this.#mTextControlElement.focus();
    this.#mTextControlElement.style.overflow = "visible";
    this.#mTextControlElement.addEventListener(
      "input",
      aEvent => {
        aEvent.target.style.overflow = "hidden";
      },
      {
        capture: true,
      }
    );
    await this.#flushPendingIMENotifications();
    const expectedIMEState = this.#getExpectedIMEState();
    return {
      description: `when ${this.#getDescription()} has focus`,
      expectedIMEState,
      expectedIMEFocus:
        expectedIMEState !=
        SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      expectedNumberOfFocusNotifications: 1,
    };
  }

  #checkResult(aExpectedResult) {
    const description = "IMEStateInTextControlOnReframeTester";
    is(
      this.#mWindowUtils.IMEStatus,
      aExpectedResult.expectedIMEState,
      `${description}: IME state should be proper one for the text control ${aExpectedResult.description}`
    );
    is(
      this.#mTIPWrapper.IMEHasFocus,
      aExpectedResult.expectedIMEFocus,
      `${description}: IME should ${
        aExpectedResult.expectedIMEFocus ? "" : "not "
      }have focus ${aExpectedResult.description}`
    );
    if (aExpectedResult.numberOfFocusNotifications !== undefined) {
      is(
        this.#mTIPWrapper.numberOfFocusNotifications,
        aExpectedResult.numberOfFocusNotifications,
        `${description}: focus notifications should've been received ${
          this.#mTIPWrapper.numberOfFocusNotifications
        } times ${aExpectedResult.description}`
      );
    }
    if (aExpectedResult.numberOfBlurNotifications !== undefined) {
      is(
        this.#mTIPWrapper.numberOfBlurNotifications,
        aExpectedResult.numberOfBlurNotifications,
        `${description}: blur notifications should've been received ${
          this.#mTIPWrapper.numberOfBlurNotifications
        } times ${aExpectedResult.description}`
      );
    }
  }

  /**
   * @param {object} aExpectedResult The expected result returned by prepareToRun().
   * @param {Window} aWindow The window whose IME state should be checked.
   * @param {TIPWrapper} aTIPWrapper The TIP wrapper of aWindow.
   */
  checkResultAfterTypingA(aExpectedResult, aWindow, aTIPWrapper) {
    this.#mWindowUtils = SpecialPowers.wrap(aWindow).windowUtils;
    this.#mTIPWrapper = aTIPWrapper;
    this.#checkResult(aExpectedResult);

    this.#mTIPWrapper.clearFocusBlurNotifications();
  }

  async prepareToRun2() {
    this.#mTextControlElement.addEventListener("focus", aEvent => {
      // Perform a style change and flush it to trigger reframing.
      aEvent.target.style.overflow = "visible";
      aEvent.target.getBoundingClientRect();
    });
    this.#mTextControlElement.blur();
    this.#mTextControlElement.focus();

    await this.#flushPendingIMENotifications();

    const expectedIMEState = this.#getExpectedIMEState();
    return {
      description: `when ${this.#getDescription()} is reframed by focus event listener`,
      expectedIMEState,
      expectedIMEFocus:
        expectedIMEState !=
        SpecialPowers.Ci.nsIDOMWindowUtils.IME_STATUS_DISABLED,
      expectedNumberOfFocusNotifications: 1,
      expectedNumberOfBlurNotifications: 1,
    };
  }

  /**
   * @param {object} aExpectedResult The expected result returned by prepareToRun().
   */
  checkResultAfterTypingA2(aExpectedResult) {
    this.#checkResult(aExpectedResult);

    this.#mTIPWrapper.clearFocusBlurNotifications();
  }
}