|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const {withExisting, withNativeCtx, withTestCtxMemo} = require('./decorators'); |
| 4 | +const {TOP_TOOLBAR_SIZE, BOTTOM_TOOLBAR_LOCATION, WEB_VIEW_SIZE, PIXEL_RATIO} = require('../test-context'); |
| 5 | +const {TOP_TOOLBAR, BOTTOM_TOOLBAR, WEB_VIEW} = require('../../native-locators'); |
| 6 | + |
| 7 | +exports.getTopToolbarHeight = async (browser) => { |
| 8 | + const action = {fn: browser.getElementSize, args: TOP_TOOLBAR, default: {width: 0, height: 0}}; |
| 9 | + const existingWrapper = {fn: withExisting, args: action}; |
| 10 | + const inNativeCtxWrapper = {fn: withNativeCtx, args: existingWrapper}; |
| 11 | + |
| 12 | + return (await withTestCtxMemo.call(browser, inNativeCtxWrapper, TOP_TOOLBAR_SIZE)).height; |
| 13 | +}; |
| 14 | + |
| 15 | +exports.getBottomToolbarY = async (browser) => { |
| 16 | + const action = {fn: browser.getLocation, args: BOTTOM_TOOLBAR, default: {x: 0, y: 0}}; |
| 17 | + const existingWrapper = {fn: withExisting, args: action}; |
| 18 | + const inNativeCtxWrapper = {fn: withNativeCtx, args: existingWrapper}; |
| 19 | + |
| 20 | + return (await withTestCtxMemo.call(browser, inNativeCtxWrapper, BOTTOM_TOOLBAR_LOCATION)).y; |
| 21 | +}; |
| 22 | + |
| 23 | +exports.getWebViewSize = async (browser) => { |
| 24 | + const action = {fn: browser.getElementSize, args: WEB_VIEW}; |
| 25 | + const inNativeCtxWrapper = {fn: withNativeCtx, args: action}; |
| 26 | + |
| 27 | + return await withTestCtxMemo.call(browser, inNativeCtxWrapper, WEB_VIEW_SIZE); |
| 28 | +}; |
| 29 | + |
| 30 | +exports.getElemCoords = async (browser, selector) => { |
| 31 | + const [{width, height}, {x, y}] = await Promise.all([browser.getElementSize(selector), browser.getLocation(selector)]); |
| 32 | + const topToolbarHeight = await exports.getTopToolbarHeight(browser); |
| 33 | + |
| 34 | + return {width, height, x, y: y + topToolbarHeight}; |
| 35 | +}; |
| 36 | + |
| 37 | +exports.getElemCenterLocation = async (browser, selector) => { |
| 38 | + const {width, height, x, y} = await exports.getElemCoords(browser, selector); |
| 39 | + |
| 40 | + return { |
| 41 | + x: Math.round(x + width / 2), |
| 42 | + y: Math.round(y + height / 2) |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +exports.getPixelRatio = async (browser) => { |
| 47 | + const action = {fn: async () => (await browser.execute(() => window.devicePixelRatio)).value}; |
| 48 | + |
| 49 | + return await withTestCtxMemo.call(browser, action, PIXEL_RATIO); |
| 50 | +}; |
| 51 | + |
| 52 | +exports.calcWebViewCoords = async (browser, {bodyWidth, pixelRatio = 1} = {}) => { |
| 53 | + const [topToolbarHeight, bottomToolbarY, webViewSize] = await Promise.all([ |
| 54 | + exports.getTopToolbarHeight(browser), |
| 55 | + exports.getBottomToolbarY(browser), |
| 56 | + exports.getWebViewSize(browser) |
| 57 | + ]); |
| 58 | + const bottomToolbarHeight = bottomToolbarY > 0 ? webViewSize.height - bottomToolbarY : 0; |
| 59 | + |
| 60 | + return { |
| 61 | + width: Math.ceil(bodyWidth * pixelRatio), |
| 62 | + height: Math.ceil((webViewSize.height - topToolbarHeight - bottomToolbarHeight) * pixelRatio), |
| 63 | + left: Math.floor((webViewSize.width - bodyWidth) / 2 * pixelRatio), |
| 64 | + top: Math.floor(topToolbarHeight * pixelRatio) |
| 65 | + }; |
| 66 | +}; |
0 commit comments