summaryrefslogtreecommitdiffstats
path: root/browser/tools/mozscreenshots/tests/browser/browser_screenshots_cropping.js
blob: 80fcde5d6886d944f04d75d16c4e51befa241b70 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 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";

/* import-globals-from ../../head.js */

const { Rect } = ChromeUtils.importESModule(
  "resource://gre/modules/Geometry.sys.mjs"
);

async function draw(window, src) {
  const { document, Image } = window;

  const promise = new Promise((resolve, reject) => {
    const img = new Image();

    img.onload = function () {
      // Create a new offscreen canvas
      const canvas = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        "canvas"
      );
      canvas.width = img.naturalWidth;
      canvas.height = img.naturalHeight;
      const ctx = canvas.getContext("2d");

      ctx.drawImage(img, 0, 0);

      resolve(canvas);
    };

    img.onerror = function () {
      reject(`error loading image ${src}`);
    };

    // Load the src image for drawing
    img.src = src;
  });

  return promise;
}

async function compareImages(window, expected, test) {
  const testCanvas = await draw(window, test);
  const expectedCanvas = await draw(window, expected);

  is(
    testCanvas.width,
    expectedCanvas.width,
    "The test and expected images must be the same size"
  );
  is(
    testCanvas.height,
    expectedCanvas.height,
    "The test and expected images must be the same size"
  );

  var maxDifference = {};
  var differences = window.windowUtils.compareCanvases(
    expectedCanvas,
    testCanvas,
    maxDifference
  );

  // Fuzz for minor differences that can be caused by the encoder.
  if (maxDifference.value > 1) {
    return differences;
  }
  return 0;
}

async function cropAndCompare(window, src, expected, test, region, subregions) {
  await TestRunner._cropImage(window, src, region, subregions, test);

  return compareImages(window, expected, PathUtils.toFileURI(test));
}

add_task(async function crop() {
  const window = Services.wm.getMostRecentWindow("navigator:browser");

  const tmp = PathUtils.tempDir;
  is(
    await cropAndCompare(
      window,
      "resource://mozscreenshots/lib/robot.png",
      "resource://mozscreenshots/lib/robot_upperleft.png",
      PathUtils.join(tmp, "test_cropped_upperleft.png"),
      new Rect(0, 0, 32, 32),
      [new Rect(0, 0, 32, 32)]
    ),
    0,
    "The image should be cropped to the upper left quadrant"
  );

  is(
    await cropAndCompare(
      window,
      "resource://mozscreenshots/lib/robot.png",
      "resource://mozscreenshots/lib/robot_center.png",
      PathUtils.join(tmp, "test_cropped_center.png"),
      new Rect(16, 16, 32, 32),
      [new Rect(16, 16, 32, 32)]
    ),
    0,
    "The image should be cropped to the center of the image"
  );

  is(
    await cropAndCompare(
      window,
      "resource://mozscreenshots/lib/robot.png",
      "resource://mozscreenshots/lib/robot_uncropped.png",
      PathUtils.join(tmp, "test_uncropped.png"),
      new Rect(-8, -9, 80, 80),
      [new Rect(-8, -9, 80, 80)]
    ),
    0,
    "The image should be not be cropped, and the cropping region should be clipped to the size of the image"
  );

  is(
    await cropAndCompare(
      window,
      "resource://mozscreenshots/lib/robot.png",
      "resource://mozscreenshots/lib/robot_diagonal.png",
      PathUtils.join(tmp, "test_diagonal.png"),
      new Rect(0, 0, 64, 64),
      [
        new Rect(0, 0, 16, 16),
        new Rect(16, 16, 16, 16),
        new Rect(32, 32, 16, 16),
        new Rect(48, 48, 16, 16),
      ]
    ),
    0,
    "The image should be contain squares across the diagonal"
  );

  is(
    await cropAndCompare(
      window,
      "resource://mozscreenshots/lib/robot.png",
      "resource://mozscreenshots/lib/robot_cropped_diagonal.png",
      PathUtils.join(tmp, "test_cropped_diagonal.png"),
      new Rect(16, 16, 48, 48),
      [new Rect(16, 16, 16, 16), new Rect(32, 32, 16, 16)]
    ),
    0,
    "The image should be cropped with squares across the diagonal"
  );
});