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..ff5eecbd8343716fb539e30233c5b4168f5f242d |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/http/tests/inspector/page-mock.js |
| @@ -0,0 +1,230 @@ |
| +var initialize_EmptyPageMock = function() { |
| + |
| +var id = 0; |
| + |
| +function nextId(prefix) { |
| + return (prefix || "") + (++id); |
| +} |
| + |
| +InspectorTest.connectToPage = function(targetName, pageMock, makeMainTarget) { |
| + var mockTarget = SDK.targetManager.createTarget(nextId("mock-target-"), targetName, pageMock.capabilities(), params => pageMock.createConnection(params)); |
| + if (makeMainTarget) { |
| + SDK.targetManager._targets = SDK.targetManager._targets.filter(target => target !== mockTarget); |
| + SDK.targetManager._targets.unshift(mockTarget); |
| + } |
| + return mockTarget; |
| +} |
| + |
| +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._executionContexts = []; |
| + this._executionContexts.push(this._createExecutionContext(this._mainFrame, false)); |
| + |
| + 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; |
| + } |
| + |
| + evalScript(url, content, isContentScript) { |
| + var id = nextId(); |
| + this._scriptContents.set(id, content); |
| + |
| + var context = this._executionContexts.find(context => context.auxData.isDefault !== isContentScript); |
| + if (!context) { |
| + context = this._createExecutionContext(this._mainFrame, isContentScript); |
| + this._fireEvent("Runtime.executionContextCreated", {context: context}); |
| + } |
| + |
| + var text = new Common.Text(content); |
| + var script = { |
| + scriptId: id, |
| + url: url, |
| + startLine: 0, |
| + startColumn: 0, |
| + endLine: text.lineCount(), |
| + endColumn: text.lineAt(text.lineCount()).length - 1, |
| + executionContextId: context.id, |
| + hash: String.hashCode(content), |
| + executionContextAuxData: context.auxData, |
| + sourceMapURL: "", |
| + hasSourceURL: true, |
| + isLiveEdit: false, |
| + isModule: false, |
| + length: content.length |
| + }; |
| + this._scripts.push(script); |
| + if (this._debuggerEnabled) |
| + this._fireEvent("Debugger.scriptParsed", script); |
| + } |
| + |
| + reload() { |
| + for (var context of this._executionContexts) |
| + this._fireEvent("Runtime.executionContextDestroyed", {executionContextId: context.id}); |
| + this._scripts = []; |
| + this._scriptContents.clear(); |
| + this._executionContexts = []; |
| + this._fireEvent("Runtime.executionContextsCleared", {}); |
| + this._executionContexts.push(this._createExecutionContext(this._mainFrame, false)); |
| + for (var context of this._executionContexts) |
| + this._fireEvent("Runtime.executionContextCreated", {context: context}); |
| + |
| + if (this._hasDOMCapability()) |
| + this._fireEvent("Page.frameNavigated", {frame: this._mainFrame}); |
|
dgozman
2017/03/16 18:04:01
Let's send all of the frame-related notifications.
lushnikov
2017/03/16 20:59:22
Done.
|
| + } |
| + |
| + _createExecutionContext(frame, isContentScript) { |
| + return { |
| + id: nextId(), |
| + auxData: {isDefault: !isContentScript, frameId: frame.id }, |
| + origin: frame.securityOrigin, |
| + name: '', |
| + }; |
| + } |
| + |
| + _enableRuntime(id, params) { |
|
dgozman
2017/03/16 18:04:01
Let's name these "runtimeEnable", "pageGetResource
lushnikov
2017/03/16 20:59:22
Done.
|
| + this._sendResponse(id, {}); |
| + for (var context of this._executionContexts) |
| + this._fireEvent("Runtime.executionContextCreated", {context: context}); |
| + } |
| + |
| + _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: [] |
| + } |
| + } |
| + 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(); |
| + } |
| +}; |
| + |
| +}; |
| + |