Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <script src="../js/resources/js-test-pre.js"></script> | |
| 4 <script> | 5 <script> |
| 5 function assertEq(left, right) | |
| 6 { | |
| 7 if (left === right) | |
| 8 log('PASS: ' + left + " === " + right); | |
| 9 else | |
| 10 log('FAIL: ' + left + "(of type " + (typeof left) + ") !== " + right + " (of type " + (typeof right) + ")"); | |
| 11 } | |
| 12 function log(str) | |
| 13 { | |
| 14 var result = document.getElementById('result'); | |
| 15 result.appendChild(document.createTextNode(str)); | |
| 16 result.appendChild(document.createElement('br')); | |
| 17 } | |
| 18 | |
| 19 function legacyCopyStart(dataTransfer) | 6 function legacyCopyStart(dataTransfer) |
| 20 { | 7 { |
| 21 dataTransfer.setData('text', 'sample'); | 8 dataTransfer.setData('text', 'sample'); |
| 22 dataTransfer.setData('url', 'http://www.google.com/'); | 9 dataTransfer.setData('url', 'http://www.google.com/'); |
| 23 dataTransfer.setData('text/html', '<em>Markup</em>'); | 10 dataTransfer.setData('text/html', '<em>Markup</em>'); |
| 24 dataTransfer.setData('custom-data', 'hello world'); | 11 dataTransfer.setData('custom-data', 'hello world'); |
| 25 } | 12 } |
| 26 | 13 |
| 14 var testDataTransfer; | |
| 27 function itemListCopyStart(dataTransfer) | 15 function itemListCopyStart(dataTransfer) |
| 28 { | 16 { |
| 17 testDataTransfer = dataTransfer; | |
| 18 shouldBeUndefined('testDataTransfer.items.item'); | |
| 29 dataTransfer.items.add('sample', 'text/plain'); | 19 dataTransfer.items.add('sample', 'text/plain'); |
| 30 dataTransfer.items.add('http://www.google.com/', 'text/uri-list'); | 20 dataTransfer.items.add('http://www.google.com/', 'text/uri-list'); |
| 31 dataTransfer.items.add('<em>Markup</em>', 'text/html'); | 21 dataTransfer.items.add('<em>Markup</em>', 'text/html'); |
| 32 dataTransfer.items.add('hello world', 'custom-data'); | 22 dataTransfer.items.add('hello world', 'custom-data'); |
| 33 } | 23 } |
| 34 | 24 |
| 35 function copy(event) | 25 function copy(event) |
| 36 { | 26 { |
| 37 event.preventDefault(); | 27 event.preventDefault(); |
| 38 var copyMethod = document.getElementById('copyMethod'); | 28 var copyMethod = document.getElementById('copyMethod'); |
| 39 if (copyMethod.selectedIndex == 0) | 29 if (copyMethod.selectedIndex == 0) |
| 40 legacyCopyStart(event.clipboardData); | 30 legacyCopyStart(event.clipboardData); |
| 41 else if (copyMethod.selectedIndex == 1) | 31 else if (copyMethod.selectedIndex == 1) |
| 42 itemListCopyStart(event.clipboardData); | 32 itemListCopyStart(event.clipboardData); |
| 43 } | 33 } |
| 44 | 34 |
| 45 function legacyPaste(dataTransfer) | 35 function legacyPaste(dataTransfer) |
| 46 { | 36 { |
| 47 assertEq(4, dataTransfer.types.length); | 37 testDataTransfer = dataTransfer; |
| 48 if (dataTransfer.types.indexOf('text/plain') < 0) | 38 shouldBe('testDataTransfer.types.length', '4'); |
| 49 log('FAIL: types array did not contain "text"'); | 39 shouldBeTrue('testDataTransfer.types.indexOf("text/plain") >= 0'); |
| 50 if (dataTransfer.types.indexOf('text/uri-list') < 0) | 40 shouldBeTrue('testDataTransfer.types.indexOf("text/uri-list") >= 0'); |
| 51 log('FAIL: types array did not contain "text/uri-list"'); | 41 shouldBeTrue('testDataTransfer.types.indexOf("text/html") >= 0'); |
| 52 if (dataTransfer.types.indexOf('text/html') < 0) | 42 shouldBeTrue('testDataTransfer.types.indexOf("custom-data") >= 0'); |
| 53 log('FAIL: types array did not contain "text/html"'); | 43 shouldBeEqualToString('testDataTransfer.getData("text")', 'sample'); |
| 54 if (dataTransfer.types.indexOf('custom-data') < 0) | 44 shouldBeEqualToString('testDataTransfer.getData("url")', 'http://www.google. com/'); |
| 55 log('FAIL: types array did not contain "custom-data"'); | 45 shouldBeEqualToString('testDataTransfer.getData("text/html")', '<em>Markup</ em>'); |
| 56 assertEq('sample', dataTransfer.getData('text')); | 46 shouldBeEqualToString('testDataTransfer.getData("custom-data")', 'hello worl d'); |
| 57 assertEq('http://www.google.com/', dataTransfer.getData('url')); | 47 setTimeout(runNext, 0); |
| 58 assertEq('<em>Markup</em>', dataTransfer.getData('text/html')); | |
| 59 assertEq('hello world', dataTransfer.getData('custom-data')); | |
| 60 runNext(); | |
| 61 } | 48 } |
| 62 | 49 |
| 50 var data, expectedData; | |
| 51 var types, expectedTypes; | |
| 63 var outstandingRequests; | 52 var outstandingRequests; |
| 64 function itemListPaste(dataTransfer) | 53 function itemListPaste(dataTransfer) |
| 65 { | 54 { |
| 55 testDataTransfer = dataTransfer; | |
| 66 outstandingRequests = 0; | 56 outstandingRequests = 0; |
| 67 assertEq(4, dataTransfer.items.length); | 57 shouldBe('testDataTransfer.items.length', '4'); |
| 68 var types = []; | 58 types = []; |
| 69 for (var i = 0; i < dataTransfer.items.length; ++i) { | 59 for (var i = 0; i < dataTransfer.items.length; ++i) { |
| 70 types.push({kind: dataTransfer.items[i].kind, type: dataTransfer.items[i ].type}); | 60 types.push({kind: dataTransfer.items[i].kind, type: dataTransfer.items[i ].type}); |
| 71 } | 61 } |
| 72 types.sort(function (a, b) { return a.type.localeCompare(b.type); }); | 62 types.sort(function (a, b) { return a.type.localeCompare(b.type); }); |
| 73 var expectedTypes = [ | 63 expectedTypes = [ |
| 74 { kind: 'string', type: 'custom-data'}, | 64 { kind: 'string', type: 'custom-data'}, |
| 75 { kind: 'string', type: 'text/html'}, | 65 { kind: 'string', type: 'text/html'}, |
| 76 { kind: 'string', type: 'text/plain'}, | 66 { kind: 'string', type: 'text/plain'}, |
| 77 { kind: 'string', type: 'text/uri-list'}, | 67 { kind: 'string', type: 'text/uri-list'}, |
| 78 ]; | 68 ]; |
| 79 assertEq(JSON.stringify(expectedTypes), JSON.stringify(types)); | 69 shouldBe('JSON.stringify(expectedTypes)', 'JSON.stringify(types)'); |
| 80 var expectedResults = { | 70 var expectedResults = { |
| 81 'custom-data': 'hello world', | 71 'custom-data': 'hello world', |
| 82 'text/html': '<em>Markup</em>', | 72 'text/html': '<em>Markup</em>', |
| 83 'text/plain': 'sample', | 73 'text/plain': 'sample', |
| 84 'text/uri-list': 'http://www.google.com/', | 74 'text/uri-list': 'http://www.google.com/', |
| 85 } | 75 } |
| 86 function makeClosure(expectedData) | 76 function makeClosure(_expectedData) |
|
haraken
2013/08/12 15:44:43
Nit: I think Blink doesn't use a variable name lik
| |
| 87 { | 77 { |
| 88 ++outstandingRequests; | 78 ++outstandingRequests; |
| 89 return function (data) { | 79 return function (_data) { |
|
haraken
2013/08/12 15:44:43
Ditto.
| |
| 90 assertEq(expectedData, data); | 80 expectedData = _expectedData; |
| 81 data = _data; | |
| 82 shouldBe('data', 'expectedData'); | |
| 91 if (--outstandingRequests == 0) | 83 if (--outstandingRequests == 0) |
| 92 window.setTimeout(runNext, 0); | 84 setTimeout(runNext, 0); |
| 93 } | 85 } |
| 94 } | 86 } |
| 95 // We use this funky loop to make sure we always print out results in the sa me order. | 87 // We use this funky loop to make sure we always print out results in the sa me order. |
| 96 for (var i = 0; i < types.length; ++i) { | 88 for (var i = 0; i < types.length; ++i) { |
| 97 for (var j = 0; j < dataTransfer.items.length; ++j) { | 89 for (var j = 0; j < dataTransfer.items.length; ++j) { |
| 98 if (types[i].type == dataTransfer.items[j].type) { | 90 if (types[i].type == dataTransfer.items[j].type) { |
| 99 dataTransfer.items[j].getAsString(makeClosure(expectedResults[ty pes[i].type])); | 91 dataTransfer.items[j].getAsString(makeClosure(expectedResults[ty pes[i].type])); |
| 100 break; | 92 break; |
| 101 } | 93 } |
| 102 } | 94 } |
| 103 } | 95 } |
| 104 } | 96 } |
| 105 | 97 |
| 106 function paste(event) | 98 function paste(event) |
| 107 { | 99 { |
| 108 var pasteMethod= document.getElementById('pasteMethod'); | 100 var pasteMethod= document.getElementById('pasteMethod'); |
| 109 if (pasteMethod.selectedIndex == 0) | 101 if (pasteMethod.selectedIndex == 0) |
| 110 legacyPaste(event.clipboardData); | 102 legacyPaste(event.clipboardData); |
| 111 else if (pasteMethod.selectedIndex == 1) | 103 else if (pasteMethod.selectedIndex == 1) |
| 112 itemListPaste(event.clipboardData); | 104 itemListPaste(event.clipboardData); |
| 113 } | 105 } |
| 114 | 106 |
| 115 function runTest(copyMethodIndex, pasteMethodIndex) | 107 function runTest(copyMethodIndex, pasteMethodIndex) |
| 116 { | 108 { |
| 117 var copyMethod = document.getElementById('copyMethod'); | 109 var copyMethod = document.getElementById('copyMethod'); |
| 118 var pasteMethod = document.getElementById('pasteMethod'); | 110 var pasteMethod = document.getElementById('pasteMethod'); |
| 119 copyMethod.selectedIndex = copyMethodIndex; | 111 copyMethod.selectedIndex = copyMethodIndex; |
| 120 pasteMethod.selectedIndex = pasteMethodIndex; | 112 pasteMethod.selectedIndex = pasteMethodIndex; |
| 121 log('Running test with ' + copyMethod.value + ' copy handler and ' + pasteMe thod.value + ' paste handler'); | 113 debug('Running test with ' + copyMethod.value + ' copy handler and ' + paste Method.value + ' paste handler'); |
| 122 | 114 |
| 123 document.execCommand('copy'); | 115 document.execCommand('copy'); |
| 124 document.execCommand('paste'); | 116 document.execCommand('paste'); |
| 125 } | 117 } |
| 126 | 118 |
| 127 var testCases = [ | 119 var testCases = [ |
| 128 [0, 0], | 120 [0, 0], |
| 129 [0, 1], | 121 [0, 1], |
| 130 [1, 0], | 122 [1, 0], |
| 131 [1, 1], | 123 [1, 1], |
| 132 ]; | 124 ]; |
| 133 function runNext() | 125 function runNext() |
| 134 { | 126 { |
| 135 if (!window.testRunner) | 127 if (!window.testRunner) |
| 136 return; | 128 return; |
| 137 var testCase = testCases.shift(); | 129 var testCase = testCases.shift(); |
| 138 if (testCase) | 130 if (testCase) |
| 139 runTest.apply(null, testCase); | 131 runTest.apply(null, testCase); |
| 140 else | 132 else |
| 141 testRunner.notifyDone(); | 133 finishJSTest(); |
| 142 } | 134 } |
| 143 | 135 |
| 144 window.onload = function() | |
| 145 { | |
| 146 if (!window.testRunner) | |
| 147 return; | |
| 148 testRunner.dumpAsText(); | |
| 149 testRunner.waitUntilDone(); | |
| 150 | |
| 151 runNext(); | |
| 152 } | |
| 153 </script> | 136 </script> |
| 154 </head> | 137 </head> |
| 155 <body oncopy="copy(event)" onpaste="paste(event)"> | 138 <body oncopy="copy(event)" onpaste="paste(event)"> |
| 156 <p>To manually test, press your browser shortcut for copy and then for paste. S everal lines that say 'PASS' should appear below. | 139 <p>To manually test, press your browser shortcut for copy and then for paste. S everal lines that say 'PASS' should appear below. |
| 157 <div>Copy handler: <select id="copyMethod"><option>Legacy</option><option>DataTr ansferItemList</option></select></div> | 140 <div>Copy handler: <select id="copyMethod"><option>Legacy</option><option>DataTr ansferItemList</option></select></div> |
| 158 <div>Paste handler: <select id="pasteMethod"><option>Legacy</option><option>Data TransferItemList</option></select></div> | 141 <div>Paste handler: <select id="pasteMethod"><option>Legacy</option><option>Data TransferItemList</option></select></div> |
| 159 <div id="result"></div> | 142 <div id="console"></div> |
|
haraken
2013/08/12 15:44:43
Nit: This won't be needed. Will be auto-inserted b
haraken
2013/08/12 15:46:12
Sorry, I see your comment in https://codereview.ch
arv (Not doing code reviews)
2013/08/12 15:46:38
It is sometimes needed... especially when you do d
| |
| 143 <script> | |
| 144 description("Tests copy / paste and DataTransferItemList"); | |
| 145 | |
| 146 window.jsTestIsAsync = true; | |
| 147 | |
| 148 runNext(); | |
| 149 </script> | |
| 150 <script src="../js/resources/js-test-post.js"></script> | |
| 160 </body> | 151 </body> |
| 161 </html> | 152 </html> |
| OLD | NEW |