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