summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/findbar_entireword_window.xul
blob: f0da61081d656c7514e8ff1444d2d659a7e11102 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?xml version="1.0"?>

<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"?>

<window id="FindbarEntireWordTest"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        onload="onLoad();"
        title="findbar test - entire words only">

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"/>

  <script type="application/javascript"><![CDATA[
    const {interfaces: Ci, classes: Cc, results: Cr, utils: Cu} = Components;
    Cu.import("resource://gre/modules/Task.jsm");
    Cu.import("resource://testing-common/ContentTask.jsm");
    ContentTask.setTestScope(window.opener.wrappedJSObject);

    var gFindBar = null;
    var gBrowser;

    var imports = ["SimpleTest", "SpecialPowers", "ok", "is", "isnot", "info"];
    for (var name of imports) {
      window[name] = window.opener.wrappedJSObject[name];
    }
    SimpleTest.requestLongerTimeout(2);

    const kBaseURL = "chrome://mochitests/content/chrome/toolkit/content/tests/chrome";
    const kTests = {
      latin1: {
        testSimpleEntireWord: {
          "and": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'and' should've been found");
            is(results.matches.total, 6, "should've found 6 matches");
          },
          "an": results => {
            is(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'an' shouldn't have been found");
            is(results.matches.total, 0, "should've found 0 matches");
          },
          "darkness": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'darkness' should've been found");
            is(results.matches.total, 3, "should've found 3 matches");
          },
          "mammon": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'mammon' should've been found");
            is(results.matches.total, 1, "should've found 1 match");
          }
        },
        testCaseSensitive: {
          "And": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'And' should've been found");
            is(results.matches.total, 1, "should've found 1 match");
          },
          "and": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'and' should've been found");
            is(results.matches.total, 5, "should've found 5 matches");
          },
          "Mammon": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'mammon' should've been found");
            is(results.matches.total, 1, "should've found 1 match");
          }
        },
        testWordBreakChars: {
          "a": results => {
            // 'a' is a common charactar, but there should only be one occurrence
            // separated by word boundaries.
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'a' should've been found");
            is(results.matches.total, 1, "should've found 1 match");
          },
          "quarrelled": results => {
            // 'quarrelled' is denoted as a word by a period char.
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'quarrelled' should've been found");
            is(results.matches.total, 1, "should've found 1 match");
          }
        },
        testQuickfind: {
          "and": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'and' should've been found");
            is(results.matches.total, 6, "should've found 6 matches");
          },
          "an": results => {
            is(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'an' shouldn't have been found");
            is(results.matches.total, 0, "should've found 0 matches");
          },
          "darkness": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'darkness' should've been found");
            is(results.matches.total, 3, "should've found 3 matches");
          },
          "mammon": results => {
            isnot(results.find.result, Ci.nsITypeAheadFind.FIND_NOTFOUND, "'mammon' should've been found");
            is(results.matches.total, 1, "should've found 1 match");
          }
        }
      }
    };

    function onLoad() {
      Task.spawn(function* () {
        yield new Promise(resolve => {
          SpecialPowers.pushPrefEnv(
            { set: [["findbar.entireword", true]] }, resolve);
        });

        gFindBar = document.getElementById("FindToolbar");
        for (let browserId of ["content", "content-remote"]) {
          // XXXmikedeboer: when multiple test samples are available, make this
          //                a nested loop that iterates over them. For now, only
          //                latin is available.
          yield startTestWithBrowser("latin1", browserId);
        }
      }).then(() => {
        window.close();
        SimpleTest.finish();
      });
    }

    function* startTestWithBrowser(testName, browserId) {
      info("Starting test with browser '" + browserId + "'");
      gBrowser = document.getElementById(browserId);
      gFindBar.browser = gBrowser;

      let promise = ContentTask.spawn(gBrowser, null, function* () {
        return new Promise(resolve => {
          addEventListener("DOMContentLoaded", function listener() {
            removeEventListener("DOMContentLoaded", listener);
            resolve();
          });
        });
      });
      gBrowser.loadURI(kBaseURL + "/sample_entireword_" + testName + ".html");
      yield promise;
      yield onDocumentLoaded(testName);
    }

    function* onDocumentLoaded(testName) {
      let suite = kTests[testName];
      yield testSimpleEntireWord(suite.testSimpleEntireWord);
      yield testCaseSensitive(suite.testCaseSensitive);
      yield testWordBreakChars(suite.testWordBreakChars);
      yield testQuickfind(suite.testQuickfind);
    }

    var enterStringIntoFindField = Task.async(function* (str, waitForResult = true) {
      for (let promise, i = 0; i < str.length; i++) {
        if (waitForResult) {
          promise = promiseFindResult();
        }
        let event = document.createEvent("KeyboardEvent");
        event.initKeyEvent("keypress", true, true, null, false, false,
                           false, false, 0, str.charCodeAt(i));
        gFindBar._findField.inputField.dispatchEvent(event);
        if (waitForResult) {
          yield promise;
        }
      }
    });

    function openFindbar() {
      document.getElementById("cmd_find").doCommand();
      return gFindBar._startFindDeferred && gFindBar._startFindDeferred.promise;
    }

    function promiseFindResult(searchString) {
      return new Promise(resolve => {
        let data = {};
        let listener = {
          onFindResult: res => {
            if (searchString && res.searchString != searchString)
              return;

            gFindBar.browser.finder.removeResultListener(listener);
            data.find = res;
            if (res.result == Ci.nsITypeAheadFind.FIND_NOTFOUND) {
              data.matches = { total: 0, current: 0 };
              resolve(data);
              return;
            }
            listener = {
              onMatchesCountResult: res => {
                gFindBar.browser.finder.removeResultListener(listener);
                data.matches = res;
                resolve(data);
              }
            };
            gFindBar.browser.finder.addResultListener(listener);
          }
        };

        gFindBar.browser.finder.addResultListener(listener);
      });
    }

    function* testIterator(tests) {
      for (let searchString of Object.getOwnPropertyNames(tests)) {
        gFindBar.clear();

        let promise = promiseFindResult(searchString);

        yield enterStringIntoFindField(searchString, false);

        let result = yield promise;
        tests[searchString](result);
      }
    }

    function* testSimpleEntireWord(tests) {
      yield openFindbar();
      ok(!gFindBar.hidden, "testSimpleEntireWord: findbar should be open");

      yield* testIterator(tests);

      gFindBar.close();
    }

    function* testCaseSensitive(tests) {
      yield openFindbar();
      ok(!gFindBar.hidden, "testCaseSensitive: findbar should be open");

      let matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
      if (!matchCaseCheckbox.hidden && !matchCaseCheckbox.checked)
        matchCaseCheckbox.click();

      yield* testIterator(tests);

      if (!matchCaseCheckbox.hidden)
        matchCaseCheckbox.click();
      gFindBar.close();
    }

    function* testWordBreakChars(tests) {
      yield openFindbar();
      ok(!gFindBar.hidden, "testWordBreakChars: findbar should be open");

      yield* testIterator(tests);

      gFindBar.close();
    }

    function* testQuickfind(tests) {
      yield ContentTask.spawn(gBrowser, null, function* () {
        let document = content.document;
        let event = document.createEvent("KeyboardEvent");
        event.initKeyEvent("keypress", true, true, null, false, false,
                           false, false, 0, "/".charCodeAt(0));
        document.documentElement.dispatchEvent(event);
      });

      ok(!gFindBar.hidden, "testQuickfind: failed to open findbar");
      ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField,
        "testQuickfind: find field is not focused");
      ok(!gFindBar.getElement("entire-word-status").hidden,
        "testQuickfind: entire word mode status text should be visible");

      yield* testIterator(tests);

      gFindBar.close();
    }
  ]]></script>

  <commandset>
    <command id="cmd_find" oncommand="document.getElementById('FindToolbar').onFindCommand();"/>
  </commandset>
  <browser type="content-primary" flex="1" id="content" src="about:blank"/>
  <browser type="content-primary" flex="1" id="content-remote" remote="true" src="about:blank"/>
  <findbar id="FindToolbar" browserid="content"/>
</window>