summaryrefslogtreecommitdiffstats
path: root/dom/inputmethod/mochitest/bug1110030_helper.js
blob: 54f15825bf38a7b2451ae119aba4174c9860b836 (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
264
265
266
267
// ***********************************
// * Global variables
// ***********************************
const kIsWin = navigator.platform.indexOf("Win") == 0;

// Bit value for the keyboard events
const kKeyDown  = 0x01;
const kKeyPress = 0x02;
const kKeyUp    = 0x04;

// Pair the event name to its bit value
const kEventCode = {
  'keydown'   : kKeyDown,
  'keypress'  : kKeyPress,
  'keyup'     : kKeyUp
};

// Holding the current test case's infomation:
var gCurrentTest;

// The current used input method of this test
var gInputMethod;

// ***********************************
// * Utilities
// ***********************************
function addKeyEventListeners(eventTarget, handler)
{
  Object.keys(kEventCode).forEach(function(type) {
    eventTarget.addEventListener(type, handler);
  });
}

function eventToCode(type)
{
  return kEventCode[type];
}

// To test key events that will be generated by input method here,
// we need to convert alphabets to native key code.
// (Our input method for testing will handle alphabets)
// On the other hand, to test key events that will not be generated by IME,
// we use 0-9 for such case in our testing.
function guessNativeKeyCode(key)
{
  let nativeCodeName = (kIsWin)? 'WIN_VK_' : 'MAC_VK_ANSI_';
  if (/^[A-Z]$/.test(key)) {
    nativeCodeName += key;
  } else if (/^[a-z]$/.test(key)) {
    nativeCodeName += key.toUpperCase();
  } else if (/^[0-9]$/.test(key)) {
    nativeCodeName += key.toString();
  } else {
    return 0;
  }

  return eval(nativeCodeName);
}

// ***********************************
// * Frame loader and frame scripts
// ***********************************
function frameScript()
{
  function handler(e) {
    sendAsyncMessage("forwardevent", { type: e.type, key: e.key });
  }
  function notifyFinish(e) {
    if (e.type != 'keyup') return;
    sendAsyncMessage("finish");
  }
  let input = content.document.getElementById('test-input');
  input.addEventListener('keydown', handler);
  input.addEventListener('keypress', handler);
  input.addEventListener('keyup', handler);
  input.addEventListener('keyup', notifyFinish);
}

function loadTestFrame(goNext) {
  let iframe = document.createElement('iframe');
  iframe.src = 'file_test_empty_app.html';
  iframe.setAttribute('mozbrowser', true);

  iframe.addEventListener("mozbrowserloadend", function onloadend() {
    iframe.removeEventListener("mozbrowserloadend", onloadend);
    iframe.focus();
    var mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
    mm.addMessageListener("forwardevent", function(msg) {
      inputtextEventReceiver(msg.json);
    });
    mm.addMessageListener("finish", function(msg) {
      if(goNext) {
        goNext();
      }
    });
    mm.loadFrameScript("data:,(" + frameScript.toString() + ")();", false);
    return;
  });

  document.body.appendChild(iframe);
}

// ***********************************
// * Event firer and listeners
// ***********************************
function fireEvent(callback)
{
  let key = gCurrentTest.key;
  synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, guessNativeKeyCode(key), {},
                      key, key, (callback) ? callback : null);
}

function hardwareEventReceiver(evt)
{
  if (!gCurrentTest) {
    return;
  }
  gCurrentTest.hardwareinput.receivedEvents |= eventToCode(evt.type);
  gCurrentTest.hardwareinput.receivedKeys += evt.key;
}

function inputtextEventReceiver(evt)
{
  if (!gCurrentTest) {
    return;
  }
  gCurrentTest.inputtext.receivedEvents |= eventToCode(evt.type);
  gCurrentTest.inputtext.receivedKeys += evt.key;
}

// ***********************************
// * Event verifier
// ***********************************
function verifyResults(test)
{
  // Verify results received from inputcontent.hardwareinput
  is(test.hardwareinput.receivedEvents,
     test.hardwareinput.expectedEvents,
     "received events from inputcontent.hardwareinput are wrong");

  is(test.hardwareinput.receivedKeys,
     test.hardwareinput.expectedKeys,
     "received keys from inputcontent.hardwareinput are wrong");

  // Verify results received from actual input text
  is(test.inputtext.receivedEvents,
     test.inputtext.expectedEvents,
     "received events from input text are wrong");

  is(test.inputtext.receivedKeys,
     test.inputtext.expectedKeys,
     "received keys from input text are wrong");
}

function areEventsSame(test)
{
  return (test.hardwareinput.receivedEvents ==
          test.hardwareinput.expectedEvents) &&
         (test.inputtext.receivedEvents ==
          test.inputtext.expectedEvents);
}

// ***********************************
// * Input Method
// ***********************************
// The method input used in this test
// only handles alphabets
function InputMethod(inputContext)
{
  this._inputContext = inputContext;
  this.init();
}

InputMethod.prototype = {
  init: function im_init() {
    this._setKepMap();
  },

  handler: function im_handler(evt) {
    // Ignore the key if the event is defaultPrevented
    if (evt.defaultPrevented) {
      return;
    }

    // Finish if there is no _inputContext
    if (!this._inputContext) {
      return;
    }

    // Generate the keyDict for inputcontext.keydown/keyup
    let keyDict = this._generateKeyDict(evt);

    // Ignore the key if IME doesn't want to handle it
    if (!keyDict) {
      return;
    }

    // Call preventDefault if the key will be handled.
    evt.preventDefault();

    // Call inputcontext.keydown/keyup
    this._inputContext[evt.type](keyDict);
  },

  mapKey: function im_keymapping(key) {
    if (!this._mappingTable) {
      return;
    }
    return this._mappingTable[key];
  },

  _setKepMap: function im_setKeyMap() {
    // A table to map characters:
    // {
    //   'A': 'B'
    //   'a': 'b'
    //   'B': 'C'
    //   'b': 'c'
    //   ..
    //   ..
    //   'Z': 'A',
    //   'z': 'a',
    // }
    this._mappingTable = {};

    let rotation = 1;

    for (let i = 0 ; i < 26 ; i++) {
      // Convert 'A' to 'B', 'B' to 'C', ..., 'Z' to 'A'
      this._mappingTable[String.fromCharCode(i + 'A'.charCodeAt(0))] =
        String.fromCharCode((i+rotation)%26 + 'A'.charCodeAt(0));

      // Convert 'a' to 'b', 'b' to 'c', ..., 'z' to 'a'
      this._mappingTable[String.fromCharCode(i + 'a'.charCodeAt(0))] =
        String.fromCharCode((i+rotation)%26 + 'a'.charCodeAt(0));
    }
  },

  _generateKeyDict: function im_generateKeyDict(evt) {

    let mappedKey = this.mapKey(evt.key);

    if (!mappedKey) {
      return;
    }

    let keyDict = {
      key: mappedKey,
      code: this._guessCodeFromKey(mappedKey),
      repeat: evt.repeat,
    };

    return keyDict;
  },

  _guessCodeFromKey: function im_guessCodeFromKey(key) {
    if (/^[A-Z]$/.test(key)) {
      return "Key" + key;
    } else if (/^[a-z]$/.test(key)) {
      return "Key" + key.toUpperCase();
    } else if (/^[0-9]$/.test(key)) {
      return "Digit" + key.toString();
    } else {
      return 0;
    }
  },
};