diff options
Diffstat (limited to 'toolkit/components/places/tests/head_common.js')
-rw-r--r-- | toolkit/components/places/tests/head_common.js | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/toolkit/components/places/tests/head_common.js b/toolkit/components/places/tests/head_common.js index d7a465786e..22d89db139 100644 --- a/toolkit/components/places/tests/head_common.js +++ b/toolkit/components/places/tests/head_common.js @@ -169,7 +169,6 @@ function readFileData(aFile) { } return bytes; } - /** * Reads the data from the named file, verifying the expected file length. * @@ -187,6 +186,42 @@ function readFileOfLength(aFileName, aExpectedLength) { } /** + * Reads the data from the specified nsIFile, then returns it as data URL. + * + * @param file + * The nsIFile to read from. + * @param mimeType + * The mime type of the file content. + * @return Promise that retunes data URL. + */ +async function readFileDataAsDataURL(file, mimeType) { + const data = readFileData(file); + return fileDataToDataURL(data, mimeType); +} + +/** + * Converts the given data to the data URL. + * + * @param data + * The file data. + * @param mimeType + * The mime type of the file content. + * @return Promise that retunes data URL. + */ +async function fileDataToDataURL(data, mimeType) { + const dataURL = await new Promise(resolve => { + const buffer = new Uint8ClampedArray(data); + const blob = new Blob([buffer], { type: mimeType }); + const reader = new FileReader(); + reader.onload = e => { + resolve(e.target.result); + }; + reader.readAsDataURL(blob); + }); + return dataURL; +} + +/** * Returns the base64-encoded version of the given string. This function is * similar to window.btoa, but is available to xpcshell tests also. * |