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.
4
5import unittest
6
7from telemetry.testing import simple_mock
8
9_ = simple_mock.DONT_CARE
10
11
12class FormBasedCredentialsBackendUnitTestBase(unittest.TestCase):
13  def setUp(self):
14    self._credentials_type = None
15
16  def testLoginUsingMock(self):
17    raise NotImplementedError()
18
19  def _LoginUsingMock(self, backend, login_page_url, email_element_id,
20                      password_element_id, form_element_id,
21                      already_logged_in_js): # pylint: disable=no-self-use
22    tab = simple_mock.MockObject()
23    ar = simple_mock.MockObject()
24
25    config = {'username': 'blah',
26              'password': 'blargh'}
27
28    tab.ExpectCall('Navigate', login_page_url)
29    tab.ExpectCall('EvaluateJavaScript', already_logged_in_js).WillReturn(False)
30    tab.ExpectCall('WaitForDocumentReadyStateToBeInteractiveOrBetter')
31
32    ar.ExpectCall('WaitForJavaScriptCondition',
33                  '(document.querySelector("#%s") !== null) || (%s)' % (
34                      form_element_id, already_logged_in_js), 60)
35    ar.ExpectCall('WaitForNavigate')
36
37    def VerifyEmail(js):
38      assert email_element_id in js
39      assert 'blah' in js
40    tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail)
41
42    def VerifyPw(js):
43      assert password_element_id in js
44      assert 'largh' in js
45    tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw)
46
47    def VerifySubmit(js):
48      assert '.submit' in js or '.click' in js
49    tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit)
50
51    # Checking for form still up.
52    tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
53
54    backend.LoginNeeded(tab, ar, config)
55