summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/extensions.py
blob: f158efd2432473b63699ab908e8c2260c0d59a76 (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
#!/usr/bin/env python
# Copyright (c) 2011 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.

"""
This module is a simple qa tool that installs extensions and tests whether the
browser crashes while visiting a list of urls.

Usage: python extensions.py -v

Note: This assumes that there is a directory of extensions called
'extensions-tool' and that there is a file of newline-separated urls to visit
called 'urls.txt' in the data directory.
"""

import glob
import logging
import os
import sys

import pyauto_functional # must be imported before pyauto
import pyauto


class ExtensionsTest(pyauto.PyUITest):
  """Test of extensions."""

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

    This method is not run automatically.
    """
    while True:
      raw_input('Interact with the browser and hit <enter> to dump history.. ')
      print '*' * 20
      self.pprint(self.GetExtensionsInfo())

  def _GetInstalledExtensionIds(self):
    return [extension['id'] for extension in self.GetExtensionsInfo()]

  def _ReturnCrashingExtensions(self, extensions, group_size, top_urls):
    """Install the given extensions in groups of group_size and return the
       group of extensions that crashes (if any).

    Args:
      extensions: A list of extensions to install.
      group_size: The number of extensions to install at one time.
      top_urls: The list of top urls to visit.

    Returns:
      The extensions in the crashing group or None if there is no crash.
    """
    curr_extension = 0
    num_extensions = len(extensions)
    self.RestartBrowser()
    orig_extension_ids = self._GetInstalledExtensionIds()

    while curr_extension < num_extensions:
      logging.debug('New group of %d extensions.' % group_size)
      group_end = curr_extension + group_size
      for extension in extensions[curr_extension:group_end]:
        logging.debug('Installing extension: %s' % extension)
        self.InstallExtension(extension)

      for url in top_urls:
        self.NavigateToURL(url)

      def _LogAndReturnCrashing():
        crashing_extensions = extensions[curr_extension:group_end]
        logging.debug('Crashing extensions: %s' % crashing_extensions)
        return crashing_extensions

      # If the browser has crashed, return the extensions in the failing group.
      try:
        num_browser_windows = self.GetBrowserWindowCount()
      except:
        return _LogAndReturnCrashing()
      else:
        if not num_browser_windows:
          return _LogAndReturnCrashing()
        else:
          # Uninstall all extensions that aren't installed by default.
          new_extension_ids = [id for id in self._GetInstalledExtensionIds()
                               if id not in orig_extension_ids]
          for extension_id in new_extension_ids:
            self.UninstallExtensionById(extension_id)

      curr_extension = group_end

    # None of the extensions crashed.
    return None

  def _GetExtensionInfoById(self, extensions, id):
    for x in extensions:
      if x['id'] == id:
        return x
    return None

  def ExtensionCrashes(self):
    """Add top extensions; confirm browser stays up when visiting top urls"""
    # TODO: provide a way in pyauto to pass args to a test - take these as args
    extensions_dir = os.path.join(self.DataDir(), 'extensions-tool')
    urls_file = os.path.join(self.DataDir(), 'urls.txt')

    assert os.path.exists(extensions_dir), \
           'The dir "%s" must exist' % os.path.abspath(extensions_dir)
    assert os.path.exists(urls_file), \
           'The file "%s" must exist' % os.path.abspath(urls_file)

    num_urls_to_visit = 100
    extensions_group_size = 20

    top_urls = [l.rstrip() for l in
                open(urls_file).readlines()[:num_urls_to_visit]]

    failed_extensions = glob.glob(os.path.join(extensions_dir, '*.crx'))
    group_size = extensions_group_size

    while(group_size and failed_extensions):
      failed_extensions = self._ReturnCrashingExtensions(
          failed_extensions, group_size, top_urls)
      group_size = group_size // 2

    self.assertFalse(failed_extensions,
                     'Extension(s) in failing group: %s' % failed_extensions)

  def testGetExtensionPermissions(self):
    """Ensures we can retrieve the host/api permissions for an extension.

    This test assumes that the 'Bookmark Manager' extension exists in a fresh
    profile.
    """
    extensions_info = self.GetExtensionsInfo()
    bm_exts = [x for x in extensions_info if x['name'] == 'Bookmark Manager']
    self.assertTrue(bm_exts, msg='Could not find info for the Bookmark '
                                 'Manager extension.')
    ext = bm_exts[0]

    permissions_host = ext['host_permissions']
    self.assertTrue(len(permissions_host) == 2 and
                    'chrome://favicon/*' in permissions_host and
                    'chrome://resources/*' in permissions_host,
                    msg='Unexpected host permissions information.')

    permissions_api = ext['api_permissions']
    self.assertTrue(len(permissions_api) == 3 and
                    'bookmarks' in permissions_api and
                    'experimental' in permissions_api and
                    'tabs' in permissions_api,
                    msg='Unexpected host permissions information.')

  def testSetExtensionStates(self):
    """Test setting different extension states."""
    crx_file_path = os.path.abspath(
        os.path.join(self.DataDir(), 'extensions', 'google_talk.crx'))
    ext_id = self.InstallExtension(crx_file_path)

    # Verify extension is in default state.
    extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id)
    self.assertTrue(extension['is_enabled'])
    self.assertFalse(extension['allowed_in_incognito'])

    # Disable the extension and verify.
    self.SetExtensionStateById(ext_id, enable=False, allow_in_incognito=False)
    extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id)
    self.assertFalse(extension['is_enabled'])

    # Enable extension and verify.
    self.SetExtensionStateById(ext_id, enable=True, allow_in_incognito=False)
    extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id)
    self.assertTrue(extension['is_enabled'])

    # Allow extension in incognito mode and verify.
    self.SetExtensionStateById(ext_id, enable=True, allow_in_incognito=True)
    extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id)
    self.assertTrue(extension['allowed_in_incognito'])

    # Disallow extension in incognito mode and verify.
    self.SetExtensionStateById(ext_id, enable=True, allow_in_incognito=False)
    extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id)
    self.assertFalse(extension['allowed_in_incognito'])

  def testTriggerBrowserAction(self):
    """Test triggering browser action."""
    dir_path = os.path.abspath(
        os.path.join(self.DataDir(), 'extensions', 'trigger_actions',
                     'browser_action'))
    ext_id = self.InstallExtension(dir_path)

    self.NavigateToURL(self.GetFileURLForDataPath('simple.html'))

    self.TriggerBrowserActionById(ext_id)

    # Verify that the browser action turned the background red.
    self.assertTrue(self.WaitUntil(
        lambda: self.GetDOMValue('document.body.style.backgroundColor'),
        expect_retval='red'),
        msg='Browser action was not triggered.')

  def testTriggerBrowserActionWithPopup(self):
    """Test triggering browser action that shows a popup."""
    dir_path = os.path.abspath(
        os.path.join(self.DataDir(), 'extensions', 'trigger_actions',
                     'browser_action_popup'))
    ext_id = self.InstallExtension(dir_path)

    self.TriggerBrowserActionById(ext_id)

    # Verify that the extension popup is displayed.
    popup = self.WaitUntilExtensionViewLoaded(
        view_type='EXTENSION_POPUP')
    self.assertTrue(popup,
        msg='Browser action failed to display the popup (views=%s).' %
        self.GetBrowserInfo()['extension_views'])

  def testTriggerPageAction(self):
    """Test triggering page action."""
    dir_path = os.path.abspath(
        os.path.join(self.DataDir(), 'extensions', 'trigger_actions',
                     'page_action'))
    ext_id = self.InstallExtension(dir_path)

    # Page action icon is displayed when a tab is created.
    self.NavigateToURL(self.GetFileURLForDataPath('simple.html'))
    self.AppendTab(pyauto.GURL('chrome://newtab'))
    self.ActivateTab(0)
    self.assertTrue(self.WaitUntil(
        lambda: ext_id in
        self.GetBrowserInfo()['windows'][0]['visible_page_actions']),
        msg='Page action icon is not visible.')

    self.TriggerPageActionById(ext_id)

    # Verify that page action turned the background red.
    self.assertTrue(self.WaitUntil(
        lambda: self.GetDOMValue('document.body.style.backgroundColor'),
        expect_retval='red'),
        msg='Page action was not triggered.')

  def testTriggerPageActionWithPopup(self):
    """Test triggering page action that shows a popup."""
    dir_path = os.path.abspath(
        os.path.join(self.DataDir(), 'extensions', 'trigger_actions',
                     'page_action_popup'))
    ext_id = self.InstallExtension(dir_path)

    # Page action icon is displayed when a tab is created.
    self.AppendTab(pyauto.GURL('chrome://newtab'))
    self.ActivateTab(0)
    self.assertTrue(self.WaitUntil(
        lambda: ext_id in
        self.GetBrowserInfo()['windows'][0]['visible_page_actions']),
        msg='Page action icon is not visible.')

    self.TriggerPageActionById(ext_id)

    # Verify that the extension popup is displayed.
    popup = self.WaitUntilExtensionViewLoaded(
        view_type='EXTENSION_POPUP')
    self.assertTrue(popup,
        msg='Page action failed to display the popup (views=%s).' %
        self.GetBrowserInfo()['extension_views'])

  def testAdblockExtensionCrash(self):
    """Test AdBlock extension successfully installed and enabled, and do not
    cause browser crash.
    """
    crx_file_path = os.path.abspath(
        os.path.join(self.DataDir(), 'extensions', 'adblock.crx'))
    ext_id = self.InstallExtension(crx_file_path)

    self.RestartBrowser(clear_profile=False)
    extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id)
    self.assertTrue(extension['is_enabled'])
    self.assertFalse(extension['allowed_in_incognito'])


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