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

<textarea id="de-DE" lang="de-DE">heute ist ein guter Tag - today is a good day</textarea>

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

const Ci = SpecialPowers.Ci;

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

var elem_de;
var editor_de;
var script;

/** Test for Bug 1209414 **/
/*
 * All we want to do in this test is change the spelling using a right-click and selection from the menu.
 * This is necessary since all the other tests use SetCurrentDictionary() which doesn't reflect
 * user behaviour.
 */

var onSpellCheck =
  SpecialPowers.Cu.import("resource://gre/modules/AsyncSpellCheckTestHelper.jsm")
               .onSpellCheck;

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  script = SpecialPowers.loadChromeScript(function() {
    const Ci = Components.interfaces;
    var chromeWin = browserElement.ownerDocument.defaultView
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDOMWindow)
                    .QueryInterface(Ci.nsIDOMChromeWindow);
    var contextMenu = chromeWin.document.getElementById("contentAreaContextMenu");
    contextMenu.addEventListener("popupshown",
                                 () => sendAsyncMessage("popupshown"), false);

    var dir = Components.classes["@mozilla.org/file/directory_service;1"]
                        .getService(Ci.nsIProperties)
                        .get("CurWorkD", Ci.nsIFile);
    dir.append("tests");
    dir.append("editor");
    dir.append("composer");
    dir.append("test");

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

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

    addMessageListener("hidepopup", function() {
      var state = contextMenu.state;

      // Select Language from the menu.  Take a look at
      // toolkit/modules/InlineSpellChecker.jsm to see how the menu works.

      contextMenu.ownerDocument.getElementById("spell-check-dictionary-en-US")
                 .doCommand();
      contextMenu.hidePopup();

      return state;
    });
    addMessageListener("destroy", () => hunspell.removeDirectory(de_DE));
    addMessageListener("contextMenu-not-null", () => contextMenu != null);
    addMessageListener("de_DE-exists", () => de_DE.exists());
  });
  is(script.sendSyncMessage("contextMenu-not-null")[0][0], true,
     "Got context menu XUL");
  is(script.sendSyncMessage("de_DE-exists")[0][0], true,
     "true expected (de_DE directory should exist)");
  script.addMessageListener("popupshown", handlePopup);

  elem_de = document.getElementById('de-DE');
  editor_de = SpecialPowers.wrap(elem_de)
                           .QueryInterface(Ci.nsIDOMNSEditableElement).editor;
  editor_de.setSpellcheckUserOverride(true);

  onSpellCheck(elem_de, function () {
    var inlineSpellChecker = editor_de.getInlineSpellChecker(true);
    var spellchecker = inlineSpellChecker.spellChecker;
    try {
      var currentDictonary = spellchecker.GetCurrentDictionary();
    } catch(e) {}

    // Check that the German dictionary is loaded and that the spell check has worked.
    is(currentDictonary, "de-DE", "expected de-DE");
    is(getMisspelledWords(editor_de), "today" + "is" + "a" + "good" + "day", "some misspelled words expected: today is a good day");

    // Focus again, just to be sure that the context-click won't trigger another spell check.
    elem_de.focus();

    // Make sure all spell checking action is done before right-click to select the en-US dictionary.
    onSpellCheck(elem_de, function () {
      synthesizeMouse(elem_de, 2, 2, { type : "contextmenu", button : 2 }, window);
    });
  });
});

function handlePopup() {
  var state = script.sendSyncMessage("hidepopup")[0][0];
  is(state, "open", "checking if popup is open");

  onSpellCheck(elem_de, function () {
    var inlineSpellChecker = editor_de.getInlineSpellChecker(true);
    var spellchecker = inlineSpellChecker.spellChecker;
    try {
      currentDictonary = spellchecker.GetCurrentDictionary();
    } catch(e) {}

    // Check that the English dictionary is loaded and that the spell check has worked.
    is(currentDictonary, "en-US", "expected en-US");
    is(getMisspelledWords(editor_de), "heute" + "ist" + "ein" + "guter", "some misspelled words expected: heute ist ein guter");

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

    // This will clear the content preferences and reset "spellchecker.dictionary".
    spellchecker.SetCurrentDictionary("");
    SimpleTest.finish();
  });
}

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