1 // Copyright (c) 2011 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.
5 var pass
= chrome
.test
.callbackPass
;
6 var fail
= chrome
.test
.callbackFail
;
7 var assertEq
= chrome
.test
.assertEq
;
8 var assertTrue
= chrome
.test
.assertTrue
;
10 '/extensions/api_test/executescript/basic/test_executescript.html';
11 var testUrl
= 'http://a.com:PORT' + relativePath
;
12 var testFailureUrl
= 'http://b.com:PORT' + relativePath
;
13 var firstEnter
= true;
15 chrome
.test
.getConfig(function(config
) {
16 testUrl
= testUrl
.replace(/PORT/, config
.testServer
.port
);
17 testFailureUrl
= testFailureUrl
.replace(/PORT/, config
.testServer
.port
);
19 chrome
.tabs
.onUpdated
.addListener(function(tabId
, changeInfo
, tab
) {
20 if (changeInfo
.status
!= 'complete')
27 chrome
.test
.runTests([
29 function executeJavaScriptCodeShouldSucceed() {
31 script_file
.code
= "document.title = 'executeScript';";
32 chrome
.tabs
.executeScript(tabId
, script_file
, function() {
33 chrome
.tabs
.get(tabId
, pass(function(tab
) {
34 assertEq('executeScript', tab
.title
);
39 function executeJavaScriptFileShouldSucceed() {
41 script_file
.file
= 'script1.js';
42 chrome
.tabs
.executeScript(tabId
, script_file
, function() {
43 chrome
.tabs
.get(tabId
, pass(function(tab
) {
44 assertEq('executeScript1', tab
.title
);
49 function insertCSSTextShouldSucceed() {
51 css_file
.code
= "p {display:none;}";
52 chrome
.tabs
.insertCSS(tabId
, css_file
, function() {
54 script_file
.file
= 'script3.js';
55 chrome
.tabs
.executeScript(tabId
, script_file
, function() {
56 chrome
.tabs
.get(tabId
, pass(function(tab
) {
57 assertEq('none', tab
.title
);
63 function insertCSSFileShouldSucceed() {
65 css_file
.file
= '1.css';
66 chrome
.tabs
.insertCSS(tabId
, css_file
, function() {
68 script_file
.file
= 'script2.js';
69 chrome
.tabs
.executeScript(tabId
, script_file
, function() {
70 chrome
.tabs
.get(tabId
, pass(function(tab
) {
71 assertEq('block', tab
.title
);
77 function insertCSSTextShouldNotAffectDOM() {
78 chrome
.tabs
.insertCSS(tabId
, {code
: 'p {display: none}'}, function() {
79 chrome
.tabs
.executeScript(
81 {code
: 'document.title = document.styleSheets.length'},
83 chrome
.tabs
.get(tabId
, pass(function(tab
) {
84 assertEq('0', tab
.title
);
90 function executeJavaScriptCodeShouldFail() {
91 chrome
.tabs
.update(tabId
, { url
: testFailureUrl
}, function() {
93 script_file
.code
= "document.title = 'executeScript';";
94 chrome
.tabs
.executeScript(tabId
, script_file
, fail(
95 'Cannot access contents of url "' + testFailureUrl
+
96 '". Extension manifest must request permission to access this ' +
101 function executeJavaScriptWithNoneValueShouldFail() {
102 var script_file
= {};
103 chrome
.tabs
.executeScript(tabId
, script_file
, fail(
104 'No source code or file specified.'));
107 function executeJavaScriptWithTwoValuesShouldFail() {
108 var script_file
= {};
109 script_file
.file
= 'script1.js';
110 script_file
.code
= 'var test = 1;';
111 chrome
.tabs
.executeScript(tabId
, script_file
, fail(
112 'Code and file should not be specified ' +
113 'at the same time in the second argument.'));
118 chrome
.tabs
.create({ url
: testUrl
});