1# Copyright 2014 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.cros import chrome
9
10
11def _ExecuteOobeCmd(browser, cmd):
12    logging.info('Invoking ' + cmd)
13    oobe = browser.oobe
14    oobe.WaitForJavaScriptCondition('typeof Oobe !== \'undefined\'',
15                                    timeout=10)
16    oobe.ExecuteJavaScript(cmd)
17
18
19def RemoraEnrollment(browser, user_id, password):
20    """Enterprise login for a Remora device.
21
22    @param browser: telemetry browser object.
23    @param user_id: login credentials user_id.
24    @param password: login credentials password.
25    """
26    browser.oobe.NavigateGaiaLogin(
27            user_id, password, enterprise_enroll=True,
28            for_user_triggered_enrollment=False)
29
30
31def EnterpriseEnrollment(browser, user_id, password, auto_login=False):
32    """Enterprise login for a kiosk device.
33
34    @param browser: telemetry browser object.
35    @param user_id: login credentials user_id.
36    @param password: login credentials password.
37    @param auto_login: also login after enrollment.
38    """
39    browser.oobe.NavigateGaiaLogin(user_id, password,
40                                   enterprise_enroll=True,
41                                   for_user_triggered_enrollment=True)
42    if auto_login:
43        browser.oobe.NavigateGaiaLogin(user_id, password)
44        # TODO(achuith): Replace with WaitForLogin.
45        utils.poll_for_condition(lambda: not browser.oobe_exists, timeout=30)
46
47
48def EnterpriseFakeEnrollment(browser, user_id, password, gaia_id,
49                             auto_login=False):
50    """Enterprise fake login.
51
52    @param browser: telemetry browser object.
53    @param user_id: login credentials user_id.
54    @param password: login credentials password.
55    @param gaia_id: login credentials gaia_id.
56    @param auto_login: also login after enrollment.
57    """
58    browser.oobe.NavigateFakeLogin(user_id, password, gaia_id,
59                                   enterprise_enroll=True)
60    # Oobe context recreated after after the enrollment.
61    utils.poll_for_condition(
62        lambda: browser.oobe_exists and
63            browser.oobe.EnterpriseWebviewVisible(user_id), timeout=30)
64
65    if auto_login:
66        browser.oobe.NavigateFakeLogin(user_id, password, gaia_id)
67        # TODO(achuith): Replace with WaitForLogin.
68        utils.poll_for_condition(lambda: not browser.oobe_exists, timeout=45)
69
70
71def OnlineDemoMode(browser):
72  """Switch to online demo mode.
73
74    @param browser: telemetry browser object.
75  """
76  _ExecuteOobeCmd(browser, 'Oobe.setUpOnlineDemoModeForTesting();')
77  utils.poll_for_condition(lambda: not browser.oobe_exists, timeout=90)
78
79
80def KioskEnrollment(browser, user_id, password, gaia_id):
81    """Kiosk Enrollment.
82
83    @param browser: telemetry browser object.
84    @param user_id: login credentials user_id.
85    @param password: login credentials password.
86    @param gaia_id: login credentials gaia_id.
87    """
88
89    cmd = ('Oobe.loginForTesting("{user}", "{password}", "{gaia_id}", true)'
90           .format(user=user_id,
91                   password=password,
92                   gaia_id=gaia_id))
93    _ExecuteOobeCmd(browser, cmd)
94
95    utils.poll_for_condition(lambda: not browser.oobe_exists, timeout=60)
96