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.0";
12 var SILENT_FLAG_REQUIRED = "Cannot attach to this target unless " +
13 "'silent-debugger-extension-api' flag is enabled.";
15 chrome.test.runTests([
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."));
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."));
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."));
39 chrome.tabs.getSelected(null, function(tab) {
41 debuggee = {tabId: tab.id};
42 chrome.debugger.attach(debuggee, protocolVersion, pass());
46 function attachAgain() {
47 chrome.debugger.attach(debuggee, protocolVersion,
48 fail("Another debugger is already attached to the tab with id: " +
52 function sendCommand() {
53 function onResponse() {
54 if (chrome.runtime.lastError &&
55 chrome.runtime.lastError.message.indexOf("invalidMethod") != -1)
56 chrome.test.succeed();
60 chrome.debugger.sendCommand(debuggee,
67 chrome.debugger.detach(debuggee, pass());
70 function sendCommandAfterDetach() {
71 chrome.debugger.sendCommand(debuggee, "Foo", null,
72 fail("Debugger is not attached to the tab with id: " + tabId + "."));
75 function detachAgain() {
76 chrome.debugger.detach(debuggee,
77 fail("Debugger is not attached to the tab with id: " + tabId + "."));
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();
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);
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);
106 function attachToMissing() {
107 var missingDebuggee = {tabId: -1};
108 chrome.debugger.attach(missingDebuggee, protocolVersion,
109 fail("No tab with given id " + missingDebuggee.tabId + "."));
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));
119 function discoverOwnBackgroundPageWithNoSilentFlag() {
120 chrome.debugger.getTargets(function(targets) {
121 var target = targets.filter(
122 function(target) { return target.type == 'background_page'})[0];
124 chrome.debugger.attach({targetId: target.id}, protocolVersion,
125 fail(SILENT_FLAG_REQUIRED));
127 chrome.test.succeed();
132 function createAndDiscoverTab() {
133 function onUpdated(tabId) {
134 chrome.tabs.onUpdated.removeListener(onUpdated);
135 chrome.debugger.getTargets(function(targets) {
136 var page = targets.filter(
138 return t.type == 'page' &&
140 t.title == 'Test page';
143 chrome.debugger.attach(
144 {targetId: page.id}, protocolVersion, pass());
146 chrome.test.fail("Cannot discover a newly created tab");
150 chrome.tabs.onUpdated.addListener(onUpdated);
151 chrome.tabs.create({url: "inspected.html"});
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];
161 chrome.debugger.attach({targetId: page.id}, protocolVersion,
162 fail(SILENT_FLAG_REQUIRED));
164 chrome.test.fail("Cannot discover a newly created worker");