Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 var initialize_EmptyPageMock = function() { | |
| 2 | |
| 3 var id = 0; | |
| 4 | |
| 5 function nextId(prefix) { | |
| 6 return (prefix || "") + (++id); | |
| 7 } | |
| 8 | |
| 9 InspectorTest.connectToPage = function(targetName, pageMock, makeMainTarget) { | |
| 10 var mockTarget = SDK.targetManager.createTarget(nextId("mock-target-"), targ etName, pageMock.capabilities(), params => pageMock.createConnection(params)); | |
| 11 if (makeMainTarget) { | |
| 12 SDK.targetManager._targets = SDK.targetManager._targets.filter(target => target !== mockTarget); | |
| 13 SDK.targetManager._targets.unshift(mockTarget); | |
| 14 } | |
| 15 return mockTarget; | |
| 16 } | |
| 17 | |
| 18 InspectorTest.PageMock = class { | |
| 19 constructor(url) { | |
| 20 this._url = url; | |
| 21 this._capabilities = SDK.Target.Capability.DOM | SDK.Target.Capability.J S | SDK.Target.Capability.Browser; | |
| 22 | |
| 23 this._mainFrame = { | |
| 24 id: nextId(), | |
| 25 loaderId: nextId(), | |
| 26 mimeType: "text/html", | |
| 27 securityOrigin: this._url, | |
| 28 url: this._url | |
| 29 }; | |
| 30 | |
| 31 this._executionContexts = []; | |
| 32 this._executionContexts.push(this._createExecutionContext(this._mainFram e, false)); | |
| 33 | |
| 34 this._debuggerEnabled = false; | |
| 35 this._scripts = []; | |
| 36 this._scriptContents = new Map(); | |
| 37 | |
| 38 this._enablePageAgent = true; | |
| 39 this._initializeDispatchMap(); | |
| 40 } | |
| 41 | |
| 42 capabilities() { | |
| 43 return this._capabilities; | |
| 44 } | |
| 45 | |
| 46 disableDOMCapability() { | |
| 47 this._capabilities = this._capabilities & (~SDK.Target.Capability.DOM); | |
| 48 this._initializeDispatchMap(); | |
| 49 } | |
| 50 | |
| 51 _hasDOMCapability() { | |
| 52 return !!(this._capabilities & SDK.Target.Capability.DOM); | |
| 53 } | |
| 54 | |
| 55 _initializeDispatchMap() { | |
| 56 this._dispatchMap = { | |
| 57 "Debugger.enable": this._enableDebugger, | |
| 58 "Debugger.getScriptSource": this._getScriptSource, | |
| 59 "Debugger.setBlackboxPatterns": (id, params) => this._sendResponse(i d, {}), | |
| 60 "Runtime.enable": this._enableRuntime, | |
| 61 }; | |
| 62 if (this._hasDOMCapability()) { | |
| 63 Object.assign(this._dispatchMap, { | |
| 64 "Page.enable": (id, params) => this._sendResponse(id, {}), | |
| 65 "Page.getResourceTree": this._getResourceTree, | |
| 66 }); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 createConnection(params) { | |
| 71 this._connection = new MockPageConnection(this, params); | |
| 72 return this._connection; | |
| 73 } | |
| 74 | |
| 75 evalScript(url, content, isContentScript) { | |
| 76 var id = nextId(); | |
| 77 this._scriptContents.set(id, content); | |
| 78 | |
| 79 var context = this._executionContexts.find(context => context.auxData.is Default !== isContentScript); | |
| 80 if (!context) { | |
| 81 context = this._createExecutionContext(this._mainFrame, isContentScr ipt); | |
| 82 this._fireEvent("Runtime.executionContextCreated", {context: context }); | |
| 83 } | |
| 84 | |
| 85 var text = new Common.Text(content); | |
| 86 var script = { | |
| 87 scriptId: id, | |
| 88 url: url, | |
| 89 startLine: 0, | |
| 90 startColumn: 0, | |
| 91 endLine: text.lineCount(), | |
| 92 endColumn: text.lineAt(text.lineCount()).length - 1, | |
| 93 executionContextId: context.id, | |
| 94 hash: String.hashCode(content), | |
| 95 executionContextAuxData: context.auxData, | |
| 96 sourceMapURL: "", | |
| 97 hasSourceURL: true, | |
| 98 isLiveEdit: false, | |
| 99 isModule: false, | |
| 100 length: content.length | |
| 101 }; | |
| 102 this._scripts.push(script); | |
| 103 if (this._debuggerEnabled) | |
| 104 this._fireEvent("Debugger.scriptParsed", script); | |
| 105 } | |
| 106 | |
| 107 reload() { | |
| 108 for (var context of this._executionContexts) | |
| 109 this._fireEvent("Runtime.executionContextDestroyed", {executionConte xtId: context.id}); | |
| 110 this._scripts = []; | |
| 111 this._scriptContents.clear(); | |
| 112 this._executionContexts = []; | |
| 113 this._fireEvent("Runtime.executionContextsCleared", {}); | |
| 114 this._executionContexts.push(this._createExecutionContext(this._mainFram e, false)); | |
| 115 for (var context of this._executionContexts) | |
| 116 this._fireEvent("Runtime.executionContextCreated", {context: context }); | |
| 117 | |
| 118 if (this._hasDOMCapability()) | |
| 119 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.
| |
| 120 } | |
| 121 | |
| 122 _createExecutionContext(frame, isContentScript) { | |
| 123 return { | |
| 124 id: nextId(), | |
| 125 auxData: {isDefault: !isContentScript, frameId: frame.id }, | |
| 126 origin: frame.securityOrigin, | |
| 127 name: '', | |
| 128 }; | |
| 129 } | |
| 130 | |
| 131 _enableRuntime(id, params) { | |
|
dgozman
2017/03/16 18:04:01
Let's name these "runtimeEnable", "pageGetResource
lushnikov
2017/03/16 20:59:22
Done.
| |
| 132 this._sendResponse(id, {}); | |
| 133 for (var context of this._executionContexts) | |
| 134 this._fireEvent("Runtime.executionContextCreated", {context: context }); | |
| 135 } | |
| 136 | |
| 137 _enableDebugger(id, params) { | |
| 138 this._debuggerEnabled = true; | |
| 139 this._sendResponse(id, {}); | |
| 140 for (var script of this._scripts) | |
| 141 this._fireEvent("Debugger.scriptParsed", script); | |
| 142 } | |
| 143 | |
| 144 _getResourceTree(id, params) { | |
| 145 var result = { | |
| 146 frameTree: { | |
| 147 frame: this._mainFrame, | |
| 148 resources: [] | |
| 149 } | |
| 150 } | |
| 151 this._sendResponse(id, result); | |
| 152 } | |
| 153 | |
| 154 _getScriptSource(id, params) { | |
| 155 if (!this._scriptContents.has(params.scriptId)) { | |
| 156 this._sendResponse(id, undefined, { | |
| 157 message: 'Can\'t get script content for id ' + params.scriptId, | |
| 158 code: 1, | |
| 159 }); | |
| 160 return; | |
| 161 } | |
| 162 var result = {scriptSource: this._scriptContents.get(params.scriptId)}; | |
| 163 this._sendResponse(id, result); | |
| 164 } | |
| 165 | |
| 166 _dispatch(id, methodName, params, message) { | |
| 167 var handler = this._dispatchMap[methodName]; | |
| 168 if (handler) | |
| 169 return handler.call(this, id, params); | |
| 170 this._sendResponse(id, undefined, { | |
| 171 message: 'Can\'t handle command ' + methodName, | |
| 172 code: Protocol.InspectorBackend.DevToolsStubErrorCode, | |
| 173 }); | |
| 174 } | |
| 175 | |
| 176 _sendResponse(id, result, error) { | |
| 177 var message = { | |
| 178 id: id, | |
| 179 result: result, | |
| 180 error: error | |
| 181 }; | |
| 182 this._connection.sendMessageToDevTools(message); | |
| 183 } | |
| 184 | |
| 185 _fireEvent(methodName, params) { | |
| 186 var message = { | |
| 187 method: methodName, | |
| 188 params: params | |
| 189 }; | |
| 190 this._connection.sendMessageToDevTools(message); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 var MockPageConnection = class { | |
| 195 /** | |
| 196 * @param {!Protocol.InspectorBackend.Connection.Params} params | |
| 197 */ | |
| 198 constructor(page, params) { | |
| 199 this._page = page; | |
| 200 this._onMessage = params.onMessage; | |
| 201 this._onDisconnect = params.onDisconnect; | |
| 202 } | |
| 203 | |
| 204 sendMessageToDevTools(message) { | |
| 205 setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0) ; | |
| 206 } | |
| 207 | |
| 208 /** | |
| 209 * @override | |
| 210 * @param {string} message | |
| 211 */ | |
| 212 sendMessage(message) { | |
| 213 var json = JSON.parse(message); | |
| 214 this._page._dispatch(json.id, json.method, json.params, message); | |
| 215 } | |
| 216 | |
| 217 /** | |
| 218 * @override | |
| 219 * @return {!Promise} | |
| 220 */ | |
| 221 disconnect() { | |
| 222 this._onDisconnect.call(null, 'force disconnect'); | |
| 223 this._onDisconnect = null; | |
| 224 this._onMessage = null; | |
| 225 return Promise.resolve(); | |
| 226 } | |
| 227 }; | |
| 228 | |
| 229 }; | |
| 230 | |
| OLD | NEW |