- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / debugger / 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 pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
7
8 var tabId;
9 var debuggee;
10 var protocolVersion = "1.0";
11
12 var SILENT_FLAG_REQUIRED = "Cannot attach to this target unless " +
13     "'silent-debugger-extension-api' flag is enabled.";
14
15 chrome.test.runTests([
16
17   function attachMalformedVersion() {
18     chrome.tabs.getSelected(null, function(tab) {
19       chrome.debugger.attach({tabId: tab.id}, "malformed-version", fail(
20           "Requested protocol version is not supported: malformed-version."));
21     });
22   },
23
24   function attachUnsupportedMinorVersion() {
25     chrome.tabs.getSelected(null, function(tab) {
26       chrome.debugger.attach({tabId: tab.id}, "1.5",
27           fail("Requested protocol version is not supported: 1.5."));
28     });
29   },
30
31   function attachUnsupportedVersion() {
32     chrome.tabs.getSelected(null, function(tab) {
33       chrome.debugger.attach({tabId: tab.id}, "100.0",
34           fail("Requested protocol version is not supported: 100.0."));
35     });
36   },
37
38   function attach() {
39     chrome.tabs.getSelected(null, function(tab) {
40       tabId = tab.id;
41       debuggee = {tabId: tab.id};
42       chrome.debugger.attach(debuggee, protocolVersion, pass());
43     });
44   },
45
46   function attachAgain() {
47     chrome.debugger.attach(debuggee, protocolVersion,
48         fail("Another debugger is already attached to the tab with id: " +
49                  tabId + "."));
50   },
51
52   function sendCommand() {
53     function onResponse() {
54       if (chrome.runtime.lastError &&
55           chrome.runtime.lastError.message.indexOf("invalidMethod") != -1)
56         chrome.test.succeed();
57       else
58         chrome.test.fail();
59     }
60     chrome.debugger.sendCommand(debuggee,
61                                "DOM.invalidMethod",
62                                null,
63                                onResponse);
64   },
65
66   function detach() {
67     chrome.debugger.detach(debuggee, pass());
68   },
69
70   function sendCommandAfterDetach() {
71     chrome.debugger.sendCommand(debuggee, "Foo", null,
72         fail("Debugger is not attached to the tab with id: " + tabId + "."));
73   },
74
75   function detachAgain() {
76     chrome.debugger.detach(debuggee,
77         fail("Debugger is not attached to the tab with id: " + tabId + "."));
78   },
79
80   function closeTab() {
81     chrome.tabs.create({url:"inspected.html"}, function(tab) {
82       function onDetach(debuggee, reason) {
83         chrome.test.assertEq(tab.id, debuggee.tabId);
84         chrome.test.assertEq("target_closed", reason);
85         chrome.debugger.onDetach.removeListener(onDetach);
86         chrome.test.succeed();
87       }
88
89       var debuggee2 = {tabId: tab.id};
90       chrome.debugger.attach(debuggee2, protocolVersion, function() {
91         chrome.debugger.onDetach.addListener(onDetach);
92         chrome.tabs.remove(tab.id);
93       });
94     });
95   },
96
97   function attachToWebUI() {
98     chrome.tabs.create({url:"chrome://version"}, function(tab) {
99       var debuggee = {tabId: tab.id};
100       chrome.debugger.attach(debuggee, protocolVersion,
101           fail("Can not attach to the page with the \"chrome://\" scheme."));
102       chrome.tabs.remove(tab.id);
103     });
104   },
105
106   function attachToMissing() {
107     var missingDebuggee = {tabId: -1};
108     chrome.debugger.attach(missingDebuggee, protocolVersion,
109         fail("No tab with given id " + missingDebuggee.tabId + "."));
110   },
111
112   function attachToOwnBackgroundPageWithNoSilentFlag() {
113     var ownExtensionId = chrome.extension.getURL('').split('/')[2];
114     var debuggeeExtension = {extensionId: ownExtensionId};
115     chrome.debugger.attach(debuggeeExtension, protocolVersion,
116         fail(SILENT_FLAG_REQUIRED));
117   },
118
119   function discoverOwnBackgroundPageWithNoSilentFlag() {
120     chrome.debugger.getTargets(function(targets) {
121       var target = targets.filter(
122           function(target) { return target.type == 'background_page'})[0];
123       if (target) {
124         chrome.debugger.attach({targetId: target.id}, protocolVersion,
125             fail(SILENT_FLAG_REQUIRED));
126       } else {
127         chrome.test.succeed();
128       }
129     });
130   },
131
132   function createAndDiscoverTab() {
133     function onUpdated(tabId) {
134       chrome.tabs.onUpdated.removeListener(onUpdated);
135       chrome.debugger.getTargets(function(targets) {
136         var page = targets.filter(
137             function(t) {
138               return t.type == 'page' &&
139                      t.tabId == tabId &&
140                      t.title == 'Test page';
141             })[0];
142         if (page) {
143           chrome.debugger.attach(
144               {targetId: page.id}, protocolVersion, pass());
145         } else {
146           chrome.test.fail("Cannot discover a newly created tab");
147         }
148       });
149     }
150     chrome.tabs.onUpdated.addListener(onUpdated);
151     chrome.tabs.create({url: "inspected.html"});
152   },
153
154   function discoverWorker() {
155     var workerPort = new SharedWorker("worker.js").port;
156     workerPort.onmessage = function() {
157       chrome.debugger.getTargets(function(targets) {
158         var page = targets.filter(
159             function(t) { return t.type == 'worker' })[0];
160         if (page) {
161           chrome.debugger.attach({targetId: page.id}, protocolVersion,
162               fail(SILENT_FLAG_REQUIRED));
163         } else {
164           chrome.test.fail("Cannot discover a newly created worker");
165         }
166       });
167     };
168     workerPort.start();
169   }
170 ]);