summaryrefslogtreecommitdiff
path: root/dom/inputmethod/mochitest/test_basic.html
blob: bf22e99dd44121aaafedbbd633da55b79f45570b (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=932145
-->
<head>
  <title>Basic test for InputMethod API.</title>
  <script type="application/javascript;version=1.7" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=932145">Mozilla Bug 932145</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">

SimpleTest.requestFlakyTimeout("untriaged");

// The input context.
var gContext = null;

inputmethod_setup(function() {
  runTest();
});

function runTest() {
  let im = navigator.mozInputMethod;

  im.oninputcontextchange = function() {
    ok(true, 'inputcontextchange event was fired.');
    im.oninputcontextchange = null;

    gContext = im.inputcontext;
    if (!gContext) {
      ok(false, 'Should have a non-null inputcontext.');
      inputmethod_cleanup();
      return;
    }

    is(gContext.type, 'input', 'The input context type should match.');
    is(gContext.inputType, 'text', 'The inputType should match.');
    is(gContext.inputMode, 'verbatim', 'The inputMode should match.');
    is(gContext.lang, 'zh', 'The language should match.');
    is(gContext.text, 'Yuan', 'Should get the text.');
    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
       'Should get the text around the cursor.');

    test_setSelectionRange();
  };

  // Set current page as an input method.
  SpecialPowers.wrap(im).setActive(true);

  let iframe = document.createElement('iframe');
  iframe.src = 'file_test_app.html';
  iframe.setAttribute('mozbrowser', true);
  document.body.appendChild(iframe);
}

function test_setSelectionRange() {
  // Move cursor position to 2.
  gContext.setSelectionRange(2, 0).then(function() {
    is(gContext.selectionStart, 2, 'selectionStart was set successfully.');
    is(gContext.selectionEnd, 2, 'selectionEnd was set successfully.');
    test_sendKey();
  }, function(e) {
    ok(false, 'setSelectionRange failed:' + e.name);
    console.error(e);
    inputmethod_cleanup();
  });
}

function test_sendKey() {
  // Add '-' to current cursor posistion and move the cursor position to 3.
  gContext.sendKey(0, '-'.charCodeAt(0), 0).then(function() {
    is(gContext.text, 'Yu-an',
       'sendKey should changed the input field correctly.');
    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yu-an',
       'sendKey should changed the input field correctly.');
    test_deleteSurroundingText();
  }, function(e) {
    ok(false, 'sendKey failed:' + e.name);
    inputmethod_cleanup();
  });
}

function test_deleteSurroundingText() {
  // Remove one character before current cursor position and move the cursor
  // position back to 2.
  gContext.deleteSurroundingText(-1, 1).then(function() {
    ok(true, 'deleteSurroundingText finished');
    is(gContext.text, 'Yuan',
       'deleteSurroundingText should changed the input field correctly.');
    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
       'deleteSurroundingText should changed the input field correctly.');
    test_replaceSurroundingText();
  }, function(e) {
    ok(false, 'deleteSurroundingText failed:' + e.name);
    inputmethod_cleanup();
  });
}

function test_replaceSurroundingText() {
  // Replace 'Yuan' with 'Xulei'.
  gContext.replaceSurroundingText('Xulei', -2, 4).then(function() {
    ok(true, 'replaceSurroundingText finished');
    is(gContext.text, 'Xulei',
       'replaceSurroundingText changed the input field correctly.');
    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei',
       'replaceSurroundingText changed the input field correctly.');
    test_setComposition();
  }, function(e) {
    ok(false, 'replaceSurroundingText failed: ' + e.name);
    inputmethod_cleanup();
  });
}

function test_setComposition() {
  gContext.setComposition('XXX').then(function() {
    ok(true, 'setComposition finished');
    test_endComposition();
  }, function(e) {
    ok(false, 'setComposition failed: ' + e.name);
    inputmethod_cleanup();
  });
}

function test_endComposition() {
  gContext.endComposition('2013').then(function() {
    is(gContext.text, 'Xulei2013',
       'endComposition changed the input field correctly.');
    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei2013',
       'endComposition changed the input field correctly.');
    test_onSelectionChange();
  }, function (e) {
    ok(false, 'endComposition failed: ' + e.name);
    inputmethod_cleanup();
  });
}

function test_onSelectionChange() {
  var sccTimeout = setTimeout(function() {
    ok(false, 'selectionchange event not fired');
    cleanup(true);
  }, 3000);

  function cleanup(failed) {
    gContext.onselectionchange = null;
    clearTimeout(sccTimeout);
    if (failed) {
      inputmethod_cleanup();
    }
    else {
      test_onSurroundingTextChange();
    }
  }

  gContext.onselectionchange = function(evt) {
    ok(true, 'onselectionchange fired');
    is(evt.detail.selectionStart, 10);
    is(evt.detail.selectionEnd, 10);
    ok(evt.detail.ownAction);
  };

  gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
    cleanup();
  }, function(e) {
    ok(false, 'sendKey failed: ' + e.name);
    cleanup(true);
  });
}

function test_onSurroundingTextChange() {
  var sccTimeout = setTimeout(function() {
    ok(false, 'surroundingtextchange event not fired');
    cleanup(true);
  }, 3000);

  function cleanup(failed) {
    gContext.onsurroundingtextchange = null;
    clearTimeout(sccTimeout);
    if (failed) {
      inputmethod_cleanup();
    }
    else {
      // in case we want more tests leave this
      inputmethod_cleanup();
    }
  }

  gContext.onsurroundingtextchange = function(evt) {
    ok(true, 'onsurroundingtextchange fired');
    is(evt.detail.text, 'Xulei2013jj');
    is(evt.detail.textBeforeCursor, 'Xulei2013jj');
    is(evt.detail.textAfterCursor, '');
    ok(evt.detail.ownAction);
  };

  gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
    cleanup();
  }, function(e) {
    ok(false, 'sendKey failed: ' + e.name);
    cleanup(true);
  });
}

</script>
</pre>
</body>
</html>