- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / app_background_page / two_pages / 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 (c.html),
9 //   which uses a different name. This second page also checks a counter which
10 //   should have a value consistent with being called once.
11 // - We then reopen the first page. This first page also checks a counter
12 //   which should have a value consistent with the page being newly opened.
13 // - The background page closes itself.
14
15 var pageA;
16 var pageC;
17 var step = 0;
18
19 var pagePrefix =
20     'http://a.com:PORT/extensions/api_test/app_background_page/common';
21
22 // Dispatch "tunneled" functions from the live web pages to this testing page.
23 chrome.extension.onRequest.addListener(function(request) {
24   window[request.name](request.args);
25 });
26
27 // At no point should a window be created that contains the background page
28 // (bg.html).
29 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
30   if (tab.url.match("bg\.html$")) {
31     chrome.test.notifyFail("popup opened instead of background page");
32   }
33 });
34
35 // Start the test by opening the first page in the app.
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     var a_url =
42         pagePrefix.replace(/PORT/, config.testServer.port) + '/a.html';
43     chrome.tabs.create({ 'url': a_url }, function(tab) {
44       pageA = tab;
45     });
46   });
47 }
48
49 // Background page opened.
50 function onBackgroundPageLoaded() {
51   // There are 3 steps to this test:
52   // #1: page A just opened and opened its background page, so we close pageA
53   //     and open pageC.
54   // #2: page C just opened and opened its background page, so we close pageC
55   //     and reopen pageA.
56   // #3: page A opened again and opened its background page, so we're done.
57   if (step == 0) {
58     // Close A, open C.
59     chrome.tabs.remove(pageA.id, function() {
60       chrome.test.getConfig(function(config) {
61         var c_url =
62             pagePrefix.replace(/PORT/, config.testServer.port) + '/c.html';
63         chrome.tabs.create({ url: c_url }, function(tab) {
64           pageC = tab;
65         });
66       });
67     });
68   } else if (step == 1) {
69     // Close C, re-open A
70     chrome.tabs.remove(pageC.id, function() {
71       chrome.test.getConfig(function(config) {
72         var a_url =
73             pagePrefix.replace(/PORT/, config.testServer.port) + '/a.html';
74         chrome.tabs.create({ url: a_url }, function(tab) {
75           pageA = tab;
76         });
77       });
78     });
79   } else if (step == 2) {
80     chrome.test.notifyPass();
81   } else {
82     chrome.test.notifyFail("onBackgroundPageLoaded() called too many times");
83   }
84   step++;
85 }
86
87 // Background page responded to pageC.
88 function onBackgroundPageResponded() {
89   chrome.test.notifyFail("onBackgroundPageResponded called unexpectedly");
90 }
91
92 // The background counter check found an unexpected value (most likely caused
93 // by an unwanted navigation.
94 function onCounterError() {
95   chrome.test.notifyFail("checkCounter found an unexpected value");
96 }