summaryrefslogtreecommitdiffstats
path: root/editor/spellchecker/tests/test_bug697981.html
blob: 8ef5dbcd92b15e29c98a5fb8fa7d3a328faba0c2 (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
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=697981
-->
<head>
  <title>Test for Bug 697981</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=697981">Mozilla Bug 697981</a>
<p id="display"></p>
</div>

<textarea id="de-DE" lang="de-DE" onfocus="deFocus()">German heute ist ein guter Tag</textarea>
<textarea id="en-US" lang="en-US" onfocus="enFocus()">Nogoodword today is a nice day</textarea>

<pre id="test">
<script class="testbody" type="text/javascript">

function getMisspelledWords(editor) {
  return editor.selectionController.getSelection(SpecialPowers.Ci.nsISelectionController.SELECTION_SPELLCHECK).toString();
}

var elem_de;
var editor_de;
var script;

var { maybeOnSpellCheck, onSpellCheck } = SpecialPowers.ChromeUtils.importESModule(
  "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs"
);

/** Test for Bug 697981 **/
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async function() {
  script = SpecialPowers.loadChromeScript(function() {
    /* eslint-env mozilla/chrome-script */
    // eslint-disable-next-line mozilla/use-services
    var dir = Cc["@mozilla.org/file/directory_service;1"]
                .getService(Ci.nsIProperties)
                .get("CurWorkD", Ci.nsIFile);
    dir.append("tests");
    dir.append("editor");
    dir.append("spellchecker");
    dir.append("tests");

    var hunspell = Cc["@mozilla.org/spellchecker/engine;1"]
                     .getService(Ci.mozISpellCheckingEngine);

    // Install de-DE dictionary.
    var de_DE = dir.clone();
    de_DE.append("de-DE");
    hunspell.addDirectory(de_DE);

    addMessageListener("de_DE-exists", () => de_DE.exists());
    addMessageListener("destroy", () => hunspell.removeDirectory(de_DE));
  });
  is(await script.sendQuery("de_DE-exists"), true,
     "true expected (de_DE directory should exist)");

  document.getElementById("de-DE").focus();
});

function deFocus() {
  elem_de = document.getElementById("de-DE");
  editor_de = SpecialPowers.wrap(elem_de).editor;
  editor_de.setSpellcheckUserOverride(true);
  var inlineSpellChecker = editor_de.getInlineSpellChecker(true);

  maybeOnSpellCheck(elem_de, function() {
    var spellchecker = inlineSpellChecker.spellChecker;
    try {
      var currentDictionaries = spellchecker.getCurrentDictionaries();
    } catch (e) {}

    // Check that the German dictionary is loaded and that the spell check has worked.
    is(currentDictionaries.length, 1, "expected one dictionary");
    is(currentDictionaries[0], "de-DE", "expected de-DE");
    is(getMisspelledWords(editor_de), "German", "one misspelled word expected: German");

    // Now focus the other textarea, which requires English spelling.
    document.getElementById("en-US").focus();
  });
}

function enFocus() {
  var elem_en = document.getElementById("en-US");
  var editor_en = SpecialPowers.wrap(elem_en).editor;
  editor_en.setSpellcheckUserOverride(true);
  var inlineSpellChecker = editor_en.getInlineSpellChecker(true);

  onSpellCheck(elem_en, async function() {
    let spellchecker = inlineSpellChecker.spellChecker;
    let currentDictionaries = spellchecker.getCurrentDictionaries();

    // Check that the English dictionary is loaded and that the spell check has worked.
    is(currentDictionaries.length, 1, "expected one dictionary");
    is(currentDictionaries[0], "en-US", "expected en-US");
    is(getMisspelledWords(editor_en), "Nogoodword", "one misspelled word expected: Nogoodword");

    // So far all was boring. The important thing is whether the spell check result
    // in the de-DE editor is still the same. After losing focus, no spell check
    // updates should take place there.
    is(getMisspelledWords(editor_de), "German", "one misspelled word expected: German");

    // Remove the fake de_DE dictionary again.
    await script.sendQuery("destroy");

    // Focus again, so the spelling gets updated, but before we need to kill the focus handler.
    elem_de.onfocus = null;
    elem_de.blur();
    elem_de.focus();

    // After removal, the de_DE editor should refresh the spelling with en-US.
    maybeOnSpellCheck(elem_de, function() {
      spellchecker = inlineSpellChecker.spellChecker;
      try {
        currentDictionaries = spellchecker.getCurrentDictionaries();
      } catch (e) {}

      // Check that the default English dictionary is loaded and that the spell check has worked.
      is(currentDictionaries.length, 1, "expected one dictionary");
      is(currentDictionaries[0], "en-US", "expected en-US");
      // eslint-disable-next-line no-useless-concat
      is(getMisspelledWords(editor_de), "heute" + "ist" + "ein" + "guter",
         "some misspelled words expected: heute ist ein guter");

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

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