tor-browser

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

test_bug1209414.html (5395B)


      1 <!DOCTYPE html>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1209414
      5 -->
      6 <head>
      7  <title>Test for Bug 1209414</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <script src="/tests/SimpleTest/EventUtils.js"></script>
     10  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
     11 </head>
     12 <body>
     13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1209414">Mozilla Bug 1209414</a>
     14 <p id="display"></p>
     15 </div>
     16 
     17 <textarea id="de-DE" lang="de-DE">heute ist ein guter Tag - today is a good day</textarea>
     18 
     19 <pre id="test">
     20 <script class="testbody" type="text/javascript">
     21 
     22 const Ci = SpecialPowers.Ci;
     23 
     24 function getMisspelledWords(editor) {
     25  return editor.selectionController.getSelection(Ci.nsISelectionController.SELECTION_SPELLCHECK).toString();
     26 }
     27 
     28 var elem_de;
     29 var editor_de;
     30 var script;
     31 
     32 /** Test for Bug 1209414 */
     33 /*
     34 * All we want to do in this test is change the spelling using a right-click and selection from the menu.
     35 * This is necessary since all the other tests use setCurrentDictionaries() which doesn't reflect
     36 * user behaviour.
     37 */
     38 
     39 let { maybeOnSpellCheck, onSpellCheck } = SpecialPowers.ChromeUtils.importESModule(
     40    "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs"
     41 );
     42 
     43 SimpleTest.waitForExplicitFinish();
     44 SimpleTest.waitForFocus(async function() {
     45  script = SpecialPowers.loadChromeScript(function() {
     46    /* eslint-env mozilla/chrome-script */
     47    var chromeWin = actorParent.rootFrameLoader
     48                    .ownerElement.ownerGlobal.browsingContext.topChromeWindow;
     49    var contextMenu = chromeWin.document.getElementById("contentAreaContextMenu");
     50    contextMenu.addEventListener("popupshown",
     51                                 () => sendAsyncMessage("popupshown"));
     52 
     53    // eslint-disable-next-line mozilla/use-services
     54    var dir = Cc["@mozilla.org/file/directory_service;1"]
     55                .getService(Ci.nsIProperties)
     56                .get("CurWorkD", Ci.nsIFile);
     57    dir.append("tests");
     58    dir.append("editor");
     59    dir.append("spellchecker");
     60    dir.append("tests");
     61 
     62    var hunspell = Cc["@mozilla.org/spellchecker/engine;1"]
     63                     .getService(Ci.mozISpellCheckingEngine);
     64 
     65    // Install de-DE dictionary.
     66    let de_DE = dir.clone();
     67    de_DE.append("de-DE");
     68    hunspell.addDirectory(de_DE);
     69 
     70    addMessageListener("hidepopup", function() {
     71      var state = contextMenu.state;
     72 
     73      // Select Language from the menu.  Take a look at
     74      // toolkit/modules/InlineSpellChecker.sys.mjs to see how the menu works.
     75      contextMenu.ownerDocument.getElementById("spell-check-dictionary-en-US")
     76                 .doCommand();
     77      contextMenu.hidePopup();
     78      return state;
     79    });
     80    addMessageListener("destroy", () => hunspell.removeDirectory(de_DE));
     81    addMessageListener("contextMenu-not-null", () => contextMenu != null);
     82    addMessageListener("de_DE-exists", () => de_DE.exists());
     83  });
     84  is(await script.sendQuery("contextMenu-not-null"), true,
     85     "Got context menu XUL");
     86  is(await script.sendQuery("de_DE-exists"), true,
     87     "true expected (de_DE directory should exist)");
     88  script.addMessageListener("popupshown", handlePopup);
     89 
     90  elem_de = document.getElementById("de-DE");
     91  editor_de = SpecialPowers.wrap(elem_de).editor;
     92  editor_de.setSpellcheckUserOverride(true);
     93 
     94  maybeOnSpellCheck(elem_de, function() {
     95    var inlineSpellChecker = editor_de.getInlineSpellChecker(true);
     96    var spellchecker = inlineSpellChecker.spellChecker;
     97    let currentDictionaries = spellchecker.getCurrentDictionaries();
     98 
     99    // Check that the German dictionary is loaded and that the spell check has worked.
    100    is(currentDictionaries.length, 1, "expected one dictionary");
    101    is(currentDictionaries[0], "de-DE", "expected de-DE");
    102    // eslint-disable-next-line no-useless-concat
    103    is(getMisspelledWords(editor_de), "today" + "is" + "a" + "good" + "day", "some misspelled words expected: today is a good day");
    104 
    105    // Focus again, just to be sure that the context-click won't trigger another spell check.
    106    elem_de.focus();
    107 
    108    // Make sure all spell checking action is done before right-click to select the en-US dictionary.
    109    maybeOnSpellCheck(elem_de, function() {
    110      synthesizeMouse(elem_de, 2, 2, { type: "contextmenu", button: 2 }, window);
    111    });
    112  });
    113 });
    114 
    115 async function handlePopup() {
    116  var state = await script.sendQuery("hidepopup");
    117  is(state, "open", "checking if popup is open");
    118 
    119  onSpellCheck(elem_de, async function() {
    120    var inlineSpellChecker = editor_de.getInlineSpellChecker(true);
    121    var spellchecker = inlineSpellChecker.spellChecker;
    122    let currentDictionaries = spellchecker.getCurrentDictionaries();
    123 
    124    // Check that the English dictionary is loaded and that the spell check has worked.
    125    is(currentDictionaries.length, 2, "expected two dictionaries");
    126    let dictionaryArray = Array.from(currentDictionaries);
    127    ok(dictionaryArray.includes("de-DE"), "expected de-DE");
    128    ok(dictionaryArray.includes("en-US"), "expected en-US");
    129    is(getMisspelledWords(editor_de), "", "No misspelled words expected");
    130 
    131    // Remove the fake de_DE dictionary again.
    132    await script.sendQuery("destroy");
    133 
    134    // This will clear the content preferences and reset "spellchecker.dictionary".
    135    spellchecker.setCurrentDictionaries([]).then(() => {
    136      SimpleTest.finish();
    137    });
    138  });
    139 }
    140 
    141 </script>
    142 </pre>
    143 </body>
    144 </html>