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 var pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
10 var protocolVersion = "1.1";
11 var protocolPreviousVersion = "1.0";
13 var SILENT_FLAG_REQUIRED = "Cannot attach to this target unless " +
14 "'silent-debugger-extension-api' flag is enabled.";
16 chrome.test.runTests([
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."));
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."));
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."));
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());
48 function attachLatestVersion() {
49 chrome.tabs.getSelected(null, function(tab) {
51 debuggee = {tabId: tab.id};
52 chrome.debugger.attach(debuggee, protocolVersion, pass());
56 function attachAgain() {
57 chrome.debugger.attach(debuggee, protocolVersion,
58 fail("Another debugger is already attached to the tab with id: " +
62 function sendCommand() {
63 function onResponse() {
64 if (chrome.runtime.lastError &&
65 chrome.runtime.lastError.message.indexOf("invalidMethod") != -1)
66 chrome.test.succeed();
70 chrome.debugger.sendCommand(debuggee,
77 chrome.debugger.detach(debuggee, pass());
80 function sendCommandAfterDetach() {
81 chrome.debugger.sendCommand(debuggee, "Foo", null,
82 fail("Debugger is not attached to the tab with id: " + tabId + "."));
85 function detachAgain() {
86 chrome.debugger.detach(debuggee,
87 fail("Debugger is not attached to the tab with id: " + tabId + "."));
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();
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);
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("Cannot access a chrome:// URL"));
112 chrome.tabs.remove(tab.id);
116 function attachToMissing() {
117 var missingDebuggee = {tabId: -1};
118 chrome.debugger.attach(missingDebuggee, protocolVersion,
119 fail("No tab with given id " + missingDebuggee.tabId + "."));
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));
129 function discoverOwnBackgroundPageWithNoSilentFlag() {
130 chrome.debugger.getTargets(function(targets) {
131 var target = targets.filter(
132 function(target) { return target.type == 'background_page'})[0];
134 chrome.debugger.attach({targetId: target.id}, protocolVersion,
135 fail(SILENT_FLAG_REQUIRED));
137 chrome.test.succeed();
142 function createAndDiscoverTab() {
143 function onUpdated(tabId, changeInfo) {
144 if (changeInfo.status == 'loading')
146 chrome.tabs.onUpdated.removeListener(onUpdated);
147 chrome.debugger.getTargets(function(targets) {
148 var page = targets.filter(
150 return t.type == 'page' &&
152 t.title == 'Test page';
155 chrome.debugger.attach(
156 {targetId: page.id}, protocolVersion, pass());
158 chrome.test.fail("Cannot discover a newly created tab");
162 chrome.tabs.onUpdated.addListener(onUpdated);
163 chrome.tabs.create({url: "inspected.html"});
166 function discoverWorker() {
167 var workerPort = new SharedWorker("worker.js").port;
168 workerPort.onmessage = function() {
169 chrome.debugger.getTargets(function(targets) {
170 var page = targets.filter(
171 function(t) { return t.type == 'worker' })[0];
173 chrome.debugger.attach({targetId: page.id}, protocolVersion,
174 fail(SILENT_FLAG_REQUIRED));
176 chrome.test.fail("Cannot discover a newly created worker");
183 function sendCommandDuringNavigation() {
184 chrome.tabs.create({url:"inspected.html"}, function(tab) {
185 var debuggee = {tabId: tab.id};
187 function checkError() {
188 if (chrome.runtime.lastError) {
189 chrome.test.fail(chrome.runtime.lastError.message);
191 chrome.tabs.remove(tab.id);
192 chrome.test.succeed();
196 function onNavigateDone() {
197 chrome.debugger.sendCommand(debuggee, "Page.disable", null, checkError);
200 function onAttach() {
201 chrome.debugger.sendCommand(debuggee, "Page.enable");
202 chrome.debugger.sendCommand(
203 debuggee, "Page.navigate", {url:"about:blank"}, onNavigateDone);
206 chrome.debugger.attach(debuggee, protocolVersion, onAttach);