013d2b0946c11c363009be8d37815fac17157a0b
[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 sel = window.getSelection();
29
30 var tests = [];
31
32 function done()
33 {
34     var next = tests.shift();
35     if (next)
36         return window.setTimeout(next, 0);
37     testRoot.style.display = "none";
38     layoutTestController.notifyDone();
39 }
40
41 function verifyMarker(node, expectedMarked)
42 {
43     if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) {
44         node.focus();
45     } else {
46         sel.selectAllChildren(node);
47     }
48     return layoutTestController.hasSpellingMarker(expectedMarked[0], expectedMarked[1]);
49 }
50
51 function pasteAndVerify(source, dest, expectedMarked)
52 {
53     sel.selectAllChildren(source);
54     document.execCommand("Copy");
55     if (dest instanceof HTMLInputElement || dest instanceof HTMLTextAreaElement) {
56         dest.value = "";
57         dest.focus();
58     } else {
59         dest.innerHTML = "";
60         sel.selectAllChildren(dest);
61     }
62     document.execCommand("Paste");
63
64     var nretry = 10;
65     var nsleep = 1;
66     function trial() { 
67         var verified = verifyMarker(dest, expectedMarked);
68         if (verified) {
69             testPassed(dest.tagName + " has a marker on '" + source.innerHTML + "'");
70             done();
71             return;
72         }
73
74         nretry--;
75         if (0 == nretry) {
76             testFailed(dest.tagName + " should have a marker on for '" + source.innerHTML + "'");
77             done();
78             return;
79         }
80         
81         nsleep *= 2;
82         window.setTimeout(trial, nsleep);
83     };
84     trial();
85 };
86
87 if (window.layoutTestController)
88     layoutTestController.setAsynchronousSpellCheckingEnabled(true);
89
90 tests.push(function() { pasteAndVerify(testSourcePlain, testInput, [0, 3]); });
91 tests.push(function() { pasteAndVerify(testSourceDecorated, testInput, [0, 3]); });
92 tests.push(function() { pasteAndVerify(testSourcePlain, testTextArea, [0, 3]); });
93 tests.push(function() { pasteAndVerify(testSourceDecorated, testTextArea, [0, 3]); });
94 tests.push(function() { pasteAndVerify(testSourcePlain, testEditable, [0, 3]); });
95 tests.push(function() { pasteAndVerify(testSourceDecorated, testEditable, [0, 2]); }); // To check "fo" part of foo.
96 done();
97
98 var successfullyParsed = true;