diff options
author | frankf@google.com <frankf@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-23 18:42:17 +0000 |
---|---|---|
committer | frankf@google.com <frankf@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-23 18:42:17 +0000 |
commit | 0e0bbb14a9751de19c3ac543c041c9df68890afb (patch) | |
tree | 9f30d96871f95ce089a6bc4addd318f17fedc66f /chrome/test | |
parent | efa9aa8e051cd37223cbfb641462a7743aacacbb (diff) | |
download | chromium_src-0e0bbb14a9751de19c3ac543c041c9df68890afb.zip chromium_src-0e0bbb14a9751de19c3ac543c041c9df68890afb.tar.gz chromium_src-0e0bbb14a9751de19c3ac543c041c9df68890afb.tar.bz2 |
Add the functionality to inject JavaScript into ExtensionHosts.
BUG=chromium-os:17902
TEST=
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=97569
Review URL: http://codereview.chromium.org/7637020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97893 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/data/extensions/js_injection_background/bg.html | 10 | ||||
-rw-r--r-- | chrome/test/data/extensions/js_injection_background/manifest.json | 10 | ||||
-rw-r--r-- | chrome/test/functional/execute_javascript.py | 42 | ||||
-rw-r--r-- | chrome/test/functional/memory.py | 2 | ||||
-rw-r--r-- | chrome/test/functional/process_count.py | 3 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 51 |
6 files changed, 111 insertions, 7 deletions
diff --git a/chrome/test/data/extensions/js_injection_background/bg.html b/chrome/test/data/extensions/js_injection_background/bg.html new file mode 100644 index 0000000..b629c2a --- /dev/null +++ b/chrome/test/data/extensions/js_injection_background/bg.html @@ -0,0 +1,10 @@ +<html> + <script> + var bool_var = true; + var int_var = 42; + var str_var = "foo"; + </script> + <body> + <input id="myinput"></input> + </body> +</html> diff --git a/chrome/test/data/extensions/js_injection_background/manifest.json b/chrome/test/data/extensions/js_injection_background/manifest.json new file mode 100644 index 0000000..a6d3076 --- /dev/null +++ b/chrome/test/data/extensions/js_injection_background/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "js_injection_background", + "description": "Tests JS injection into an extension's background page. + The name of a DOM node in the background page is returned and verified.", + "version": "0.1", + "background_page": "bg.html", + "browser_action": { + "default_title": "Browser Action" + } +} diff --git a/chrome/test/functional/execute_javascript.py b/chrome/test/functional/execute_javascript.py index 1173d4a..af7a9d0 100644 --- a/chrome/test/functional/execute_javascript.py +++ b/chrome/test/functional/execute_javascript.py @@ -12,6 +12,11 @@ from pyauto import PyUITest class ExecuteJavascriptTest(PyUITest): + def _GetExtensionInfoById(self, extensions, id): + for x in extensions: + if x['id'] == id: + return x + return None def testExecuteJavascript(self): self.NavigateToURL(self.GetFileURLForDataPath( @@ -28,6 +33,43 @@ class ExecuteJavascriptTest(PyUITest): v = self.GetDOMValue('document.getElementById("myinput").nodeName') self.assertEqual(v, 'INPUT') + def testExecuteJavascriptInExtension(self): + """Test we can inject JavaScript into an extension.""" + dir_path = os.path.abspath( + os.path.join(self.DataDir(), 'extensions', 'js_injection_background')) + ext_id = self.InstallExtension(dir_path, False); + self.assertTrue(ext_id, msg='Failed to install extension: %s.' % dir_path) + + # Verify extension is enabled. + extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id) + self.assertTrue(extension['is_enabled'], + msg='Extension was disabled by default') + + # Get the background page's view. + info = self.GetBrowserInfo()['extension_views'] + view = [x for x in info if + x['extension_id'] == ext_id and + x['view_type'] == 'EXTENSION_BACKGROUND_PAGE'] + self.assertEqual(1, len(view), + msg='problematic background view: view = %s.' % view) + background_view = view[0] + + # Get values from background page's DOM + v = self.ExecuteJavascriptInRenderView( + 'window.domAutomationController.send(' + 'document.getElementById("myinput").nodeName)', background_view['view']) + self.assertEqual(v, 'INPUT', + msg='Incorrect value returned (v = %s).' % v) + v = self.ExecuteJavascriptInRenderView( + 'window.domAutomationController.send(bool_var)', background_view['view']) + self.assertEqual(v, True, msg='Incorrect value returned (v = %s).' % v) + v = self.ExecuteJavascriptInRenderView( + 'window.domAutomationController.send(int_var)', background_view['view']) + self.assertEqual(v, 42, msg='Incorrect value returned (v = %s).' % v) + v = self.ExecuteJavascriptInRenderView( + 'window.domAutomationController.send(str_var)', background_view['view']) + self.assertEqual(v, 'foo', msg='Incorrect value returned (v = %s).' % v) + if __name__ == '__main__': pyauto_functional.Main() diff --git a/chrome/test/functional/memory.py b/chrome/test/functional/memory.py index a06eb23..bf7e044 100644 --- a/chrome/test/functional/memory.py +++ b/chrome/test/functional/memory.py @@ -56,7 +56,7 @@ class MemoryTest(pyauto.PyUITest): The integer process identifier (PID) for the specified process, or None if the PID cannot be identified. """ - info = self.GetBrowserInfo()['extension_processes'] + info = self.GetBrowserInfo()['extension_views'] pid = [x['pid'] for x in info if x['name'] == '%s' % name] if pid: return pid[0] diff --git a/chrome/test/functional/process_count.py b/chrome/test/functional/process_count.py index f870e10..f45464e 100644 --- a/chrome/test/functional/process_count.py +++ b/chrome/test/functional/process_count.py @@ -27,8 +27,7 @@ class ProcessCountTest(pyauto.PyUITest): actual_count = ( 1 + # Browser process. sum([len(tab_info['tabs']) for tab_info in info['windows']]) + - len(info['child_processes']) + - len(info['extension_processes'])) + len(info['child_processes']) + len(info['extension_views'])) self.assertEqual(actual_count, expected_count, msg='Number of processes should be %d, but was %d.' % diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index 8a2e3d4..fe2143a 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -1253,10 +1253,16 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): u'child_processes': [ { u'name': u'Shockwave Flash', u'pid': 93766, u'type': u'Plug-in'}], - # There's one extension process per extension. - u'extension_processes': [ - { u'name': u'Webpage Screenshot', u'pid': 93938}, - { u'name': u'Google Voice (by Google)', u'pid': 93852}], + u'extension_views': [ { + u'name': u'Webpage Screenshot', + u'pid': 93938, + u'extension_id': u'dgcoklnmbeljaehamekjpeidmbicddfj', + u'url': u'chrome-extension://dgcoklnmbeljaehamekjpeidmbicddfj/' + 'bg.html', + u'view': { + u'render_process_id': 2, + u'render_view_id': 1}, + u'view_type': u'EXTENSION_BACKGROUND_PAGE'}] u'properties': { u'BrowserProcessExecutableName': u'Chromium', u'BrowserProcessExecutablePath': u'Chromium.app/Contents/MacOS/' @@ -2234,6 +2240,43 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): } return self._GetResultFromJSONRequest(cmd_dict, windex=windex) + def ExecuteJavascriptInRenderView(self, js, view, frame_xpath=''): + """Executes a script in the specified frame of an render view. + + The invoked javascript function must send a result back via the + domAutomationController.send function, or this function will never return. + + Args: + js: script to be executed + view: A dictionary representing a unique id for the render view as + returned for example by + self.GetBrowserInfo()['extension_views'][]['view']. + Example: + { 'render_process_id': 1, + 'render_view_id' : 2} + + frame_xpath: XPath of the frame to execute the script. Default is no + frame. Example: + '//frames[1]' + + Returns: + a value that was sent back via the domAutomationController.send method + + Raises: + pyauto_errors.JSONInterfaceError if the automation call returns an error. + """ + cmd_dict = { + 'command': 'ExecuteJavascriptInRenderView', + 'javascript' : js, + 'view' : view, + 'frame_xpath' : frame_xpath, + } + result = self._GetResultFromJSONRequest(cmd_dict)['result'] + # Wrap result in an array before deserializing because valid JSON has an + # array or an object as the root. + json_string = '[' + result + ']' + return json.loads(json_string)[0] + def CallJavascriptFunc(self, function, args=[], tab_index=0, windex=0): """Executes a script which calls a given javascript function. |