Bug 1772588 [wpt PR 34302] - [wpt] Add test for block-in-inline offsetParent., a...
[gecko.git] / editor / AsyncSpellCheckTestHelper.jsm
blob78d5f2993f8e9fea619ff52571fdfef257d3092a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 var EXPORTED_SYMBOLS = [
6   "maybeOnSpellCheck",
7   "onSpellCheck",
8   "getDictionaryContentPref",
9 ];
11 const SPELL_CHECK_ENDED_TOPIC = "inlineSpellChecker-spellCheck-ended";
12 const SPELL_CHECK_STARTED_TOPIC = "inlineSpellChecker-spellCheck-started";
14 const CP = Cc["@mozilla.org/content-pref/service;1"].getService(
15   Ci.nsIContentPrefService2
18 /**
19  * Waits until spell checking has stopped on the given element.
20  *
21  * When a spell check is pending, this waits indefinitely until the spell check
22  * ends.  When a spell check is not pending, it waits a small number of turns of
23  * the event loop: if a spell check begins, it resumes waiting indefinitely for
24  * the end, and otherwise it stops waiting and calls the callback.
25  *
26  * This this can therefore trap spell checks that have not started at the time
27  * of calling, spell checks that have already started, multiple consecutive
28  * spell checks, and the absence of spell checks altogether.
29  *
30  * @param editableElement  The element being spell checked.
31  * @param callback         Called when spell check has completed or enough turns
32  *                         of the event loop have passed to determine it has not
33  *                         started.
34  */
35 function maybeOnSpellCheck(editableElement, callback) {
36   let editor = editableElement.editor;
37   if (!editor) {
38     let win = editableElement.ownerGlobal;
39     editor = win.docShell.editingSession.getEditorForWindow(win);
40   }
41   if (!editor) {
42     throw new Error("Unable to find editor for element " + editableElement);
43   }
45   try {
46     // False is important here.  Pass false so that the inline spell checker
47     // isn't created if it doesn't already exist.
48     var isc = editor.getInlineSpellChecker(false);
49   } catch (err) {
50     // getInlineSpellChecker throws if spell checking is not enabled instead of
51     // just returning null, which seems kind of lame.  (Spell checking is not
52     // enabled on Android.)  The point here is only to determine whether spell
53     // check is pending, and if getInlineSpellChecker throws, then it's not
54     // pending.
55   }
56   let waitingForEnded = isc && isc.spellCheckPending;
57   let count = 0;
59   function observe(subj, topic, data) {
60     if (subj != editor) {
61       return;
62     }
63     count = 0;
64     let expectedTopic = waitingForEnded
65       ? SPELL_CHECK_ENDED_TOPIC
66       : SPELL_CHECK_STARTED_TOPIC;
67     if (topic != expectedTopic) {
68       Cu.reportError("Expected " + expectedTopic + " but got " + topic + "!");
69     }
70     waitingForEnded = !waitingForEnded;
71   }
73   // eslint-disable-next-line mozilla/use-services
74   let os = Cc["@mozilla.org/observer-service;1"].getService(
75     Ci.nsIObserverService
76   );
77   os.addObserver(observe, SPELL_CHECK_STARTED_TOPIC);
78   os.addObserver(observe, SPELL_CHECK_ENDED_TOPIC);
80   let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
81   timer.init(
82     function tick() {
83       // Wait an arbitrarily large number -- 100 -- turns of the event loop before
84       // declaring that no spell checks will start.
85       if (waitingForEnded || ++count < 100) {
86         return;
87       }
88       timer.cancel();
89       os.removeObserver(observe, SPELL_CHECK_STARTED_TOPIC);
90       os.removeObserver(observe, SPELL_CHECK_ENDED_TOPIC);
91       callback();
92     },
93     0,
94     Ci.nsITimer.TYPE_REPEATING_SLACK
95   );
98 /**
99  * Waits until spell checking has stopped on the given element.
101  * @param editableElement  The element being spell checked.
102  * @param callback         Called when spell check has completed or enough turns
103  *                         of the event loop have passed to determine it has not
104  *                         started.
105  */
106 function onSpellCheck(editableElement, callback) {
107   const { TestUtils } = ChromeUtils.import(
108     "resource://testing-common/TestUtils.jsm"
109   );
111   let editor = editableElement.editor;
112   TestUtils.topicObserved(SPELL_CHECK_ENDED_TOPIC, s => s == editor).then(
113     callback
114   );
117 async function getDictionaryContentPref() {
118   let dictionaries = await new Promise(resolve => {
119     let value = "";
120     CP.getByDomainAndName("mochi.test", "spellcheck.lang", null, {
121       handleResult(pref) {
122         value = pref.value;
123       },
124       handleCompletion() {
125         resolve(value);
126       },
127     });
128   });
130   return dictionaries;