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
5"""Facade to access the CFM functionality."""
6
7import time
8
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.common_lib.cros import enrollment, cfm_util
11
12
13class TimeoutException(Exception):
14    """Timeout Exception class."""
15    pass
16
17
18class CFMFacadeNative(object):
19    """Facade to access the CFM functionality.
20
21    The methods inside this class only accept Python native types.
22    """
23    _USER_ID = 'cfmtest@croste.tv'
24    _PWD = 'test0000'
25    _EXT_ID = 'ikfcpmgefdpheiiomgmhlmmkihchmdlj'
26    _ENROLLMENT_DELAY = 15
27
28
29    def __init__(self, resource):
30        """Initializes a CFMFacadeNative.
31
32        @param resource: A FacadeResource object.
33        """
34        self._resource = resource
35
36
37    def enroll_device(self):
38        """Enroll device into CFM."""
39        self._resource.start_custom_chrome({"auto_login": False,
40                                            "disable_gaia_services": False})
41        enrollment.RemoraEnrollment(self._resource._browser, self._USER_ID,
42                self._PWD)
43        # Timeout to allow for the device to stablize and go back to the
44        # login screen before proceeding.
45        time.sleep(self._ENROLLMENT_DELAY)
46        self.restart_chrome_for_cfm()
47        self.check_hangout_extension_context()
48
49
50    def restart_chrome_for_cfm(self):
51        """Restart chrome with custom values for CFM."""
52        custom_chrome_setup = {"clear_enterprise_policy": False,
53                               "dont_override_profile": True,
54                               "disable_gaia_services": False,
55                               "disable_default_apps": False,
56                               "auto_login": False}
57        self._resource.start_custom_chrome(custom_chrome_setup)
58
59
60    def check_hangout_extension_context(self):
61        """Check to make sure hangout app launched.
62
63        @raises error.TestFail if the URL checks fails.
64        """
65        ext_contexts = cfm_util.wait_for_kiosk_ext(
66                self._resource._browser, self._EXT_ID)
67        ext_urls = set([context.EvaluateJavaScript('location.href;')
68                        for context in ext_contexts])
69        expected_urls = set(
70                ['chrome-extension://' + self._EXT_ID + '/' + path
71                 for path in ['hangoutswindow.html?windowid=0',
72                              '_generated_background_page.html']])
73        if expected_urls != ext_urls:
74            raise error.TestFail(
75                    'Unexpected extension context urls, expected %s, got %s'
76                    % (expected_urls, ext_urls))
77
78    @property
79    def _webview_context(self):
80        """Get webview context object."""
81        return cfm_util.get_cfm_webview_context(self._resource._browser,
82                self._EXT_ID)
83
84
85    def wait_for_telemetry_commands(self):
86        """Wait for telemetry commands."""
87        cfm_util.wait_for_telemetry_commands(self._webview_context)
88
89
90    # UI commands/functions
91    def wait_for_oobe_start_page(self):
92        """Wait for oobe start screen to launch."""
93        cfm_util.wait_for_oobe_start_page(self._webview_context)
94
95
96    def skip_oobe_screen(self):
97        """Skip Chromebox for Meetings oobe screen."""
98        cfm_util.skip_oobe_screen(self._webview_context)
99
100
101    def is_oobe_start_page(self):
102        """Check if device is on CFM oobe start screen.
103
104        @return a boolean, based on oobe start page status.
105        """
106        return cfm_util.is_oobe_start_page(self._webview_context)
107
108
109    # Hangouts commands/functions
110    def start_new_hangout_session(self, session_name):
111        """Start a new hangout session.
112
113        @param session_name: Name of the hangout session.
114        """
115        cfm_util.start_new_hangout_session(self._webview_context, session_name)
116
117
118    def end_hangout_session(self):
119        """End current hangout session."""
120        cfm_util.end_hangout_session(self._webview_context)
121
122
123    def is_in_hangout_session(self):
124        """Check if device is in hangout session.
125
126        @return a boolean, for hangout session state.
127        """
128        return cfm_util.is_in_hangout_session(self._webview_context)
129
130
131    def is_ready_to_start_hangout_session(self):
132        """Check if device is ready to start a new hangout session.
133
134        @return a boolean for hangout session ready state.
135        """
136        return cfm_util.is_ready_to_start_hangout_session(
137                self._webview_context)
138
139
140    # Diagnostics commands/functions
141    def is_diagnostic_run_in_progress(self):
142        """Check if hotrod diagnostics is running.
143
144        @return a boolean for diagnostic run state.
145        """
146        return cfm_util.is_diagnostic_run_in_progress(self._webview_context)
147
148
149    def wait_for_diagnostic_run_to_complete(self):
150        """Wait for hotrod diagnostics to complete."""
151        cfm_util.wait_for_diagnostic_run_to_complete(self._webview_context)
152
153
154    def run_diagnostics(self):
155        """Run hotrod diagnostics."""
156        cfm_util.run_diagnostics(self._webview_context)
157
158
159    def get_last_diagnostics_results(self):
160        """Get latest hotrod diagnostics results.
161
162        @return a dict with diagnostic test results.
163        """
164        return cfm_util.get_last_diagnostics_results(self._webview_context)
165
166
167    # Mic audio commands/functions
168    def is_mic_muted(self):
169        """Check if mic is muted.
170
171        @return a boolean for mic mute state.
172        """
173        return cfm_util.is_mic_muted(self._webview_context)
174
175
176    def mute_mic(self):
177        """Local mic mute from toolbar."""
178        cfm_util.mute_mic(self._webview_context)
179
180
181    def unmute_mic(self):
182        """Local mic unmute from toolbar."""
183        cfm_util.unmute_mic(self._webview_context)
184
185
186    def remote_mute_mic(self):
187        """Remote mic mute request from cPanel."""
188        cfm_util.remote_mute_mic(self._webview_context)
189
190
191    def remote_unmute_mic(self):
192        """Remote mic unmute request from cPanel."""
193        cfm_util.remote_unmute_mic(self._webview_context)
194
195
196    def get_mic_devices(self):
197        """Get all mic devices detected by hotrod.
198
199        @return a list of mic devices.
200        """
201        return cfm_util.get_mic_devices(self._webview_context)
202
203
204    def get_preferred_mic(self):
205        """Get mic preferred for hotrod.
206
207        @return a str with preferred mic name.
208        """
209        return cfm_util.get_preferred_mic(self._webview_context)
210
211
212    def set_preferred_mic(self, mic):
213        """Set preferred mic for hotrod.
214
215        @param mic: String with mic name.
216        """
217        cfm_util.set_preferred_mic(self._webview_context, mic)
218
219
220    # Speaker commands/functions
221    def get_speaker_devices(self):
222        """Get all speaker devices detected by hotrod.
223
224        @return a list of speaker devices.
225        """
226        return cfm_util.get_speaker_devices(self._webview_context)
227
228
229    def get_preferred_speaker(self):
230        """Get speaker preferred for hotrod.
231
232        @return a str with preferred speaker name.
233        """
234        return cfm_util.get_preferred_speaker(self._webview_context)
235
236
237    def set_preferred_speaker(self, speaker):
238        """Set preferred speaker for hotrod.
239
240        @param speaker: String with speaker name.
241        """
242        cfm_util.set_preferred_speaker(self._webview_context, speaker)
243
244
245    def set_speaker_volume(self, volume_level):
246        """Set speaker volume.
247
248        @param volume_level: String value ranging from 0-100 to set volume to.
249        """
250        cfm_util.set_speaker_volume(self._webview_context, volume_level)
251
252
253    def get_speaker_volume(self):
254        """Get current speaker volume.
255
256        @return a str value with speaker volume level 0-100.
257        """
258        return cfm_util.get_speaker_volume(self._webview_context)
259
260
261    def play_test_sound(self):
262        """Play test sound."""
263        cfm_util.play_test_sound(self._webview_context)
264
265
266    # Camera commands/functions
267    def get_camera_devices(self):
268        """Get all camera devices detected by hotrod.
269
270        @return a list of camera devices.
271        """
272        return cfm_util.get_camera_devices(self._webview_context)
273
274
275    def get_preferred_camera(self):
276        """Get camera preferred for hotrod.
277
278        @return a str with preferred camera name.
279        """
280        return cfm_util.get_preferred_camera(self._webview_context)
281
282
283    def set_preferred_camera(self, camera):
284        """Set preferred camera for hotrod.
285
286        @param camera: String with camera name.
287        """
288        cfm_util.set_preferred_camera(self._webview_context, camera)
289
290
291    def is_camera_muted(self):
292        """Check if camera is muted (turned off).
293
294        @return a boolean for camera muted state.
295        """
296        return cfm_util.is_camera_muted(self._webview_context)
297
298
299    def mute_camera(self):
300        """Turned camera off."""
301        cfm_util.mute_camera(self._webview_context)
302
303
304    def unmute_camera(self):
305        """Turned camera on."""
306        cfm_util.unmute_camera(self._webview_context)
307