Bug 1587846 - [remote] Fix payload of return value for Page.captureScreenshot. r=remote-protocol-reviewers,ato

The actual screenshot data should be returned via the "data"
property of an object. Also the data URL prefix has to be
stripped off, so that only the base64 encoded data will be
transmitted.

Differential Revision: https://phabricator.services.mozilla.com/D52144

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henrik Skupin 2019-11-07 19:01:25 +00:00
parent a7e1fdc48c
commit 605cf70003
2 changed files with 36 additions and 32 deletions

View File

@ -120,7 +120,12 @@ class Page extends Domain {
// because it is no longer needed.
snapshot.close();
return canvas.toDataURL();
const url = canvas.toDataURL();
// only return the base64 encoded data without the data URL prefix
const data = url.substring(url.indexOf(",") + 1);
return { data };
}
async enable() {

View File

@ -9,26 +9,28 @@ async function getDevicePixelRatio() {
});
}
async function getImageDetails(client, image) {
return ContentTask.spawn(gBrowser.selectedBrowser, image, async function(
image
) {
let infoPromise = new Promise(resolve => {
const img = new content.Image();
img.addEventListener(
"load",
() => {
resolve({
width: img.width,
height: img.height,
});
},
{ once: true }
);
img.src = image;
});
return infoPromise;
});
async function getImageDetails(format, data) {
return ContentTask.spawn(
gBrowser.selectedBrowser,
{ format, data },
async function({ format, data }) {
return new Promise(resolve => {
const img = new content.Image();
img.addEventListener(
"load",
() => {
resolve({
width: img.width,
height: img.height,
});
},
{ once: true }
);
img.src = `data:image/${format};base64,${data}`;
});
}
);
}
async function getViewportRect() {
@ -42,33 +44,30 @@ async function getViewportRect() {
});
}
add_task(async function documentSmallerThanViewport(client) {
add_task(async function documentSmallerThanViewport({ Page }) {
loadURL(toDataURL("<div>Hello world"));
const { Page } = client;
info("Check that captureScreenshot() captures the viewport by default");
const screenshot = await Page.captureScreenshot();
const { data } = await Page.captureScreenshot();
ok(!!data, "Screenshot data is not empty");
const scale = await getDevicePixelRatio();
const viewportRect = await getViewportRect();
const { width, height } = await getImageDetails(client, screenshot);
const { width, height } = await getImageDetails("png", data);
is(width, (viewportRect.width - viewportRect.left) * scale);
is(height, (viewportRect.height - viewportRect.top) * scale);
});
add_task(async function documentLargerThanViewport(client) {
add_task(async function documentLargerThanViewport({ Page }) {
loadURL(toDataURL("<div style='margin: 100vh 100vw'>Hello world"));
const { Page } = client;
info("Check that captureScreenshot() captures the viewport by default");
const screenshot = await Page.captureScreenshot();
const { data } = await Page.captureScreenshot();
ok(!!data, "Screenshot data is not empty");
const scale = await getDevicePixelRatio();
const viewportRect = await getViewportRect();
const { width, height } = await getImageDetails(client, screenshot);
const { width, height } = await getImageDetails("png", data);
is(width, (viewportRect.width - viewportRect.left) * scale);
is(height, (viewportRect.height - viewportRect.top) * scale);