| OLD | NEW |
| 1 <script> | 1 <!-- |
| 2 | 2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. Use of this |
| 3 var pass = chrome.test.callbackPass; | 3 * source code is governed by a BSD-style license that can be found in the |
| 4 var fail = chrome.test.callbackFail; | 4 * LICENSE file. |
| 5 var assertEq = chrome.test.assertEq; | 5 --> |
| 6 var assertTrue = chrome.test.assertTrue; | 6 <script src="test.js"></script> |
| 7 var relativePath = | |
| 8 '/files/extensions/api_test/executescript/basic/test_executescript.html'; | |
| 9 var testUrl = 'http://a.com:PORT' + relativePath; | |
| 10 var testFailureUrl = 'http://b.com:PORT' + relativePath; | |
| 11 var firstEnter = true; | |
| 12 | |
| 13 chrome.test.getConfig(function(config) { | |
| 14 testUrl = testUrl.replace(/PORT/, config.testServer.port); | |
| 15 testFailureUrl = testFailureUrl.replace(/PORT/, config.testServer.port); | |
| 16 | |
| 17 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { | |
| 18 if (changeInfo.status != 'complete') | |
| 19 return; | |
| 20 if (!firstEnter) { | |
| 21 return; | |
| 22 } | |
| 23 firstEnter = false; | |
| 24 | |
| 25 chrome.test.runTests([ | |
| 26 | |
| 27 function executeJavaScriptCodeShouldSucceed() { | |
| 28 var script_file = {}; | |
| 29 script_file.code = "document.title = 'executeScript';"; | |
| 30 chrome.tabs.executeScript(tabId, script_file, function() { | |
| 31 chrome.tabs.get(tabId, pass(function(tab) { | |
| 32 assertEq('executeScript', tab.title); | |
| 33 })); | |
| 34 }); | |
| 35 }, | |
| 36 | |
| 37 function executeJavaScriptFileShouldSucceed() { | |
| 38 var script_file = {}; | |
| 39 script_file.file = 'script1.js'; | |
| 40 chrome.tabs.executeScript(tabId, script_file, function() { | |
| 41 chrome.tabs.get(tabId, pass(function(tab) { | |
| 42 assertEq('executeScript1', tab.title); | |
| 43 })); | |
| 44 }); | |
| 45 }, | |
| 46 | |
| 47 function insertCSSTextShouldSucceed() { | |
| 48 var css_file = {}; | |
| 49 css_file.code = "p {display:none;}"; | |
| 50 chrome.tabs.insertCSS(tabId, css_file, function() { | |
| 51 var script_file = {}; | |
| 52 script_file.file = 'script3.js'; | |
| 53 chrome.tabs.executeScript(tabId, script_file, function() { | |
| 54 chrome.tabs.get(tabId, pass(function(tab) { | |
| 55 assertEq('none', tab.title); | |
| 56 })); | |
| 57 }); | |
| 58 }); | |
| 59 }, | |
| 60 | |
| 61 function insertCSSFileShouldSucceed() { | |
| 62 var css_file = {}; | |
| 63 css_file.file = '1.css'; | |
| 64 chrome.tabs.insertCSS(tabId, css_file, function() { | |
| 65 var script_file = {}; | |
| 66 script_file.file = 'script2.js'; | |
| 67 chrome.tabs.executeScript(tabId, script_file, function() { | |
| 68 chrome.tabs.get(tabId, pass(function(tab) { | |
| 69 assertEq('block', tab.title); | |
| 70 })); | |
| 71 }); | |
| 72 }); | |
| 73 }, | |
| 74 | |
| 75 function insertCSSTextShouldNotAffectDOM() { | |
| 76 chrome.tabs.insertCSS(tabId, {code: 'p {display: none}'}, function() { | |
| 77 chrome.tabs.executeScript( | |
| 78 tabId, | |
| 79 {code: 'document.title = document.styleSheets.length'}, | |
| 80 function() { | |
| 81 chrome.tabs.get(tabId, pass(function(tab) { | |
| 82 assertEq('0', tab.title); | |
| 83 })); | |
| 84 }); | |
| 85 }); | |
| 86 }, | |
| 87 | |
| 88 function executeJavaScriptCodeShouldFail() { | |
| 89 chrome.tabs.update(tabId, { url: testFailureUrl }, function() { | |
| 90 var script_file = {}; | |
| 91 script_file.code = "document.title = 'executeScript';"; | |
| 92 chrome.tabs.executeScript(tabId, script_file, fail( | |
| 93 'Cannot access contents of url "' + testFailureUrl + | |
| 94 '". Extension manifest must request permission to access this ' + | |
| 95 'host.')); | |
| 96 }); | |
| 97 }, | |
| 98 | |
| 99 function executeJavaScriptWithNoneValueShouldFail() { | |
| 100 var script_file = {}; | |
| 101 chrome.tabs.executeScript(tabId, script_file, fail( | |
| 102 'No source code or file specified.')); | |
| 103 }, | |
| 104 | |
| 105 function executeJavaScriptWithTwoValuesShouldFail() { | |
| 106 var script_file = {}; | |
| 107 script_file.file = 'script1.js'; | |
| 108 script_file.code = 'var test = 1;'; | |
| 109 chrome.tabs.executeScript(tabId, script_file, fail( | |
| 110 'Code and file should not be specified ' + | |
| 111 'at the same time in the second argument.')); | |
| 112 } | |
| 113 ]); | |
| 114 }); | |
| 115 | |
| 116 chrome.tabs.create({ url: testUrl }); | |
| 117 }); | |
| 118 | |
| 119 </script> | |
| OLD | NEW |