'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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; }; }();

var _test = require('probe.gl/test');

var _probe = require('probe.gl');

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // 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.

/* global process */


var log = new _probe.Log('render-test');

// DEFAULT config, intended to be overridden in the node script that calls us

// read the webpack env from 3 arg (node script arg)
var webpackEnv = 'render';
if (process.argv.length >= 3) {
  webpackEnv = process.argv[2];
}

var SERVER_CONFIG = {
  parameters: ['--env.' + webpackEnv]
};

var RenderTestDriver = function (_BrowserDriver) {
  _inherits(RenderTestDriver, _BrowserDriver);

  function RenderTestDriver() {
    _classCallCheck(this, RenderTestDriver);

    return _possibleConstructorReturn(this, (RenderTestDriver.__proto__ || Object.getPrototypeOf(RenderTestDriver)).apply(this, arguments));
  }

  _createClass(RenderTestDriver, [{
    key: 'run',
    value: function run() {
      var _this2 = this;

      var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : SERVER_CONFIG;

      log.log({ message: 'Running rendering tests in Chrome instance...', color: _probe.COLOR.YELLOW })();
      this.time = Date.now();
      return Promise.resolve().then(function (_) {
        return _this2.startServer(config);
      }).then(function (_) {
        return _this2.startBrowser();
      }).then(function (_) {
        return _this2.newPage();
      }).then(function (_) {
        return _this2.waitForBrowserMessage('renderTestComplete');
      }).then(function (resultString) {
        var result = JSON.parse(resultString);
        if (result.success !== Boolean(result.success) || typeof result.failedTest !== 'string') {
          throw new Error('Illegal response "' + resultString + '" returned from Chrome test script');
        }
        if (!result.success) {
          throw new Error(result.failedTest);
        }
        _this2._success();
      }).catch(function (error) {
        _this2._failure(error);
      });
    }
  }, {
    key: '_success',
    value: function _success() {
      var elapsed = ((Date.now() - this.time) / 1000).toFixed(1);
      log.log({
        message: 'Rendering test successfully completed in ' + elapsed + 's!',
        color: _probe.COLOR.BRIGHT_GREEN
      })();
      this.setShellStatus(true);
      this.exit();
    }
  }, {
    key: '_failure',
    value: function _failure(error) {
      log.error({
        message: 'Rendering test failed: ' + error.message,
        color: _probe.COLOR.BRIGHT_RED
      })();
      // Don't call exit(). Leave browser running so user can inspect image that failed to render
      this.setShellStatus(false);
      return Promise.all([
      // this.stopBrowser(), // Don't stop the browser
      this.stopServer()]).then(function (_) {
        // this.exitProcess()); - Don't exit the process
      });
    }
  }]);

  return RenderTestDriver;
}(_test.BrowserDriver);

exports.default = RenderTestDriver;
//# sourceMappingURL=render-test-driver.js.map