var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import { window, document } from 'global';

import SceneRenderer from './scene-renderer';

import { diffImagePixels } from './luma.gl/gpgpu';
import { createImage, getImagePixelData } from './luma.gl/io-basic/browser-image-utils';

var RenderTest = function () {
  function RenderTest() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        testCases = _ref.testCases,
        _ref$width = _ref.width,
        width = _ref$width === undefined ? 800 : _ref$width,
        _ref$height = _ref.height,
        height = _ref$height === undefined ? 450 : _ref$height,
        _ref$colorDeltaThresh = _ref.colorDeltaThreshold,
        colorDeltaThreshold = _ref$colorDeltaThresh === undefined ? 255 * 0.05 : _ref$colorDeltaThresh,
        _ref$testPassThreshol = _ref.testPassThreshold,
        testPassThreshold = _ref$testPassThreshol === undefined ? 0.99 : _ref$testPassThreshol,
        _ref$stopOnFail = _ref.stopOnFail,
        stopOnFail = _ref$stopOnFail === undefined ? true : _ref$stopOnFail,
        _ref$reportResult = _ref.reportResult,
        reportResult = _ref$reportResult === undefined ? reportResultInBrowser : _ref$reportResult,
        _ref$reportFinalResul = _ref.reportFinalResult,
        reportFinalResult = _ref$reportFinalResul === undefined ? reportFinalResultInBrowser : _ref$reportFinalResul;

    _classCallCheck(this, RenderTest);

    this.sceneRenderer = new SceneRenderer({
      width: width,
      height: height,
      scenes: testCases,
      onSceneRendered: this._onSceneRendered.bind(this),
      onComplete: this._onComplete.bind(this)
    });

    this.colorDeltaThreshold = colorDeltaThreshold;
    this.testPassThreshold = testPassThreshold;
    this.reportResult = reportResult;
    this.reportFinalResult = reportFinalResult;

    Object.assign(this, initializeDOM({ width: width, height: height }));
  }

  _createClass(RenderTest, [{
    key: 'run',
    value: function run() {
      this.passed = true;
      this.failedTest = null;
      this.sceneRenderer.run();
    }

    // PRIVATE METHODS

  }, {
    key: '_diffResult',
    value: function _diffResult(_ref2) {
      var name = _ref2.name,
          image = _ref2.image,
          referenceImage = _ref2.referenceImage;

      var referencePixelData = getImagePixelData(referenceImage);
      var resultPixelData = getImagePixelData(image);

      var percentage = diffImagePixels(resultPixelData, referencePixelData);

      var passed = percentage >= this.testPassThreshold;
      this.passed = this.passed && passed;
      this.failedTest = this.failedTest || (this.passed ? null : name);

      return {
        name: name,
        passed: passed,
        percentage: percentage,
        testPassThreshold: this.testPassThreshold
      };
    }
  }, {
    key: '_loadReferenceImage',
    value: function _loadReferenceImage(url) {
      var _this = this;

      return new Promise(function (resolve) {
        _this.referenceImage.onload = function () {
          resolve(_this.referenceImage);
        };

        _this.referenceImage.src = url;
      });
    }
  }, {
    key: '_onSceneRendered',
    value: function _onSceneRendered(_ref3) {
      var _this2 = this;

      var name = _ref3.name,
          image = _ref3.image,
          referenceImageUrl = _ref3.referenceImageUrl;

      return this._loadReferenceImage(referenceImageUrl).then(function (referenceImage) {
        // Both images are loaded, compare results
        var _diffResult2 = _this2._diffResult({
          name: name,
          image: image,
          referenceImage: referenceImage
        }),
            passed = _diffResult2.passed,
            percentage = _diffResult2.percentage,
            testPassThreshold = _diffResult2.testPassThreshold;

        // Print diff result


        _this2.reportResult({
          renderTest: _this2,
          name: name,
          passed: passed,
          percentage: percentage,
          testPassThreshold: testPassThreshold
        });

        if (!passed) {
          _this2.sceneRenderer.stop();
        }
      });
    }
  }, {
    key: '_onComplete',
    value: function _onComplete() {
      this.reportFinalResult({ passed: this.passed, renderTest: this });
      this._reportToTestDriver();
    }

    // Node test driver (puppeteer) may not have had time to expose the function
    // if the test suite is short. If not available, wait a second and try again

  }, {
    key: '_reportToTestDriver',
    value: function _reportToTestDriver() {
      if (window.testDone) {
        var result = { success: this.passed, failedTest: this.failedTest || '' };
        var resultString = JSON.stringify(result);
        window.testDone(resultString);
      } else {
        console.warn('testDone not exposed, waiting 1 second'); // eslint-disable-line
        window.setTimeout(this._reportToTestDriver.bind(this), 1000);
      }
    }
  }]);

  return RenderTest;
}();

export default RenderTest;


function initializeDOM(_ref4) {
  var width = _ref4.width,
      height = _ref4.height;

  // DeckGL container
  var deckGLContainer = document.createElement('div');
  deckGLContainer.style.position = 'absolute';
  // hide deckgl canvas
  deckGLContainer.style.visibility = 'hidden';

  var referenceImage = createImage(width, height);

  var resultContainer = document.createElement('div');
  resultContainer.style.position = 'absolute';
  resultContainer.style.top = '0px';
  resultContainer.style.left = width + 'px';
  resultContainer.style.zIndex = 1;

  // Final flag container
  var flagContainer = document.createElement('div');
  flagContainer.style.position = 'absolute';
  flagContainer.style.top = height + 50 + 'px';

  document.body.appendChild(deckGLContainer);
  document.body.appendChild(referenceImage);
  // document.body.appendChild(resultImage);
  document.body.appendChild(resultContainer);
  document.body.appendChild(flagContainer);

  return { resultContainer: resultContainer, flagContainer: flagContainer, referenceImage: referenceImage };
}

// Default reporting method
function reportResultInBrowser(_ref5) {
  var name = _ref5.name,
      passed = _ref5.passed,
      percentage = _ref5.percentage,
      renderTest = _ref5.renderTest;

  console.log('Rendered new scene ' + name + ' ' + passed); // eslint-disable-line

  var outputString = name + ': ' + (percentage * 100).toFixed(3) + '% ' + (passed ? 'PASS' : 'FAIL');

  var paragraph = document.createElement('p');
  var testResult = document.createTextNode(outputString);
  paragraph.style.color = passed ? '#74ff69' : '#ff2857';
  paragraph.appendChild(testResult);
  renderTest.resultContainer.appendChild(paragraph);
}

function reportFinalResultInBrowser(_ref6) {
  var passed = _ref6.passed,
      renderTest = _ref6.renderTest;

  var outputString = '' + (passed ? 'All CASES PASSED!' : 'NOT All CASES PASSED!');

  var paragraph = document.createElement('p');
  var testResult = document.createTextNode(outputString);
  paragraph.style.color = passed ? '#0bff1c' : '#ff0921';
  paragraph.appendChild(testResult);
  renderTest.flagContainer.appendChild(paragraph);
}
//# sourceMappingURL=render-test.js.map