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.
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
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.
16 var backgroundPageResponded
= false;
19 'http://a.com:PORT/extensions/api_test/app_background_page/common';
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
);
26 // At no point should a window be created that contains the background page
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");
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
) {
41 pagePrefix
.replace(/PORT/, config
.testServer
.port
) + '/a.html';
42 chrome
.tabs
.create({ 'url': a_url
}, function(tab
) {
48 // Background page opened by pageA.
49 function onBackgroundPageLoaded() {
50 chrome
.tabs
.remove(pageA
.id
, function() {
51 chrome
.test
.getConfig(function(config
) {
53 pagePrefix
.replace(/PORT/, config
.testServer
.port
) + '/b.html';
54 chrome
.tabs
.create({ url
: b_url
}, function(tab
) {
61 // Background page responded to pageB.
62 function onBackgroundPageResponded() {
63 backgroundPageResponded
= true;
66 // Background page is closing itself.
67 function onBackgroundPageClosing() {
68 if (!backgroundPageResponded
) {
69 chrome
.test
.notifyFail("background never responded to pageB");
71 chrome
.test
.notifyPass();
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");