{"size": 2244, "ext": "js", "max_stars_repo_path": "apps/sms/views/shared/test/unit/wbmp_test.js", "max_stars_repo_name": "TheoChevalier/gaia", "max_stars_repo_stars_event_min_datetime": "2016-08-17T08:52:51.000Z", "max_stars_repo_stars_event_max_datetime": "2020-03-29T04:56:45.000Z", "max_issues_repo_path": "apps/sms/views/shared/test/unit/wbmp_test.js", "max_issues_repo_name": "TheoChevalier/gaia", "max_issues_repo_issues_event_min_datetime": "2017-02-21T21:36:12.000Z", "max_issues_repo_issues_event_max_datetime": "2017-02-21T21:36:30.000Z", "max_forks_repo_path": "apps/sms/views/shared/test/unit/wbmp_test.js", "max_forks_repo_name": "TheoChevalier/gaia", "max_forks_repo_forks_event_min_datetime": "2021-11-18T21:21:19.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-18T21:21:19.000Z"}
{"max_stars_count": 3, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 25.5, "max_line_length": 76, "alphanum_fraction": 0.6118538324}
/*global WBMP,
         AssetsHelper,
         Promise
*/

'use strict';

require('/views/shared/js/wbmp.js');

suite('wbmp.js >', function() {

  var wbmpBlob, wbmpAsPngBlob;

  suiteSetup(function(done) {
    var mediaFolder = '/views/shared/test/unit/media';
    var blobPromises = [
      AssetsHelper.loadFileBlob(`${mediaFolder}/grid.png`).then(
        (blob) => wbmpAsPngBlob = blob
      ),
      AssetsHelper.loadFileBlob(`${mediaFolder}/grid.wbmp`).then(
        (blob) => wbmpBlob = blob
      )
    ];

    Promise.all(blobPromises).then(() => done(), done);
  });

  function writeBlobToCanvas(blob, callback) {
    var canvas = document.createElement('canvas');
    var image = new Image();

    var blobUrl = window.URL.createObjectURL(blob);
    image.src = blobUrl;
    image.onload = function() {
      window.URL.revokeObjectURL(this.src);
      canvas.height = image.naturalHeight;
      canvas.width = image.naturalWidth;
      var context = canvas.getContext('2d');
      context.drawImage(image, 0, 0);
      callback(canvas);
    };
  }

  function assertPngEquals(pngBlob1, pngBlob2, done) {
    var canvases = [];

    function canvasReady(canvas) {
      canvases.push(canvas);
      if (canvases.length === 2) {
        done(compareCanvases);
      }
    }

    function compareCanvases() {
      var c1 = canvases[0];
      var c2 = canvases[1];
      assert.equal(c1.width, c2.width);
      assert.equal(c1.height, c2.height);

      var id1 = c1.getContext('2d').getImageData(0, 0, c1.width, c1.height);
      var id2 = c2.getContext('2d').getImageData(0, 0, c2.width, c2.height);

      var data1 = id1.data;
      var data2 = id2.data;

      assert.equal(data1.length, data2.length);

      for (var i = 0, l = data1.length; i < l; i++) {
        assert.equal(data1[i], data2[i]);
      }
    }

    writeBlobToCanvas(pngBlob1, canvasReady);
    writeBlobToCanvas(pngBlob2, canvasReady);
  }

  test('Decode correctly', function(done) {
    var reader = new FileReader();
    reader.readAsArrayBuffer(wbmpBlob);
    reader.onload = function() {
      var arrayBuffer = this.result;
      WBMP.decode(arrayBuffer, function(blob) {
        assertPngEquals(blob, wbmpAsPngBlob, done);
      });
    };
  });
});
