summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_bug596333.html
blob: a94726325624572e879e6bb80e1ecf8e721541d0 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=596333
-->
<head>
  <title>Test for Bug 596333</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="spellcheck.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=596333">Mozilla Bug 596333</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 596333 **/
const Ci = SpecialPowers.Ci;

SimpleTest.waitForExplicitFinish();
addLoadEvent(runTest);

var gMisspeltWords;

function getEditor() {
  return SpecialPowers.wrap(document.getElementById("edit")).editor;
}

function append(str) {
  var edit = document.getElementById("edit");
  edit.focus();
  edit.selectionStart = edit.selectionEnd = edit.value.length;
  var editor = getEditor();

  for (var i = 0; i < str.length; ++i) {
    synthesizeKey(str[i], {});
  }
}

function getLoadContext() {
  return SpecialPowers.wrap(window)
         .QueryInterface(Ci.nsIInterfaceRequestor)
         .getInterface(Ci.nsIWebNavigation)
         .QueryInterface(Ci.nsILoadContext);
}

function paste(str) {
  var edit = document.getElementById("edit");
  var Cc = SpecialPowers.Cc, Ci = SpecialPowers.Ci;
  var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
  trans.init(getLoadContext());
  var s = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
  s.data = str;
  trans.setTransferData("text/unicode", s, str.length * 2);

  getEditor().pasteTransferable(trans);
}

function runOnFocus() {
  var edit = document.getElementById("edit");

  gMisspeltWords = ["haz", "cheezburger"];
  ok(isSpellingCheckOk(getEditor(), gMisspeltWords),
     "All misspellings before editing are accounted for.");
  append(" becaz I'm a lulcat!");
  onSpellCheck(edit, function () {
    gMisspeltWords.push("becaz");
    gMisspeltWords.push("lulcat");
    ok(isSpellingCheckOk(getEditor(), gMisspeltWords),
       "All misspellings after typing are accounted for.");

    // Now, type an invalid word, and instead of hitting "space" at the end, just blur
    // the textarea and see if the spell check after the blur event catches it.
    append(" workd");
    edit.blur();
    onSpellCheck(edit, function () {
      gMisspeltWords.push("workd");
      ok(isSpellingCheckOk(getEditor(), gMisspeltWords),
         "All misspellings after blur are accounted for.");

      // Also, test the case when we're entering the first word in a textarea
      gMisspeltWords = ["workd"];
      edit.value = "";
      append("workd ");
      onSpellCheck(edit, function () {
        ok(isSpellingCheckOk(getEditor(), gMisspeltWords),
           "Misspelling in the first entered word is accounted for.");

        // Make sure that pasting would also trigger spell checking for the previous word
        gMisspeltWords = ["workd"];
        edit.value = "";
        append("workd");
        paste("           x");
        onSpellCheck(edit, function () {
          ok(isSpellingCheckOk(getEditor(), gMisspeltWords),
             "Misspelling is accounted for after pasting.");

          SimpleTest.finish();
        });
      });
    });
  });
}

function runTest()
{
  var edit = document.getElementById("edit");
  edit.focus();

  SpecialPowers.Cu.import("resource://gre/modules/AsyncSpellCheckTestHelper.jsm", window);
  onSpellCheck(edit, runOnFocus);
}
</script>
</pre>

<textarea id="edit">I can haz cheezburger</textarea>

</body>
</html>