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 6 7from autotest_lib.client.bin import utils 8from autotest_lib.client.common_lib import error 9 10DEFAULT_TIMEOUT = 30 11DIAGNOSTIC_RUN_TIMEOUT = 180 12 13 14class CfmHangoutsAPI(object): 15 """Utility class for interacting with Hangouts in CFM.""" 16 17 def __init__(self, webview_context): 18 self._webview_context = webview_context 19 20 21 def wait_for_telemetry_commands(self): 22 """Wait for hotrod app to load and telemetry commands to be available. 23 """ 24 self._webview_context.WaitForJavaScriptCondition( 25 "typeof window.hrOobIsStartPageForTest == 'function'", 26 timeout=DEFAULT_TIMEOUT) 27 logging.info('Hotrod telemetry commands available for testing.') 28 29 30 def wait_for_meetings_in_call_page(self): 31 """Waits for the in-call page to launch.""" 32 raise NotImplementedError 33 34 35 def wait_for_meetings_landing_page(self): 36 """Waits for the landing page screen.""" 37 raise NotImplementedError 38 39 40 # UI commands/functions 41 def wait_for_oobe_start_page(self): 42 """Wait for oobe start screen to launch.""" 43 self._webview_context.WaitForJavaScriptCondition( 44 "window.hasOwnProperty('hrOobIsStartPageForTest') " 45 "&& window.hrOobIsStartPageForTest() === true;", 46 timeout=DEFAULT_TIMEOUT) 47 logging.info('Reached oobe start page') 48 49 50 def skip_oobe_screen(self): 51 """Skip Chromebox for Meetings oobe screen.""" 52 self._webview_context.ExecuteJavaScript("window.hrOobSkipForTest()") 53 utils.poll_for_condition( 54 lambda: not self._webview_context.EvaluateJavaScript( 55 "window.hrOobIsStartPageForTest()"), 56 exception=error.TestFail('Not able to skip oobe screen.'), 57 timeout=DEFAULT_TIMEOUT, 58 sleep_interval=1) 59 logging.info('Skipped oobe screen.') 60 61 62 def is_oobe_start_page(self): 63 """Check if device is on CFM oobe start screen.""" 64 if self._webview_context.EvaluateJavaScript( 65 "window.hrOobIsStartPageForTest()"): 66 logging.info('Is on oobe start page.') 67 return True 68 logging.info('Is not on oobe start page.') 69 return False 70 71 72 # Hangouts commands/functions 73 def start_new_hangout_session(self, hangout_name): 74 """Start a new hangout session. 75 76 @param hangout_name: Name of the hangout session. 77 """ 78 if not self.is_ready_to_start_hangout_session(): 79 if self.is_in_hangout_session(): 80 self.end_hangout_session() 81 utils.poll_for_condition( 82 lambda: self._webview_context.EvaluateJavaScript( 83 "window.hrIsReadyToStartHangoutForTest()"), 84 exception=error.TestFail( 85 'Not ready to start hangout session.'), 86 timeout=DEFAULT_TIMEOUT, 87 sleep_interval=1) 88 89 self._webview_context.ExecuteJavaScript("window.hrStartCallForTest('" + 90 hangout_name + "')") 91 utils.poll_for_condition( 92 lambda: self._webview_context.EvaluateJavaScript( 93 "window.hrIsInHangoutForTest()"), 94 exception=error.TestFail('Not able to start session.'), 95 timeout=DEFAULT_TIMEOUT, 96 sleep_interval=1) 97 logging.info('Started hangout session: %s', hangout_name) 98 99 100 def end_hangout_session(self): 101 """End current hangout session.""" 102 self._webview_context.ExecuteJavaScript("window.hrHangupCallForTest()") 103 utils.poll_for_condition( 104 lambda: not self._webview_context.EvaluateJavaScript( 105 "window.hrIsInHangoutForTest()"), 106 exception=error.TestFail('Not able to end session.'), 107 timeout=DEFAULT_TIMEOUT, 108 sleep_interval=1) 109 110 logging.info('Ended hangout session.') 111 112 113 def is_in_hangout_session(self): 114 """Check if device is in hangout session.""" 115 if self._webview_context.EvaluateJavaScript( 116 "window.hrIsInHangoutForTest()"): 117 logging.info('Is in hangout session.') 118 return True 119 logging.info('Is not in hangout session.') 120 return False 121 122 123 def is_ready_to_start_hangout_session(self): 124 """Check if device is ready to start a new hangout session.""" 125 if (self._webview_context.EvaluateJavaScript( 126 "window.hrIsReadyToStartHangoutForTest()")): 127 logging.info('Is ready to start hangout session.') 128 return True 129 logging.info('Is not ready to start hangout session.') 130 return False 131 132 133 def join_meeting_session(self, meeting_name): 134 """Joins a meeting. 135 136 @param meeting_name: Name of the meeting session. 137 """ 138 raise NotImplementedError 139 140 141 def end_meeting_session(self): 142 """End current meeting session.""" 143 raise NotImplementedError 144 145 146 def is_in_meeting_session(self): 147 """Check if device is in meeting session.""" 148 raise NotImplementedError 149 150 151 def get_participant_count(self): 152 """Returns the total number of participants in a hangout.""" 153 return self._webview_context.EvaluateJavaScript( 154 "window.hrGetParticipantsCountInCallForTest()") 155 156 157 # Diagnostics commands/functions 158 def is_diagnostic_run_in_progress(self): 159 """Check if hotrod diagnostics is running.""" 160 if (self._webview_context.EvaluateJavaScript( 161 "window.hrIsDiagnosticRunInProgressForTest()")): 162 logging.info('Diagnostic run is in progress.') 163 return True 164 logging.info('Diagnostic run is not in progress.') 165 return False 166 167 168 def wait_for_diagnostic_run_to_complete(self): 169 """Wait for hotrod diagnostics to complete.""" 170 utils.poll_for_condition( 171 lambda: not self._webview_context.EvaluateJavaScript( 172 "window.hrIsDiagnosticRunInProgressForTest()"), 173 exception=error.TestError('Diagnostic run still in progress ' 174 'after 3 minutes.'), 175 timeout=DIAGNOSTIC_RUN_TIMEOUT, 176 sleep_interval=1) 177 178 179 def run_diagnostics(self): 180 """Run hotrod diagnostics.""" 181 if self.is_diagnostic_run_in_progress(): 182 self.wait_for_diagnostic_run_to_complete() 183 self._webview_context.ExecuteJavaScript( 184 "window.hrRunDiagnosticsForTest()") 185 logging.info('Started diagnostics run.') 186 187 188 def get_last_diagnostics_results(self): 189 """Get latest hotrod diagnostics results.""" 190 if self.is_diagnostic_run_in_progress(): 191 self.wait_for_diagnostic_run_to_complete() 192 return self._webview_context.EvaluateJavaScript( 193 "window.hrGetLastDiagnosticsResultForTest()") 194 195 196 # Mic audio commands/functions 197 def is_mic_muted(self): 198 """Check if mic is muted.""" 199 if self._webview_context.EvaluateJavaScript( 200 "window.hrGetAudioInMutedForTest()"): 201 logging.info('Mic is muted.') 202 return True 203 logging.info('Mic is not muted.') 204 return False 205 206 207 def mute_mic(self): 208 """Local mic mute from toolbar.""" 209 self._webview_context.ExecuteJavaScript( 210 "window.hrSetAudioInMutedForTest(true)") 211 logging.info('Locally muted mic.') 212 213 214 def unmute_mic(self): 215 """Local mic unmute from toolbar.""" 216 self._webview_context.ExecuteJavaScript( 217 "window.hrSetAudioInMutedForTest(false)") 218 logging.info('Locally unmuted mic.') 219 220 221 def remote_mute_mic(self): 222 """Remote mic mute request from cPanel.""" 223 self._webview_context.ExecuteJavaScript("window.hrMuteAudioForTest()") 224 logging.info('Remotely muted mic.') 225 226 227 def remote_unmute_mic(self): 228 """Remote mic unmute request from cPanel.""" 229 self._webview_context.ExecuteJavaScript( 230 "window.hrUnmuteAudioForTest()") 231 logging.info('Remotely unmuted mic.') 232 233 234 def get_mic_devices(self): 235 """Get all mic devices detected by hotrod.""" 236 return self._webview_context.EvaluateJavaScript( 237 "window.hrGetAudioInDevicesForTest()") 238 239 240 def get_preferred_mic(self): 241 """Get mic preferred for hotrod.""" 242 return self._webview_context.EvaluateJavaScript( 243 "window.hrGetAudioInPrefForTest()") 244 245 246 def set_preferred_mic(self, mic): 247 """Set preferred mic for hotrod. 248 249 @param mic: String with mic name. 250 """ 251 self._webview_context.ExecuteJavaScript( 252 "window.hrSetAudioInPrefForTest('" + mic + "')") 253 logging.info('Setting preferred mic to %s.', mic) 254 255 256 # Speaker commands/functions 257 def get_speaker_devices(self): 258 """Get all speaker devices detected by hotrod.""" 259 return self._webview_context.EvaluateJavaScript( 260 "window.hrGetAudioOutDevicesForTest()") 261 262 263 def get_preferred_speaker(self): 264 """Get speaker preferred for hotrod.""" 265 return self._webview_context.EvaluateJavaScript( 266 "window.hrGetAudioOutPrefForTest()") 267 268 269 def set_preferred_speaker(self, speaker): 270 """Set preferred speaker for hotrod. 271 272 @param mic: String with speaker name. 273 """ 274 self._webview_context.ExecuteJavaScript( 275 "window.hrSetAudioOutPrefForTest('" + speaker + "')") 276 logging.info('Set preferred speaker to %s.', speaker) 277 278 279 def set_speaker_volume(self, vol_level): 280 """Set speaker volume. 281 282 @param vol_level: String value ranging from 0-100 to set volume to. 283 """ 284 self._webview_context.ExecuteJavaScript( 285 "window.hrSetAudioOutVolumeLevelForTest('" + vol_level + "')") 286 logging.info('Set speaker volume to %s', vol_level) 287 288 289 def get_speaker_volume(self): 290 """Get current speaker volume.""" 291 return self._webview_context.EvaluateJavaScript( 292 "window.hrGetAudioOutVolumeLevelForTest()") 293 294 295 def play_test_sound(self): 296 """Play test sound.""" 297 self._webview_context.ExecuteJavaScript( 298 "window.hrPlayTestSoundForTest()") 299 logging.info('Playing test sound.') 300 301 302 # Camera commands/functions 303 def get_camera_devices(self): 304 """Get all camera devices detected by hotrod.""" 305 return self._webview_context.EvaluateJavaScript( 306 "window.hrGetVideoCaptureDevicesForTest()") 307 308 309 def get_preferred_camera(self): 310 """Get camera preferred for hotrod.""" 311 return self._webview_context.EvaluateJavaScript( 312 "window.hrGetVideoCapturePrefForTest()") 313 314 315 def set_preferred_camera(self, camera): 316 """Set preferred camera for hotrod. 317 318 @param mic: String with camera name. 319 """ 320 self._webview_context.ExecuteJavaScript( 321 "window.hrSetVideoCapturePrefForTest('" + camera + "')") 322 logging.info('Set preferred camera to %s.', camera) 323 324 325 def is_camera_muted(self): 326 """Check if camera is muted (turned off).""" 327 if self._webview_context.EvaluateJavaScript( 328 "window.hrGetVideoCaptureMutedForTest()"): 329 logging.info('Camera is muted.') 330 return True 331 logging.info('Camera is not muted.') 332 return False 333 334 335 def mute_camera(self): 336 """Turned camera off.""" 337 self._webview_context.ExecuteJavaScript( 338 "window.hrSetVideoCaptureMutedForTest(true)") 339 logging.info('Camera muted.') 340 341 342 def unmute_camera(self): 343 """Turned camera on.""" 344 self._webview_context.ExecuteJavaScript( 345 "window.hrSetVideoCaptureMutedForTest(false)") 346 logging.info('Camera unmuted.') 347