1 // Copyright (c) 2012 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.
6 '/extensions/api_test/executescript/callback/test.html';
7 var testUrl
= 'http://b.com:PORT' + relativePath
;
9 chrome
.test
.getConfig(function(config
) {
10 testUrl
= testUrl
.replace(/PORT/, config
.testServer
.port
);
11 chrome
.tabs
.onUpdated
.addListener(function(tabId
, changeInfo
, tab
) {
12 if (changeInfo
.status
!= 'complete')
14 chrome
.tabs
.onUpdated
.removeListener(arguments
.callee
);
15 chrome
.test
.runTests([
17 function executeCallbackIntShouldSucceed() {
18 var scriptDetails
= {code
: '3'};
19 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
20 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
21 chrome
.test
.assertEq(3, scriptVal
[0]);
26 function executeCallbackDoubleShouldSucceed() {
27 var scriptDetails
= {code
: '1.4'};
28 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
29 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
30 chrome
.test
.assertEq(1.4, scriptVal
[0]);
35 function executeCallbackStringShouldSucceed() {
36 var scriptDetails
= {code
: '"foobar"'};
37 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
38 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
39 chrome
.test
.assertEq('foobar', scriptVal
[0]);
44 function executeCallbackTrueShouldSucceed() {
45 var scriptDetails
= {code
: 'true'};
46 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
47 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
48 chrome
.test
.assertEq(true, scriptVal
[0]);
53 function executeCallbackFalseShouldSucceed() {
54 var scriptDetails
= {code
: 'false'};
55 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
56 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
57 chrome
.test
.assertEq(false, scriptVal
[0]);
62 function executeCallbackNullShouldSucceed() {
63 var scriptDetails
= {code
: 'null'};
64 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
65 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
66 chrome
.test
.assertEq(null, scriptVal
[0]);
71 function executeCallbackArrayShouldSucceed() {
72 var scriptDetails
= {code
: '[1, "5", false, null]'};
73 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
74 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
75 chrome
.test
.assertEq([1, "5", false, null], scriptVal
[0]);
80 function executeCallbackObjShouldSucceed() {
81 var scriptDetails
= {code
: 'var obj = {"id": "foo", "bar": 9}; obj'};
82 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
83 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
84 chrome
.test
.assertEq({"id": "foo", "bar": 9}, scriptVal
[0]);
89 // DOM objects (nodes, properties, etc) should be converted to empty
90 // objects. We could try to convert them the best they can but it's
91 // undefined what that means. Ideally it'd just throw an exception but
92 // the backwards compatible ship sailed long ago.
93 function executeCallbackDOMObjShouldSucceedAndReturnNull() {
95 'document.getElementById("testDiv")',
96 'new XMLHttpRequest()',
99 ].forEach(function(expr
) {
100 chrome
.tabs
.executeScript(tabId
,
101 {code
: 'var obj = ' + expr
+ '; obj'},
102 chrome
.test
.callbackPass(function(result
) {
103 chrome
.test
.assertEq([{}], result
, 'Failed for ' + expr
);
108 // All non-integer properties are droped.
109 function executeCallbackArrayWithNonNumericFieldsShouldSucceed() {
110 var scriptDetails
= {}
111 scriptDetails
.code
= 'var arr = [1, 2]; arr.foo = "bar"; arr;';
112 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
113 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
114 chrome
.test
.assertEq([1, 2], scriptVal
[0]);
119 function executeCallbackObjWithNumericFieldsShouldSucceed() {
120 var scriptDetails
= {}
121 scriptDetails
.code
= 'var obj = {1: 1, "2": "a", "foo": "bar"}; obj;';
122 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
123 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
124 chrome
.test
.assertEq({'foo': 'bar', 1: 1, '2': 'a'}, scriptVal
[0]);
129 function executeCallbackRecursiveObjShouldSucceed() {
130 var scriptDetails
= {code
: 'var foo = {"a": 1}; foo.bar = foo; foo;'};
131 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
132 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
133 chrome
.test
.assertEq({'a': 1, 'bar': null}, scriptVal
[0]);
138 function executeCallbackRecursiveArrayShouldSucceed() {
140 {code
: 'var arr = [1, "2", 3.4]; arr.push(arr); arr;'};
141 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
142 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
143 chrome
.test
.assertEq([1, "2", 3.4, null], scriptVal
[0]);
148 function executeCallbackWindowShouldSucceed() {
149 var scriptDetails
= {code
: 'window;'};
150 chrome
.tabs
.executeScript(tabId
, scriptDetails
, function(scriptVal
) {
151 chrome
.tabs
.get(tabId
, chrome
.test
.callbackPass(function(tab
) {
152 // Test passes as long as the window was converted in some form and
154 chrome
.test
.assertFalse(null == scriptVal
[0]);
160 chrome
.tabs
.create({ url
: testUrl
});