{"size": 1748, "ext": "js", "max_stars_repo_path": "node_modules/@applitools/eyes-sdk-core/lib/capture/MobileApplicationScreenshotImageProvider.js", "max_stars_repo_name": "Kiera569/show-codes", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node_modules/@applitools/eyes-sdk-core/lib/capture/MobileApplicationScreenshotImageProvider.js", "max_issues_repo_name": "Kiera569/show-codes", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "node_modules/@applitools/eyes-sdk-core/lib/capture/MobileApplicationScreenshotImageProvider.js", "max_forks_repo_name": "Kiera569/show-codes", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.1935483871, "max_line_length": 80, "alphanum_fraction": 0.7053775744}
'use strict'

const ImageProvider = require('./ImageProvider')
const MutableImage = require('../images/MutableImage')

/**
 * An image provider based on WebDriver's interface.
 */
class MobileApplicationScreenshotImageProvider extends ImageProvider {
  /**
   * @param {Logger} logger A Logger instance.
   * @param {ImageRotation} rotation
   * @param {EyesWrappedDriver} driver
   */
  constructor(logger, driver, rotation) {
    super()

    this._logger = logger
    this._driver = driver
    this._rotation = rotation
  }

  set rotation(rotation) {
    this._rotation = rotation
  }

  /**
   * Uses Appium hook available through the JS executor
   * http://appium.io/docs/en/commands/mobile-command/
   * Supported on iOS (XCUITest) and Android (UIAutomator2 only)
   * @return {Promise<MutableImage>}
   */
  async takeScreenshot({skipNativeHook}) {
    // to preserve checkRegion support on native apps
    if (skipNativeHook) {
      return await this._driver.takeScreenshot()
    }
    let screenshot = await this._driver.execute('mobile: viewportScreenshot')
    // trimming line breaks since 3rd party grid providers can return them
    screenshot = screenshot.replace(/\r\n/g, '')
    return new MutableImage(screenshot)
  }

  /**
   * @override
   * @return {Promise<MutableImage>}
   */
  async getImage() {
    this._logger.verbose('Getting screenshot as base64...')
    const image = await this.takeScreenshot({
      skipNativeHook: process.env.APPLITOOLS_SKIP_MOBILE_NATIVE_SCREENSHOT_HOOK,
    })
    if (this._rotation) {
      await image.rotate(this._rotation)
    }
    process.env.APPLITOOLS_SKIP_MOBILE_NATIVE_SCREENSHOT_HOOK = undefined
    return image
  }
}

module.exports = MobileApplicationScreenshotImageProvider
