1 // Copyright (c) 2013 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 REMOTE_DEBUGGER_HOST = 'localhost:9222';
7 function requestUrl(path, callback) {
8 var req = new XMLHttpRequest();
9 req.open('GET', 'http://' + REMOTE_DEBUGGER_HOST + path, true);
10 req.onload = function() {
11 if (req.status == 200)
12 callback(req.responseText);
16 req.onerror = function() {
17 chrome.test.fail('XHR failed: ' + req.status);
23 function checkTarget(targets, url, type, opt_title, opt_faviconUrl) {
25 targets.filter(function(t) { return t.url == url }) [0];
27 chrome.test.fail('Cannot find a target with url ' + url);
29 var wsAddress = REMOTE_DEBUGGER_HOST + '/devtools/page/' + target.id;
32 '/devtools/inspector.html?ws=' + wsAddress,
33 target.devtoolsFrontendUrl);
34 // On some platforms (e.g. Chrome OS) target.faviconUrl might be empty for
35 // a freshly created tab. Ignore the check then.
36 if (target.faviconUrl && opt_faviconUrl)
37 chrome.test.assertEq(opt_faviconUrl, target.faviconUrl);
38 // Sometimes thumbnailUrl is not available for a freshly loaded tab.
39 if (target.thumbnailUrl)
40 chrome.test.assertEq('/thumb/' + target.id, target.thumbnailUrl);
41 chrome.test.assertEq(opt_title || target.url, target.title);
42 chrome.test.assertEq(type, target.type);
43 chrome.test.assertEq('ws://' + wsAddress, target.webSocketDebuggerUrl);
48 function waitForTab(filter, callback) {
50 function onUpdated(updatedTabId, changeInfo, updatedTab) {
51 if (!filter(updatedTab) && !fired)
54 chrome.tabs.onUpdated.removeListener(onUpdated);
61 chrome.tabs.onUpdated.addListener(onUpdated);
63 chrome.tabs.query({}, function(tabs) {
65 for (var i = 0; i < tabs.length; ++i)
66 if (filter(tabs[i])) {
74 function listenOnce(event, func) {
75 var listener = function() {
76 event.removeListener(listener);
77 func.apply(null, arguments)
79 event.addListener(listener);
82 function runNewPageTest(devtoolsUrl, expectedUrl) {
87 function checkResult() {
90 chrome.test.assertEq(newTab.url, expectedUrl);
91 chrome.test.assertEq(json.url, expectedUrl);
92 chrome.test.assertTrue(newTab.active);
93 chrome.test.succeed();
96 function onCreated(createdTab) {
99 return tab.id == createdTab.id &&
101 tab.status == "complete";
109 listenOnce(chrome.tabs.onCreated, onCreated);
111 requestUrl(devtoolsUrl,
113 json = JSON.parse(text);
118 var extensionTargetId;
120 var extensionDevtoolsFrontendUrl;
121 var extensionWebSocketDebuggerUrl;
123 chrome.test.runTests([
124 function discoverTargets() {
125 var testPageUrl = chrome.extension.getURL('test_page.html');
127 chrome.tabs.create({url: testPageUrl});
130 return tab.url == testPageUrl && tab.status == "complete";
133 requestUrl('/json', function(text) {
134 var targets = JSON.parse(text);
135 checkTarget(targets, 'about:blank', 'page');
137 chrome.extension.getURL('_generated_background_page.html'),
139 'Remote Debugger Test');
140 var target = checkTarget(targets,
141 testPageUrl, 'page', 'Test page',
142 chrome.extension.getURL('favicon.png'));
144 extensionTargetId = target.id;
145 extensionDevtoolsFrontendUrl = target.devtoolsFrontendUrl;
146 extensionWebSocketDebuggerUrl = target.webSocketDebuggerUrl;
148 chrome.test.succeed();
153 function versionInfo() {
154 requestUrl('/json/version',
156 var versionInfo = JSON.parse(text);
157 chrome.test.assertTrue(/^Chrome\//.test(versionInfo["Browser"]));
158 chrome.test.assertTrue("Protocol-Version" in versionInfo);
159 chrome.test.assertTrue("User-Agent" in versionInfo);
160 chrome.test.assertTrue("WebKit-Version" in versionInfo);
161 chrome.test.succeed();
165 function activatePage() {
166 requestUrl('/json/activate/' + extensionTargetId,
168 chrome.test.assertEq(text, "Target activated");
172 tab.status == "complete" &&
173 tab.title == 'Test page';
176 extensionTabId = tab.id;
177 chrome.test.succeed();
182 function checkInspectablePagesUI() {
183 requestUrl("", function(text) {
185 chrome.test.assertTrue(
186 /<html[\s\S]*<head[\s\S]*<title[\s\S]*<script[\s\S]*<body/
188 chrome.test.succeed();
192 function checkDebuggerUI() {
193 requestUrl(extensionDevtoolsFrontendUrl, function(text) {
194 chrome.test.assertTrue(/blink-dev-tools/.test(text));
195 chrome.test.succeed();
199 function checkWebSocketAPI() {
200 var socket = new WebSocket(extensionWebSocketDebuggerUrl);
202 socket.onopen = function () {
204 method: "Runtime.evaluate",
205 params: {"expression": "1+1"},
209 socket.send(JSON.stringify(command));
212 socket.onerror = function (error) {
213 chrome.test.fail('WebSocket error ' + error);
216 socket.onmessage = function(e) {
217 var message = JSON.parse(e.data);
219 chrome.test.assertEq(1, message.id);
220 chrome.test.assertEq(2, message.result.result.value);
221 chrome.test.assertEq("number", message.result.result.type);
222 chrome.test.assertTrue(typeof message.wasThrown === "undefined");
226 socket.onclose = function() {
227 chrome.test.succeed();
231 function closePage() {
232 function onRemoved(tabId) {
233 chrome.test.assertEq(tabId, extensionTabId);
234 chrome.test.succeed();
237 listenOnce(chrome.tabs.onRemoved, onRemoved);
239 requestUrl('/json/close/' + extensionTargetId, function(text) {
240 chrome.test.assertEq(text, "Target is closing");
244 function newSpecificPage() {
245 runNewPageTest('/json/new?chrome://version/', "chrome://version/");
248 function newDefaultPage() {
249 runNewPageTest('/json/new', "about:blank");