summaryrefslogtreecommitdiffstats
path: root/dom/inputmethod/mochitest/test_forward_hardware_key_to_ime.html
blob: 13bd58ce9a758939185bce0a8fa968045da939c1 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1110030
-->
<head>
  <title>Forwarding Hardware Key to InputMethod</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>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/NativeKeyCodes.js"></script>
  <script type="text/javascript" src="bug1110030_helper.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=1110030">Mozilla Bug 1110030</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
// The input context.
var gContext = null;

// The test cases.
var gTests;

inputmethod_setup(function() {
  setInputContext();
});

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

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

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

    prepareTest();
  };

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

  // verifyResultsAndMoveNext will be called after input#text-input
  // receives all expected key events and it will verify results
  // and start next test.
  loadTestFrame(verifyResultsAndMoveNext);
}

function prepareTest()
{
  // Set the used input method of this test
  gInputMethod = new InputMethod(gContext);

  // Add listenr to hardwareinput
  addKeyEventListeners(gContext.hardwareinput, function (evt) {
    hardwareEventReceiver(evt);
    gInputMethod.handler(evt);
  });

  // Set the test cases
  gTests = [
    // Case 1: IME handle the key input
    {
      key: 'z',
      hardwareinput: {
        expectedEvents: kKeyDown | kKeyUp,
        receivedEvents: 0,
        expectedKeys: 'zz', // One for keydown, the other for keyup
        receivedKeys: '',
      },
      inputtext: {
        expectedEvents: kKeyDown | kKeyPress | kKeyUp,
        receivedEvents: 0,
        expectedKeys: gInputMethod.mapKey('z') +  // for keydown
                      gInputMethod.mapKey('z') +  // for keypress
                      gInputMethod.mapKey('z'),   // for keyup
        receivedKeys: '',
      }
    },
    // case 2: IME doesn't handle the key input
    {
      key: '7',
      hardwareinput: {
        expectedEvents: kKeyDown | kKeyUp,
        receivedEvents: 0,
        expectedKeys: '77', // One for keydown, the other for keyup
        receivedKeys: '',
      },
      inputtext: {
        expectedEvents: kKeyDown | kKeyPress | kKeyUp,
        receivedEvents: 0,
        expectedKeys: '777', // keydown, keypress, keyup all will receive key
        receivedKeys: '',
      }
    },
    // case 3: IME is disable
    // This case is same as
    // dom/events/test/test_dom_before_after_keyboard_event*.html
  ];

  startTesting();
}

function startTesting()
{
  if (gTests.length <= 0) {
    finish();
    return;
  }

  gCurrentTest = gTests.shift();

  fireEvent();
}

function verifyResultsAndMoveNext()
{
  verifyResults(gCurrentTest);
  startTesting();
}

function finish()
{
  inputmethod_cleanup();
}

function errorHandler(msg)
{
  // Clear the test cases
  if (gTests) {
    gTests = [];
  }

  ok(false, msg);

  inputmethod_cleanup();
}

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