summaryrefslogtreecommitdiffstats
path: root/browser/components/screenshots/tests/browser/browser_screenshots_test_full_page.js
blob: 006a9819ede58771b35241258f612182e07025a1 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function assertPixel(actual, expected, message) {
  info(message);
  isfuzzy(actual[0], expected[0], 1, "R color value");
  isfuzzy(actual[1], expected[1], 1, "G color value");
  isfuzzy(actual[2], expected[2], 1, "B color value");
}

add_task(async function test_fullpageScreenshot() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_PAGE,
    },
    async browser => {
      let helper = new ScreenshotsHelper(browser);
      let contentInfo = await helper.getContentDimensions();
      ok(contentInfo, "Got dimensions back from the content");
      let devicePixelRatio = await getContentDevicePixelRatio(browser);

      let expectedWidth = Math.floor(
        devicePixelRatio * contentInfo.scrollWidth
      );
      let expectedHeight = Math.floor(
        devicePixelRatio * contentInfo.scrollHeight
      );

      // click toolbar button so panel shows
      helper.triggerUIFromToolbar();

      let panel = await helper.waitForPanel();

      let screenshotReady = TestUtils.topicObserved(
        "screenshots-preview-ready"
      );

      // click the full page button in panel
      let visiblePage = panel
        .querySelector("screenshots-buttons")
        .shadowRoot.querySelector(".full-page");
      visiblePage.click();

      let dialog = helper.getDialog();

      await screenshotReady;

      let copyButton = dialog._frame.contentDocument.getElementById("copy");
      ok(copyButton, "Got the copy button");

      let clipboardChanged = helper.waitForRawClipboardChange(
        expectedWidth,
        expectedHeight
      );

      // click copy button on dialog box
      copyButton.click();

      info("Waiting for clipboard change");
      let result = await clipboardChanged;

      info("result: " + JSON.stringify(result, null, 2));
      info("contentInfo: " + JSON.stringify(contentInfo, null, 2));

      Assert.equal(result.width, expectedWidth, "Widths should be equal");
      Assert.equal(result.height, expectedHeight, "Heights should be equal");

      // Due to https://bugzil.la/1396587, the pasted image colors differ from
      // the original image on macOS. Once that bug is fixed, we can remove the
      // special check for macOS.
      if (AppConstants.platform === "macosx") {
        assertPixel(result.color.topLeft, [130, 130, 130], "Top left pixel");
        assertPixel(result.color.topRight, [66, 170, 171], "Top right pixel");
        assertPixel(
          result.color.bottomLeft,
          [125, 75, 125],
          "Bottom left pixel"
        );
        assertPixel(
          result.color.bottomRight,
          [64, 145, 169],
          "Bottom right pixel"
        );
      } else {
        assertPixel(result.color.topLeft, [111, 111, 111], "Top left pixel");
        assertPixel(result.color.topRight, [55, 155, 155], "Top right pixel");
        assertPixel(
          result.color.bottomLeft,
          [105, 55, 105],
          "Bottom left pixel"
        );
        assertPixel(
          result.color.bottomRight,
          [52, 127, 152],
          "Bottom right pixel"
        );
      }
    }
  );
});

add_task(async function test_fullpageScreenshotScrolled() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_PAGE,
    },
    async browser => {
      let helper = new ScreenshotsHelper(browser);

      await SpecialPowers.spawn(browser, [], () => {
        content.scrollTo(0, 2008);
      });
      let contentInfo = await helper.getContentDimensions();
      ok(contentInfo, "Got dimensions back from the content");
      let devicePixelRatio = await getContentDevicePixelRatio(browser);

      let expectedWidth = Math.floor(
        devicePixelRatio * contentInfo.scrollWidth
      );
      let expectedHeight = Math.floor(
        devicePixelRatio * contentInfo.scrollHeight
      );

      // click toolbar button so panel shows
      helper.triggerUIFromToolbar();
      await helper.waitForOverlay();

      let panel = await helper.waitForPanel();

      let screenshotReady = TestUtils.topicObserved(
        "screenshots-preview-ready"
      );

      // click the full page button in panel
      let visiblePage = panel
        .querySelector("screenshots-buttons")
        .shadowRoot.querySelector(".full-page");
      visiblePage.click();

      let dialog = helper.getDialog();

      await screenshotReady;

      let copyButton = dialog._frame.contentDocument.getElementById("copy");
      ok(copyButton, "Got the copy button");

      let clipboardChanged = helper.waitForRawClipboardChange(
        expectedWidth,
        expectedHeight
      );

      // click copy button on dialog box
      copyButton.click();

      info("Waiting for clipboard change");
      let result = await clipboardChanged;

      info("result: " + JSON.stringify(result, null, 2));
      info("contentInfo: " + JSON.stringify(contentInfo, null, 2));

      Assert.equal(result.width, expectedWidth, "Widths should be equal");
      Assert.equal(result.height, expectedHeight, "Heights should be equal");

      // Due to https://bugzil.la/1396587, the pasted image colors differ from
      // the original image on macOS. Once that bug is fixed, we can remove the
      // special check for macOS.
      if (AppConstants.platform === "macosx") {
        assertPixel(result.color.topLeft, [130, 130, 130], "Top left pixel");
        assertPixel(result.color.topRight, [66, 170, 171], "Top right pixel");
        assertPixel(
          result.color.bottomLeft,
          [125, 75, 125],
          "Bottom left pixel"
        );
        assertPixel(
          result.color.bottomRight,
          [64, 145, 169],
          "Bottom right pixel"
        );
      } else {
        assertPixel(result.color.topLeft, [111, 111, 111], "Top left pixel");
        assertPixel(result.color.topRight, [55, 155, 155], "Top right pixel");
        assertPixel(
          result.color.bottomLeft,
          [105, 55, 105],
          "Bottom left pixel"
        );
        assertPixel(
          result.color.bottomRight,
          [52, 127, 152],
          "Bottom right pixel"
        );
      }
    }
  );
});