1 # Copyright 2013 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.
9 from telemetry import test
10 from telemetry.core import browser_finder
11 from telemetry.core import util
12 from telemetry.unittest import options_for_unittests
13 from telemetry.unittest import simple_mock
15 _ = simple_mock.DONT_CARE
18 def _GetCredentialsPath():
19 # TODO: This shouldn't depend on tools/perf.
20 credentials_path = os.path.join(util.GetChromiumSrcDir(),
21 'tools', 'perf', 'data', 'credentials.json')
22 if not os.path.exists(credentials_path):
24 return credentials_path
27 class FormBasedCredentialsBackendUnitTestBase(unittest.TestCase):
29 self._credentials_type = None
32 def testRealLoginIfPossible(self):
33 credentials_path = _GetCredentialsPath()
34 if not credentials_path:
35 logging.warning('Credentials file not found, skipping test.')
38 options = options_for_unittests.GetCopy()
39 with browser_finder.FindBrowser(options).Create() as b:
40 b.credentials.credentials_path = credentials_path
41 if not b.credentials.CanLogin(self._credentials_type):
43 ret = b.credentials.LoginNeeded(b.tabs[0], self._credentials_type)
47 def testRealLoginWithDontOverrideProfileIfPossible(self):
48 credentials_path = _GetCredentialsPath()
49 if not credentials_path:
50 logging.warning('Credentials file not found, skipping test.')
53 options = options_for_unittests.GetCopy()
55 # Login once to make sure our default profile is logged in.
56 with browser_finder.FindBrowser(options).Create() as b:
57 b.credentials.credentials_path = credentials_path
59 if not b.credentials.CanLogin(self._credentials_type):
64 # Should not be logged in, since this is a fresh credentials
66 self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))
69 ret = b.credentials.LoginNeeded(tab, self._credentials_type)
71 # Make sure login was successful.
73 self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
75 # Reset state. Now the backend thinks we're logged out, even
76 # though we are logged in in our current browser session. This
77 # simulates the effects of running with --dont-override-profile.
78 b.credentials._ResetLoggedInState() # pylint: disable=W0212
80 # Make sure the backend thinks we're logged out.
81 self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))
82 self.assertTrue(b.credentials.CanLogin(self._credentials_type))
84 # Attempt to login again. This should detect that we've hit
85 # the 'logged in' page instead of the login form, and succeed
86 # instead of timing out.
87 ret = b.credentials.LoginNeeded(tab, self._credentials_type)
89 # Make sure our login attempt did in fact succeed and set the
90 # backend's internal state to 'logged in'.
92 self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
94 def testLoginUsingMock(self):
95 raise NotImplementedError()
97 def _LoginUsingMock(self, backend, login_page_url, email_element_id,
98 password_element_id): # pylint: disable=R0201
99 tab = simple_mock.MockObject()
101 config = {'username': 'blah',
102 'password': 'blargh'}
104 tab.ExpectCall('Navigate', login_page_url)
105 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
106 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(True)
107 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
108 tab.ExpectCall('WaitForDocumentReadyStateToBeInteractiveOrBetter')
111 assert email_element_id in js
113 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail)
116 assert password_element_id in js
118 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw)
120 def VerifySubmit(js):
121 assert '.submit' in js
122 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit)
124 # Checking for form still up.
125 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
127 backend.LoginNeeded(tab, config)