summaryrefslogtreecommitdiffstats
path: root/extensions/spellcheck/tests/chrome/test_add_remove_dictionaries.xul
blob: 661eaccff496d1612b7fe679bd3eb091e631c84a (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<window title="Add and remove dictionaries test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="RunTest();">

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  </body>

  <script type="application/javascript">
  <![CDATA[
SimpleTest.waitForExplicitFinish();

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

function getDictionaryList(editor) {
  var spellchecker = editor.getInlineSpellChecker(true).spellChecker;
  var o1 = {};
  spellchecker.GetDictionaryList(o1, {});
  return o1.value;
}

function getCurrentDictionary(editor) {
  var spellchecker = editor.getInlineSpellChecker(true).spellChecker;
  return spellchecker.GetCurrentDictionary();
}

function setCurrentDictionary(editor, dictionary) {
  var spellchecker = editor.getInlineSpellChecker(true).spellChecker;
  spellchecker.SetCurrentDictionary(dictionary);
}

function RunTest() {
  var textbox = document.getElementById('textbox');
  textbox.focus();
  var editor = textbox.editor;

  var dir = Components.classes["@mozilla.org/file/directory_service;1"].
            getService(Components.interfaces.nsIProperties).
            get("CurWorkD", Components.interfaces.nsIFile);
  dir.append("chrome");
  dir.append("extensions");
  dir.append("spellcheck");
  dir.append("tests");
  dir.append("chrome");

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

  // install base dictionary
  var base = dir.clone();
  base.append("base");
  ok(base.exists());
  hunspell.addDirectory(base);

  // install map dictionary
  var map = dir.clone();
  map.append("map");
  ok(map.exists());
  hunspell.addDirectory(map);

  Components.utils.import("resource://gre/modules/AsyncSpellCheckTestHelper.jsm");
  onSpellCheck(textbox, function () {

    // test that base and map dictionaries are available
    var dicts = getDictionaryList(editor);
    isnot(dicts.indexOf("base-utf"), -1, "base is available");
    isnot(dicts.indexOf("maputf"), -1, "map is available");

    // select base dictionary
    setCurrentDictionary(editor, "base-utf");

    onSpellCheck(textbox, function () {
      // test that base dictionary is in use
      is(getMisspelledWords(editor), "Frühstück" + "qwertyu", "base misspellings");
      is(getCurrentDictionary(editor), "base-utf", "current dictionary");

      // select map dictionary
      setCurrentDictionary(editor, "maputf");

      // Focus again, so the spelling gets updated.
      textbox.blur();
      textbox.focus();

      onSpellCheck(textbox, function () {
        // test that map dictionary is in use
        is(getMisspelledWords(editor), "created" + "imply" + "tomorrow" + "qwertyu", "map misspellings (1)");
        is(getCurrentDictionary(editor), "maputf", "current dictionary");

        // uninstall map dictionary
        hunspell.removeDirectory(map);

        // Focus again, so the spelling gets updated.
        textbox.blur();
        textbox.focus();

        onSpellCheck(textbox, function () {
          // test that map dictionary is not in use
          isnot(getMisspelledWords(editor), "created" + "imply" + "tomorrow" + "qwertyu", "map misspellings (2)");
          isnot(getCurrentDictionary(editor), "maputf", "current dictionary");

          // test that base dictionary is available and map dictionary is unavailable
          var dicts = getDictionaryList(editor);
          isnot(dicts.indexOf("base-utf"), -1, "base is available");
          is(dicts.indexOf("maputf"), -1, "map is unavailable");

          // uninstall base dictionary
          hunspell.removeDirectory(base);

          onSpellCheck(textbox, function () {
            SimpleTest.finish();
          });
        });
      });
    });
  });
}
  ]]>
  </script>
  <textbox id="textbox" spellcheck="true" value="created imply Frühstück tomorrow qwertyu"/>
</window>