summaryrefslogtreecommitdiffstats
path: root/dom/inputmethod/mochitest/test_unload.html
blob: bef9d1485b21225bbb4bd1b58d6d81824bf83f11 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1122463
https://bugzilla.mozilla.org/show_bug.cgi?id=820057
-->
<head>
  <title>Test focus when page unloads</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=1122463">Mozilla Bug 1122463</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=820057">Mozilla Bug 820057</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">

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

let appFrameScript = function appFrameScript() {
  let form1 = content.document.body.firstElementChild;
  let input1 = form1.firstElementChild;
  let submit1 = form1.lastElementChild;
  let input2;

  let cancelSubmit = function(evt) {
    evt.preventDefault();
  };

  // Content of the second page.
  form1.action = 'file_test_unload_action.html';

  let i = 1;

  input1.focus();

  addMessageListener('test:next', function() {
    i++;
    switch (i) {
      case 2:
        // Click the submit button, trigger the submit event and make our
        // installed event listener preventing the submission.
        form1.addEventListener('submit', cancelSubmit);
        submit1.click();

        sendAsyncMessage('test:step');

        break;

      case 3:
        // Actually submit the form.
        form1.removeEventListener('submit', cancelSubmit);
        submit1.click();

        break;

      case 4:
        if (!content.document.body) {
          content.onload = function() {
            content.onload = null;

            let input2 = content.document.body.firstElementChild;
            input2.focus();
          };

          return;
        }

        input2 = content.document.body.firstElementChild;
        input2.focus();

        break;

      case 5:
        content.location.href = 'data:text/html,Hello!';

        break;
    }
  });
};

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

  let i = 0;
  function nextStep() {
    let inputcontext = navigator.mozInputMethod.inputcontext;

    i++;
    switch (i) {
      // focus on the first input receives the first input context.
      case 1:
        ok(!!inputcontext, '1) Receving the first input context');
        is(inputcontext.textAfterCursor, 'First');

        mm.sendAsyncMessage('test:next');
        break;

      // Cancelled submission should not cause us lost focus.
      case 2:
        ok(!!inputcontext, '2) Receving the first input context');
        is(inputcontext.textAfterCursor, 'First');

        mm.sendAsyncMessage('test:next');
        break;

      // Real submit and page transition should cause us lost focus.
      // XXX: Unless we could delay the page transition, we does not know if
      // the inputcontext is lost because of the submit or the pagehide/beforeload
      // event.
      case 3:
        is(inputcontext, null, '3) Receving null inputcontext');

        mm.sendAsyncMessage('test:next');

        break;

      // Regaining focus of input in the second page.
      case 4:
        ok(!!inputcontext, '4) Receving the second input context');
        is(inputcontext.textAfterCursor, 'Second');

        mm.sendAsyncMessage('test:next');

        break;

      // Page transition should cause us lost focus
      case 5:
        is(inputcontext, null, '5) Receving null inputcontext');

        inputmethod_cleanup();

        break;
    }
  }

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

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

  let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
  im.oninputcontextchange = nextStep;

  let frameScriptLoaded = false;
  iframe.addEventListener('mozbrowserloadend', function() {
    if (frameScriptLoaded)
      return;

    frameScriptLoaded = true;
    mm.addMessageListener('test:step', nextStep);
    mm.loadFrameScript('data:,(' + encodeURIComponent(appFrameScript.toString()) + ')();', false);
  });
}

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