summaryrefslogtreecommitdiffstats
path: root/dom/inputmethod/mochitest/test_bug1043828.html
blob: 84c1dc0895f7cb514c7c4e5abd91c11dbb9d9a52 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1043828
-->
<head>
  <title>Basic test for Switching Keyboards.</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=1043828">Mozilla Bug 1043828</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">

SimpleTest.requestFlakyTimeout("untriaged");

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

// The KB frame script running in Keyboard B.
function kbFrameScript() {
  function tryGetText() {
    var ctx = content.navigator.mozInputMethod.inputcontext;
    if (ctx) {
      var p = ctx.getText();
      p.then(function(){
        sendAsyncMessage('test:InputMethod:getText:Resolve');
      }, function(e){
        sendAsyncMessage('test:InputMethod:getText:Reject');
      });
    } else {
      dump("Could not get inputcontext") ;
    }
  }

  addMessageListener('test:InputMethod:getText:Do', function(){
    tryGetText();
  });
}

function runTest() {
  let app, keyboardA, keyboardB;
  let getTextPromise;
  let mmKeyboardA, mmKeyboardB;

  /**
   * Test flow:
   * 1. Create two keyboard iframes & a mozbrowser iframe with a text field in it & focus the text
   *    field.
   * 2. Set keyboard frame A as active input. Wait 200ms.
   * 3. Set keyboard frame B as active input. Wait 200ms.
   * 4. Set keyboard frame A as inactive. Wait 200ms.
   * 5. Allow frame b to use getText() with inputcontext to get the content from the text field
   *    iframe. Wait 200ms.
   * [Test would succeed if the Promise returned by getText() resolves correctly.
   *  Test would fail if otherwise]
   */

  let path = location.pathname;
  let basePath = location.protocol + '//' + location.host +
               path.substring(0, path.lastIndexOf('/'));

  const WAIT_TIME = 200;

  // STEP 1: Create the frames.
  function step1() {
    // app
    app = document.createElement('iframe');
    app.src = basePath + '/file_test_app.html';
    app.setAttribute('mozbrowser', true);
    document.body.appendChild(app);

    // keyboards
    keyboardA = document.createElement('iframe');
    keyboardA.setAttribute('mozbrowser', true);
    document.body.appendChild(keyboardA);

    keyboardB = document.createElement('iframe');
    keyboardB.setAttribute('mozbrowser', true);
    document.body.appendChild(keyboardB);

    // simulate two different keyboard apps
    let imeUrl = basePath + '/file_blank.html';

    keyboardA.src = imeUrl;
    keyboardB.src = imeUrl;

    var handler = {
      handleEvent: function(){
        keyboardB.removeEventListener('mozbrowserloadend', this);

        mmKeyboardB = SpecialPowers.getBrowserFrameMessageManager(keyboardB);

        mmKeyboardB.loadFrameScript('data:,(' + kbFrameScript.toString() + ')();', false);

        mmKeyboardB.addMessageListener('test:InputMethod:getText:Resolve', function() {
          info('getText() was resolved');
          inputmethod_cleanup();
        });

        mmKeyboardB.addMessageListener('test:InputMethod:getText:Reject', function() {
          ok(false, 'getText() was rejected');
          inputmethod_cleanup();
        });

        setTimeout(function(){
          step2();
        }, WAIT_TIME);
      }
    };

    keyboardB.addEventListener('mozbrowserloadend', handler);
  }

  // STEP 2: Set keyboard A active
  function step2() {
    info('step2');
    let req = keyboardA.setInputMethodActive(true);

    req.onsuccess = function(){
      setTimeout(function(){
        step3();
      }, WAIT_TIME);
    };

    req.onerror = function(){
      ok(false, 'setInputMethodActive failed: ' + this.error.name);
      inputmethod_cleanup();
    };
  }

  // STEP 3: Set keyboard B active
  function step3() {
    info('step3');
    let req = keyboardB.setInputMethodActive(true);

    req.onsuccess = function(){
      setTimeout(function(){
        step4();
      }, WAIT_TIME);
    };

    req.onerror = function(){
      ok(false, 'setInputMethodActive failed: ' + this.error.name);
      inputmethod_cleanup();
    };
  }

  // STEP 4: Set keyboard A inactive
  function step4() {
    info('step4');
    let req = keyboardA.setInputMethodActive(false);

    req.onsuccess = function(){
      setTimeout(function(){
        step5();
      }, WAIT_TIME);
    };

    req.onerror = function(){
      ok(false, 'setInputMethodActive failed: ' + this.error.name);
      inputmethod_cleanup();
    };
  }

  // STEP 5: getText
  function step5() {
    info('step5');
    mmKeyboardB.sendAsyncMessage('test:InputMethod:getText:Do');
  }

  step1();
}

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