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 (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.
20 'http://a.com:PORT/extensions/api_test/app_background_page/common';
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);
27 // At no point should a window be created that contains the background page
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");
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) {
42 pagePrefix.replace(/PORT/, config.testServer.port) + '/a.html';
43 chrome.tabs.create({ 'url': a_url }, function(tab) {
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
54 // #2: page C just opened and opened its background page, so we close pageC
56 // #3: page A opened again and opened its background page, so we're done.
59 chrome.tabs.remove(pageA.id, function() {
60 chrome.test.getConfig(function(config) {
62 pagePrefix.replace(/PORT/, config.testServer.port) + '/c.html';
63 chrome.tabs.create({ url: c_url }, function(tab) {
68 } else if (step == 1) {
70 chrome.tabs.remove(pageC.id, function() {
71 chrome.test.getConfig(function(config) {
73 pagePrefix.replace(/PORT/, config.testServer.port) + '/a.html';
74 chrome.tabs.create({ url: a_url }, function(tab) {
79 } else if (step == 2) {
80 chrome.test.notifyPass();
82 chrome.test.notifyFail("onBackgroundPageLoaded() called too many times");
87 // Background page responded to pageC.
88 function onBackgroundPageResponded() {
89 chrome.test.notifyFail("onBackgroundPageResponded called unexpectedly");
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");