summaryrefslogtreecommitdiffstats
path: root/editor/spellchecker/tests/test_multiple_content_languages.html
blob: 49d3bb5ace314e96791007b516a5a1915884ec55 (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
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html>
<head>
  <title>Test for multiple Content-Language values</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<p id="display"></p>
<iframe id="content"></iframe>

<pre id="test">
<script class="testbody">

/** Test for multiple Content-Language values **/
/** Visit the elements defined above and check the dictionaries we got **/
SimpleTest.waitForExplicitFinish();
var content = document.getElementById("content");

var tests = [
    // text area, value of spellchecker.dictionary, result.
    // Result: Document language.
    [ "none",  "", ["en-US", "en-GB"] ],

    // Result: Element language.
    [ "en-GB", "", ["en-GB"] ],
    // Result: Random en-* or en-US (if application locale is en-US).
    [ "en-ZA-not-avail", "", ["*"] ],
    [ "en",              "", ["*"] ],
    // Result: Locale.
    [ "ko-not-avail",    "", ["en-US"] ],

    // Result: Document language, plus preference value in all cases.
    [ "none",            "en-AU", ["en-US", "en-GB", "en-AU"] ],
    [ "en-ZA-not-avail", "en-AU", ["en-AU"] ],
    [ "ko-not-avail",    "en-AU", ["en-AU"] ],

    // Result: Document language, plus first matching preference language.
    [ "none",            "en-AU,en-US", ["en-US", "en-GB", "en-AU"] ],
    // Result: First matching preference language.
    [ "en-ZA-not-avail", "en-AU,en-US", ["en-AU"] ],
    // Result: Fall back to preference languages.
    [ "ko-not-avail",    "en-AU,en-US", ["en-AU", "en-US"] ],

    // Result: Random en-*.
    [ "en-ZA-not-avail", "de-DE", ["*"] ],
    // Result: Preference value.
    [ "ko-not-avail",    "de-DE", ["de-DE"] ],
  ];

var loadCount = 0;
var retrying = false;
var script;

var loadListener = async function(evt) {
  if (loadCount == 0) {
    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 en-GB, en-AU and de-DE dictionaries.
      var en_GB = dir.clone();
      var en_AU = dir.clone();
      var de_DE = dir.clone();
      en_GB.append("en-GB");
      en_AU.append("en-AU");
      de_DE.append("de-DE");
      hunspell.addDirectory(en_GB);
      hunspell.addDirectory(en_AU);
      hunspell.addDirectory(de_DE);

      addMessageListener("check-existence",
                         () => [en_GB.exists(), en_AU.exists(),
                                de_DE.exists()]);
      addMessageListener("destroy", () => {
        hunspell.removeDirectory(en_GB);
        hunspell.removeDirectory(en_AU);
        hunspell.removeDirectory(de_DE);
      });
    });
    var existenceChecks = await script.sendQuery("check-existence");
    is(existenceChecks[0], true, "true expected (en-GB directory should exist)");
    is(existenceChecks[1], true, "true expected (en-AU directory should exist)");
    is(existenceChecks[2], true, "true expected (de-DE directory should exist)");
  }

  SpecialPowers.pushPrefEnv({set: [["spellchecker.dictionary", tests[loadCount][1]]]},
                            function() { continueTest(evt); });
};

function continueTest(evt) {
  var doc = evt.target.contentDocument;
  var elem = doc.getElementById(tests[loadCount][0]);
  var editor = SpecialPowers.wrap(elem).editor;
  editor.setSpellcheckUserOverride(true);
  var inlineSpellChecker = editor.getInlineSpellChecker(true);
  const is_en_US = SpecialPowers.Services.locale.appLocaleAsBCP47 == "en-US";

  const { onSpellCheck } = SpecialPowers.ChromeUtils.importESModule(
    "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs"
  );
  onSpellCheck(elem, async function() {
    var spellchecker = inlineSpellChecker.spellChecker;
    let currentDictionaries;
    try {
      currentDictionaries = spellchecker.getCurrentDictionaries();
    } catch (e) {}

    if (!currentDictionaries && !retrying) {
      // It's possible for an asynchronous font-list update to cause a reflow
      // that disrupts the async spell-check and results in not getting a
      // current dictionary here; if that happens, we retry the same testcase
      // by reloading the iframe without bumping loadCount.
      info(`No current dictionary: retrying testcase ${loadCount}`);
      retrying = true;
    } else {
      let expectedDictionaries = tests[loadCount][2];
      let dictionaryArray = Array.from(currentDictionaries);
      is(
        dictionaryArray.length,
        expectedDictionaries.length,
        "Expected matching dictionary count"
      );
      if (expectedDictionaries[0] != "*") {
        ok(
          dictionaryArray.every(dict => expectedDictionaries.includes(dict)),
          "active dictionaries should match expectation"
        );
      } else if (is_en_US && tests[loadCount][0].startsWith("en")) {
        // Current application locale is en-US and content lang is en or
        // en-unknown, so we should use en-US dictionary as default.
        is(
          dictionaryArray[0],
          "en-US",
          "expected en-US that is application locale"
        );
      } else {
        let dict = dictionaryArray[0];
        var gotEn = (dict == "en-GB" || dict == "en-AU" || dict == "en-US");
        is(gotEn, true, "expected en-AU or en-GB or en-US");
      }

      loadCount++;
      retrying = false;
    }

    if (loadCount < tests.length) {
      // Load the iframe again.
      content.src = "http://mochi.test:8888/tests/editor/spellchecker/tests/multiple_content_languages_subframe.html?firstload=false";
    } else {
      // Remove the fake  dictionaries again, since it's otherwise picked up by later tests.
      await script.sendQuery("destroy");

      SimpleTest.finish();
    }
  });
}

content.addEventListener("load", loadListener);

content.src = "http://mochi.test:8888/tests/editor/spellchecker/tests/multiple_content_languages_subframe.html?firstload=true";

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