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