1# Copyright 2016 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file 4 5import logging 6import time 7 8from telemetry.core import exceptions 9from autotest_lib.client.bin import utils 10from autotest_lib.client.common_lib import error 11from autotest_lib.client.common_lib.cros import chrome 12 13DEFAULT_TIMEOUT = 30 14SHORT_TIMEOUT = 5 15 16 17def get_webview_contexts(browser, ext_id): 18 """Get all webview contexts for an extension. 19 20 @param browser: Telemetry browser object. 21 @param ext_id: Extension id of the kiosk app. 22 @return A list of webview contexts. 23 """ 24 ext_contexts = wait_for_kiosk_ext(browser, ext_id) 25 26 for context in ext_contexts: 27 context.WaitForDocumentReadyStateToBeInteractiveOrBetter() 28 tagName = context.EvaluateJavaScript( 29 "document.querySelector('webview') ? 'WEBVIEW' : 'NOWEBVIEW'") 30 if tagName == "WEBVIEW": 31 def _webview_context(): 32 try: 33 return context.GetWebviewContexts() 34 except (chrome.Error): 35 logging.exception( 36 'An error occured while getting the webview contexts.') 37 return None 38 39 return utils.poll_for_condition( 40 _webview_context, 41 exception=error.TestFail('Webview not available.'), 42 timeout=DEFAULT_TIMEOUT, 43 sleep_interval=1) 44 45 46# TODO(dtosic): deprecate this method in favor of 'get_webview_contexts()' 47def get_webview_context(browser, ext_id): 48 """Get context for CFM webview. 49 50 @param browser: Telemetry browser object. 51 @param ext_id: Extension id of the kiosk app. 52 @return webview context. 53 """ 54 ext_contexts = wait_for_kiosk_ext(browser, ext_id) 55 56 for context in ext_contexts: 57 context.WaitForDocumentReadyStateToBeInteractiveOrBetter() 58 tagName = context.EvaluateJavaScript( 59 "document.querySelector('webview') ? 'WEBVIEW' : 'NOWEBVIEW'") 60 if tagName == "WEBVIEW": 61 def _webview_context(): 62 try: 63 wb_contexts = context.GetWebviewContexts() 64 if len(wb_contexts) == 1: 65 return wb_contexts[0] 66 if len(wb_contexts) == 2: 67 return wb_contexts[1] 68 69 except (KeyError, chrome.Error): 70 pass 71 return None 72 return utils.poll_for_condition( 73 _webview_context, 74 exception=error.TestFail('Webview not available.'), 75 timeout=DEFAULT_TIMEOUT, 76 sleep_interval=1) 77 78 79def wait_for_kiosk_ext(browser, ext_id): 80 """Wait for kiosk extension launch. 81 82 @param browser: Telemetry browser object. 83 @param ext_id: Extension id of the kiosk app. 84 @return extension contexts. 85 """ 86 def _kiosk_ext_contexts(): 87 try: 88 ext_contexts = browser.extensions.GetByExtensionId(ext_id) 89 if len(ext_contexts) > 1: 90 return ext_contexts 91 except (KeyError, chrome.Error): 92 pass 93 return [] 94 return utils.poll_for_condition( 95 _kiosk_ext_contexts, 96 exception=error.TestFail('Kiosk app failed to launch'), 97 timeout=DEFAULT_TIMEOUT, 98 sleep_interval=1) 99 100 101def config_riseplayer(browser, ext_id, app_config_id): 102 """ 103 Configure Rise Player app with a specific display id. 104 105 Step through the configuration screen of the Rise Player app 106 which is launched within the browser and enter a display id 107 within the configuration frame to initiate media display. 108 109 @param browser: browser instance containing the Rise Player kiosk app. 110 @param ext_id: extension id of the Rise Player Kiosk App. 111 @param app_config_id: display id for the Rise Player app . 112 113 """ 114 if not app_config_id: 115 raise error.TestFail( 116 'Error in configuring Rise Player: app_config_id is None') 117 config_js = """ 118 var frameId = 'btn btn-primary display-register-button' 119 document.getElementsByClassName(frameId)[0].click(); 120 $( "input:text" ).val("%s"); 121 document.getElementsByClassName(frameId)[4].click(); 122 """ % app_config_id 123 124 kiosk_webview_context = get_webview_context( 125 browser, ext_id) 126 # Wait for the configuration frame to load. 127 time.sleep(SHORT_TIMEOUT) 128 kiosk_webview_context.ExecuteJavaScript(config_js) 129 # TODO (krishnargv): Find a way to verify that content is playing 130 # within the RisePlayer app. 131 verify_app_config_id = """ 132 /rvashow.*.display&id=%s.*/.test(location.href) 133 """ % app_config_id 134 #Verify that Risepplayer successfully validates the display id. 135 try: 136 kiosk_webview_context.WaitForJavaScriptCondition( 137 verify_app_config_id, 138 timeout=DEFAULT_TIMEOUT) 139 except exceptions.TimeoutException: 140 raise error.TestFail('Error in configuring Rise Player with id: %s' 141 % app_config_id) 142