- add sources.
[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 import logging
5 import os
6 import unittest
7
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
13
14 _ = simple_mock.DONT_CARE
15
16
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):
22     return None
23   return credentials_path
24
25
26 class FormBasedCredentialsBackendUnitTestBase(unittest.TestCase):
27   def setUp(self):
28     self._credentials_type = None
29
30   @DisabledTest
31   def testRealLoginIfPossible(self):
32     credentials_path = _GetCredentialsPath()
33     if not credentials_path:
34       logging.warning('Credentials file not found, skipping test.')
35       return
36
37     options = options_for_unittests.GetCopy()
38     with browser_finder.FindBrowser(options).Create() as b:
39       b.Start()
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   @DisabledTest
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.Start()
58       b.credentials.credentials_path = credentials_path
59
60       if not b.credentials.CanLogin(self._credentials_type):
61         return
62
63       tab = b.tabs[0]
64
65       # Should not be logged in, since this is a fresh credentials
66       # instance.
67       self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))
68
69       # Log in.
70       ret = b.credentials.LoginNeeded(tab, self._credentials_type)
71
72       # Make sure login was successful.
73       self.assertTrue(ret)
74       self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
75
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
80
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))
84
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)
89
90       # Make sure our login attempt did in fact succeed and set the
91       # backend's internal state to 'logged in'.
92       self.assertTrue(ret)
93       self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))
94
95   def testLoginUsingMock(self):
96     raise NotImplementedError()
97
98   def _LoginUsingMock(self, backend, login_page_url, email_element_id,
99                       password_element_id): # pylint: disable=R0201
100     tab = simple_mock.MockObject()
101
102     config = {'username': 'blah',
103               'password': 'blargh'}
104
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')
110
111     def VerifyEmail(js):
112       assert email_element_id in js
113       assert 'blah' in js
114     tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail)
115
116     def VerifyPw(js):
117       assert password_element_id in js
118       assert 'largh' in js
119     tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw)
120
121     def VerifySubmit(js):
122       assert '.submit' in js
123     tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit)
124
125     # Checking for form still up.
126     tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
127
128     backend.LoginNeeded(tab, config)