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 assertEq = chrome.test.assertEq;
6 var assertTrue = chrome.test.assertTrue;
7 var numReceivedRequests = 0;
8 var relativePath = 'extensions/api_test/executescript/permissions/';
9 var testFile = relativePath + 'empty.html';
10 var testFileFrames = relativePath + 'frames.html';
13 chrome.extension.onRequest.addListener(function(req) {
14 numReceivedRequests++;
17 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
18 if (tab.status == 'complete' && onTabLoaded)
22 chrome.test.getConfig(function(config) {
24 function fixPort(url) {
25 return url.replace(/PORT/, config.testServer.port);
28 chrome.test.runTests([
29 // Test a race that used to occur here (see bug 30937).
30 // Open a tab that we're not allowed to execute in (c.com), then
31 // navigate it to a tab we *are* allowed to execute in (a.com),
32 // then quickly run script in the tab before it navigates. It
33 // should appear to work (no error -- it could have been a developer
34 // mistake), but not actually do anything.
36 chrome.tabs.create({url: fixPort('http://c.com:PORT/') + testFile});
37 onTabLoaded = function(tab) {
39 numReceivedRequests = 0;
41 tab.id, {url: fixPort('http://a.com:PORT/') + testFile});
42 chrome.tabs.executeScript(tab.id, {file: 'script.js'});
43 window.setTimeout(function() {
44 assertEq(0, numReceivedRequests);
45 chrome.test.succeed();
50 // Inject into all frames. This should only affect frames we have
51 // access to. This page has three subframes, one each from a.com,
52 // b.com, and c.com. We have access to two of those, plus the root
53 // frame, so we should get three responses.
56 {url: fixPort('http://a.com:PORT/') + testFileFrames});
57 onTabLoaded = function(tab) {
58 numReceivedRequests = 0;
59 chrome.tabs.executeScript(tab.id,
60 {file: 'script.js', allFrames: true});
61 window.setTimeout(function() {
62 chrome.test.assertEq(3, numReceivedRequests);
63 chrome.test.succeed();