• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 The Chromium 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
5from functools import partial
6import logging
7
8from telemetry.core import exceptions
9from telemetry.internal.browser import web_contents
10
11import py_utils
12
13
14class Oobe(web_contents.WebContents):
15  def __init__(self, inspector_backend):
16    super(Oobe, self).__init__(inspector_backend)
17
18  def _GaiaIFrameContext(self):
19    max_context_id = self.EnableAllContexts()
20    logging.debug('%d contexts in Gaia page' % max_context_id)
21    for gaia_iframe_context in range(max_context_id + 1):
22      try:
23        if self.EvaluateJavaScript(
24            "document.readyState == 'complete' && "
25            "document.getElementById('Email') != null",
26            context_id=gaia_iframe_context):
27          return gaia_iframe_context
28      except exceptions.EvaluateException:
29        pass
30    return None
31
32  def _GaiaWebviewContext(self):
33    webview_contexts = self.GetWebviewContexts()
34    if webview_contexts:
35      return webview_contexts[0]
36    return None
37
38  def _ExecuteOobeApi(self, api, *args):
39    logging.info('Invoking %s' % api)
40    self.WaitForJavaScriptCondition("typeof Oobe == 'function'", timeout=120)
41
42    if self.EvaluateJavaScript(
43        "typeof {{ @api }} == 'undefined'", api=api):
44      raise exceptions.LoginException('%s js api missing' % api)
45
46    # Example values:
47    #   |api|:    'doLogin'
48    #   |args|:   ['username', 'pass', True]
49    #   Executes: 'doLogin("username", "pass", true)'
50    self.ExecuteJavaScript('{{ @f }}({{ *args }})', f=api, args=args)
51
52  def NavigateGuestLogin(self):
53    """Logs in as guest."""
54    self._ExecuteOobeApi('Oobe.guestLoginForTesting')
55
56  def NavigateFakeLogin(self, username, password, gaia_id,
57                        enterprise_enroll=False):
58    """Fake user login."""
59    self._ExecuteOobeApi('Oobe.loginForTesting', username, password, gaia_id,
60                         enterprise_enroll)
61
62  def NavigateGaiaLogin(self, username, password,
63                        enterprise_enroll=False,
64                        for_user_triggered_enrollment=False):
65    """Logs in using the GAIA webview or IFrame, whichever is
66    present. |enterprise_enroll| allows for enterprise enrollment.
67    |for_user_triggered_enrollment| should be False for remora enrollment."""
68    self._ExecuteOobeApi('Oobe.skipToLoginForTesting')
69    if for_user_triggered_enrollment:
70      self._ExecuteOobeApi('Oobe.switchToEnterpriseEnrollmentForTesting')
71
72    self._NavigateGaiaLogin(username, password, enterprise_enroll)
73
74    if enterprise_enroll:
75      self.WaitForJavaScriptCondition(
76          'Oobe.isEnrollmentSuccessfulForTest()', timeout=30)
77      self._ExecuteOobeApi('Oobe.enterpriseEnrollmentDone')
78
79  def _NavigateGaiaLogin(self, username, password, enterprise_enroll):
80    """Invokes NavigateIFrameLogin or NavigateWebViewLogin as appropriate."""
81    def _GetGaiaFunction():
82      if self._GaiaWebviewContext() is not None:
83        return partial(Oobe._NavigateWebViewLogin,
84                       wait_for_close=not enterprise_enroll)
85      elif self._GaiaIFrameContext() is not None:
86        return partial(Oobe._NavigateIFrameLogin,
87                       add_user_for_testing=not enterprise_enroll)
88      return None
89    py_utils.WaitFor(_GetGaiaFunction, 20)(self, username, password)
90
91  def _NavigateIFrameLogin(self, username, password, add_user_for_testing):
92    """Logs into the IFrame-based GAIA screen"""
93    gaia_iframe_context = py_utils.WaitFor(self._GaiaIFrameContext, timeout=30)
94
95    if add_user_for_testing:
96      self._ExecuteOobeApi('Oobe.showAddUserForTesting')
97    self.ExecuteJavaScript("""
98        document.getElementById('Email').value= {{ username }};
99        document.getElementById('Passwd').value= {{ password }};
100        document.getElementById('signIn').click();""",
101        username=username, password=password,
102        context_id=gaia_iframe_context)
103
104  def _NavigateWebViewLogin(self, username, password, wait_for_close):
105    """Logs into the webview-based GAIA screen"""
106    self._NavigateWebViewEntry('identifierId', username, 'identifierNext')
107    self._NavigateWebViewEntry('password', password, 'passwordNext')
108    if wait_for_close:
109      py_utils.WaitFor(lambda: not self._GaiaWebviewContext(), 60)
110
111  def _NavigateWebViewEntry(self, field, value, next_field):
112    self._WaitForField(field)
113    self._WaitForField(next_field)
114    gaia_webview_context = self._GaiaWebviewContext()
115    gaia_webview_context.EvaluateJavaScript("""
116       document.getElementById({{ field }}).value= {{ value }};
117       document.getElementById({{ next_field }}).click()""",
118       field=field, value=value, next_field=next_field)
119
120  def _WaitForField(self, field):
121    gaia_webview_context = py_utils.WaitFor(self._GaiaWebviewContext, 5)
122    py_utils.WaitFor(gaia_webview_context.HasReachedQuiescence, 20)
123    gaia_webview_context.WaitForJavaScriptCondition(
124        "document.getElementById({{ field }}) != null",
125        field=field, timeout=20)
126