OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import time | |
7 | |
8 import pyauto_functional # Must be imported before pyauto | |
9 import pyauto | |
10 import test_utils | |
11 | |
12 from webdriver_pages import settings | |
13 from webdriver_pages.settings import Behaviors, ContentTypes | |
14 from webdriver_pages.settings import RestoreOnStartupType | |
15 | |
16 | |
17 class PrefsUITest(pyauto.PyUITest): | |
18 """TestCase for Preferences UI.""" | |
19 | |
20 INFOBAR_TYPE = 'rph_infobar' | |
21 | |
22 def setUp(self): | |
23 pyauto.PyUITest.setUp(self) | |
24 self._driver = self.NewWebDriver() | |
25 | |
26 def Debug(self): | |
27 """Test method for experimentation. | |
28 | |
29 This method will not run automatically. | |
30 """ | |
31 driver = self.NewWebDriver() | |
32 page = settings.ContentSettingsPage.FromNavigation(driver) | |
33 import pdb | |
34 pdb.set_trace() | |
35 | |
36 def _GetGeolocationContentSettingsBehavior(self): | |
37 """Get the Content Settings behavior for Geolocation. | |
38 | |
39 Returns: | |
40 The exceptions behavior for the specified content type. | |
41 The content type is the available content setting available. | |
42 """ | |
43 behavior_key = self.GetPrefsInfo().Prefs( | |
44 pyauto.kGeolocationDefaultContentSetting) | |
45 behaviors_dict = {1: 'ALLOW', 2: 'BLOCK', 3: 'ASK'} | |
46 self.assertTrue( | |
47 behavior_key in behaviors_dict, | |
48 msg=('Invalid default behavior key "%s" for "geolocation" content' % | |
49 behavior_key)) | |
50 return behaviors_dict[behavior_key] | |
51 | |
52 def _VerifyContentExceptionUI(self, content_type, hostname_pattern, behavior, | |
53 incognito=False): | |
54 """Find hostname pattern and behavior within UI on content exceptions page. | |
55 | |
56 Args: | |
57 content_type: The string content settings type to manage. | |
58 hostname_pattern: The URL or pattern associated with the behavior. | |
59 behavior: The exception to allow or block the hostname. | |
60 incognito: Incognito list displayed on exceptions settings page. | |
61 Default to False. | |
62 """ | |
63 page = settings.ManageExceptionsPage.FromNavigation( | |
64 self._driver, content_type) | |
65 self.assertTrue(page.GetExceptions(incognito).has_key(hostname_pattern), | |
66 msg=('No displayed host name matches pattern "%s"' | |
67 % hostname_pattern)) | |
68 self.assertEqual(behavior, page.GetExceptions(incognito)[hostname_pattern], | |
69 msg=('Displayed behavior "%s" does not match behavior "%s"' | |
70 % (page.GetExceptions(incognito)[hostname_pattern], | |
71 behavior))) | |
72 | |
73 def testLocationSettingOptionsUI(self): | |
74 """Verify the location options setting UI. | |
75 | |
76 Set the options through the UI using webdriver and verify the settings in | |
77 pyAuto. | |
78 """ | |
79 page = settings.ContentSettingsPage.FromNavigation(self._driver) | |
80 page.SetContentTypeOption(ContentTypes.GEOLOCATION, Behaviors.ALLOW) | |
81 self.assertEqual( | |
82 1, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting)) | |
83 page.SetContentTypeOption(ContentTypes.GEOLOCATION, Behaviors.BLOCK) | |
84 self.assertEqual( | |
85 2, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting)) | |
86 page.SetContentTypeOption(ContentTypes.GEOLOCATION, Behaviors.ASK) | |
87 self.assertEqual( | |
88 3, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting)) | |
89 | |
90 def testBehaviorValueCorrectlyDisplayed(self): | |
91 """Verify the set behavior value is correctly displayed.""" | |
92 # Block all sites. | |
93 self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 2) | |
94 self.assertEqual( | |
95 self._GetGeolocationContentSettingsBehavior(), Behaviors.BLOCK.upper(), | |
96 msg='The behavior was incorrectly set.') | |
97 # Allow all sites. | |
98 self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 1) | |
99 self.assertEqual( | |
100 self._GetGeolocationContentSettingsBehavior(), Behaviors.ALLOW.upper(), | |
101 msg='The behavior was incorrectly set.') | |
102 # Ask for permission when site wants to track. | |
103 self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 3) | |
104 self.assertEqual( | |
105 self._GetGeolocationContentSettingsBehavior(), Behaviors.ASK.upper(), | |
106 msg='The behavior was incorrectly set.') | |
107 | |
108 def testExceptionsEntryCorrectlyDisplayed(self): | |
109 """Verify the exceptions line entry is correctly displayed in the UI.""" | |
110 geo_exception = ( | |
111 {'http://maps.google.com:80,http://maps.google.com:80': | |
112 {'geolocation': 2}}) | |
113 self.SetPrefs(pyauto.kContentSettingsPatternPairs, geo_exception) | |
114 self._VerifyContentExceptionUI( | |
115 ContentTypes.GEOLOCATION, 'http://maps.google.com:80', | |
116 Behaviors.BLOCK) | |
117 geo_exception = ( | |
118 {'http://maps.google.com:80,http://maps.google.com:80': | |
119 {'geolocation': 1}}) | |
120 self.SetPrefs(pyauto.kContentSettingsPatternPairs, geo_exception) | |
121 self._VerifyContentExceptionUI( | |
122 ContentTypes.GEOLOCATION, 'http://maps.google.com:80', | |
123 Behaviors.ALLOW) | |
124 geo_exception = ( | |
125 {'http://maps.google.com:80,http://maps.google.com:80': | |
126 {'geolocation': 3}}) | |
127 self.SetPrefs(pyauto.kContentSettingsPatternPairs, geo_exception) | |
128 self._VerifyContentExceptionUI( | |
129 ContentTypes.GEOLOCATION, 'http://maps.google.com:80', Behaviors.ASK) | |
130 | |
131 def testAddNewExceptionUI(self): | |
132 """Verify new exception added for hostname pattern and behavior in UI.""" | |
133 content_type = ContentTypes.PLUGINS | |
134 page = settings.ManageExceptionsPage.FromNavigation( | |
135 self._driver, content_type) | |
136 | |
137 pattern, behavior = ('bing.com', Behaviors.BLOCK) | |
138 page.AddNewException(pattern, behavior) | |
139 self.assertEqual(page.GetExceptions()[pattern], Behaviors.BLOCK, | |
140 msg='The behavior "%s" was not added for pattern "%s"' | |
141 % (behavior, pattern)) | |
142 | |
143 def testChangeExceptionBehaviorUI(self): | |
144 """Verify behavior for hostname pattern is changed in the UI.""" | |
145 content_type = ContentTypes.PLUGINS | |
146 page = settings.ManageExceptionsPage.FromNavigation( | |
147 self._driver, content_type) | |
148 | |
149 pattern, behavior = ('bing.com', Behaviors.BLOCK) | |
150 page.AddNewException(pattern, behavior) | |
151 new_behavior = Behaviors.ALLOW | |
152 page.SetBehaviorForPattern(pattern, new_behavior) | |
153 self.assertEqual(page.GetExceptions()[pattern], Behaviors.ALLOW, | |
154 msg='The behavior for "%s" did not change: "%s"' | |
155 % (pattern, behavior)) | |
156 | |
157 def testDeleteExceptionUI(self): | |
158 """Verify exception deleted for hostname pattern and behavior in the UI.""" | |
159 content_type = ContentTypes.PLUGINS | |
160 page = settings.ManageExceptionsPage.FromNavigation( | |
161 self._driver, content_type) | |
162 | |
163 pattern, behavior = ('bing.com', Behaviors.BLOCK) | |
164 page.AddNewException(pattern, behavior) | |
165 self.assertEqual(page.GetExceptions()[pattern], Behaviors.BLOCK, | |
166 msg='The behavior "%s" was not added for pattern "%s"' | |
167 % (behavior, pattern)) | |
168 page.DeleteException(pattern) | |
169 self.assertEqual(page.GetExceptions().get(pattern, KeyError), KeyError, | |
170 msg='Pattern "%s" was not deleted' % pattern) | |
171 | |
172 def testNoInitialLineEntryInUI(self): | |
173 """Verify no initial line entry is displayed in UI.""" | |
174 # Ask for permission when site wants to track. | |
175 self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 3) | |
176 self.assertEqual( | |
177 3, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting)) | |
178 page = settings.ManageExceptionsPage.FromNavigation( | |
179 self._driver, ContentTypes.GEOLOCATION) | |
180 self.assertEqual(0, len(page.GetExceptions())) | |
181 | |
182 def testCorrectCookiesSessionInUI(self): | |
183 """Verify exceptions for cookies in UI list entry.""" | |
184 # Block cookies for for a session for google.com. | |
185 self.SetPrefs(pyauto.kContentSettingsPatternPairs, | |
186 {'http://google.com:80': {'cookies': 2}}) | |
187 self._VerifyContentExceptionUI( | |
188 ContentTypes.COOKIES, 'http://google.com:80', Behaviors.BLOCK) | |
189 | |
190 def testInitialLineEntryInIncognitoUI(self): | |
191 """Verify initial line entry is displayed in Incognito UI.""" | |
192 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) # Display incognito list. | |
193 page = settings.ManageExceptionsPage.FromNavigation( | |
194 self._driver, ContentTypes.PLUGINS) | |
195 self.assertEqual(1, len(page.GetExceptions(incognito=True))) | |
196 | |
197 def testIncognitoExceptionsEntryCorrectlyDisplayed(self): | |
198 """Verify exceptions entry is correctly displayed in the incognito UI.""" | |
199 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) # Display incognito list. | |
200 page = settings.ManageExceptionsPage.FromNavigation( | |
201 self._driver, ContentTypes.PLUGINS) | |
202 pattern, behavior = ('http://maps.google.com:80', Behaviors.BLOCK) | |
203 page.AddNewException(pattern, behavior, incognito=True) | |
204 self._VerifyContentExceptionUI( | |
205 ContentTypes.PLUGINS, 'http://maps.google.com:80', | |
206 Behaviors.BLOCK, incognito=True) | |
207 | |
208 def testSetCookieAndDeleteInContentSettings(self): | |
209 """Verify a cookie can be deleted in the Content Settings UI.""" | |
210 # Create a cookie. | |
211 cookie_dict = { | |
212 'name': 'test_cookie', | |
213 'value': 'test_value', | |
214 'expiry': time.time() + 30, | |
215 } | |
216 site = '127.0.0.1' | |
217 self.NavigateToURL(self.GetHttpURLForDataPath('google', 'google.html')) | |
218 self._driver.add_cookie(cookie_dict) | |
219 page = settings.CookiesAndSiteDataSettings.FromNavigation(self._driver) | |
220 page.DeleteSiteData(site) | |
221 self.assertTrue(site not in page.GetSiteNameList(), | |
222 'Site "%s" was not deleted.' % site) | |
223 | |
224 def testRemoveMailProtocolHandler(self): | |
225 """Verify the mail protocol handler is added and removed successfully.""" | |
226 url = self.GetHttpURLForDataPath('settings', 'protocol_handler.html') | |
227 self.NavigateToURL(url) | |
228 # Returns a dictionary with the mail handler that was asked for | |
229 # registration. | |
230 asked_handler_dict = self._driver.execute_script( | |
231 'return registerMailClient()') | |
232 self.PerformActionOnInfobar( | |
233 'accept', infobar_index=test_utils.WaitForInfobarTypeAndGetIndex( | |
234 self, self.INFOBAR_TYPE)) | |
235 self._driver.find_element_by_id('test_mail_protocol').click() | |
236 | |
237 protocol_handlers_list = ( | |
238 self.GetPrefsInfo().Prefs(pyauto.kRegisteredProtocolHandlers)) | |
239 registered_mail_handler = {} | |
240 for handler_dict in protocol_handlers_list: | |
241 if (handler_dict['protocol'] == 'mailto' and | |
242 handler_dict['url'] == asked_handler_dict['url'] and | |
243 handler_dict['title'] == asked_handler_dict['title'] and | |
244 handler_dict.get('default')): | |
245 registered_mail_handler = handler_dict | |
246 break | |
247 # Verify the mail handler is registered as asked. | |
248 self.assertNotEqual( | |
249 registered_mail_handler, {}, | |
250 msg='Mail protocol handler was not registered correctly.') | |
251 # Verify the registered mail handler works as expected. | |
252 self.assertTrue( | |
253 self._driver.execute_script( | |
254 'return doesQueryConformsToProtocol("%s", "%s")' | |
255 % (asked_handler_dict['query_key'], | |
256 asked_handler_dict['query_value'])), | |
257 msg='Mail protocol did not register correctly.') | |
258 | |
259 self._driver.get('chrome://settings-frame/handlers') | |
260 # There are 3 DIVs in a handler entry. The last one acts as a remove button. | |
261 # The remove button is also equivalent to setting the site to NONE. | |
262 self._driver.find_element_by_id('handlers-list').\ | |
263 find_element_by_xpath('.//div[@role="listitem"]').\ | |
264 find_element_by_xpath('.//div[@class="handlers-site-column"]').\ | |
265 find_element_by_xpath('.//option[@value="-1"]').click() | |
266 | |
267 self._driver.get(url) | |
268 self._driver.find_element_by_id('test_mail_protocol').click() | |
269 self.assertEqual(url, self._driver.current_url, | |
270 msg='Mail protocol still registered.') | |
271 | |
272 class BasicSettingsUITest(pyauto.PyUITest): | |
273 """Testcases for uber page basic settings UI.""" | |
274 | |
275 def setUp(self): | |
276 pyauto.PyUITest.setUp(self) | |
277 self._driver = self.NewWebDriver() | |
278 | |
279 def Debug(self): | |
280 """chrome://plugins test debug method. | |
281 | |
282 This method will not run automatically. | |
283 """ | |
284 driver = self.NewWebDriver() | |
285 page = settings.BasicSettingsPage.FromNavigation(driver) | |
286 import pdb | |
287 pdb.set_trace() | |
288 | |
289 def testOnStartupSettings(self): | |
290 """Verify user can set startup options.""" | |
291 page = settings.BasicSettingsPage.FromNavigation(self._driver) | |
292 page.SetOnStartupOptions(RestoreOnStartupType.NEW_TAB_PAGE) | |
293 self.assertEqual(RestoreOnStartupType.NEW_TAB_PAGE, | |
294 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
295 page.SetOnStartupOptions(RestoreOnStartupType.RESTORE_SESSION) | |
296 self.assertEqual(RestoreOnStartupType.RESTORE_SESSION, | |
297 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
298 page.SetOnStartupOptions(RestoreOnStartupType.RESTORE_URLS) | |
299 self.assertEqual(RestoreOnStartupType.RESTORE_URLS, | |
300 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
301 | |
302 def testSetStartupPages(self): | |
303 """Verify user can add urls for startup pages.""" | |
304 page = settings.BasicSettingsPage.FromNavigation(self._driver) | |
305 for url in ['www.google.com', 'http://www.amazon.com', 'ebay.com']: | |
306 page.AddStartupPage(url) | |
307 self.assertEqual(RestoreOnStartupType.RESTORE_URLS, | |
308 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
309 startup_urls = self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup) | |
310 self.assertEqual(startup_urls[0], 'http://www.google.com/') | |
311 self.assertEqual(startup_urls[1], 'http://www.amazon.com/') | |
312 self.assertEqual(startup_urls[2], 'http://ebay.com/') | |
313 | |
314 def testUseCurrentPagesForStartup(self): | |
315 """Verify user can start up browser using current pages.""" | |
316 page = settings.BasicSettingsPage.FromNavigation(self._driver) | |
317 self.OpenNewBrowserWindow(True) | |
318 url1 = self.GetHttpURLForDataPath('title2.html') | |
319 url2 = self.GetHttpURLForDataPath('title3.html') | |
320 self.NavigateToURL(url1, 1, 0) | |
321 self.AppendTab(pyauto.GURL(url2), 1) | |
322 title_list = ['Title Of Awesomeness', | |
323 'Title Of More Awesomeness'] | |
324 page.UseCurrentPageForStartup(title_list) | |
325 page.VerifyStartupURLs(title_list) | |
326 self.assertEqual(RestoreOnStartupType.RESTORE_URLS, | |
327 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
328 startup_urls = self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup) | |
329 self.assertEqual(len(startup_urls), 3) | |
330 self.assertEqual(startup_urls[1], url1) | |
331 self.assertEqual(startup_urls[2], url2) | |
332 | |
333 def testCancelStartupURLSetting(self): | |
334 """Verify canceled start up URLs settings are not saved.""" | |
335 page = settings.BasicSettingsPage.FromNavigation(self._driver) | |
336 for url in ['www.google.com', 'http://www.amazon.com']: | |
337 page.CancelStartupURLSetting(url) | |
338 startup_urls = self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup) | |
339 self.assertEqual(len(startup_urls), 0) | |
340 | |
341 | |
342 if __name__ == '__main__': | |
343 pyauto_functional.Main() | |
OLD | NEW |