summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/browser/browser_getScreenshots.js
blob: 6e285c2114826ca61e3b722ceb8c251ebfbe1b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

// a blue page
const TEST_URL =
  "https://example.com/browser/browser/components/newtab/test/browser/blue_page.html";
const XHTMLNS = "http://www.w3.org/1999/xhtml";

ChromeUtils.defineModuleGetter(
  this,
  "Screenshots",
  "resource://activity-stream/lib/Screenshots.jsm"
);

function get_pixels(stringOrObject, width, height) {
  return new Promise(resolve => {
    // get the pixels out of the screenshot that we just took
    let img = document.createElementNS(XHTMLNS, "img");
    let imgPath;

    if (typeof stringOrObject === "string") {
      Assert.ok(
        Services.prefs.getBoolPref(
          "browser.tabs.remote.separatePrivilegedContentProcess"
        ),
        "The privileged about content process should be enabled."
      );
      imgPath = stringOrObject;
      Assert.ok(
        imgPath.startsWith("moz-page-thumb://"),
        "Thumbnails should be retrieved using moz-page-thumb://"
      );
    } else {
      imgPath = URL.createObjectURL(stringOrObject.data);
    }

    img.setAttribute("src", imgPath);
    img.addEventListener(
      "load",
      () => {
        let canvas = document.createElementNS(XHTMLNS, "canvas");
        canvas.setAttribute("width", width);
        canvas.setAttribute("height", height);
        let ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, width, height);
        const result = ctx.getImageData(0, 0, width, height).data;
        URL.revokeObjectURL(imgPath);
        resolve(result);
      },
      { once: true }
    );
  });
}

add_task(async function test_screenshot() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.pagethumbnails.capturing_disabled", false]],
  });

  // take a screenshot of a blue page and save it as a blob
  const screenshotAsObject = await Screenshots.getScreenshotForURL(TEST_URL);
  let pixels = await get_pixels(screenshotAsObject, 10, 10);
  let rgbaCount = { r: 0, g: 0, b: 0, a: 0 };
  while (pixels.length) {
    // break the pixels into arrays of 4 components [red, green, blue, alpha]
    let [r, g, b, a, ...rest] = pixels;
    pixels = rest;
    // count the number of each coloured pixels
    if (r === 255) {
      rgbaCount.r += 1;
    }
    if (g === 255) {
      rgbaCount.g += 1;
    }
    if (b === 255) {
      rgbaCount.b += 1;
    }
    if (a === 255) {
      rgbaCount.a += 1;
    }
  }

  // in the end, we should only have 100 blue pixels (10 x 10) with full opacity
  Assert.equal(rgbaCount.b, 100, "Has 100 blue pixels");
  Assert.equal(rgbaCount.a, 100, "Has full opacity");
  Assert.equal(rgbaCount.r, 0, "Does not have any red pixels");
  Assert.equal(rgbaCount.g, 0, "Does not have any green pixels");
});