Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/http/tests/inspector/page-mock.js |
| diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector/page-mock.js b/third_party/WebKit/LayoutTests/http/tests/inspector/page-mock.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..201df64850e529d24113e0d1f402bb99b91b56b8 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/http/tests/inspector/page-mock.js |
| @@ -0,0 +1,214 @@ |
| +var initialize_EmptyPageMock = function() { |
| + |
| +var id = 0; |
| + |
| +function nextId(prefix) { |
| + return (prefix || "") + (++id); |
| +} |
| + |
| +InspectorTest.connectToPage = function(targetName, pageMock) { |
| + return SDK.targetManager.createTarget(nextId("mock-target-"), targetName, pageMock.capabilities(), params => pageMock.createConnection(params)); |
| +} |
| + |
| +InspectorTest.PageMock = class { |
| + constructor(url) { |
| + this._url = url; |
| + this._capabilities = SDK.Target.Capability.DOM | SDK.Target.Capability.JS | SDK.Target.Capability.Browser; |
| + |
| + this._mainFrame = { |
| + id: nextId(), |
| + loaderId: nextId(), |
| + mimeType: "text/html", |
| + securityOrigin: this._url, |
| + url: this._url |
| + }; |
| + |
| + this._createExecutionContext(); |
| + |
| + this._debuggerEnabled = false; |
| + this._scripts = []; |
| + this._scriptContents = new Map(); |
| + |
| + this._enablePageAgent = true; |
| + this._initializeDispatchMap(); |
| + } |
| + |
| + capabilities() { |
| + return this._capabilities; |
| + } |
| + |
| + disableDOMCapability() { |
| + this._capabilities = this._capabilities & (~SDK.Target.Capability.DOM); |
| + this._initializeDispatchMap(); |
| + } |
| + |
| + _hasDOMCapability() { |
| + return !!(this._capabilities & SDK.Target.Capability.DOM); |
| + } |
| + |
| + _initializeDispatchMap() { |
| + this._dispatchMap = { |
| + "Debugger.enable": this._enableDebugger, |
| + "Debugger.getScriptSource": this._getScriptSource, |
| + "Debugger.setBlackboxPatterns": (id, params) => this._sendResponse(id, {}), |
| + "Runtime.enable": this._enableRuntime, |
| + }; |
| + if (this._hasDOMCapability()) { |
| + Object.assign(this._dispatchMap, { |
| + "Page.enable": (id, params) => this._sendResponse(id, {}), |
| + "Page.getResourceTree": this._getResourceTree, |
| + }); |
| + } |
| + } |
| + |
| + createConnection(params) { |
| + this._connection = new MockPageConnection(this, params); |
| + return this._connection; |
| + } |
| + |
| + addScript(url, content, hasSourceURL, isContentScript) { |
|
dgozman
2017/03/16 00:17:10
Note that content scripts must have non-default ex
lushnikov
2017/03/16 06:30:49
Done.
|
| + url = hasSourceURL ? url : Common.ParsedURL.completeURL(this._url, url); |
| + var id = nextId(); |
| + this._scriptContents.set(id, content); |
| + var auxData = isContentScript ? {isDefault: false} : undefined; |
|
dgozman
2017/03/16 00:17:10
this._executionContext.auxData
lushnikov
2017/03/16 06:30:50
Done.
|
| + var script = { |
| + scriptId: id, |
| + url: url, |
| + startLine: 0, |
| + startColumn: 0, |
| + endLine: 0, |
|
dgozman
2017/03/16 00:17:09
Let's call lineEndings on it and pass true endLine
lushnikov
2017/03/16 06:30:49
Done.
|
| + endColumn: content.length, |
| + executionContextId: this._executionContext.id, |
| + hash: String.hashCode(content), |
| + executionContextAuxData: auxData, |
| + sourceMapURL: "", |
| + hasSourceURL: hasSourceURL, |
| + isLiveEdit: false, |
| + isModule: false, |
| + length: content.length |
| + }; |
| + this._scripts.push(script); |
| + if (this._debuggerEnabled) |
| + this._fireEvent("Debugger.scriptParsed", script); |
| + } |
| + |
| + reload() { |
| + this._fireEvent("Runtime.executionContextDestroyed", {executionContextId: this._executionContext.id}); |
|
dgozman
2017/03/16 00:17:09
I believe this one is not required.
lushnikov
2017/03/16 06:30:49
We currently send it whenever the page gets reload
|
| + this._scripts = []; |
| + this._scriptContents.clear(); |
| + this._fireEvent("Runtime.executionContextsCleared", {}); |
| + this._createExecutionContext(); |
| + this._fireEvent("Runtime.executionContextCreated", {context: this._executionContext}); |
| + |
| + if (this._hasDOMCapability()) |
| + this._fireEvent("Page.frameNavigated", {frame: this._mainFrame}); |
| + } |
| + |
| + _createExecutionContext() { |
| + this._executionContext = { |
| + id: nextId(), |
| + auxData: {isDefault: true, frameId: this._mainFrame.id}, |
| + origin: this._mainFrame.securityOrigin, |
| + name: '', |
| + }; |
| + } |
| + |
| + _enableRuntime(id, params) { |
| + this._sendResponse(id, {}); |
| + this._fireEvent("Runtime.executionContextCreated", {context: this._executionContext}); |
| + } |
| + |
| + _enableDebugger(id, params) { |
| + this._debuggerEnabled = true; |
| + this._sendResponse(id, {}); |
| + for (var script of this._scripts) |
| + this._fireEvent("Debugger.scriptParsed", script); |
| + } |
| + |
| + _getResourceTree(id, params) { |
| + var result = { |
| + frameTree: { |
| + frame: this._mainFrame, |
| + resources: [] |
|
dgozman
2017/03/16 00:17:09
Should we list all scripts here?
lushnikov
2017/03/16 06:30:49
I consider those to be evals for simplicity; renam
|
| + } |
| + } |
| + this._sendResponse(id, result); |
| + } |
| + |
| + _getScriptSource(id, params) { |
| + if (!this._scriptContents.has(params.scriptId)) { |
| + this._sendResponse(id, undefined, { |
| + message: 'Can\'t get script content for id ' + params.scriptId, |
| + code: 1, |
| + }); |
| + return; |
| + } |
| + var result = {scriptSource: this._scriptContents.get(params.scriptId)}; |
| + this._sendResponse(id, result); |
| + } |
| + |
| + _dispatch(id, methodName, params, message) { |
| + var handler = this._dispatchMap[methodName]; |
| + if (handler) |
| + return handler.call(this, id, params); |
| + this._sendResponse(id, undefined, { |
| + message: 'Can\'t handle command ' + methodName, |
| + code: Protocol.InspectorBackend.DevToolsStubErrorCode, |
| + }); |
| + } |
| + |
| + _sendResponse(id, result, error) { |
| + var message = { |
| + id: id, |
| + result: result, |
| + error: error |
| + }; |
| + this._connection.sendMessageToDevTools(message); |
| + } |
| + |
| + _fireEvent(methodName, params) { |
| + var message = { |
| + method: methodName, |
| + params: params |
| + }; |
| + this._connection.sendMessageToDevTools(message); |
| + } |
| +} |
| + |
| +var MockPageConnection = class { |
| + /** |
| + * @param {!Protocol.InspectorBackend.Connection.Params} params |
| + */ |
| + constructor(page, params) { |
| + this._page = page; |
| + this._onMessage = params.onMessage; |
| + this._onDisconnect = params.onDisconnect; |
| + } |
| + |
| + sendMessageToDevTools(message) { |
| + setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {string} message |
| + */ |
| + sendMessage(message) { |
| + var json = JSON.parse(message); |
| + this._page._dispatch(json.id, json.method, json.params, message); |
| + } |
| + |
| + /** |
| + * @override |
| + * @return {!Promise} |
| + */ |
| + disconnect() { |
| + this._onDisconnect.call(null, 'force disconnect'); |
| + this._onDisconnect = null; |
| + this._onMessage = null; |
| + return Promise.resolve(); |
| + } |
| +}; |
| + |
| +}; |
| + |