summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/prefs_ui.py
blob: 5e665d7cb1b47ede84013b248f6371bf736ba4c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import time

import pyauto_functional  # Must be imported before pyauto
import pyauto
import test_utils

from webdriver_pages import settings
from webdriver_pages.settings import Behaviors, ContentTypes
from webdriver_pages.settings import RestoreOnStartupType


class PrefsUITest(pyauto.PyUITest):
  """TestCase for Preferences UI."""

  INFOBAR_TYPE = 'rph_infobar'

  def setUp(self):
    pyauto.PyUITest.setUp(self)
    self._driver = self.NewWebDriver()

  def Debug(self):
    """Test method for experimentation.

    This method will not run automatically.
    """
    driver = self.NewWebDriver()
    page = settings.ContentSettingsPage.FromNavigation(driver)
    import pdb
    pdb.set_trace()

  def _GetGeolocationContentSettingsBehavior(self):
    """Get the Content Settings behavior for Geolocation.

    Returns:
      The exceptions behavior for the specified content type.
      The content type is the available content setting available.
    """
    behavior_key = self.GetPrefsInfo().Prefs(
        pyauto.kGeolocationDefaultContentSetting)
    behaviors_dict = {1: 'ALLOW', 2: 'BLOCK', 3: 'ASK'}
    self.assertTrue(
        behavior_key in behaviors_dict,
        msg=('Invalid default behavior key "%s" for "geolocation" content' %
             behavior_key))
    return behaviors_dict[behavior_key]

  def _VerifyContentExceptionUI(self, content_type, hostname_pattern, behavior,
                                incognito=False):
    """Find hostname pattern and behavior within UI on content exceptions page.

    Args:
      content_type: The string content settings type to manage.
      hostname_pattern: The URL or pattern associated with the behavior.
      behavior: The exception to allow or block the hostname.
      incognito: Incognito list displayed on exceptions settings page.
                 Default to False.
    """
    page = settings.ManageExceptionsPage.FromNavigation(
        self._driver, content_type)
    self.assertTrue(page.GetExceptions(incognito).has_key(hostname_pattern),
                     msg=('No displayed host name matches pattern "%s"'
                          % hostname_pattern))
    self.assertEqual(behavior, page.GetExceptions(incognito)[hostname_pattern],
                     msg=('Displayed behavior "%s" does not match behavior "%s"'
                          % (page.GetExceptions(incognito)[hostname_pattern],
                             behavior)))

  def testLocationSettingOptionsUI(self):
    """Verify the location options setting UI.

    Set the options through the UI using webdriver and verify the settings in
    pyAuto.
    """
    page = settings.ContentSettingsPage.FromNavigation(self._driver)
    page.SetContentTypeOption(ContentTypes.GEOLOCATION, Behaviors.ALLOW)
    self.assertEqual(
        1, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting))
    page.SetContentTypeOption(ContentTypes.GEOLOCATION, Behaviors.BLOCK)
    self.assertEqual(
        2, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting))
    page.SetContentTypeOption(ContentTypes.GEOLOCATION, Behaviors.ASK)
    self.assertEqual(
        3, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting))

  def testBehaviorValueCorrectlyDisplayed(self):
    """Verify the set behavior value is correctly displayed."""
    # Block all sites.
    self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 2)
    self.assertEqual(
        self._GetGeolocationContentSettingsBehavior(), Behaviors.BLOCK.upper(),
        msg='The behavior was incorrectly set.')
    # Allow all sites.
    self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 1)
    self.assertEqual(
        self._GetGeolocationContentSettingsBehavior(), Behaviors.ALLOW.upper(),
        msg='The behavior was incorrectly set.')
    # Ask for permission when site wants to track.
    self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 3)
    self.assertEqual(
        self._GetGeolocationContentSettingsBehavior(), Behaviors.ASK.upper(),
        msg='The behavior was incorrectly set.')

  def testExceptionsEntryCorrectlyDisplayed(self):
    """Verify the exceptions line entry is correctly displayed in the UI."""
    geo_exception = (
        {'http://maps.google.com:80,http://maps.google.com:80':
            {'geolocation': 2}})
    self.SetPrefs(pyauto.kContentSettingsPatternPairs, geo_exception)
    self._VerifyContentExceptionUI(
        ContentTypes.GEOLOCATION, 'http://maps.google.com:80',
        Behaviors.BLOCK)
    geo_exception = (
        {'http://maps.google.com:80,http://maps.google.com:80':
            {'geolocation': 1}})
    self.SetPrefs(pyauto.kContentSettingsPatternPairs, geo_exception)
    self._VerifyContentExceptionUI(
        ContentTypes.GEOLOCATION, 'http://maps.google.com:80',
        Behaviors.ALLOW)
    geo_exception = (
        {'http://maps.google.com:80,http://maps.google.com:80':
            {'geolocation': 3}})
    self.SetPrefs(pyauto.kContentSettingsPatternPairs, geo_exception)
    self._VerifyContentExceptionUI(
        ContentTypes.GEOLOCATION, 'http://maps.google.com:80', Behaviors.ASK)

  def testAddNewExceptionUI(self):
    """Verify new exception added for hostname pattern and behavior in UI."""
    content_type = ContentTypes.PLUGINS
    page = settings.ManageExceptionsPage.FromNavigation(
        self._driver, content_type)

    pattern, behavior = ('bing.com', Behaviors.BLOCK)
    page.AddNewException(pattern, behavior)
    self.assertEqual(page.GetExceptions()[pattern], Behaviors.BLOCK,
                     msg='The behavior "%s" was not added for pattern "%s"'
                     % (behavior, pattern))

  def testChangeExceptionBehaviorUI(self):
    """Verify behavior for hostname pattern is changed in the UI."""
    content_type = ContentTypes.PLUGINS
    page = settings.ManageExceptionsPage.FromNavigation(
        self._driver, content_type)

    pattern, behavior = ('bing.com', Behaviors.BLOCK)
    page.AddNewException(pattern, behavior)
    new_behavior = Behaviors.ALLOW
    page.SetBehaviorForPattern(pattern, new_behavior)
    self.assertEqual(page.GetExceptions()[pattern], Behaviors.ALLOW,
                     msg='The behavior for "%s" did not change: "%s"'
                     % (pattern, behavior))

  def testDeleteExceptionUI(self):
    """Verify exception deleted for hostname pattern and behavior in the UI."""
    content_type = ContentTypes.PLUGINS
    page = settings.ManageExceptionsPage.FromNavigation(
        self._driver, content_type)

    pattern, behavior = ('bing.com', Behaviors.BLOCK)
    page.AddNewException(pattern, behavior)
    self.assertEqual(page.GetExceptions()[pattern], Behaviors.BLOCK,
                     msg='The behavior "%s" was not added for pattern "%s"'
                     % (behavior, pattern))
    page.DeleteException(pattern)
    self.assertEqual(page.GetExceptions().get(pattern, KeyError), KeyError,
                     msg='Pattern "%s" was not deleted' % pattern)

  def testNoInitialLineEntryInUI(self):
    """Verify no initial line entry is displayed in UI."""
    # Ask for permission when site wants to track.
    self.SetPrefs(pyauto.kGeolocationDefaultContentSetting, 3)
    self.assertEqual(
        3, self.GetPrefsInfo().Prefs(pyauto.kGeolocationDefaultContentSetting))
    page = settings.ManageExceptionsPage.FromNavigation(
        self._driver, ContentTypes.GEOLOCATION)
    self.assertEqual(0, len(page.GetExceptions()))

  def testCorrectCookiesSessionInUI(self):
    """Verify exceptions for cookies in UI list entry."""
    # Block cookies for for a session for google.com.
    self.SetPrefs(pyauto.kContentSettingsPatternPairs,
                  {'http://google.com:80': {'cookies': 2}})
    self._VerifyContentExceptionUI(
        ContentTypes.COOKIES, 'http://google.com:80', Behaviors.BLOCK)

  def testInitialLineEntryInIncognitoUI(self):
    """Verify initial line entry is displayed in Incognito UI."""
    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)  # Display incognito list.
    page = settings.ManageExceptionsPage.FromNavigation(
        self._driver, ContentTypes.PLUGINS)
    self.assertEqual(1, len(page.GetExceptions(incognito=True)))

  def testIncognitoExceptionsEntryCorrectlyDisplayed(self):
    """Verify exceptions entry is correctly displayed in the incognito UI."""
    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)  # Display incognito list.
    page = settings.ManageExceptionsPage.FromNavigation(
        self._driver, ContentTypes.PLUGINS)
    pattern, behavior = ('http://maps.google.com:80', Behaviors.BLOCK)
    page.AddNewException(pattern, behavior, incognito=True)
    self._VerifyContentExceptionUI(
        ContentTypes.PLUGINS, 'http://maps.google.com:80',
        Behaviors.BLOCK, incognito=True)

  def testSetPasswordAndDelete(self):
    """Verify a password can be deleted in the Content Settings UI."""
    password_list = []
    # Add two passwords, 1 for example0.com and 1 for example1.com.
    for i in range(2):
      # Construct a password dictionary with required fields.
      password = {
          'username_value': 'user%s@example.com'  % i,
          'password_value': 'test.password',
          'signon_realm': 'https://www.example%s.com/'  % i,
          'time': 1279650942.0,
          'origin_url': 'https://www.example%s.com/login'  % i,
          'username_element': 'username%s',
          'password_element': 'password',
          'submit_element': 'submit',
          'action_target': 'https://www.example%s.com/login/'  % i,
          'blacklist': False,
      }

      password_list.append(password)
      self.AddSavedPassword(password)

    saved_passwords = self.GetSavedPasswords()
    self.assertEquals(len(password_list), len(saved_passwords),
                      msg='Not all Passwords were saved.')
    for password in password_list:
      self.assertTrue(password in saved_passwords,
                      msg='Passwords were not saved correctly.')

    page = settings.PasswordsSettings.FromNavigation(self._driver)
    # Delete one of the passwords.
    password = password_list[1]
    page.DeleteItem(password['origin_url'], password['username_value'])
    self.assertTrue(password not in self.GetSavedPasswords(),
                    msg='Password was not deleted.')

  def testSetCookieAndDeleteInContentSettings(self):
    """Verify a cookie can be deleted in the Content Settings UI."""
    # Create a cookie.
    cookie_dict = {
        'name': 'test_cookie',
        'value': 'test_value',
        'expiry': time.time() + 30,
    }
    site = '127.0.0.1'
    self.NavigateToURL(self.GetHttpURLForDataPath('google', 'google.html'))
    self._driver.add_cookie(cookie_dict)
    page = settings.CookiesAndSiteDataSettings.FromNavigation(self._driver)
    page.DeleteSiteData(site)
    self.assertTrue(site not in page.GetSiteNameList(),
                    'Site "%s" was not deleted.'  % site)

  def testRemoveMailProtocolHandler(self):
    """Verify the mail protocol handler is added and removed successfully."""
    url = self.GetHttpURLForDataPath('settings', 'protocol_handler.html')
    self.NavigateToURL(url)
    # Returns a dictionary with the mail handler that was asked for
    # registration.
    asked_handler_dict = self._driver.execute_script(
        'return registerMailClient()')
    self.PerformActionOnInfobar(
        'accept', infobar_index=test_utils.WaitForInfobarTypeAndGetIndex(
            self, self.INFOBAR_TYPE))
    self._driver.find_element_by_id('test_mail_protocol').click()

    protocol_handlers_list = (
        self.GetPrefsInfo().Prefs(pyauto.kRegisteredProtocolHandlers))
    registered_mail_handler = {}
    for handler_dict in protocol_handlers_list:
      if (handler_dict['protocol'] == 'mailto' and
          handler_dict['url'] == asked_handler_dict['url'] and
          handler_dict['title'] == asked_handler_dict['title'] and
          handler_dict.get('default')):
        registered_mail_handler = handler_dict
        break
      # Verify the mail handler is registered as asked.
      self.assertNotEqual(
      registered_mail_handler, {},
      msg='Mail protocol handler was not registered correctly.')
      # Verify the registered mail handler works as expected.
      self.assertTrue(
          self._driver.execute_script(
              'return doesQueryConformsToProtocol("%s", "%s")'
              % (asked_handler_dict['query_key'],
                 asked_handler_dict['query_value'])),
              msg='Mail protocol did not register correctly.')

    self._driver.get('chrome://settings-frame/handlers')
    # There are 3 DIVs in a handler entry. The last one acts as a remove button.
    # The remove button is also equivalent to setting the site to NONE.
    self._driver.find_element_by_id('handlers-list').\
        find_element_by_xpath('.//div[@role="listitem"]').\
        find_element_by_xpath('.//div[@class="handlers-site-column"]').\
        find_element_by_xpath('.//option[@value="-1"]').click()

    self._driver.get(url)
    self._driver.find_element_by_id('test_mail_protocol').click()
    self.assertEqual(url, self._driver.current_url,
                     msg='Mail protocol still registered.')

class BasicSettingsUITest(pyauto.PyUITest):
  """Testcases for uber page basic settings UI."""

  def setUp(self):
    pyauto.PyUITest.setUp(self)
    self._driver = self.NewWebDriver()

  def Debug(self):
    """chrome://plugins test debug method.

    This method will not run automatically.
    """
    driver = self.NewWebDriver()
    page = settings.BasicSettingsPage.FromNavigation(driver)
    import pdb
    pdb.set_trace()

  def testOnStartupSettings(self):
    """Verify user can set startup options."""
    page = settings.BasicSettingsPage.FromNavigation(self._driver)
    page.SetOnStartupOptions(RestoreOnStartupType.NEW_TAB_PAGE)
    self.assertEqual(RestoreOnStartupType.NEW_TAB_PAGE,
        self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup))
    page.SetOnStartupOptions(RestoreOnStartupType.RESTORE_SESSION)
    self.assertEqual(RestoreOnStartupType.RESTORE_SESSION,
        self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup))
    page.SetOnStartupOptions(RestoreOnStartupType.RESTORE_URLS)
    self.assertEqual(RestoreOnStartupType.RESTORE_URLS,
        self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup))

  def testSetStartupPages(self):
    """Verify user can add urls for startup pages."""
    page = settings.BasicSettingsPage.FromNavigation(self._driver)
    for url in ['www.google.com', 'http://www.amazon.com', 'ebay.com']:
      page.AddStartupPage(url)
    self.assertEqual(RestoreOnStartupType.RESTORE_URLS,
        self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup))
    startup_urls = self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)
    self.assertEqual(startup_urls[0], 'http://www.google.com/')
    self.assertEqual(startup_urls[1], 'http://www.amazon.com/')
    self.assertEqual(startup_urls[2], 'http://ebay.com/')

  def testUseCurrentPagesForStartup(self):
    """Verify user can start up browser using current pages."""
    page = settings.BasicSettingsPage.FromNavigation(self._driver)
    self.OpenNewBrowserWindow(True)
    url1 = self.GetHttpURLForDataPath('title2.html')
    url2 = self.GetHttpURLForDataPath('title3.html')
    self.NavigateToURL(url1, 1, 0)
    self.AppendTab(pyauto.GURL(url2), 1)
    title_list = ['Title Of Awesomeness',
                  'Title Of More Awesomeness']
    page.UseCurrentPageForStartup(title_list)
    page.VerifyStartupURLs(title_list)
    self.assertEqual(RestoreOnStartupType.RESTORE_URLS,
        self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup))
    startup_urls = self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)
    self.assertEqual(len(startup_urls), 3)
    self.assertEqual(startup_urls[1], url1)
    self.assertEqual(startup_urls[2], url2)

  def testCancelStartupURLSetting(self):
    """Verify canceled start up URLs settings are not saved."""
    page = settings.BasicSettingsPage.FromNavigation(self._driver)
    for url in ['www.google.com', 'http://www.amazon.com']:
      page.CancelStartupURLSetting(url)
    startup_urls = self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)
    self.assertEqual(len(startup_urls), 0)


if __name__ == '__main__':
  pyauto_functional.Main()