Chromium Code Reviews| 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 testSetImageDataClipboard(imageUrl, imageType, expectedError) { |
| 10 testSetImageDataClipboardWithAdditionalData( | |
| 11 imageUrl, imageType, null, expectedError); | |
| 12 } | |
| 13 | |
| 14 function verifySetImageDataResult(expectedError) { | |
| 15 chrome.test.assertEq(expectedError, chrome.runtime.lastError); | |
|
Devlin
2017/05/17 16:08:43
instead of this, which requires callers to pass in
jennyz
2017/05/18 23:33:49
Done.
| |
| 16 chrome.test.succeed(); | |
| 17 } | |
| 18 | |
| 19 function testSetImageDataClipboardWithAdditionalData( | |
|
Devlin
2017/05/17 16:08:43
Seems like we can combine this with testSetImageDa
jennyz
2017/05/18 23:33:48
Done.
| |
| 20 imageUrl, imageType, additional_items, expectedError) { | |
|
Devlin
2017/05/17 16:08:44
additional_items: useJsStyle :)
jennyz
2017/05/18 23:33:49
Done.
| |
| 10 var oReq = new XMLHttpRequest(); | 21 var oReq = new XMLHttpRequest(); |
| 11 oReq.open('GET', imageUrl, true); | 22 oReq.open('GET', imageUrl, true); |
| 12 oReq.responseType = 'arraybuffer'; | 23 oReq.responseType = 'arraybuffer'; |
| 13 | 24 |
| 14 oReq.onload = function (oEvent) { | 25 oReq.onload = function (oEvent) { |
| 15 var arrayBuffer = oReq.response; | 26 var arrayBuffer = oReq.response; |
| 16 var binaryString = ''; | 27 var binaryString = ''; |
| 17 | 28 |
| 18 if (arrayBuffer) { | 29 if (arrayBuffer) { |
| 19 chrome.clipboard.setImageData(arrayBuffer, imageType, function() { | 30 if (additional_items == null) { |
|
Devlin
2017/05/17 16:08:43
why not just if (additionalItems)?
jennyz
2017/05/18 23:33:49
Done.
| |
| 20 chrome.test.assertEq(expectSucceed, !chrome.runtime.lastError); | 31 chrome.clipboard.setImageData(arrayBuffer, imageType, |
| 21 chrome.test.succeed(); | 32 function() { |
| 22 }); | 33 verifySetImageDataResult(expectedError); |
| 34 }); | |
| 35 } else { | |
| 36 chrome.clipboard.setImageData(arrayBuffer, imageType, additional_items, | |
| 37 function() { | |
| 38 verifySetImageDataResult(expectedError); | |
| 39 }); | |
| 40 } | |
| 23 } else { | 41 } else { |
| 24 chrome.test.fail('Failed to load the png image file'); | 42 chrome.test.fail('Failed to load the image file'); |
| 25 } | 43 } |
| 26 }; | 44 }; |
| 27 | 45 |
| 28 oReq.send(null); | 46 oReq.send(null); |
| 29 } | 47 } |
| 30 | 48 |
| 31 function testSavePngImageToClipboard(baseUrl) { | 49 function testSavePngImageToClipboard(baseUrl) { |
| 32 testSetImageDataClipboard(baseUrl + '/icon1.png', 'png', true); | 50 testSetImageDataClipboard( |
| 51 baseUrl + '/icon1.png', 'png', undefined); | |
|
Devlin
2017/05/17 16:08:43
no need to pass undefined
jennyz
2017/05/18 23:33:48
Done.
| |
| 33 } | 52 } |
| 34 | 53 |
| 35 function testSaveJpegImageToClipboard(baseUrl) { | 54 function testSaveJpegImageToClipboard(baseUrl) { |
| 36 testSetImageDataClipboard(baseUrl + '/test.jpg', 'jpeg', true); | 55 testSetImageDataClipboard( |
| 56 baseUrl + '/test.jpg', 'jpeg', undefined); | |
| 37 } | 57 } |
| 38 | 58 |
| 39 function testSaveBadImageData(baseUrl) { | 59 function testSaveBadImageData(baseUrl) { |
| 40 testSetImageDataClipboard(baseUrl + '/redirect_target.gif', 'jpeg', false); | 60 var expectedError = { |
| 61 'message': 'Image data decoding failed.' | |
|
Devlin
2017/05/17 16:08:44
no need for quotes around keys, but moot with othe
jennyz
2017/05/18 23:33:49
Done.
| |
| 62 }; | |
| 63 testSetImageDataClipboard( | |
| 64 baseUrl + '/redirect_target.gif', 'jpeg', expectedError); | |
| 65 } | |
| 66 | |
| 67 function testSavePngImageWithAdditionalDataToClipboard(baseUrl) { | |
| 68 var additional_items = []; | |
| 69 var text_item = { | |
| 70 type: 'text_plain', | |
| 71 data: 'Hello, world' | |
| 72 } | |
| 73 var html_item = { | |
| 74 type: 'text_html', | |
| 75 data: '<b>This is an html markup</b>' | |
| 76 } | |
| 77 additional_items.push(text_item); | |
| 78 additional_items.push(html_item); | |
| 79 testSetImageDataClipboardWithAdditionalData( | |
| 80 baseUrl + '/icon1.png', 'png', additional_items, undefined); | |
| 81 } | |
| 82 | |
| 83 function testSavePngImageWithAdditionalDataToClipboardDuplicateTypeItems(baseUrl ) { | |
|
Devlin
2017/05/17 16:08:44
lines <= 80 char
jennyz
2017/05/18 23:33:49
Done.
| |
| 84 var additional_items = []; | |
| 85 var text_item1 = { | |
| 86 type: 'text_plain', | |
| 87 data: 'Hello, world' | |
| 88 } | |
| 89 var text_item2 = { | |
| 90 type: 'text_plain', | |
| 91 data: 'Another text item' | |
| 92 } | |
| 93 additional_items.push(text_item1); | |
| 94 additional_items.push(text_item2); | |
| 95 var expectedError = { | |
| 96 'message': 'Unsupported additional_items parameter data.' | |
| 97 }; | |
| 98 testSetImageDataClipboardWithAdditionalData( | |
| 99 baseUrl + '/icon1.png', 'png', additional_items, expectedError); | |
| 41 } | 100 } |
| 42 | 101 |
| 43 function bindTest(test, param) { | 102 function bindTest(test, param) { |
| 44 var result = test.bind(null, param); | 103 var result = test.bind(null, param); |
| 45 result.generatedName = test.name; | 104 result.generatedName = test.name; |
| 46 return result; | 105 return result; |
| 47 } | 106 } |
| 48 | 107 |
| 49 chrome.test.getConfig(function(config) { | 108 chrome.test.getConfig(function(config) { |
| 50 var baseUrl = 'http://localhost:' + config.testServer.port + '/extensions'; | 109 var baseUrl = 'http://localhost:' + config.testServer.port + '/extensions'; |
| 51 chrome.test.runTests([ | 110 chrome.test.runTests([ |
| 52 bindTest(testSavePngImageToClipboard, baseUrl), | 111 bindTest(testSavePngImageToClipboard, baseUrl), |
| 53 bindTest(testSaveJpegImageToClipboard, baseUrl), | 112 bindTest(testSaveJpegImageToClipboard, baseUrl), |
| 54 bindTest(testSaveBadImageData, baseUrl) | 113 bindTest(testSaveBadImageData, baseUrl), |
| 114 bindTest(testSavePngImageWithAdditionalDataToClipboard, baseUrl), | |
| 115 bindTest(testSavePngImageWithAdditionalDataToClipboardDuplicateTypeItems, | |
| 116 baseUrl) | |
| 55 ]); | 117 ]); |
| 56 }) | 118 }) |
| OLD | NEW |