Index: third_party/WebKit/LayoutTests/http/tests/inspector-protocol-2/resources/inspector-protocol-test.html |
diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol-2/resources/inspector-protocol-test.html b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol-2/resources/inspector-protocol-test.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..da4603c45cb4787c91804bb69a13dee20617ffb9 |
--- /dev/null |
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol-2/resources/inspector-protocol-test.html |
@@ -0,0 +1,297 @@ |
+<!-- |
+Copyright 2017 The Chromium Authors. All rights reserved. |
+Use of this source code is governed by a BSD-style license that can be |
+found in the LICENSE file. |
+--> |
+<html> |
+<head> |
+<script> |
+ |
+var InspectorTest = {}; |
+InspectorTest._outputElement = null; |
+InspectorTest._dumpInspectorProtocolMessages = false; |
+InspectorTest._sessions = new Map(); |
+ |
+InspectorTest.startDumpingProtocolMessages = function() { |
+ InspectorTest._dumpInspectorProtocolMessages = true; |
+}; |
+ |
+InspectorTest.completeTest = function() { |
+ testRunner.notifyDone(); |
+}; |
+ |
+InspectorTest.log = function(text) { |
+ if (!InspectorTest._outputElement) { |
+ var intermediate = document.createElement('div'); |
+ document.body.appendChild(intermediate); |
+ var intermediate2 = document.createElement('div'); |
+ intermediate.appendChild(intermediate2); |
+ InspectorTest._outputElement = document.createElement('div'); |
+ InspectorTest._outputElement.className = 'output'; |
+ InspectorTest._outputElement.id = 'output'; |
+ InspectorTest._outputElement.style.whiteSpace = 'pre'; |
+ intermediate2.appendChild(InspectorTest._outputElement); |
+ } |
+ InspectorTest._outputElement.appendChild(document.createTextNode(text)); |
+ InspectorTest._outputElement.appendChild(document.createElement('br')); |
+}; |
+ |
+InspectorTest.logMessage = function(originalMessage) { |
+ var message = JSON.parse(JSON.stringify(originalMessage)); |
+ if (message.id) |
+ message.id = "<messageId>"; |
+ const nonStableFields = new Set(['nodeId', 'objectId']); |
+ var objects = [message]; |
+ while (objects.length) { |
+ var object = objects.shift(); |
+ for (var key in object) { |
+ if (nonStableFields.has(key)) |
+ object[key] = `<${key}>`; |
+ else if (typeof object[key] === "string" && object[key].match(/\d+:\d+:\d+:debug/)) |
+ object[key] = object[key].replace(/\d+/, '<scriptId>'); |
+ else if (typeof object[key] === "object") |
+ objects.push(object[key]); |
+ } |
+ } |
+ InspectorTest.logObject(message); |
+ return originalMessage; |
+}; |
+ |
+InspectorTest.logObject = function(object, title) { |
+ var lines = []; |
+ |
+ function dumpValue(value, prefix, prefixWithName) { |
+ if (typeof value === "object" && value !== null) { |
+ if (value instanceof Array) |
+ dumpItems(value, prefix, prefixWithName); |
+ else |
+ dumpProperties(value, prefix, prefixWithName); |
+ } else { |
+ lines.push(prefixWithName + String(value).replace(/\n/g, " ")); |
+ } |
+ } |
+ |
+ function dumpProperties(object, prefix, firstLinePrefix) { |
+ prefix = prefix || ""; |
+ firstLinePrefix = firstLinePrefix || prefix; |
+ lines.push(firstLinePrefix + "{"); |
+ |
+ var propertyNames = Object.keys(object); |
+ propertyNames.sort(); |
+ for (var i = 0; i < propertyNames.length; ++i) { |
+ var name = propertyNames[i]; |
+ if (!object.hasOwnProperty(name)) |
+ continue; |
+ var prefixWithName = " " + prefix + name + " : "; |
+ dumpValue(object[name], " " + prefix, prefixWithName); |
+ } |
+ lines.push(prefix + "}"); |
+ } |
+ |
+ function dumpItems(object, prefix, firstLinePrefix) { |
+ prefix = prefix || ""; |
+ firstLinePrefix = firstLinePrefix || prefix; |
+ lines.push(firstLinePrefix + "["); |
+ for (var i = 0; i < object.length; ++i) |
+ dumpValue(object[i], " " + prefix, " " + prefix + "[" + i + "] : "); |
+ lines.push(prefix + "]"); |
+ } |
+ |
+ dumpValue(object, "", title || ""); |
+ InspectorTest.log(lines.join("\n")); |
+}; |
+ |
+InspectorTest.die = function(message, error) { |
+ InspectorTest.log(`${message}: ${error}\n${error.stack}`); |
+ InspectorTest.completeTest(); |
+ throw new Error(); |
+}; |
+ |
+InspectorTest.createPage = async function(url) { |
+ var targetId = (await DevToolsAPI._sendCommandOrDie('Target.createTarget', {url: url || 'about:blank'})).targetId; |
+ return new InspectorTest.Page(targetId); |
+}; |
+ |
+InspectorTest.Page = class { |
+ constructor(targetId) { |
+ this._targetId = targetId; |
+ } |
+ |
+ async createSession() { |
+ await DevToolsAPI._sendCommandOrDie('Target.attachToTarget', {targetId: this._targetId}); |
+ var session = new InspectorTest.Session(this); |
+ InspectorTest._sessions.set(this._targetId, session); |
+ return session; |
+ } |
+}; |
+ |
+InspectorTest.Session = class { |
+ constructor(page) { |
+ this._page = page; |
+ this._requestId = 0; |
+ this._dispatchTable = new Map(); |
+ this._eventHandlers = new Map(); |
+ this.Protocol = this._setupProtocol(); |
+ } |
+ |
+ sendRawCommand(requestId, message) { |
+ DevToolsAPI._sendCommandOrDie('Target.sendMessageToTarget', {targetId: this._page._targetId, message: message}); |
+ return new Promise(f => this._dispatchTable.set(requestId, f)); |
+ } |
+ |
+ sendCommand(method, params) { |
+ var requestId = ++this._requestId; |
+ var messageObject = { 'id': requestId, 'method': method, 'params': params }; |
+ if (InspectorTest._dumpInspectorProtocolMessages.has(method)) |
+ InspectorTest.log(`frontend => backend: ${JSON.stringify(messageObject)}`); |
+ return this.sendRawCommand(requestId, JSON.stringify(messageObject)); |
+ } |
+ |
+ _dispatchMessage(message) { |
+ if (typeof message.id === 'number') { |
+ var handler = this._dispatchTable.get(message.id); |
+ if (handler) { |
+ this._dispatchTable.delete(message.id); |
+ handler(message); |
+ } |
+ } else { |
+ var eventName = message.method; |
+ for (var handler of (this._eventHandlers.get(eventName) || [])) |
+ handler(message); |
+ } |
+ } |
+ |
+ _setupProtocol() { |
+ return new Proxy({}, { get: (target, agentName, receiver) => new Proxy({}, { |
+ get: (target, methodName, receiver) => { |
+ const eventPattern = /^on(ce)?([A-Z][A-Za-z0-9]+)/; |
+ var match = eventPattern.exec(methodName); |
+ if (!match) |
+ return args => this.sendCommand(`${agentName}.${methodName}`, args || {}); |
+ var eventName = match[2]; |
+ eventName = eventName.charAt(0).toLowerCase() + eventName.slice(1); |
+ if (match[1]) |
+ return () => this._waitForEvent(`${agentName}.${eventName}`); |
+ return listener => this._addEventHandler(`${agentName}.${eventName}`, listener); |
+ } |
+ })}); |
+ } |
+ |
+ _addEventHandler(eventName, handler) { |
+ var handlers = this._eventHandlers.get(eventName) || []; |
+ handlers.push(handler); |
+ this._eventHandlers.set(eventName, handlers); |
+ } |
+ |
+ _removeEventHandler(eventName, handler) { |
+ var handlers = this._eventHandlers.get(eventName) || []; |
+ var index = handlers.indexOf(handler); |
+ if (index === -1) |
+ return; |
+ handlers.splice(index, 1); |
+ this._eventHandlers.set(eventName, handlers); |
+ } |
+ |
+ _waitForEvent(eventName) { |
+ return new Promise(callback => { |
+ var handler = result => { |
+ this._removeEventHandler(eventName, handler); |
+ callback(result); |
+ }; |
+ this._addEventHandler(eventName, handler); |
+ }); |
+ } |
+}; |
+ |
+var DevToolsAPI = {}; |
+DevToolsAPI._requestId = 0; |
+DevToolsAPI._embedderMessageId = 0; |
+DevToolsAPI._dispatchTable = new Map(); |
+ |
+DevToolsAPI.dispatchMessage = function(messageOrObject) { |
+ var messageObject = (typeof messageOrObject === 'string' ? JSON.parse(messageOrObject) : messageOrObject); |
+ var messageId = messageObject.id; |
+ try { |
+ if (typeof messageId === 'number') { |
+ var handler = DevToolsAPI._dispatchTable.get(messageId); |
+ if (handler) { |
+ DevToolsAPI._dispatchTable.delete(messageId); |
+ handler(messageObject); |
+ } |
+ } else { |
+ var eventName = messageObject.method; |
+ if (eventName === 'Target.receivedMessageFromTarget') { |
+ var targetId = messageObject.params.targetId; |
+ var message = messageObject.params.message; |
+ var session = InspectorTest._sessions.get(targetId); |
+ if (session) |
+ session._dispatchMessage(JSON.parse(message)); |
+ } |
+ } |
+ } catch(e) { |
+ InspectorTest.die(`Exception when dispatching message\n${JSON.stringify(messageObject)}`, e); |
+ } |
+}; |
+ |
+DevToolsAPI._sendCommand = function(method, params) { |
+ var requestId = ++DevToolsAPI._requestId; |
+ var messageObject = { 'id': requestId, 'method': method, 'params': params }; |
+ var embedderMessage = { 'id': ++DevToolsAPI._embedderMessageId, 'method': 'dispatchProtocolMessage', 'params': [JSON.stringify(messageObject)] }; |
+ DevToolsHost.sendMessageToEmbedder(JSON.stringify(embedderMessage)); |
+ return new Promise(f => DevToolsAPI._dispatchTable.set(requestId, f)); |
+}; |
+ |
+DevToolsAPI._sendCommandOrDie = function(method, params, errorMessage) { |
+ errorMessage = errorMessage || 'Unexpected error'; |
+ return DevToolsAPI._sendCommand(method, params).then(message => { |
+ if (message.error) |
+ InspectorTest.die('Error starting the test', e); |
+ return message.result; |
+ }); |
+}; |
+ |
+DevToolsAPI.embedderMessageAck = function() { |
+}; |
+ |
+InspectorTest.start = async function(description, url) { |
+ try { |
+ InspectorTest.log(description); |
+ var page = await InspectorTest.createPage(url); |
+ var session = await page.createSession(); |
+ return { page: page, session: session, Protocol: session.Protocol }; |
+ } catch (e) { |
+ InspectorTest.die('Error starting the test', e); |
+ } |
+}; |
+ |
+InspectorTest.debugTest = function(testFunction) { |
+ window.test = testFunction; |
+ InspectorTest.log = console.log; |
+ InspectorTest.completeTest = () => console.log('Test completed'); |
+ return () => {}; |
+}; |
+ |
+window.addEventListener('load', async () => { |
+ testRunner.dumpAsText(); |
+ testRunner.waitUntilDone(); |
+ testRunner.setCanOpenWindows(true); |
+ try { |
+ var testScriptURL = window.location.search.substring(1); |
+ var testScript = await (await fetch(testScriptURL)).text(); |
+ eval(`${testScript}\n//# sourceURL=${testScriptURL}`); |
+ } catch(e) { |
+ InspectorTest.die('Unable to execute test script', e); |
+ } |
+}, false); |
+ |
+window.addEventListener('error', (message, source, lineno, colno, error) => { |
+ InspectorTest.die('General error', error); |
+}, false); |
+ |
+window.addEventListener('unhandledrejection', error => { |
+ InspectorTest.die('General error', error); |
+}, false); |
+ |
+</script> |
+</head> |
+</html> |