tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_multiple_content_languages.html (6341B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4  <title>Test for multiple Content-Language values</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
      7 </head>
      8 <body>
      9 <p id="display"></p>
     10 <iframe id="content"></iframe>
     11 
     12 <pre id="test">
     13 <script class="testbody">
     14 
     15 /** Test for multiple Content-Language values */
     16 /** Visit the elements defined above and check the dictionaries we got */
     17 SimpleTest.waitForExplicitFinish();
     18 var content = document.getElementById("content");
     19 
     20 var tests = [
     21    // text area, value of spellchecker.dictionary, result.
     22    // Result: Document language.
     23    [ "none",  "", ["en-US", "en-GB"] ],
     24 
     25    // Result: Element language.
     26    [ "en-GB", "", ["en-GB"] ],
     27    // Result: Random en-* or en-US (if application locale is en-US).
     28    [ "en-ZA-not-avail", "", ["*"] ],
     29    [ "en",              "", ["*"] ],
     30    // Result: Locale.
     31    [ "ko-not-avail",    "", ["en-US"] ],
     32 
     33    // Result: Document language, plus preference value in all cases.
     34    [ "none",            "en-AU", ["en-US", "en-GB", "en-AU"] ],
     35    [ "en-ZA-not-avail", "en-AU", ["en-AU"] ],
     36    [ "ko-not-avail",    "en-AU", ["en-AU"] ],
     37 
     38    // Result: Document language, plus first matching preference language.
     39    [ "none",            "en-AU,en-US", ["en-US", "en-GB", "en-AU"] ],
     40    // Result: First matching preference language.
     41    [ "en-ZA-not-avail", "en-AU,en-US", ["en-AU"] ],
     42    // Result: Fall back to preference languages.
     43    [ "ko-not-avail",    "en-AU,en-US", ["en-AU", "en-US"] ],
     44 
     45    // Result: Random en-*.
     46    [ "en-ZA-not-avail", "de-DE", ["*"] ],
     47    // Result: Preference value.
     48    [ "ko-not-avail",    "de-DE", ["de-DE"] ],
     49  ];
     50 
     51 var loadCount = 0;
     52 var retrying = false;
     53 var script;
     54 
     55 var loadListener = async function(evt) {
     56  if (loadCount == 0) {
     57    script = SpecialPowers.loadChromeScript(function() {
     58      /* eslint-env mozilla/chrome-script */
     59      // eslint-disable-next-line mozilla/use-services
     60      var dir = Cc["@mozilla.org/file/directory_service;1"]
     61                  .getService(Ci.nsIProperties)
     62                  .get("CurWorkD", Ci.nsIFile);
     63      dir.append("tests");
     64      dir.append("editor");
     65      dir.append("spellchecker");
     66      dir.append("tests");
     67 
     68      var hunspell = Cc["@mozilla.org/spellchecker/engine;1"]
     69                       .getService(Ci.mozISpellCheckingEngine);
     70 
     71      // Install en-GB, en-AU and de-DE dictionaries.
     72      var en_GB = dir.clone();
     73      var en_AU = dir.clone();
     74      var de_DE = dir.clone();
     75      en_GB.append("en-GB");
     76      en_AU.append("en-AU");
     77      de_DE.append("de-DE");
     78      hunspell.addDirectory(en_GB);
     79      hunspell.addDirectory(en_AU);
     80      hunspell.addDirectory(de_DE);
     81 
     82      addMessageListener("check-existence",
     83                         () => [en_GB.exists(), en_AU.exists(),
     84                                de_DE.exists()]);
     85      addMessageListener("destroy", () => {
     86        hunspell.removeDirectory(en_GB);
     87        hunspell.removeDirectory(en_AU);
     88        hunspell.removeDirectory(de_DE);
     89      });
     90    });
     91    var existenceChecks = await script.sendQuery("check-existence");
     92    is(existenceChecks[0], true, "true expected (en-GB directory should exist)");
     93    is(existenceChecks[1], true, "true expected (en-AU directory should exist)");
     94    is(existenceChecks[2], true, "true expected (de-DE directory should exist)");
     95  }
     96 
     97  SpecialPowers.pushPrefEnv({set: [["spellchecker.dictionary", tests[loadCount][1]]]},
     98                            function() { continueTest(evt); });
     99 };
    100 
    101 function continueTest(evt) {
    102  var doc = evt.target.contentDocument;
    103  var elem = doc.getElementById(tests[loadCount][0]);
    104  var editor = SpecialPowers.wrap(elem).editor;
    105  editor.setSpellcheckUserOverride(true);
    106  var inlineSpellChecker = editor.getInlineSpellChecker(true);
    107  const is_en_US = SpecialPowers.Services.locale.appLocaleAsBCP47 == "en-US";
    108 
    109  const { onSpellCheck } = SpecialPowers.ChromeUtils.importESModule(
    110    "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs"
    111  );
    112  onSpellCheck(elem, async function() {
    113    var spellchecker = inlineSpellChecker.spellChecker;
    114    let currentDictionaries;
    115    try {
    116      currentDictionaries = spellchecker.getCurrentDictionaries();
    117    } catch (e) {}
    118 
    119    if (!currentDictionaries && !retrying) {
    120      // It's possible for an asynchronous font-list update to cause a reflow
    121      // that disrupts the async spell-check and results in not getting a
    122      // current dictionary here; if that happens, we retry the same testcase
    123      // by reloading the iframe without bumping loadCount.
    124      info(`No current dictionary: retrying testcase ${loadCount}`);
    125      retrying = true;
    126    } else {
    127      let expectedDictionaries = tests[loadCount][2];
    128      let dictionaryArray = Array.from(currentDictionaries);
    129      is(
    130        dictionaryArray.length,
    131        expectedDictionaries.length,
    132        "Expected matching dictionary count"
    133      );
    134      if (expectedDictionaries[0] != "*") {
    135        ok(
    136          dictionaryArray.every(dict => expectedDictionaries.includes(dict)),
    137          "active dictionaries should match expectation"
    138        );
    139      } else if (is_en_US && tests[loadCount][0].startsWith("en")) {
    140        // Current application locale is en-US and content lang is en or
    141        // en-unknown, so we should use en-US dictionary as default.
    142        is(
    143          dictionaryArray[0],
    144          "en-US",
    145          "expected en-US that is application locale"
    146        );
    147      } else {
    148        let dict = dictionaryArray[0];
    149        var gotEn = (dict == "en-GB" || dict == "en-AU" || dict == "en-US");
    150        is(gotEn, true, "expected en-AU or en-GB or en-US");
    151      }
    152 
    153      loadCount++;
    154      retrying = false;
    155    }
    156 
    157    if (loadCount < tests.length) {
    158      // Load the iframe again.
    159      content.src = "http://mochi.test:8888/tests/editor/spellchecker/tests/multiple_content_languages_subframe.html?firstload=false";
    160    } else {
    161      // Remove the fake  dictionaries again, since it's otherwise picked up by later tests.
    162      await script.sendQuery("destroy");
    163 
    164      SimpleTest.finish();
    165    }
    166  });
    167 }
    168 
    169 content.addEventListener("load", loadListener);
    170 
    171 content.src = "http://mochi.test:8888/tests/editor/spellchecker/tests/multiple_content_languages_subframe.html?firstload=true";
    172 
    173 </script>
    174 </pre>
    175 </body>
    176 </html>