| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Test clipboard extension api chrome.clipboard.onClipboardDataChanged event. | 5 // Test clipboard extension api chrome.clipboard.onClipboardDataChanged event. |
| 6 | 6 |
| 7 var testSuccessCount = 0; | 7 var testSuccessCount = 0; |
| 8 | 8 |
| 9 function testSetImageDataClipboard(imageUrl, imageType, expectSucceed) { | 9 function verifySetImageDataResult(expectedError) { |
| 10 if (expectedError) |
| 11 chrome.test.assertLastError(expectedError); |
| 12 chrome.test.succeed(); |
| 13 } |
| 14 |
| 15 function testSetImageDataClipboard( |
| 16 imageUrl, imageType, expectedError, additionalItems) { |
| 10 var oReq = new XMLHttpRequest(); | 17 var oReq = new XMLHttpRequest(); |
| 11 oReq.open('GET', imageUrl, true); | 18 oReq.open('GET', imageUrl, true); |
| 12 oReq.responseType = 'arraybuffer'; | 19 oReq.responseType = 'arraybuffer'; |
| 13 | 20 |
| 14 oReq.onload = function (oEvent) { | 21 oReq.onload = function (oEvent) { |
| 15 var arrayBuffer = oReq.response; | 22 var arrayBuffer = oReq.response; |
| 16 var binaryString = ''; | 23 var binaryString = ''; |
| 17 | 24 |
| 18 if (arrayBuffer) { | 25 if (arrayBuffer) { |
| 19 chrome.clipboard.setImageData(arrayBuffer, imageType, function() { | 26 if (additionalItems) { |
| 20 chrome.test.assertEq(expectSucceed, !chrome.runtime.lastError); | 27 chrome.clipboard.setImageData(arrayBuffer, imageType, additionalItems, |
| 21 chrome.test.succeed(); | 28 function() { |
| 22 }); | 29 verifySetImageDataResult(expectedError); |
| 30 }); |
| 31 } else { |
| 32 chrome.clipboard.setImageData(arrayBuffer, imageType, |
| 33 function() { |
| 34 verifySetImageDataResult(expectedError); |
| 35 }); |
| 36 } |
| 23 } else { | 37 } else { |
| 24 chrome.test.fail('Failed to load the png image file'); | 38 chrome.test.fail('Failed to load the image file'); |
| 25 } | 39 } |
| 26 }; | 40 }; |
| 27 | 41 |
| 28 oReq.send(null); | 42 oReq.send(null); |
| 29 } | 43 } |
| 30 | 44 |
| 31 function testSavePngImageToClipboard(baseUrl) { | 45 function testSavePngImageToClipboard(baseUrl) { |
| 32 testSetImageDataClipboard(baseUrl + '/icon1.png', 'png', true); | 46 testSetImageDataClipboard(baseUrl + '/icon1.png', 'png'); |
| 33 } | 47 } |
| 34 | 48 |
| 35 function testSaveJpegImageToClipboard(baseUrl) { | 49 function testSaveJpegImageToClipboard(baseUrl) { |
| 36 testSetImageDataClipboard(baseUrl + '/test.jpg', 'jpeg', true); | 50 testSetImageDataClipboard(baseUrl + '/test.jpg', 'jpeg'); |
| 37 } | 51 } |
| 38 | 52 |
| 39 function testSaveBadImageData(baseUrl) { | 53 function testSaveBadImageData(baseUrl) { |
| 40 testSetImageDataClipboard(baseUrl + '/redirect_target.gif', 'jpeg', false); | 54 testSetImageDataClipboard( |
| 55 baseUrl + '/redirect_target.gif', 'jpeg', 'Image data decoding failed.'); |
| 56 } |
| 57 |
| 58 function testSavePngImageWithAdditionalDataToClipboard(baseUrl) { |
| 59 var additional_items = []; |
| 60 var text_item = { |
| 61 type: 'textPlain', |
| 62 data: 'Hello, world' |
| 63 } |
| 64 var html_item = { |
| 65 type: 'textHtml', |
| 66 data: '<b>This is an html markup</b>' |
| 67 } |
| 68 additional_items.push(text_item); |
| 69 additional_items.push(html_item); |
| 70 testSetImageDataClipboard( |
| 71 baseUrl + '/icon1.png', 'png', undefined, additional_items); |
| 72 } |
| 73 |
| 74 function testSavePngImageWithAdditionalDataToClipboardDuplicateTypeItems( |
| 75 baseUrl) { |
| 76 var additional_items = []; |
| 77 var text_item1 = { |
| 78 type: 'textPlain', |
| 79 data: 'Hello, world' |
| 80 } |
| 81 var text_item2 = { |
| 82 type: 'textPlain', |
| 83 data: 'Another text item' |
| 84 } |
| 85 additional_items.push(text_item1); |
| 86 additional_items.push(text_item2); |
| 87 testSetImageDataClipboard( |
| 88 baseUrl + '/icon1.png', 'png', |
| 89 'Unsupported additionalItems parameter data.', |
| 90 additional_items); |
| 41 } | 91 } |
| 42 | 92 |
| 43 function bindTest(test, param) { | 93 function bindTest(test, param) { |
| 44 var result = test.bind(null, param); | 94 var result = test.bind(null, param); |
| 45 result.generatedName = test.name; | 95 result.generatedName = test.name; |
| 46 return result; | 96 return result; |
| 47 } | 97 } |
| 48 | 98 |
| 49 chrome.test.getConfig(function(config) { | 99 chrome.test.getConfig(function(config) { |
| 50 var baseUrl = 'http://localhost:' + config.testServer.port + '/extensions'; | 100 var baseUrl = 'http://localhost:' + config.testServer.port + '/extensions'; |
| 51 chrome.test.runTests([ | 101 chrome.test.runTests([ |
| 52 bindTest(testSavePngImageToClipboard, baseUrl), | 102 bindTest(testSavePngImageToClipboard, baseUrl), |
| 53 bindTest(testSaveJpegImageToClipboard, baseUrl), | 103 bindTest(testSaveJpegImageToClipboard, baseUrl), |
| 54 bindTest(testSaveBadImageData, baseUrl) | 104 bindTest(testSaveBadImageData, baseUrl), |
| 105 bindTest(testSavePngImageWithAdditionalDataToClipboard, baseUrl), |
| 106 bindTest(testSavePngImageWithAdditionalDataToClipboardDuplicateTypeItems, |
| 107 baseUrl) |
| 55 ]); | 108 ]); |
| 56 }) | 109 }) |
| OLD | NEW |