- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / app_background_page / no_js / test.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 // This test checks that setting allow_js_access to false is effective:
6 // - A background page is opened via window.open (which is verified by the
7 //   AppBackgroundPageApiTest.NoJsBackgroundPage code).
8 // - The return value of the window.open call is null (since the background
9 //   page is not scriptable)
10 // - Attempts to call window.open(...., "background") again will not result in
11 //   existing background page being closed and a new one being re-opened.
12
13 var pagePrefix =
14     'http://a.com:PORT/extensions/api_test/app_background_page/no_js';
15 var launchUrl;
16 var launchTabId;
17 var backgroundPageLoaded = false;
18
19 // Dispatch "tunneled" functions from the live web pages to this testing page.
20 chrome.extension.onRequest.addListener(function(request) {
21   window[request.name](request.args);
22 });
23
24 // At no point should a window be created that contains the background page
25 // (bg.html).
26 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
27   if (tab.url.match("bg\.html$")) {
28     chrome.test.notifyFail("popup opened instead of background page");
29   }
30 });
31
32 // Start the test by opening the first page in the app. This will try to create
33 // a background page whose name is "bg", which will succeed, but will not return
34 // a Window object. However, the background contents should load, which will
35 // then invoke onBackgroundPageLoaded.
36 window.onload = function() {
37   // We wait for window.onload before getting the test config.  If the
38   // config is requested before onload, then sometimes onload has already
39   // fired by the time chrome.test.getConfig()'s callback runs.
40   chrome.test.getConfig(function(config) {
41     launchUrl =
42         pagePrefix.replace(/PORT/, config.testServer.port) + '/launch.html';
43     chrome.tabs.create(
44         {url: launchUrl},
45         function(tab) {
46             launchTabId = tab.id;
47         });
48   });
49 }
50
51 function onBackgroundWindowNotNull() {
52   chrome.test.notifyFail('Unexpected non-null window.open result');
53 }
54
55 function onBackgroundPageLoaded() {
56   if (backgroundPageLoaded) {
57     chrome.test.notifyFail('Background page loaded more than once.');
58     return;
59   }
60
61   backgroundPageLoaded = true;
62
63   // Close the existing page and re-open it, which will try to call
64   // window.open(..., "background") again.
65   chrome.tabs.remove(
66       launchTabId,
67       function() {
68         chrome.tabs.create(
69             {url: launchUrl },
70             function(tab) {
71               // We wait for a bit before declaring the test as passed, since
72               // it might take a while for the additional background contents
73               // to be recreated.
74               setTimeout(chrome.test.notifyPass, 2000);
75             });
76       });
77 }