Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / debugger_extension / background.js
1 // Copyright (c) 2013 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 pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
7
8 var debuggee;
9 var protocolVersion = "1.1";
10
11 chrome.test.runTests([
12
13   function attach() {
14     var extensionId = chrome.extension.getURL('').split('/')[2];
15     debuggee = {extensionId: extensionId};
16     chrome.debugger.attach(debuggee, protocolVersion, pass());
17   },
18
19   function attachToMissing() {
20     var missingDebuggee = {extensionId: "foo"};
21     chrome.debugger.attach(missingDebuggee, protocolVersion,
22         fail("No background page with given id " +
23             missingDebuggee.extensionId + "."));
24   },
25
26   function attachAgain() {
27     chrome.debugger.attach(debuggee, protocolVersion,
28         fail("Another debugger is already attached " +
29             "to the background page with id: " + debuggee.extensionId + "."));
30   },
31
32   function detach() {
33     chrome.debugger.detach(debuggee, pass());
34   },
35
36   function detachAgain() {
37     chrome.debugger.detach(debuggee,
38         fail("Debugger is not attached to the background page with id: " +
39             debuggee.extensionId + "."));
40   },
41
42   function discoverOwnBackgroundPage() {
43     chrome.debugger.getTargets(function(targets) {
44       var target = targets.filter(
45         function(t) {
46           return t.type == 'background_page' &&
47                  t.extensionId == debuggee.extensionId &&
48                  t.title == 'Extension Debugger';
49         })[0];
50       if (target) {
51         chrome.debugger.attach({targetId: target.id}, protocolVersion, pass());
52       } else {
53         chrome.test.fail("Cannot discover own background page");
54       }
55     });
56   },
57
58   function discoverWorker() {
59     var workerPort = new SharedWorker("worker.js").port;
60     workerPort.onmessage = function() {
61       chrome.debugger.getTargets(function(targets) {
62         var page = targets.filter(
63             function(t) { return t.type == 'worker' })[0];
64         if (page) {
65           chrome.debugger.attach({targetId: page.id}, protocolVersion, pass());
66         } else {
67           chrome.test.fail("Cannot discover a newly created worker");
68         }
69       });
70     };
71     workerPort.start();
72   }
73 ]);