[Chromium] Layout test does not return correct misspelling positions if there are...
[WebKit-https.git] / LayoutTests / editing / spelling / script-tests / spellcheck-paste.js
1
2 description('For Bug 40092: Spell checking for pasted text.');
3
4 layoutTestController.waitUntilDone();
5
6 var testRoot = document.createElement("div");
7 document.body.insertBefore(testRoot, document.body.firstChild);
8
9 var testTextArea = document.createElement("textarea");
10 testRoot.appendChild(testTextArea);
11
12 var testInput = document.createElement("input");
13 testInput.setAttribute("type", "text");
14 testRoot.appendChild(testInput);
15
16 var testEditable = document.createElement("div");
17 testEditable.setAttribute("contentEditable", "true");
18 testRoot.appendChild(testEditable);
19
20 var testSourcePlain = document.createElement("div");
21 testSourcePlain.innerHTML = "foo bar";
22 testRoot.appendChild(testSourcePlain);
23
24 var testSourceDecorated = document.createElement("div");
25 testSourceDecorated.innerHTML = "fo<b>o ba</b>r";
26 testRoot.appendChild(testSourceDecorated);
27
28 var testSourceMulti = document.createElement("div");
29 testSourceMulti.innerHTML = "zz zz zz";
30 testRoot.appendChild(testSourceMulti);
31
32 var sel = window.getSelection();
33
34 var tests = [];
35
36 function done()
37 {
38     var next = tests.shift();
39     if (next)
40         return window.setTimeout(next, 0);
41     testRoot.style.display = "none";
42     layoutTestController.notifyDone();
43 }
44
45 function verifyMarker(node, expectedMarked)
46 {
47     if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) {
48         node.focus();
49     } else {
50         sel.selectAllChildren(node);
51     }
52
53     var ok = true;
54     for (var i = 0; ok && i < expectedMarked.length; ++i)
55         ok = layoutTestController.hasSpellingMarker(expectedMarked[i][0], expectedMarked[i][1]);
56     return ok;
57 }
58
59 function pasteAndVerify(source, dest, expectedMarked)
60 {
61     sel.selectAllChildren(source);
62     document.execCommand("Copy");
63     if (dest instanceof HTMLInputElement || dest instanceof HTMLTextAreaElement) {
64         dest.value = "";
65         dest.focus();
66     } else {
67         dest.innerHTML = "";
68         sel.selectAllChildren(dest);
69     }
70     document.execCommand("Paste");
71
72     var nretry = 10;
73     var nsleep = 1;
74     function trial() { 
75         var verified = verifyMarker(dest, expectedMarked);
76         if (verified) {
77             testPassed(dest.tagName + " has a marker on '" + source.innerHTML + "'");
78             done();
79             return;
80         }
81
82         nretry--;
83         if (0 == nretry) {
84             testFailed(dest.tagName + " should have a marker on for '" + source.innerHTML + "'");
85             done();
86             return;
87         }
88         
89         nsleep *= 2;
90         window.setTimeout(trial, nsleep);
91     };
92     trial();
93 };
94
95 if (window.layoutTestController)
96     layoutTestController.setAsynchronousSpellCheckingEnabled(true);
97
98 tests.push(function() { pasteAndVerify(testSourcePlain, testInput, [[0, 3]]); });
99 tests.push(function() { pasteAndVerify(testSourceDecorated, testInput, [[0, 3]]); });
100 tests.push(function() { pasteAndVerify(testSourceMulti, testInput, [[0, 2], [3, 2]]); });
101 tests.push(function() { pasteAndVerify(testSourcePlain, testTextArea, [[0, 3]]); });
102 tests.push(function() { pasteAndVerify(testSourceDecorated, testTextArea, [[0, 3]]); });
103 tests.push(function() { pasteAndVerify(testSourceMulti, testTextArea, [[0, 2], [3, 2]]); });
104 tests.push(function() { pasteAndVerify(testSourcePlain, testEditable, [[0, 3]]); });
105 tests.push(function() { pasteAndVerify(testSourceDecorated, testEditable, [[0, 2]]); }); // To check "fo" part of foo.
106 tests.push(function() { pasteAndVerify(testSourceMulti, testEditable, [[0, 2], [3, 2]]); });
107 done();
108
109 var successfullyParsed = true;