Skip to content

Commit 50db9ee

Browse files
committed
fix(screenshot): set "left" and "top" crop coords to zero when they are negative
1 parent 6c85066 commit 50db9ee

File tree

2 files changed

+31
-13
lines changed
  • lib/command-helpers/element-utils
  • test/lib/command-helpers/element-utils

2 files changed

+31
-13
lines changed

lib/command-helpers/element-utils/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ exports.calcWebViewCoords = async (browser, {bodyWidth, pixelRatio = 1} = {}) =>
6464
return {
6565
width: Math.ceil(bodyWidth * pixelRatio),
6666
height: Math.ceil((webViewSize.height - topToolbarHeight - bottomToolbarHeight) * pixelRatio),
67-
left: Math.floor((webViewSize.width - bodyWidth) / 2 * pixelRatio),
68-
top: Math.floor(topToolbarHeight * pixelRatio)
67+
left: Math.max(Math.floor((webViewSize.width - bodyWidth) / 2 * pixelRatio), 0),
68+
top: Math.max(Math.floor(topToolbarHeight * pixelRatio), 0)
6969
};
7070
};

test/lib/command-helpers/element-utils/index.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -228,32 +228,50 @@ describe('"element-utils" helper', () => {
228228
});
229229
});
230230

231-
describe('should correctly calc web view "left" coord', () => {
232-
it('with substract passed body width from web view width and take half of it', async () => {
233-
utils.getWebViewSize.withArgs(browser).returns({width: 20});
231+
describe('web view "left" coord', () => {
232+
describe('should correctly calc', () => {
233+
it('with substract passed body width from web view width and take half of it', async () => {
234+
utils.getWebViewSize.withArgs(browser).returns({width: 20});
234235

235-
const {left} = await utils.calcWebViewCoords(browser, {bodyWidth: 10});
236+
const {left} = await utils.calcWebViewCoords(browser, {bodyWidth: 10});
236237

237-
assert.equal(left, 5);
238+
assert.equal(left, 5);
239+
});
240+
241+
it('with multiply real web view width by passed pixel ratio', async () => {
242+
utils.getWebViewSize.withArgs(browser).returns({width: 20});
243+
244+
const {left} = await utils.calcWebViewCoords(browser, {bodyWidth: 10, pixelRatio: 2});
245+
246+
assert.equal(left, 10);
247+
});
238248
});
239249

240-
it('with multiply real web view width by passed pixel ratio', async () => {
241-
utils.getWebViewSize.withArgs(browser).returns({width: 20});
250+
it('should set to zero if calculated coord is negative', async () => {
251+
utils.getWebViewSize.withArgs(browser).returns({width: 10});
242252

243-
const {left} = await utils.calcWebViewCoords(browser, {bodyWidth: 10, pixelRatio: 2});
253+
const {left} = await utils.calcWebViewCoords(browser, {bodyWidth: 20});
244254

245-
assert.equal(left, 10);
255+
assert.equal(left, 0);
246256
});
247257
});
248258

249-
describe('should correctly calc web view "top" coord', () => {
250-
it('with multiply top toolbar height by passed pixel ratio', async () => {
259+
describe('web view "top" coord', () => {
260+
it('should correctly calc with multiply top toolbar height by passed pixel ratio', async () => {
251261
utils.getTopToolbarHeight.withArgs(browser).returns(2);
252262

253263
const {top} = await utils.calcWebViewCoords(browser, {pixelRatio: 2});
254264

255265
assert.equal(top, 4);
256266
});
267+
268+
it('should set to zero if calculated coord is negative', async () => {
269+
utils.getTopToolbarHeight.withArgs(browser).returns(-10);
270+
271+
const {top} = await utils.calcWebViewCoords(browser, {pixelRatio: 1});
272+
273+
assert.equal(top, 0);
274+
});
257275
});
258276

259277
it('should return "width", "height", "left" and "top" coords', async () => {

0 commit comments

Comments
 (0)