OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 var pass = chrome.test.callbackPass; |
| 6 var fail = chrome.test.callbackFail; |
| 7 var assertEq = chrome.test.assertEq; |
| 8 var assertTrue = chrome.test.assertTrue; |
| 9 var relativePath = |
| 10 '/files/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; |
| 14 |
| 15 chrome.test.getConfig(function(config) { |
| 16 testUrl = testUrl.replace(/PORT/, config.testServer.port); |
| 17 testFailureUrl = testFailureUrl.replace(/PORT/, config.testServer.port); |
| 18 |
| 19 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { |
| 20 if (changeInfo.status != 'complete') |
| 21 return; |
| 22 if (!firstEnter) { |
| 23 return; |
| 24 } |
| 25 firstEnter = false; |
| 26 |
| 27 chrome.test.runTests([ |
| 28 |
| 29 function executeJavaScriptCodeShouldSucceed() { |
| 30 var script_file = {}; |
| 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); |
| 35 })); |
| 36 }); |
| 37 }, |
| 38 |
| 39 function executeJavaScriptFileShouldSucceed() { |
| 40 var script_file = {}; |
| 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); |
| 45 })); |
| 46 }); |
| 47 }, |
| 48 |
| 49 function insertCSSTextShouldSucceed() { |
| 50 var css_file = {}; |
| 51 css_file.code = "p {display:none;}"; |
| 52 chrome.tabs.insertCSS(tabId, css_file, function() { |
| 53 var script_file = {}; |
| 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); |
| 58 })); |
| 59 }); |
| 60 }); |
| 61 }, |
| 62 |
| 63 function insertCSSFileShouldSucceed() { |
| 64 var css_file = {}; |
| 65 css_file.file = '1.css'; |
| 66 chrome.tabs.insertCSS(tabId, css_file, function() { |
| 67 var script_file = {}; |
| 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); |
| 72 })); |
| 73 }); |
| 74 }); |
| 75 }, |
| 76 |
| 77 function insertCSSTextShouldNotAffectDOM() { |
| 78 chrome.tabs.insertCSS(tabId, {code: 'p {display: none}'}, function() { |
| 79 chrome.tabs.executeScript( |
| 80 tabId, |
| 81 {code: 'document.title = document.styleSheets.length'}, |
| 82 function() { |
| 83 chrome.tabs.get(tabId, pass(function(tab) { |
| 84 assertEq('0', tab.title); |
| 85 })); |
| 86 }); |
| 87 }); |
| 88 }, |
| 89 |
| 90 function executeJavaScriptCodeShouldFail() { |
| 91 chrome.tabs.update(tabId, { url: testFailureUrl }, function() { |
| 92 var script_file = {}; |
| 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 ' + |
| 97 'host.')); |
| 98 }); |
| 99 }, |
| 100 |
| 101 function executeJavaScriptWithNoneValueShouldFail() { |
| 102 var script_file = {}; |
| 103 chrome.tabs.executeScript(tabId, script_file, fail( |
| 104 'No source code or file specified.')); |
| 105 }, |
| 106 |
| 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.')); |
| 114 } |
| 115 ]); |
| 116 }); |
| 117 |
| 118 chrome.tabs.create({ url: testUrl }); |
| 119 }); |
| 120 |
OLD | NEW |