summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_console_screenshot.js
blob: a46a99050b96c5903a529c0b032711b8790ac4b8 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that :screenshot command works properly in the Browser Console

"use strict";

const COLOR_DIV_1 = "rgb(255, 0, 0)";
const COLOR_DIV_2 = "rgb(0, 200, 0)";
const COLOR_DIV_3 = "rgb(0, 0, 150)";
const COLOR_DIV_4 = "rgb(100, 0, 100)";

const TEST_URI = `data:text/html,<!DOCTYPE html><meta charset=utf8>
    <style>
      body {
        margin: 0;
        height: 100vh;
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: 1fr 1fr;
      }
      div:nth-child(1) { background-color: ${COLOR_DIV_1}; }
      div:nth-child(2) { background-color: ${COLOR_DIV_2}; }
      div:nth-child(3) { background-color: ${COLOR_DIV_3}; }
      div:nth-child(4) { background-color: ${COLOR_DIV_4}; }
    </style>
    <body>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </body>`;

add_task(async function () {
  await addTab(TEST_URI);
  const hud = await BrowserConsoleManager.toggleBrowserConsole();

  info("Execute :screenshot");
  const file = new FileUtils.File(
    PathUtils.join(PathUtils.tempDir, "TestScreenshotFile.png")
  );
  // on some machines, such as macOS, dpr is set to 2. This is expected behavior, however
  // to keep tests consistant across OSs we are setting the dpr to 1
  const command = `:screenshot ${file.path} --dpr 1`;

  await executeAndWaitForMessageByType(
    hud,
    command,
    `Saved to ${file.path}`,
    ".console-api"
  );

  info("Create an image using the downloaded file as source");
  const image = new Image();
  image.src = PathUtils.toFileURI(file.path);
  await once(image, "load");

  info(
    "Retrieve the position of the elements relatively to the browser viewport"
  );
  const bodyBounds = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    async () => {
      return content.document.body.getBoxQuadsFromWindowOrigin()[0].getBounds();
    }
  );

  const center = [
    bodyBounds.left + bodyBounds.width / 2,
    bodyBounds.top + bodyBounds.height / 2,
  ];

  info(
    "Check that the divs of the content page were rendered on the screenshot"
  );
  checkImageColorAt({
    image,
    x: center[0] - 50,
    y: center[1] - 50,
    expectedColor: COLOR_DIV_1,
    label: "The screenshot did render the first div of the content page",
  });
  checkImageColorAt({
    image,
    x: center[0] + 50,
    y: center[1] - 50,
    expectedColor: COLOR_DIV_2,
    label: "The screenshot did render the second div of the content page",
  });
  checkImageColorAt({
    image,
    x: center[0] - 50,
    y: center[1] + 50,
    expectedColor: COLOR_DIV_3,
    label: "The screenshot did render the third div of the content page",
  });
  checkImageColorAt({
    image,
    x: center[0] + 50,
    y: center[1] + 50,
    expectedColor: COLOR_DIV_4,
    label: "The screenshot did render the fourth div of the content page",
  });

  info("Remove the downloaded screenshot file and cleanup downloads");
  await IOUtils.remove(file.path);
  await resetDownloads();
});