dom/inputmethod/mochitest/test_bug944397.html

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=944397
     5 -->
     6 <head>
     7   <title>Basic test for InputMethod API.</title>
     8   <script type="application/javascript;version=1.7" src="/tests/SimpleTest/SimpleTest.js"></script>
     9   <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
    10   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    11 </head>
    12 <body>
    13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=944397">Mozilla Bug 944397</a>
    14 <p id="display"></p>
    15 <pre id="test">
    16 <script class="testbody" type="application/javascript;version=1.7">
    18 inputmethod_setup(function() {
    19   runTest();
    20 });
    22 // The frame script running in file_test_app.html.
    23 function appFrameScript() {
    24   let input = content.document.getElementById('test-input');
    25   input.oninput = function() {
    26     sendAsyncMessage('test:InputMethod:oninput', {
    27       value: input.value
    28     });
    29   };
    30 }
    32 function runTest() {
    33   let app, keyboard;
    35   /**
    36    * So this test does the following:
    37    * 1. Create a mozbrowser iframe with a text field in it, and focus the text field
    38    * 2. 100ms. after loading we create new keyboard iframe, that will try to execute
    39    *    replaceSurroundingText on the current active inputcontext
    40    * 3. That should trigger 'input' event on the said text field
    41    * 4. And if that happens we know everything is OK
    42    */
    44   let path = location.pathname;
    45   let basePath = location.protocol + '//' + location.host +
    46                path.substring(0, path.lastIndexOf('/'));
    48   // STEP 1: Create an app frame to recieve keyboard inputs.
    49   function step1() {
    50     app = document.createElement('iframe');
    51     app.src = basePath + '/file_test_app.html';
    52     app.setAttribute('mozbrowser', true);
    53     document.body.appendChild(app);
    54     app.addEventListener('mozbrowserloadend', function() {
    55       let mm = SpecialPowers.getBrowserFrameMessageManager(app);
    56       mm.loadFrameScript('data:,(' + appFrameScript.toString() + ')();', false);
    57       mm.addMessageListener("test:InputMethod:oninput", function(ev) {
    58         step4(SpecialPowers.wrap(ev).json.value);
    59       });
    61       step2();
    62     });
    63   }
    65   function step2() {
    66     // STEP 2a: Create a browser frame to load the input method app.
    67     keyboard = document.createElement('iframe');
    68     keyboard.setAttribute('mozbrowser', true);
    69     document.body.appendChild(keyboard);
    71     // STEP 2b: Grant input privileges to the keyboard iframe
    72     let imeUrl = basePath + '/file_inputmethod.html#data';
    74     SpecialPowers.pushPermissions([{
    75       type: 'input',
    76       allow: true,
    77       context: imeUrl
    78     }], function() {
    79       // STEP 2c: Tell Gecko to use this iframe as its keyboard app
    80       let req = keyboard.setInputMethodActive(true);
    82       req.onsuccess = function() {
    83         ok(true, 'setInputMethodActive succeeded.');
    84       };
    86       req.onerror = function() {
    87         ok(false, 'setInputMethodActive failed: ' + this.error.name);
    88         inputmethod_cleanup();
    89       };
    91       // STEP 3: Loads the input method app to the browser frame after a delay.
    92       setTimeout(function() {
    93         keyboard.src = imeUrl;
    94       }, 100);
    95     });
    96   }
    98   function step4(val) {
    99     ok(true, 'Keyboard input was received.');
   100     is(val, '#dataYuan', 'Input value');
   101     inputmethod_cleanup();
   102   }
   104   step1();
   105 }
   107 </script>
   108 </pre>
   109 </body>
   110 </html>

mercurial