import { CHROME_APP_ID , DEFAULT_TIMEOUT , SAFARI_BUNDLE_ID } from './constants' ; import { assertIdDefined , Platform , UNSUPPORTED_PLATFORM_ERROR , } from './internal/utils' ; export enum APP_RUNNING_STATE { NOT_INSTALLED = 0 , NOT_RUNNING = 1 , BACKGROUND_OR_SUSPENDED = 2 , BACKGROUND = 3 , FOREGROUND = 4 } param { string } appId param { string } bundleId returns { APP_RUNNING_STATE } see http category export function queryAppState ( appId ? , bundleId ? ) { if ( browser . isIOS ) { assertIdDefined ( bundleId , Platform . IOS ) ; return browser . execute ( 'mobile: queryAppState' , { bundleId : bundleId } ) ; } else if ( browser . isAndroid ) { assertIdDefined ( appId , Platform . ANDROID ) ; return browser . queryAppState ( appId ) ; } throw new Error ( UNSUPPORTED_PLATFORM_ERROR ) ; } param { boolean } state param { boolean } wait param { string } appId param { string } bundleId param { number } timeout category export function isAppState ( state , wait = false , appId ? , bundleId ? , timeout = DEFAULT_TIMEOUT ) { if ( wait ) { return browser . waitUntil ( ( ) => { return queryAppState ( appId , bundleId ) === state ; } , { timeout : timeout , timeoutMsg : 'App is not in state: ' + state } ) ; } else { return queryAppState ( appId , bundleId ) === state ; } } param { boolean } state param { boolean } wait category export function isBrowserAppState ( state , wait = false ) { if ( browser . isIOS ) { return isAppState ( state , wait , undefined , SAFARI_BUNDLE_ID ) ; } else if ( browser . isAndroid ) { return isAppState ( state , wait , CHROME_APP_ID , undefined ) ; } throw new Error ( UNSUPPORTED_PLATFORM_ERROR ) ; } category export function openSafari ( ) { browser . waitUntil ( ( ) => { browser . execute ( 'mobile: launchApp' , { bundleId : SAFARI_BUNDLE_ID } ) ; return isBrowserAppState ( APP_RUNNING_STATE . FOREGROUND , false ) ; } ) ; } param { boolean } wait param { string } appId param { string } bundleId category export function openApp ( wait = false , appId ? , bundleId ? ) { browser . waitUntil ( ( ) => { if ( browser . isAndroid ) { browser . activateApp ( appId ) ; } else if ( browser . isIOS ) { browser . execute ( 'mobile: activateApp' , { bundleId : bundleId } ) ; } else { throw new Error ( UNSUPPORTED_PLATFORM_ERROR ) ; } return isAppState ( APP_RUNNING_STATE . FOREGROUND , wait , appId , bundleId ) ; } ) ; }