Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / backends / form_based_credentials_backend_unittest_base.py
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
5 import logging
6 import os
7 import unittest
8
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
14
15 _ = simple_mock.DONT_CARE
16
17
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):
23     return None
24   return credentials_path
25
26
27 class FormBasedCredentialsBackendUnitTestBase(unittest.TestCase):
28   def setUp(self):
29     self._credentials_type = None
30
31   @test.Disabled
32   def testRealLoginIfPossible(self):
33     credentials_path = _GetCredentialsPath()
34     if not credentials_path:
35       logging.warning('Credentials file not found, skipping test.')
36       return
37
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):
42         return
43       ret = b.credentials.LoginNeeded(b.tabs[0], self._credentials_type)
44       self.assertTrue(ret)
45
46   @test.Disabled
47   def testRealLoginWithDontOverrideProfileIfPossible(self):
48     credentials_path = _GetCredentialsPath()
49     if not credentials_path:
50       logging.warning('Credentials file not found, skipping test.')
51       return
52
53     options = options_for_unittests.GetCopy()
54
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
58
59       if not b.credentials.CanLogin(self._credentials_type):
60         return
61
62       tab = b.tabs[0]
63
64       # Should not be logged in, since this is a fresh credentials
65       # instance.
66       self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))
67
68       # Log in.
69       ret = b.credentials.LoginNeeded(tab, self._credentials_type)
70
71       # Make sure login was successful.
72       self.assertTrue(ret)
73       self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
74
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
79
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))
83
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)
88
89       # Make sure our login attempt did in fact succeed and set the
90       # backend's internal state to 'logged in'.
91       self.assertTrue(ret)
92       self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
93
94   def testLoginUsingMock(self):
95     raise NotImplementedError()
96
97   def _LoginUsingMock(self, backend, login_page_url, email_element_id,
98                       password_element_id): # pylint: disable=R0201
99     tab = simple_mock.MockObject()
100
101     config = {'username': 'blah',
102               'password': 'blargh'}
103
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')
109
110     def VerifyEmail(js):
111       assert email_element_id in js
112       assert 'blah' in js
113     tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail)
114
115     def VerifyPw(js):
116       assert password_element_id in js
117       assert 'largh' in js
118     tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw)
119
120     def VerifySubmit(js):
121       assert '.submit' in js
122     tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit)
123
124     # Checking for form still up.
125     tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
126
127     backend.LoginNeeded(tab, config)