summaryrefslogtreecommitdiffstats
path: root/editor/composer/test/test_bug717433.html
blob: 59ad9b247c78558b6e5f306295a2a0798451fd43 (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
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=717433
-->
<head>
  <title>Test for Bug 717433</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=717433">Mozilla Bug 717433</a>
<p id="display"></p>
<iframe id="content"></iframe>

</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 717433 **/
SimpleTest.waitForExplicitFinish();
var content = document.getElementById('content');
// Load a subframe containing an editor with language "en". At first
// load, it will set the dictionary to en-GB or en-US. We set the other one.
// At second load, it will return the current dictionary. We can check that the
// dictionary is correctly remembered between loads.

var firstLoad = true;
var expected = "";
var script;

var loadListener = function(evt) {

  if (firstLoad) {
    script = SpecialPowers.loadChromeScript(function() {
      var dir = Components.classes["@mozilla.org/file/directory_service;1"]
                          .getService(Components.interfaces.nsIProperties)
                          .get("CurWorkD", Components.interfaces.nsIFile);
      dir.append("tests");
      dir.append("editor");
      dir.append("composer");
      dir.append("test");

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

      // Install en-GB dictionary.
      var en_GB = dir.clone();
      en_GB.append("en-GB");
      hunspell.addDirectory(en_GB);

      addMessageListener("en_GB-exists", () => en_GB.exists());
      addMessageListener("destroy", () => hunspell.removeDirectory(en_GB));
    });
    is(script.sendSyncMessage("en_GB-exists")[0][0], true,
       "true expected (en-GB directory should exist)");
  }

  var doc = evt.target.contentDocument;
  var elem = doc.getElementById('textarea');
  var editor = SpecialPowers.wrap(elem).QueryInterface(SpecialPowers.Ci.nsIDOMNSEditableElement).editor;
  editor.setSpellcheckUserOverride(true);
  var inlineSpellChecker = editor.getInlineSpellChecker(true);

  SpecialPowers.Cu.import("resource://gre/modules/AsyncSpellCheckTestHelper.jsm")
  .onSpellCheck(elem, function () {
    var spellchecker = inlineSpellChecker.spellChecker;
    try {
      var currentDictonary = spellchecker.GetCurrentDictionary();
    } catch(e) {}

    if (firstLoad) {
      firstLoad = false;

      // First time around, we get a random dictionary based on the language "en".
      if (currentDictonary == "en-GB") {
        spellchecker.SetCurrentDictionary("en-US");
        expected = "en-US";
      } else if (currentDictonary == "en-US") {
        spellchecker.SetCurrentDictionary("en-GB");
        expected = "en-GB";
      } else {
        is(true, false, "Neither en-US nor en-GB are current");
      }
      content.src = 'http://mochi.test:8888/tests/editor/composer/test/bug717433_subframe.html?firstload=false';
    } else {
      is(currentDictonary, expected, expected + " expected");
      content.removeEventListener('load', loadListener, false);

      // Remove the fake en-GB dictionary again, since it's otherwise picked up by later tests.
      script.sendSyncMessage("destroy");

      // This will clear the content preferences and reset "spellchecker.dictionary".
      spellchecker.SetCurrentDictionary("");
      SimpleTest.finish();
    }
  });
}

content.addEventListener('load', loadListener, false);

content.src = 'http://mochi.test:8888/tests/editor/composer/test/bug717433_subframe.html?firstload=true';

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