Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge a644d68 into 17f2cc5
Browse files Browse the repository at this point in the history
  • Loading branch information
scf2k committed Apr 24, 2015
2 parents 17f2cc5 + a644d68 commit cfb9c92
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/browser/client-scripts/gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function prepareScreenshotUnsafe(selectors, opts) {

var viewportHeight = window.innerHeight || document.documentElement.clientHeight,
documentHeight = document.documentElement.scrollHeight,
documentWidth = document.documentElement.scrollWidth,
coverage;

if (opts.coverage) {
Expand All @@ -50,6 +51,7 @@ function prepareScreenshotUnsafe(selectors, opts) {
ignoreAreas: findIgnoreAreas(opts.ignoreSelectors),
viewportHeight: Math.round(viewportHeight),
documentHeight: Math.round(documentHeight),
documentWidth: Math.round(documentWidth),
coverage: coverage,
canHaveCaret: isEditable(document.activeElement)
};
Expand Down
13 changes: 10 additions & 3 deletions lib/capture-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = inherit({
));
}

if (isOutsideOfImage(captureArea, imageSize)) {
if (isOutsideOfImage(prepareData, imageSize)) {
throw new StateError(
'Can not capture specified region of the page\n' +
'The size of a region is larger then image, captured by browser\n' +
Expand All @@ -105,8 +105,15 @@ module.exports = inherit({

});

function isOutsideOfImage(area, imageSize) {
return area.top < 0 || area.left < 0 || area.left + area.width > imageSize.width;
function isOutsideOfImage(data, imageSize) {
var area = data.captureArea,
rightBorder = 0;

if (imageSize.width < data.documentWidth) {
rightBorder = data.viewportOffset.left;
}

return area.top < 0 || area.left < 0 || area.left + area.width - rightBorder > imageSize.width;
}

function getToImageCoordsFunction(image, prepareData) {
Expand Down
68 changes: 67 additions & 1 deletion test/capture-session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('capture session', function() {
});
});

it('should fail when crop area is not located within document area', function() {
it('should fail when crop area is located outside of the document area by Y axis', function() {
this.state.name = 'state';
this.state.suite = {name: 'suite'};
this.browser.id = 'bro';
Expand Down Expand Up @@ -228,5 +228,71 @@ describe('capture session', function() {

return assert.isRejected(this.session.capture(this.state), StateError);
});

it('should fail when crop area is located outside of the document area by X axis', function() {
this.state.name = 'state';
this.state.suite = {name: 'suite'};
this.browser.id = 'bro';

this.browser.prepareScreenshot.returns({
documentHeight: 100,
documentWidth: 150,
viewportOffset: {
top: 0,
left: 50
},
captureArea: {
top: 0,
left: 50,
width: 101,
height: 100
},
ignoreAreas: []
});
var image = {
getSize: sinon.stub().returns({
width: 100,
height: 200
}),

crop: sinon.stub().returns(q())
};
this.browser.captureFullscreenImage.returns(q(image));

return assert.isRejected(this.session.capture(this.state), StateError);
});

it('should not fail and take into account scrolling position while validating crop area by X axis', function() {
this.state.name = 'state';
this.state.suite = {name: 'suite'};
this.browser.id = 'bro';

this.browser.prepareScreenshot.returns({
documentHeight: 100,
documentWidth: 150,
viewportOffset: {
top: 0,
left: 50
},
captureArea: {
top: 0,
left: 50,
width: 100,
height: 100
},
ignoreAreas: []
});
var image = {
getSize: sinon.stub().returns({
width: 100,
height: 200
}),

crop: sinon.stub().returns(q())
};
this.browser.captureFullscreenImage.returns(q(image));

return assert.isFulfilled(this.session.capture(this.state));
});
});
});

0 comments on commit cfb9c92

Please sign in to comment.