summaryrefslogtreecommitdiffstats
path: root/browser/components/screenshots/tests/browser/browser_screenshots_test_screenshot_too_big.js
blob: 1c1bac32db071812272126948d2318359f5e3a00 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const SCREENSHOTS_EVENTS = [
  { category: "screenshots", method: "failed", object: "screenshot_too_large" },
  { category: "screenshots", method: "failed", object: "screenshot_too_large" },
  { category: "screenshots", method: "failed", object: "screenshot_too_large" },
  { category: "screenshots", method: "failed", object: "screenshot_too_large" },
];

const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);
ChromeUtils.defineESModuleGetters(this, {
  ScreenshotsUtils: "resource:///modules/ScreenshotsUtils.sys.mjs",
});

add_task(async function test_screenshot_too_large_cropped() {
  await clearAllTelemetryEvents();
  const screenshotsLocalization = new Localization(
    ["browser/screenshots.ftl"],
    true
  );

  let [errorTitle, errorMessage] = screenshotsLocalization.formatMessagesSync([
    { id: "screenshots-too-large-error-title" },
    { id: "screenshots-too-large-error-details" },
  ]);
  let showAlertMessageStub = sinon
    .stub(ScreenshotsUtils, "showAlertMessage")
    .callsFake(function (title, message) {
      is(title, errorTitle.value, "Got error title");
      is(message, errorMessage.value, "Got error message");
    });

  let rect = { x: 0, y: 0, width: 40000, height: 40000, devicePixelRatio: 1 };

  ScreenshotsUtils.cropScreenshotRectIfNeeded(rect);

  is(rect.width, MAX_CAPTURE_DIMENSION, "The width is 32767");
  is(
    rect.height,
    Math.floor(MAX_CAPTURE_AREA / MAX_CAPTURE_DIMENSION),
    "The height is 124925329 / 32767"
  );

  rect.width = 40000;
  rect.hegith = 1;

  ScreenshotsUtils.cropScreenshotRectIfNeeded(rect);

  is(
    rect.width,
    MAX_CAPTURE_DIMENSION,
    "The width was cropped to the max capture dimension (32767)."
  );

  rect.width = 1;
  rect.height = 40000;

  ScreenshotsUtils.cropScreenshotRectIfNeeded(rect);

  is(
    rect.height,
    MAX_CAPTURE_DIMENSION,
    "The height was cropped to the max capture dimension (32767)."
  );

  rect.width = 12345;
  rect.height = 12345;

  ScreenshotsUtils.cropScreenshotRectIfNeeded(rect);

  is(rect.width, 12345, "The width was not cropped");
  is(
    rect.height,
    Math.floor(MAX_CAPTURE_AREA / 12345),
    "The height was cropped to 10119"
  );

  showAlertMessageStub.restore();

  await assertScreenshotsEvents(SCREENSHOTS_EVENTS);
});