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.
8 from telemetry.core import browser_finder
9 from telemetry.core import util
10 from telemetry.unittest import simple_mock
11 from telemetry.unittest import options_for_unittests
12 from telemetry.unittest import DisabledTest
14 _ = simple_mock.DONT_CARE
17 def _GetCredentialsPath():
18 # TODO: This shouldn't depend on tools/perf.
19 credentials_path = os.path.join(util.GetChromiumSrcDir(),
20 'tools', 'perf', 'data', 'credentials.json')
21 if not os.path.exists(credentials_path):
23 return credentials_path
26 class FormBasedCredentialsBackendUnitTestBase(unittest.TestCase):
28 self._credentials_type = None
31 def testRealLoginIfPossible(self):
32 credentials_path = _GetCredentialsPath()
33 if not credentials_path:
34 logging.warning('Credentials file not found, skipping test.')
37 options = options_for_unittests.GetCopy()
38 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:
58 b.credentials.credentials_path = credentials_path
60 if not b.credentials.CanLogin(self._credentials_type):
65 # Should not be logged in, since this is a fresh credentials
67 self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))
70 ret = b.credentials.LoginNeeded(tab, self._credentials_type)
72 # Make sure login was successful.
74 self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
76 # Reset state. Now the backend thinks we're logged out, even
77 # though we are logged in in our current browser session. This
78 # simulates the effects of running with --dont-override-profile.
79 b.credentials._ResetLoggedInState() # pylint: disable=W0212
81 # Make sure the backend thinks we're logged out.
82 self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))
83 self.assertTrue(b.credentials.CanLogin(self._credentials_type))
85 # Attempt to login again. This should detect that we've hit
86 # the 'logged in' page instead of the login form, and succeed
87 # instead of timing out.
88 ret = b.credentials.LoginNeeded(tab, self._credentials_type)
90 # Make sure our login attempt did in fact succeed and set the
91 # backend's internal state to 'logged in'.
93 self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
95 def testLoginUsingMock(self):
96 raise NotImplementedError()
98 def _LoginUsingMock(self, backend, login_page_url, email_element_id,
99 password_element_id): # pylint: disable=R0201
100 tab = simple_mock.MockObject()
102 config = {'username': 'blah',
103 'password': 'blargh'}
105 tab.ExpectCall('Navigate', login_page_url)
106 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
107 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(True)
108 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
109 tab.ExpectCall('WaitForDocumentReadyStateToBeInteractiveOrBetter')
112 assert email_element_id in js
114 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail)
117 assert password_element_id in js
119 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw)
121 def VerifySubmit(js):
122 assert '.submit' in js
123 tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit)
125 # Checking for form still up.
126 tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
128 backend.LoginNeeded(tab, config)