- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / executescript / permissions / test.js
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 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';
11 var onTabLoaded;
12
13 chrome.extension.onRequest.addListener(function(req) {
14   numReceivedRequests++;
15 });
16
17 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
18   if (tab.status == 'complete' && onTabLoaded)
19     onTabLoaded(tab);
20 });
21
22 chrome.test.getConfig(function(config) {
23
24   function fixPort(url) {
25     return url.replace(/PORT/, config.testServer.port);
26   }
27
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.
35     function() {
36       chrome.tabs.create({url: fixPort('http://c.com:PORT/') + testFile});
37       onTabLoaded = function(tab) {
38         onTabLoaded = null;
39         numReceivedRequests = 0;
40         chrome.tabs.update(
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();
46         }, 4000);
47       };
48     },
49
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.
54     function() {
55       chrome.tabs.create(
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();
64         }, 4000);
65       };
66     }
67   ]);
68
69 });