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

Statements: 17.62% (40 / 227)      Branches: 0% (0 / 121)      Functions: 8.93% (5 / 56)      Lines: 17.62% (40 / 227)      Ignored: none     

All files » taxi/lib/helpers/ » screenshot.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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930    1 1 1   1   1 1   1                     1 4                             1 12 12                             1                             1 4                       1                                     1                             1                                     1   2                                               1                                                           1                                                         1                                                                                                                                                         1                                                                                   1                                                                                       1   2           2                                                                                                                                                   1                                           1                                                                                                                                                     1                                                                                                             1                                                                                                                                                                         1                                                                                                         1                                             1                                       1                                                                         1                                                             1                                                                                                       1                 1  
'use strict';
 
var logMethods = require('../log');
var type = require('../type');
var when = require('../when');
 
var screenshotScripts = require('../scripts/screenshot');
 
var PNGImage = require('pngjs-image');
var fs = require('fs');
 
module.exports = Screenshot;
 
/**
 * Screenshot object
 *
 * @constructor
 * @class Screenshot
 * @module WebDriver
 * @submodule Helpers
 * @param {Driver} driver
 */
function Screenshot (driver) {
    this._driver = driver;
}
 
 
/////////////////////
// Private Methods //
/////////////////////
 
/**
 * Logs a method call by an event
 *
 * @param {object} event
 * @method _logMethodCall
 * @private
 */
Screenshot.prototype._logMethodCall = function (event) {
    event.target = 'Screenshot';
    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 {*}
 */
Screenshot.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}
 */
Screenshot.prototype.getDriver = function () {
    return this._driver;
};
 
 
/**
 * Takes a screenshot as-is from the webdriver protocol
 *
 * @method takeRawScreenshot
 * @param {Object} [options]
 * @return {Buffer}
 */
//Screenshot.prototype.id = 0; //TODO: Turn on for debugging
Screenshot.prototype.takeRawScreenshot = function (options) {
 
    return when(this._driver._requestJSON('GET', '/screenshot', undefined, options), function (base64Data) {
        var buffer = new Buffer(base64Data, 'base64');
        //var browserName = this.getDriver().deviceName() != '' ? this.getDriver().deviceName() : this.getDriver().browserName();
        //Screenshot.prototype.id++;
        //fs.writeFileSync(__dirname + '/' + browserName + " " + this.getDriver().deviceOrientation() + " " + this.getDriver().browserVersion() + " " + this.getDriver().platform() + " " + Screenshot.prototype.id + '.png', buffer);
        return buffer;
    }.bind(this));
};
 
 
/**
 * Takes a screenshot pre-processed and converted into an image
 *
 * @method takeProcessedScreenshot
 * @param {Object} [options]
 * @return {PNGImage}
 */
Screenshot.prototype.takeProcessedScreenshot = function (options) {
 
	return when(this.takeRawScreenshot(options), function (buffer) {
		return this._screenshotPrePrecessing(PNGImage.loadImageSync(buffer));
	}.bind(this));
};
 
 
/**
 * Gets the max. allowed resolution for one screenshot. If the document exceeds this resolution, then
 * the screenshot will be stitched together from multiple smaller screenshots called sections.
 *
 * @method getMaxImageResolution
 * @return {int}
 */
Screenshot.prototype.getMaxImageResolution = function () {
    return this.getDriver().utils().resolve(this.getDriver().getValue('maxImageResolution'));
};
 
 
/**
 * Takes a screenshot of the whole document
 *
 * @method documentScreenshot
 * @param {object} [options]
 * @param {int} [options.horizontalPadding=0] Padding of the document for adjustment
 * @param {function} [options.eachFn] Will execute method on client before each screenshot is taken. First parameter is index of screenshot.
 * @param {function} [options.completeFn] Will execute method on client after all screenshots are taken.
 * @param {ActiveWindow|Element} [options.context] Context of screenshot
 * @param {object[]|Element[]|string[]} [options.blockOuts] List of areas/elements that should be blocked-out
 * @param {object} [options.blockOutColor=black] Color to be used for blocking-out areas {red, green, blue, alpha}
 * @param {int} [options.wait=100] Wait in ms before each screenshot
 * @return {Buffer}
 */
Screenshot.prototype.documentScreenshot = function (options) {
 
    return this._takeScreenshot(function (initData) {
        return {
            x: 0,
            y: 0,
            width: initData.document.width,
            height: initData.document.height
        };
    }, options);
};
 
/**
 * Takes a screenshot of the current view-port
 *
 * @method viewPortScreenshot
 * @param {object} [options]
 * @param {int} [options.horizontalPadding=0] Padding of the document for adjustment
 * @param {function} [options.eachFn] Will execute method on client before each screenshot is taken. First parameter is index of screenshot.
 * @param {function} [options.completeFn] Will execute method on client after all screenshots are taken.
 * @param {ActiveWindow|Element} [options.context] Context of screenshot
 * @param {object[]|Element[]|string[]} [options.blockOuts] List of areas/elements that should be blocked-out
 * @param {object} [options.blockOutColor=black] Color to be used for blocking-out areas {red, green, blue, alpha}
 * @param {int} [options.wait=100] Wait in ms before each screenshot
 * @return {Buffer}
 */
Screenshot.prototype.viewPortScreenshot = function (options) {
 
    return this._takeScreenshot(function (initData) {
        return {
            x: initData.viewPort.x,
            y: initData.viewPort.y,
            width: initData.viewPort.width,
            height: initData.viewPort.height
        };
    }, options);
};
 
/**
 * Takes a screenshot of a specific area
 *
 * @method areaScreenshot
 * @param {int} [x=0] X-coordinate for area
 * @param {int} [y=0] Y-coordinate for area
 * @param {int} [width=document.width-x] Width of area to be captured
 * @param {int} [height=document.height-y] Height of area to be captured
 * @param {object} [options]
 * @param {int} [options.horizontalPadding=0] Padding of the document for adjustment
 * @param {function} [options.eachFn] Will execute method on client before each screenshot is taken. First parameter is index of screenshot.
 * @param {function} [options.completeFn] Will execute method on client after all screenshots are taken.
 * @param {ActiveWindow|Element} [options.context] Context of screenshot
 * @param {object[]|Element[]|string[]} [options.blockOuts] List of areas/elements that should be blocked-out
 * @param {object} [options.blockOutColor=black] Color to be used for blocking-out areas {red, green, blue, alpha}
 * @param {int} [options.wait=100] Wait in ms before each screenshot
 * @return {Buffer}
 */
Screenshot.prototype.areaScreenshot = function (x, y, width, height, options) {
 
    return this._takeScreenshot(function (initData) {
 
        var localX = x || 0,
            localY = y || 0,
            localWidth = width || initData.document.width - localX,
            localHeight = height || initData.document.height - localY;
 
        return {
            x: localX,
            y: localY,
            width: localWidth,
            height: localHeight
        };
    }, options);
};
 
/**
 * Determines the areas that should be blocked-out
 *
 * @method _determineBlockOuts
 * @param {object} area Area that will be captured
 * @param {object} context Context of the capture (ActiveWindow, Element)
 * @param {object[]|Element[]|string[]} blockOuts List of block-out areas
 * @param {object} [blockOutColor=black] Color to be used for blocking-out areas {red, green, blue, alpha}
 * @return {object[]} List of rectangular areas {x, y, width, height}
 * @private
 */
Screenshot.prototype._determineBlockOuts = function (area, context, blockOuts, blockOutColor) {
 
	var defaultColor = blockOutColor || { red: 0, green: 0, blue: 0 };
 
	return when(this.getDriver().utils().map(blockOuts, function (blockOutEl) {
 
		if (typeof blockOutEl === 'string') {
 
			return when(context.getElements(blockOutEl), function (elements) {
 
				return elements.map(function (el) {
					return el.getFrame();
				});
			});
 
		} else if (typeof blockOutEl === 'object') {
 
			if (blockOutEl instanceof require('../element')) {
				return blockOutEl.getFrame();
			} else {
				return this.getDriver().utils().resolve(blockOutEl);
			}
 
		} else {
			throw new Error('Unknown block-out type: ' + typeof blockOutEl);
		}
 
	}.bind(this)), function (blockOuts) {
 
		var flattenList = [], reducedList;
 
		// Make sure that we have a flat list
		blockOuts.forEach(function (blockOut) {
			if ((typeof blockOut === 'object') && (blockOut.length !== undefined)) { // Array
				blockOut.forEach(function (item) {
					flattenList.push(item);
				});
			} else {
				flattenList.push(blockOut);
			}
		});
 
		// Validate, reduce, and convert coordinates of each block-out area
		flattenList = flattenList.map(function (blockOut) {
 
			this._validateRectItem(blockOut);
			this._reduceRectItemToArea(blockOut, area);
 
			// Convert coordinates from root to captured area
			blockOut.x -= area.x;
			blockOut.y -= area.y;
 
			// Use default block-out color if none specified
			blockOut.color = blockOut.color || defaultColor;
 
			return blockOut;
 
		}.bind(this));
 
		// Remove block-outs that doesn't need to be done
		reducedList = flattenList.filter(function (blockOut) {
			return ((blockOut.width !== 0) && (blockOut.height !== 0));
		});
 
		return reducedList;
 
	}.bind(this));
};
 
/**
 * Reduces a frame-item to a specified area
 *
 * @method _reduceRectItemToArea
 * @param {object} item Item that should be reduced
 * @param {object} area Area to reduce it to
 * @private
 */
Screenshot.prototype._reduceRectItemToArea = function (item, area) {
 
	if (item.x < area.x) {
		item.width -= Math.abs(item.x - area.x);
		item.x = area.x;
	}
	if (item.x > area.x + area.width) {
		item.x = area.x + area.width;
		item.width = 0;
	}
 
	if (item.y < area.y) {
		item.height -= Math.abs(item.y - area.y);
		item.y = area.y;
	}
	if (item.y > area.y + area.height) {
		item.y = area.y + area.height;
		item.height = 0;
	}
 
	if (item.width < 0) {
		item.width = 0;
	}
	if (item.width > area.width) {
		item.width = area.width;
	}
 
	if (item.height < 0) {
		item.height = 0;
	}
	if (item.height > area.height) {
		item.height = area.height;
	}
};
 
/**
 * Calidates if a rectangular item has all the required properties
 *
 * @method _validateRectItem
 * @param {object} item
 * @private
 */
Screenshot.prototype._validateRectItem = function (item) {
	['x', 'y', 'width', 'height'].forEach(function (property) {
		if (item[property] === undefined) {
			throw new Error('The "' + property + '" property needs to be defined for item: ' + JSON.stringify(item));
		}
		if (typeof item[property] !== 'number') {
			throw new Error('The "' + property + '" property needs to be numeric: ' + JSON.stringify(item));
		}
	});
};
 
 
/**
 * Takes a screenshot of an area that will be specified through the return-value of the callback
 *
 * @method _takeScreenshot
 * @param {function} fn Function to be called to get area to be captured. Supplies the data retrieved from the client as the first parameters.
 * @param {object} [options]
 * @param {int} [options.horizontalPadding=0] Padding of the document for adjustment
 * @param {function} [options.eachFn] Will execute method on client before each screenshot is taken. First parameter is index of screenshot.
 * @param {function} [options.completeFn] Will execute method on client after all screenshots are taken.
 * @param {object} [options.padding] Padding for screenshots
 * @param {object} [options.padding.viewPort] Padding for view-port screenshots, ignoring areas of the view-port screenshot
 * @param {int} [options.padding.viewPort.top=0] Padding of view-port screenshot from the top
 * @param {int} [options.padding.viewPort.bottom=0] Padding of view-port screenshot from the bottom
 * @param {int} [options.padding.viewPort.left=0] Padding of view-port screenshot from the left
 * @param {int} [options.padding.viewPort.right=0] Padding of view-port screenshot from the right
 * @param {object} [options.padding.screenshot] Padding for screenshot, ignoring areas of the screenshot
 * @param {int} [options.padding.screenshot.top=0] Padding of screenshot from the top
 * @param {int} [options.padding.screenshot.bottom=0] Padding of screenshot from the bottom
 * @param {int} [options.padding.screenshot.left=0] Padding of screenshot from the left
 * @param {int} [options.padding.screenshot.right=0] Padding of screenshot from the right
 * @param {object} [options.padding.document] Padding for document screenshots, ignoring areas of the document screenshot
 * @param {int} [options.padding.document.top=0] Padding of document screenshot from the top
 * @param {int} [options.padding.document.bottom=0] Padding of document screenshot from the bottom
 * @param {int} [options.padding.document.left=0] Padding of document screenshot from the left
 * @param {int} [options.padding.document.right=0] Padding of document screenshot from the right
 * @param {ActiveWindow|Element} [options.context] Context of screenshot
 * @param {object[]|Element[]|string[]} [options.blockOuts] List of areas/elements that should be blocked-out
 * @param {object} [options.blockOutColor=black] Color to be used for blocking-out areas {red, green, blue, alpha}
 * @param {int} [options.wait=100] Wait in ms before each screenshot
 * @return {Buffer}
 * @private
 */
Screenshot.prototype._takeScreenshot = function (fn, options) {
 
	var DevicePixelRatio = require('./devicePixelRatio'),
		devicePixelRatio = new DevicePixelRatio(this.getDriver()),
 
		Stitching = require('./stitching'),
		stitching = new Stitching(this.getDriver());
 
	return when(devicePixelRatio.getDevicePixelRatio(options), function (devicePixelRatio) {
 
        return when(stitching.doesNeedStitching(options), function (needsStitching) {
 
            return when(this.getMaxImageResolution(), function (maxImageResolution) {
 
                return when(this._execute(screenshotScripts.init, [needsStitching]), function (initData) {
                    initData = JSON.parse(initData);
 
                    var area, sections, padding;
 
                    // Make sure to account for the device-pixel-ratio
                    maxImageResolution *= (1 / devicePixelRatio);
 
                    // Make sure the document is not too wide; can fix this by increasing maxImageResolution
                    // We might need to capture only smaller areas, but we need to account also for correct behaving
                    // browsers like firefox which will always return the whole document.
                    if (initData.document.width > maxImageResolution) {
                        throw new Error('The maxImageResolution needs to be greater or equal to one time the document width.');
                    }
 
                    // Set defaults for padding (if none given) and validate
                    padding = this._browserPadding((options && options.padding) || {});
 
                    area = fn(initData);
 
                    // Add requested document padding
                    area.x = area.x + padding.document.left;
                    area.y = area.y + padding.document.top;
                    area.width = area.width - padding.document.left - padding.document.right;
                    area.height = area.height - padding.document.top - padding.document.bottom;
 
                    this._validateArea(area, padding, initData);
 
                    // Determine sections and view-ports to capture
                    sections = this._gatherSections(area, padding, initData, maxImageResolution, needsStitching);
 
                    return when(this._determineBlockOuts(area, options.context || this, options.blockOuts || [], options.blockOutColor), function (blockOuts) {
						return when(this._takeSectionScreenshots(sections, initData, options), function () {
 
							// Stitch images together
							var image;
 
							if (needsStitching) {
								image = this._stitchImages(area, sections, devicePixelRatio);
							} else {
								image = sections[0].viewPorts[0].image;
								image.clip(area.x, area.y, area.width, area.height);
							}
 
							return when(this._execute(screenshotScripts.revert, [initData]), function () {
 
								// Apply block-outs (if available)
								this._applyBlockOuts(image, blockOuts, devicePixelRatio);
 
								return image.toBlobSync();
 
							}.bind(this));
						}.bind(this));
                    }.bind(this));
                }.bind(this));
            }.bind(this));
        }.bind(this));
    }.bind(this));
};
 
/**
 * Applies all areas selected to be blocked-out
 *
 * @param {PNGImage} image Image to be blocked-out
 * @param {object[]} blockOuts List of block-out areas
 * @param {number} devicePixelRatio Device pixel ratio for browser
 * @private
 */
Screenshot.prototype._applyBlockOuts = function (image, blockOuts, devicePixelRatio) {
	blockOuts.forEach(function (blockOut) {
		image.fillRect(
			Math.floor(blockOut.x * devicePixelRatio),
			Math.floor(blockOut.y * devicePixelRatio),
			Math.floor(blockOut.width * devicePixelRatio),
			Math.floor(blockOut.height * devicePixelRatio),
			blockOut.color
		);
	});
};
 
/**
 * Determines if the screenshots need additional padding
 *
 * Note:
 *   Overwrite this method if you want to implement standard browser padding.
 *
 * @method _browserPadding
 * @param {object} padding
 * @private
 */
Screenshot.prototype._browserPadding = function (padding) {
 
    var paddingViewPortTop = 0, paddingViewPortBottom = 0,
        paddingViewPortLeft = 0, paddingViewPortRight = 0,
        paddingScreenshotTop = 0, paddingScreenshotBottom = 0,
        paddingScreenshotLeft = 0, paddingScreenshotRight = 0,
        paddingDocumentTop = 0, paddingDocumentBottom = 0,
        paddingDocumentLeft = 0, paddingDocumentRight = 0,
 
        browserName = this.getDriver().browserName().toLowerCase(),
        browserVersion = this.getDriver().browserVersion(),
        deviceName = this.getDriver().deviceName().toLowerCase(),
        deviceOrientation = this.getDriver().deviceOrientation().toLowerCase();
 
    // Exceptions
    switch (browserName) {
 
        case 'iphone':
			paddingScreenshotTop = 65; // Browser address bar needs to be trimmed
			paddingScreenshotBottom = 2; // Browser returns view-port that is one pixel smaller than reported
            break;
 
        case 'internet explorer':
            // We expect for future versions to behave the same (black border on the right)
            if (browserVersion >= 10) {
                paddingDocumentRight = 4;
            }
            break;
    }
 
    padding.viewPort = padding.viewPort || {};
    padding.viewPort.top = padding.viewPort.top || paddingViewPortTop;
    padding.viewPort.bottom = padding.viewPort.bottom || paddingViewPortBottom;
    padding.viewPort.left = padding.viewPort.left || paddingViewPortLeft;
    padding.viewPort.right = padding.viewPort.right || paddingViewPortRight;
 
    padding.screenshot = padding.screenshot || {};
    padding.screenshot.top = padding.screenshot.top || paddingScreenshotTop;
    padding.screenshot.bottom = padding.screenshot.bottom || paddingScreenshotBottom;
    padding.screenshot.left = padding.screenshot.left || paddingScreenshotLeft;
    padding.screenshot.right = padding.screenshot.right || paddingScreenshotRight;
 
    padding.document = padding.document || {};
    padding.document.top = padding.document.top || paddingDocumentTop;
    padding.document.bottom = padding.document.bottom || paddingDocumentBottom;
    padding.document.left = padding.document.left || paddingDocumentLeft;
    padding.document.right = padding.document.right || paddingDocumentRight;
 
    return padding;
};
 
/**
 * Validates and corrects area that was given to capture
 *
 * @method _validateArea
 * @param {object} area
 * @param {object} padding Padding for screenshots
 * @param {object} padding.viewPort Padding for view-port screenshots, ignoring areas of the view-port screenshot
 * @param {int} padding.viewPort.top Padding of view-port screenshot from the top
 * @param {int} padding.viewPort.bottom Padding of view-port screenshot from the bottom
 * @param {int} padding.viewPort.left Padding of view-port screenshot from the left
 * @param {int} padding.viewPort.right Padding of view-port screenshot from the right
 * @param {object} padding.screenshot Padding for screenshots, ignoring areas of the screenshot
 * @param {int} padding.screenshot.top Padding of screenshot from the top
 * @param {int} padding.screenshot.bottom Padding of screenshot from the bottom
 * @param {int} padding.screenshot.left Padding of screenshot from the left
 * @param {int} padding.screenshot.right Padding of screenshot from the right
 * @param {object} padding.document Padding for document screenshots, ignoring areas of the document screenshot
 * @param {int} padding.document.top Padding of document screenshot from the top
 * @param {int} padding.document.bottom Padding of document screenshot from the bottom
 * @param {int} padding.document.left Padding of document screenshot from the left
 * @param {int} padding.document.right Padding of document screenshot from the right
 * @param {object} initData
 * @private
 */
Screenshot.prototype._validateArea = function (area, padding, initData) {
 
    if (area.width < 0) {
        throw new Error('Width of area to capture cannot be negative.');
    }
    if (area.height < 0) {
        throw new Error('Height of area to capture cannot be negative.');
    }
 
    if (area.x < 0) {
        area.x = 0;
    }
    if (area.x >= initData.document.width) {
        area.x = initData.document.width - 1;
    }
    if (area.y < 0) {
        area.y = 0;
    }
    if (area.y >= initData.document.height) {
        area.y = initData.document.height - 1;
    }
 
    if (area.x + area.width > initData.document.width) {
        area.width = initData.document.width - area.x;
    }
    if (area.y + area.height > initData.document.height) {
        area.height = initData.document.height - area.y;
    }
};
 
 
/**
 * Calculates the sections that need to be captured on their own to be able to capture the whole document.
 *
 * This method depends on the value given to "maxImageResolution".
 *
 * @method _gatherSections
 * @param {object} area Area to capture
 * @param {object} padding Padding for screenshots
 * @param {object} padding.viewPort Padding for view-port screenshots, ignoring areas of the view-port screenshot
 * @param {int} padding.viewPort.top Padding of view-port screenshot from the top
 * @param {int} padding.viewPort.bottom Padding of view-port screenshot from the bottom
 * @param {int} padding.viewPort.left Padding of view-port screenshot from the left
 * @param {int} padding.viewPort.right Padding of view-port screenshot from the right
 * @param {object} padding.screenshot Padding for screenshots, ignoring areas of the screenshot
 * @param {int} padding.screenshot.top Padding of screenshot from the top
 * @param {int} padding.screenshot.bottom Padding of screenshot from the bottom
 * @param {int} padding.screenshot.left Padding of screenshot from the left
 * @param {int} padding.screenshot.right Padding of screenshot from the right
 * @param {object} initData Data that was initially gathered from the client
 * @param {int} maxImageResolution Max. number of pixels allowed for a screenshot
 * @param {boolean} needsStitching Does the browser need stitching?
 * @return {object[]} List of sections
 * @private
 */
Screenshot.prototype._gatherSections = function (area, padding, initData, maxImageResolution, needsStitching) {
 
    var sections = [], section,
        sectionCount, sectionHeight,
        i, index = 0,
        yOffset,
        documentWidth,
        viewPortHeight = initData.viewPort.height - padding.viewPort.top - padding.viewPort.bottom - padding.screenshot.bottom;
 
    documentWidth = initData.document.width - area.x;
 
    // Calculate max. section height considering the max resolution to capture
    sectionHeight = Math.floor(maxImageResolution / documentWidth);
 
    if (needsStitching) {
        // When the screenshots are stitched together,
        // we try here to reduce the amount of screenshots per section.
        // We don't want to be wasteful so we make sure that the section border is on
        // even viewPort height; we want to avoid taking a viewPort screenshot and only use
        // the upper part of it, discarding it and requesting it for the next section.
        sectionHeight = Math.floor(sectionHeight / viewPortHeight) * viewPortHeight;
    }
 
    sectionCount = Math.ceil(area.height / sectionHeight);
 
    for (i = 0; i < sectionCount; i++) {
 
        yOffset = (i * sectionHeight);
        section = {
            shift: (sectionCount !== 1),
			x: area.x,
			y: area.y + yOffset, // Vertical offset for sections
			dstX: 0,
			dstY: yOffset, // Vertical offset for sections
            width: area.width,
            height: Math.min(sectionHeight, area.height - yOffset),
            viewPorts: undefined
        };
 
        if (needsStitching) {
            section.viewPorts = this._gatherViewPortSections(section, padding, initData, index);
            index += section.viewPorts.length;
        } else {
            section.viewPorts = [
                {
                    srcX: padding.viewPort.left + padding.screenshot.left,
                    srcY: padding.viewPort.top + padding.screenshot.top,
                    x: 0,
                    y: 0,
                    width: section.width,
                    height: section.height,
                    image: undefined,
                    index: index
                }
            ];
            index++;
        }
 
        sections.push(section);
    }
 
    return sections;
};
 
/**
 * Calculates the sections that need to be captured when the browser is not able to return pixels outside of the view-port.
 *
 * @method _gatherViewPortSections
 * @param {object} section Section that should be captured
 * @param {object} padding Padding for screenshots
 * @param {object} padding.viewPort Padding for view-port screenshots, ignoring areas of the view-port screenshot
 * @param {int} padding.viewPort.top Padding of view-port screenshot from the top
 * @param {int} padding.viewPort.bottom Padding of view-port screenshot from the bottom
 * @param {int} padding.viewPort.left Padding of view-port screenshot from the left
 * @param {int} padding.viewPort.right Padding of view-port screenshot from the right
 * @param {object} padding.screenshot Padding for screenshots, ignoring areas of the screenshot
 * @param {int} padding.screenshot.top Padding of screenshot from the top
 * @param {int} padding.screenshot.bottom Padding of screenshot from the bottom
 * @param {int} padding.screenshot.left Padding of screenshot from the left
 * @param {int} padding.screenshot.right Padding of screenshot from the right
 * @param {object} initData Data that was initially gathered from the client
 * @param {int} index Index of first view-port
 * @return {object[]}
 * @private
 */
Screenshot.prototype._gatherViewPortSections = function (section, padding, initData, index) {
 
    var viewPorts = [],
 
        sectionWidth = section.width,
        sectionHeight = section.height,
 
        viewPortWidth = initData.viewPort.width - padding.viewPort.left - padding.viewPort.right - padding.screenshot.right,
        viewPortHeight = initData.viewPort.height - padding.viewPort.top - padding.viewPort.bottom - padding.screenshot.bottom,
 
        columns = Math.ceil(sectionWidth / viewPortWidth),
        rows = Math.ceil(sectionHeight / viewPortHeight),
 
        offsetX, offsetY,
        x, y;
 
    for (y = 0; y < rows; y++) {
        for (x = 0; x < columns; x++) {
 
            offsetX = x * viewPortWidth;
            offsetY = y * viewPortHeight;
 
            viewPorts.push({
                srcX: padding.viewPort.left + padding.screenshot.left,
                srcY: padding.viewPort.top + padding.screenshot.top,
                x: offsetX,
                y: offsetY,
                width: Math.min(viewPortWidth, sectionWidth - offsetX),
                height: Math.min(viewPortHeight, sectionHeight - offsetY),
                image: undefined,
                index: index
            });
 
            index++;
        }
    }
 
    return viewPorts;
};
 
 
/**
 * Takes all the screenshots defined in section
 *
 * @method _takeSectionScreenshots
 * @param {object[]} sections List of sections to capture
 * @param {object} initData Data that was initially gathered from the client
 * @param {object} [options]
 * @param {function} [options.eachFn] Will execute method on client before each screenshot is taken. First parameter is index of screenshot.
 * @param {function} [options.completeFn] Will execute method on client after all screenshots are taken.
 * @param {int} [options.wait=100] Wait in ms before each screenshot
 * @private
 */
Screenshot.prototype._takeSectionScreenshots = function (sections, initData, options) {
 
    return when(this.getDriver().utils().map(sections, function (section) {
        return this._takeViewPortScreenshots(section, section.viewPorts, initData, options);
 
    }.bind(this)), function () {
        return this._execute(options.completeFn);
    }.bind(this));
};
 
/**
 * Takes all the screenshots defined in the view-ports
 *
 * @method _takeViewPortScreenshots
 * @param {object} section Section to capture
 * @param {object[]} viewPorts List of view-ports to capture
 * @param {object} initData Data that was initially gathered from the client
 * @param {object} [options]
 * @param {function} [options.eachFn] Will execute method on client before each screenshot is taken. First parameter is index of screenshot.
 * @param {function} [options.completeFn] Will execute method on client after all screenshots are taken.
 * @param {int} [options.wait=100] Wait in ms before each screenshot
 * @private
 */
Screenshot.prototype._takeViewPortScreenshots = function (section, viewPorts, initData, options) {
 
    return this.getDriver().utils().map(viewPorts, function (viewPort) {
        return this._takeViewPortScreenshot(section, viewPort, initData, options);
    }.bind(this));
};
 
/**
 * Takes a screenshot of the given view-port in relation to the section
 *
 * @method _takeViewPortScreenshot
 * @param {object} section Section to capture
 * @param {object} viewPort View-ports to capture
 * @param {object} initData Data that was initially gathered from the client
 * @param {object} [options]
 * @param {function} [options.eachFn] Will execute method on client before each screenshot is taken. First parameter is index of screenshot.
 * @param {function} [options.completeFn] Will execute method on client after all screenshots are taken.
 * @param {int} [options.wait=100] Wait in ms before each screenshot
 * @private
 */
Screenshot.prototype._takeViewPortScreenshot = function (section, viewPort, initData, options) {
 
    var offsetX = section.x + viewPort.x,
        offsetY = section.y + viewPort.y,
        viewPortIndex = viewPort.index,
        sectionHeight = section.shift ? section.height : null,
		wait = (options && options.wait) || 100;
 
    return when(this._execute(screenshotScripts.documentOffset, [offsetX, offsetY, sectionHeight, initData]), function () {
 
        // Just wait a little bit. Some browsers need time for that.
        return when(this.getDriver().utils().sleep(wait), function () {
 
            return when(this._execute(options.eachFn, [viewPortIndex]), function () {
 
                // Take screenshot with current section and view-port
                return when(this.takeProcessedScreenshot(), function (image) {
 
                    viewPort.image = image;
 
                }.bind(this));
            }.bind(this));
        }.bind(this));
    }.bind(this));
};
 
/**
 * Processes the screenshots before it is used for stitching
 *
 * Note:
 *  Some browser need some pre-processing like rotating the screenshot.
 *
 * @method _screenshotPrePrecessing
 * @param {PNGImage} image Image to process
 * @return {PNGImage}
 * @private
 */
Screenshot.prototype._screenshotPrePrecessing = function (image) {
 
    var browserName = this.getDriver().browserName().toLowerCase(),
        browserVersion = this.getDriver().browserVersion(),
        deviceName = this.getDriver().deviceName().toLowerCase(),
        deviceOrientation = this.getDriver().deviceOrientation().toLowerCase();
 
    // Exceptions
    switch (browserName) {
 
        case 'iphone':
            if (deviceOrientation === 'landscape') {
                image = image.rotateCCW();
            }
            break;
    }
 
    return image;
};
 
 
/**
 * Stitches all the sections and view-ports together to one final image
 *
 * @method _stitchImages
 * @param {object} area Area that should have been captured on the page
 * @param {object[]} sections List of sections that were captured
 * @param {number} devicePixelRatio Device pixel ratio for browser
 * @return {PNGImage}
 * @private
 */
Screenshot.prototype._stitchImages = function (area, sections, devicePixelRatio) {
 
    var image;
 
    image = PNGImage.createImage(
        Math.floor(area.width * devicePixelRatio),
        Math.floor(area.height * devicePixelRatio)
    );
 
    sections.forEach(function (section) {
 
        section.viewPorts.forEach(function (viewPort) {
 
            var offsetX = Math.floor((section.dstX + viewPort.x) * devicePixelRatio),
                offsetY = Math.floor((section.dstY + viewPort.y) * devicePixelRatio),
                width = Math.floor(viewPort.width * devicePixelRatio),
                height = Math.floor(viewPort.height * devicePixelRatio);
 
            viewPort.image.getImage().bitblt(
                image.getImage(),
				Math.floor(viewPort.srcX * devicePixelRatio),
				Math.floor(viewPort.srcY * devicePixelRatio),
                Math.min(width - Math.floor(section.dstX * devicePixelRatio), viewPort.image.getWidth()),
                Math.min(height - Math.floor(section.dstY * devicePixelRatio), viewPort.image.getHeight()),
                offsetX,
                offsetY
            );
 
            // Free-up some memory
            viewPort.image = null;
        });
    });
 
    // Force garbage-collection if available (can be turned on with option "--expose-gc" when running node)
    if (global.gc) {
        global.gc();
    }
 
    return image;
};
 
/**
 * Executes a script in the browser and returns the result.
 *
 * This is a convenience method for accessing the execute method.
 *
 * @method _execute
 * @param {String|Function} script
 * @param {Array} [args]
 * @return {*}
 * @private
 */
Screenshot.prototype._execute = function (script, args) {
    if (script) {
        return this.getDriver().browser().activeWindow().execute(script, args);
 
    } else { // Ignore script if there is nothing - might happen with screenshot requests
        return this.getDriver().utils().resolve(undefined);
    }
};
 
logMethods(Screenshot.prototype);