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("Can not attach to the page with the \"chrome://\" scheme."));
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) {
144 chrome.tabs.onUpdated.removeListener(onUpdated);
145 chrome.debugger.getTargets(function(targets) {
146 var page = targets.filter(
148 return t.type == 'page' &&
150 t.title == 'Test page';
153 chrome.debugger.attach(
154 {targetId: page.id}, protocolVersion, pass());
156 chrome.test.fail("Cannot discover a newly created tab");
160 chrome.tabs.onUpdated.addListener(onUpdated);
161 chrome.tabs.create({url: "inspected.html"});
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];
171 chrome.debugger.attach({targetId: page.id}, protocolVersion,
172 fail(SILENT_FLAG_REQUIRED));
174 chrome.test.fail("Cannot discover a newly created worker");
181 function sendCommandDuringNavigation() {
182 chrome.tabs.create({url:"inspected.html"}, function(tab) {
183 var debuggee = {tabId: tab.id};
185 function checkError() {
186 if (chrome.runtime.lastError) {
187 chrome.test.fail(chrome.runtime.lastError.message);
189 chrome.tabs.remove(tab.id);
190 chrome.test.succeed();
194 function onNavigateDone() {
195 chrome.debugger.sendCommand(debuggee, "Page.disable", null, checkError);
198 function onAttach() {
199 chrome.debugger.sendCommand(debuggee, "Page.enable");
200 chrome.debugger.sendCommand(
201 debuggee, "Page.navigate", {url:"about:blank"}, onNavigateDone);
204 chrome.debugger.attach(debuggee, protocolVersion, onAttach);