Code coverage report for taxi/lib/helpers/comparison.js

Statements: 61.22% (30 / 49)      Branches: 8.33% (1 / 12)      Functions: 50% (6 / 12)      Lines: 61.22% (30 / 49)      Ignored: none     

All files » taxi/lib/helpers/ » comparison.js
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    1 1 1   1                       1 2   2 2 2                               1 8 8                             1                             1 2                     1   2       2 2 2   2                     1                       1                                                   1                                             1                                   1 2 2 2         1  
'use strict';
 
var logMethods = require('../log');
var type = require('../type');
var when = require('../when');
 
module.exports = Comparison;
 
/**
 * Comparison object
 *
 * @constructor
 * @class Comparison
 * @module WebDriver
 * @submodule Helpers
 * @param {Driver} driver
 * @param {string[]} comparisonList List of tools to use for comparison
 */
function Comparison (driver, comparisonList) {
	this._driver = driver;
 
	this._comparisons = {};
	comparisonList.forEach(function (comparison) {
		this._loadComparison(comparison);
	}.bind(this));
}
 
 
/////////////////////
// Private Methods //
/////////////////////
 
/**
 * Logs a method call by an event
 *
 * @param {object} event
 * @method _logMethodCall
 * @private
 */
Comparison.prototype._logMethodCall = function (event) {
	event.target = 'Comparison';
	this._driver._logMethodCall(event);
};
 
 
/**
 * Performs a context dependent JSON request for the current session.
 * The result is parsed for errors.
 *
 * @method _requestJSON
 * @private
 * @param {String} method
 * @param {String} path
 * @param {*} [body]
 * @return {*}
 */
Comparison.prototype._requestJSON = function (method, path, body) {
	return this._driver._requestJSON(method, path, body);
};
 
 
////////////////////
// Public Methods //
////////////////////
 
/**
 * Gets the driver object.
 * Direct-access. No need to wait.
 *
 * @return {Driver}
 */
Comparison.prototype.getDriver = function () {
	return this._driver;
};
 
 
/**
 * Loads a specific comparison plugin
 *
 * @method _loadComparison
 * @param {string} name
 * @private
 */
Comparison.prototype._loadComparison = function (name) {
 
	var lowerCasedName = name.toLowerCase(),
		Class,
		instance;
 
	Class = require('../comparison/' + lowerCasedName);
	instance = new Class(this.getDriver());
	instance.setup();
 
	this._comparisons[lowerCasedName] = instance;
};
 
 
/**
 * Gets a specific comparison tool by name
 *
 * @method getComparison
 * @param {string} name
 * @returns {BlinkDiffComparison}
 */
Comparison.prototype.getComparison = function (name) {
	return this._comparisons[name.toLowerCase()];
};
 
/**
 * Compares a specific screenshot with all comparison plugins
 *
 * @method compare
 * @param {string} title Unique title of image
 * @param {Buffer} imageBlob Image buffer of current screenshot
 * @param {object} [options] Comparison options
 */
Comparison.prototype.compare = function (title, imageBlob, options) {
 
	var compare;
 
	options = options || {};
 
	compare = options.compare || this._getCompareList();
	delete options.compare;
 
	if (typeof compare === 'string') {
		compare = [compare];
	}
 
	return this._compare(compare, title, imageBlob, options);
};
 
/**
 * Uses a list comparison plugins to compare a screenshot
 *
 * @method _compare
 * @param {string[]} plugins
 * @param {string} title
 * @param {Buffer} imageBlob
 * @param {object} options
 * @private
 */
Comparison.prototype._compare = function (plugins, title, imageBlob, options) {
 
	if (plugins.length !== 0) {
 
		return when(this.getComparison(plugins[0]).compare(title, imageBlob, options), function () {
 
			plugins.shift();
			this._compare(plugins, title, imageBlob, options);
 
		}.bind(this));
 
	} else {
		this.getDriver().utils().resolve(null);
	}
};
 
/**
 * Gets a list of comparison plugins
 *
 * @method _getCompareList
 * @return {string[]}
 * @private
 */
Comparison.prototype._getCompareList = function () {
 
	var comparison = [];
 
	for (var k in this._comparisons) {
		if (this._comparisons.hasOwnProperty(k)) {
			comparison.push(k);
		}
	}
 
	return comparison;
};
 
/**
 * Tear-down of comparison plugins
 *
 * @method tearDown
 */
Comparison.prototype.tearDown = function () {
	for (var k in this._comparisons) {
		Eif (this._comparisons.hasOwnProperty(k)) {
			this._comparisons[k].tearDown();
		}
	}
};
 
logMethods(Comparison.prototype);