- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / app_background_page / basic / 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 // This test runs through an expected use of a live background page:
6 // - A live (web-extent) web page is loaded (a.html), which opens the background
7 //   page.
8 // - The first page is closed and a second live web page is loaded (b.html),
9 //   which attempts to get still-running running background page. This second
10 //   page also checks a counter which should have a value consistent with being
11 //   called once from each of the first and second pages.
12 // - The background page closes itself.
13
14 var pageA;
15 var pageB;
16 var backgroundPageResponded = false;
17
18 var pagePrefix =
19     'http://a.com:PORT/extensions/api_test/app_background_page/common';
20
21 // Dispatch "tunneled" functions from the live web pages to this testing page.
22 chrome.extension.onRequest.addListener(function(request) {
23   window[request.name](request.args);
24 });
25
26 // At no point should a window be created that contains the background page
27 // (bg.html).
28 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
29   if (tab.url.match("bg\.html$")) {
30     chrome.test.notifyFail("popup opened instead of background page");
31   }
32 });
33
34 // Start the test by opening the first page in the app.
35 window.onload = function() {
36   // We wait for window.onload before getting the test config.  If the
37   // config is requested before onload, then sometimes onload has already
38   // fired by the time chrome.test.getConfig()'s callback runs.
39   chrome.test.getConfig(function(config) {
40     var a_url =
41         pagePrefix.replace(/PORT/, config.testServer.port) + '/a.html';
42     chrome.tabs.create({ 'url': a_url }, function(tab) {
43       pageA = tab;
44     });
45   });
46 }
47
48 // Background page opened by pageA.
49 function onBackgroundPageLoaded() {
50   chrome.tabs.remove(pageA.id, function() {
51     chrome.test.getConfig(function(config) {
52       var b_url =
53           pagePrefix.replace(/PORT/, config.testServer.port) + '/b.html';
54       chrome.tabs.create({ url: b_url }, function(tab) {
55         pageB = tab;
56       });
57     });
58   });
59 }
60
61 // Background page responded to pageB.
62 function onBackgroundPageResponded() {
63   backgroundPageResponded = true;
64 }
65
66 // Background page is closing itself.
67 function onBackgroundPageClosing() {
68   if (!backgroundPageResponded) {
69     chrome.test.notifyFail("background never responded to pageB");
70   } else {
71     chrome.test.notifyPass();
72   }
73 }
74
75 // The background counter check found an unexpected value (most likely caused
76 // by an unwanted navigation.
77 function onCounterError() {
78   chrome.test.notifyFail("checkCounter found an unexpected value");
79 }