- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / content_scripts / permissions / background.js
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.
4
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;
10
11 var testTabId;
12 var port;
13
14 function testUrl(domain) {
15   return 'http://' + domain + ':' + port +
16       '/extensions/test_file.html';
17 }
18
19 function error(domain) {
20   return 'Cannot access contents of url "' + testUrl(domain) + '".' +
21     ' Extension manifest must request permission to access this host.';
22 }
23
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') {
31       callback(tab);
32       done();
33     }
34   });
35
36   chrome.tabs.create({url: testUrl(domain)}, pass(function(tab) {
37     createdTabId = tab.id;
38   }));
39 }
40
41 chrome.test.getConfig(function(config) {
42   port = config.testServer.port;
43   chrome.test.runTests([
44
45     // Before enabling the optional host permission, we shouldn't be able to
46     // inject content scripts.
47     function noAccess() {
48       createTestTab('a.com', pass(function(tab) {
49         testTabId = tab.id;
50         chrome.tabs.executeScript(
51             tab.id, {code: 'document.title = "success"'},
52             callbackFail(error('a.com')));
53       }));
54     },
55
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) {
62         assertTrue(granted);
63
64         // Try accessing the existing tab.
65         chrome.tabs.executeScript(
66             testTabId, {code: 'document.title = "success"'},
67             pass(function() {
68           chrome.tabs.get(testTabId, pass(function(tab) {
69             assertEq('success', tab.title);
70           }));
71         }));
72
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"'},
77               pass(function() {
78             chrome.tabs.get(tab.id, pass(function(tab) {
79               assertEq('success', tab.title);
80             }));
81           }));
82         }));
83       }));
84     },
85
86     // Try the host again, except outside of the permissions.request callback.
87     function sameHost() {
88       createTestTab('a.com', pass(function(tab) {
89         chrome.tabs.executeScript(
90             tab.id, {code: 'document.title = "success"'},
91             pass(function() {
92           chrome.tabs.get(tab.id, pass(function(tab) {
93             assertEq('success', tab.title);
94           }));
95         }));
96       }));
97     },
98
99     // Try injecting the script into a new tab with a new host.
100     function newHost() {
101       createTestTab('b.com', pass(function(tab) {
102         chrome.tabs.executeScript(
103             tab.id, {code: 'document.title = "success"'},
104             pass(function() {
105           chrome.tabs.get(tab.id, pass(function(tab) {
106             assertEq('success', tab.title);
107           }));
108         }));
109       }));
110     }
111   ]);
112 });