- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / content_scripts / fragment / background.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 var failed = false;
6
7 function did_fail() {
8   return failed;
9 }
10
11 function set_failed(val) {
12   failed = val;
13 }
14
15 function fail() {
16   set_failed(true);
17   if (!did_fail()) {
18     chrome.test.fail();
19   }
20 }
21
22 var test_url = "http://localhost:PORT/extensions/test_file.html";
23
24 // For running in normal chrome (ie outside of the browser_tests environment),
25 // set debug to 1 here.
26 var debug = 0;
27 if (debug) {
28   test_url = "http://www.google.com";
29   chrome.test.log = function(msg) { console.log(msg) };
30   chrome.test.runTests = function(tests) {
31     for (var i in tests) {
32       tests[i]();
33     }
34   };
35   chrome.test.succeed = function(){ console.log("succeed"); };
36   chrome.test.fail = function(){ console.log("fail"); };
37 }
38
39 function runTests() {
40   chrome.test.runTests([
41     function test1() {
42       chrome.extension.onRequest.addListener(function(req, sender) {
43         chrome.test.log("got request: " + JSON.stringify(req));
44         if (req == "fail") {
45           fail();
46         } else if (req == "content_script_start") {
47           var tab = sender.tab;
48           if (tab.url.indexOf("#") != -1) {
49             fail();
50           } else {
51             chrome.tabs.update(tab.id, {"url": tab.url + "#foo"});
52           }
53         }
54       });
55       chrome.tabs.onUpdated.addListener(function(tabid, info, tab) {
56         chrome.test.log("onUpdated status: " + info.status + " url:" + tab.url);
57         if (info.status == "complete" && tab.url.indexOf("#foo") != -1) {
58           setTimeout(function() {
59             if (!did_fail()) {
60               chrome.test.succeed();
61             }
62           }, 750);
63         }
64       });
65       chrome.test.log("creating tab");
66       chrome.tabs.create({"url": test_url});
67     }
68   ]);
69 }
70
71 if (debug) {
72   runTests();
73 } else {
74   chrome.test.getConfig(function(config) {
75     test_url = test_url.replace(/PORT/, config.testServer.port);
76     runTests();
77   });
78 }