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 time 6 7from telemetry.core import exceptions 8from autotest_lib.client.bin import utils 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.common_lib.cros import chrome 11 12DEFAULT_TIMEOUT = 30 13SHORT_TIMEOUT = 5 14 15def get_webview_context(browser, ext_id): 16 """Get context for CFM webview. 17 18 @param broswer: Telemetry broswer object. 19 @param ext_id: Extension id of the kiosk app. 20 @return webview context. 21 """ 22 ext_contexts = wait_for_kiosk_ext(browser, ext_id) 23 24 for context in ext_contexts: 25 context.WaitForDocumentReadyStateToBeInteractiveOrBetter() 26 tagName = context.EvaluateJavaScript( 27 "document.querySelector('webview') ? 'WEBVIEW' : 'NOWEBVIEW'") 28 if tagName == "WEBVIEW": 29 def _webview_context(): 30 try: 31 wb_contexts = context.GetWebviewContexts() 32 if len(wb_contexts) == 1: 33 return wb_contexts[0] 34 if len(wb_contexts) == 2: 35 return wb_contexts[1] 36 37 except (KeyError, chrome.Error): 38 pass 39 return None 40 return utils.poll_for_condition( 41 _webview_context, 42 exception=error.TestFail('Webview not available.'), 43 timeout=DEFAULT_TIMEOUT, 44 sleep_interval=1) 45 46 47def wait_for_kiosk_ext(browser, ext_id): 48 """Wait for kiosk extension launch. 49 50 @param browser: Telemetry browser object. 51 @param ext_id: Extension id of the kiosk app. 52 @return extension contexts. 53 """ 54 def _kiosk_ext_contexts(): 55 try: 56 ext_contexts = browser.extensions.GetByExtensionId(ext_id) 57 if len(ext_contexts) > 1: 58 return ext_contexts 59 except (KeyError, chrome.Error): 60 pass 61 return [] 62 return utils.poll_for_condition( 63 _kiosk_ext_contexts, 64 exception=error.TestFail('Kiosk app failed to launch'), 65 timeout=DEFAULT_TIMEOUT, 66 sleep_interval=1) 67 68 69def config_riseplayer(browser, ext_id, app_config_id): 70 """ 71 Configure Rise Player app with a specific display id. 72 73 Step through the configuration screen of the Rise Player app 74 which is launched within the browser and enter a display id 75 within the configuration frame to initiate media display. 76 77 @param browser: browser instance containing the Rise Player kiosk app. 78 @param ext_id: extension id of the Rise Player Kiosk App. 79 @param app_config_id: display id for the Rise Player app . 80 81 """ 82 if not app_config_id: 83 raise error.TestFail( 84 'Error in configuring Rise Player: app_config_id is None') 85 config_js = """ 86 var frameId = 'btn btn-primary display-register-button' 87 document.getElementsByClassName(frameId)[0].click(); 88 $( "input:text" ).val("%s"); 89 document.getElementsByClassName(frameId)[4].click(); 90 """ % app_config_id 91 92 kiosk_webview_context = get_webview_context( 93 browser, ext_id) 94 # Wait for the configuration frame to load. 95 time.sleep(SHORT_TIMEOUT) 96 kiosk_webview_context.ExecuteJavaScript(config_js) 97 # TODO (krishnargv): Find a way to verify that content is playing 98 # within the RisePlayer app. 99 verify_app_config_id = """ 100 /rvashow.*.display&id=%s.*/.test(location.href) 101 """ % app_config_id 102 #Verify that Risepplayer successfully validates the display id. 103 try: 104 kiosk_webview_context.WaitForJavaScriptCondition( 105 verify_app_config_id, 106 timeout=DEFAULT_TIMEOUT) 107 except exceptions.TimeoutException: 108 raise error.TestFail('Error in configuring Rise Player with id: %s' 109 % app_config_id) 110