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.
5 var assertEq = chrome.test.assertEq;
6 var assertTrue = chrome.test.assertTrue;
7 var pass = chrome.test.callbackPass;
8 var callbackFail = chrome.test.callbackFail;
9 var listenForever = chrome.test.listenForever;
14 function testUrl(domain) {
15 return 'http://' + domain + ':' + port +
16 '/extensions/test_file.html';
19 function error(domain) {
20 return 'Cannot access contents of url "' + testUrl(domain) + '".' +
21 ' Extension manifest must request permission to access this host.';
24 // Creates a new tab, navigated to the specified |domain|.
25 function createTestTab(domain, callback) {
26 var createdTabId = -1;
27 var done = listenForever(
28 chrome.tabs.onUpdated,
29 function(tabId, changeInfo, tab) {
30 if (tabId == createdTabId && changeInfo.status != 'loading') {
36 chrome.tabs.create({url: testUrl(domain)}, pass(function(tab) {
37 createdTabId = tab.id;
41 chrome.test.getConfig(function(config) {
42 port = config.testServer.port;
43 chrome.test.runTests([
45 // Before enabling the optional host permission, we shouldn't be able to
46 // inject content scripts.
48 createTestTab('a.com', pass(function(tab) {
50 chrome.tabs.executeScript(
51 tab.id, {code: 'document.title = "success"'},
52 callbackFail(error('a.com')));
56 // Add the host permission and see if we can inject a content script into
57 // existing and new tabs.
58 function addPermission() {
59 chrome.permissions.request(
60 {origins: ["http://*/*"]},
61 pass(function(granted) {
64 // Try accessing the existing tab.
65 chrome.tabs.executeScript(
66 testTabId, {code: 'document.title = "success"'},
68 chrome.tabs.get(testTabId, pass(function(tab) {
69 assertEq('success', tab.title);
73 // Make sure we can inject a script into a new tab with that host.
74 createTestTab('a.com', pass(function(tab) {
75 chrome.tabs.executeScript(
76 tab.id, {code: 'document.title = "success"'},
78 chrome.tabs.get(tab.id, pass(function(tab) {
79 assertEq('success', tab.title);
86 // Try the host again, except outside of the permissions.request callback.
88 createTestTab('a.com', pass(function(tab) {
89 chrome.tabs.executeScript(
90 tab.id, {code: 'document.title = "success"'},
92 chrome.tabs.get(tab.id, pass(function(tab) {
93 assertEq('success', tab.title);
99 // Try injecting the script into a new tab with a new host.
101 createTestTab('b.com', pass(function(tab) {
102 chrome.tabs.executeScript(
103 tab.id, {code: 'document.title = "success"'},
105 chrome.tabs.get(tab.id, pass(function(tab) {
106 assertEq('success', tab.title);