summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_GetScreenshotDppx.js
blob: 1d61f6afc136d284576ef247f55029907cef61af (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
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test the getScreenshot property for mozbrowser
"use strict";

SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);

function runTest() {
  var dppxPref = 'layout.css.devPixelsPerPx';
  var cssPixelWidth = 600;
  var cssPixelHeight = 400;

  var iframe1 = document.createElement('iframe');
  iframe1.setAttribute('width', cssPixelWidth);
  iframe1.setAttribute('height', cssPixelHeight);
  iframe1.setAttribute('mozbrowser', 'true');

  iframe1.src = 'data:text/html,<html><body>hello</body></html>';
  document.body.appendChild(iframe1);

  var images = [];

  function screenshotTaken(image) {
    images.push(image);
    if (images.length === 1) {
      ok(true, 'Got initial non blank screenshot');

      if (image.width !== cssPixelWidth || image.height !== cssPixelHeight) {
        ok(false, 'The pixel width of the image received is not correct');
        SimpleTest.finish();
        return;
      }
      ok(true, 'The pixel width of the image received is correct');

      SpecialPowers.pushPrefEnv(
        {'set': [['layout.css.devPixelsPerPx', 2]]}, takeScreenshot);
    }
    else if (images.length === 2) {
      ok(true, 'Got updated screenshot after source page changed');

      if (image.width !== cssPixelWidth * 2 ||
          image.height !== cssPixelHeight * 2) {
        ok(false, 'The pixel width of the 2dppx image received is not correct');
        SimpleTest.finish();
        return;
      }
      ok(true, 'The pixel width of the 2dppx image received is correct');
      SimpleTest.finish();
    }
  }

  function takeScreenshot() {
    function gotImage(e) {
      // |this| is the Image.

      URL.revokeObjectURL(this.src);

      if (e.type === 'error' || !this.width || !this.height) {
        tryAgain();

        return;
      }

      screenshotTaken(this);
    }

    function tryAgain() {
      if (--attempts === 0) {
        ok(false, 'Timed out waiting for correct screenshot');
        SimpleTest.finish();
      } else {
        setTimeout(function() {
          iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess =
            getScreenshotImageData;
        }, 200);
      }
    }

    function getScreenshotImageData(e) {
      var blob = e.target.result;
      if (blob.size === 0) {
        tryAgain();

        return;
      }

      var img = new Image();
      img.src = URL.createObjectURL(blob);
      img.onload = img.onerror = gotImage;
    }

    var attempts = 10;
    iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess =
      getScreenshotImageData;
  }

  function iframeLoadedHandler() {
    SpecialPowers.pushPrefEnv(
      {'set': [['layout.css.devPixelsPerPx', 1]]}, takeScreenshot);
  }

  iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler);
}

addEventListener('testready', runTest);