Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/inspector/page-mock.js

Issue 2879503002: [Devtools] Added product entry tests for network
Patch Set: [Devtools] Added product entry tests for network Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 var initialize_EmptyPageMock = function() { 1 var initialize_EmptyPageMock = function() {
2 2
3 var id = 0; 3 var id = 0;
4 4
5 function nextId(prefix) { 5 function nextId(prefix) {
6 return (prefix || '') + (++id); 6 return (prefix || '') + (++id);
7 } 7 }
8 8
9 InspectorTest.connectToPage = function(targetName, pageMock, makeMainTarget) { 9 InspectorTest.connectToPage = function(targetName, pageMock, makeMainTarget) {
10 var mockTarget = SDK.targetManager.createTarget(nextId('mock-target-'), targ etName, pageMock.capabilities(), params => pageMock.createConnection(params)); 10 var mockTarget = SDK.targetManager.createTarget(nextId('mock-target-'), targ etName, pageMock.capabilities(), params => pageMock.createConnection(params));
11 if (makeMainTarget) { 11 if (makeMainTarget) {
12 SDK.targetManager._targets = SDK.targetManager._targets.filter(target => target !== mockTarget); 12 SDK.targetManager._targets = SDK.targetManager._targets.filter(target => target !== mockTarget);
13 SDK.targetManager._targets.unshift(mockTarget); 13 SDK.targetManager._targets.unshift(mockTarget);
14 } 14 }
15 return mockTarget; 15 return mockTarget;
16 } 16 }
17 17
18 InspectorTest.PageMock = class { 18 InspectorTest.PageMock = class {
19 constructor(url) { 19 constructor(url) {
20 this._url = url; 20 this._url = url;
21 this._capabilities = SDK.Target.Capability.DOM | SDK.Target.Capability.J S | SDK.Target.Capability.Browser; 21 this._capabilities = SDK.Target.Capability.DOM | SDK.Target.Capability.J S | SDK.Target.Capability.Browser | SDK.Target.Capability.Network;
22 /** @type {!Set<string>} */ 22 /** @type {!Set<string>} */
23 this._enabledDomains = new Set(); 23 this._enabledDomains = new Set();
24 24
25 this._mainFrame = { 25 this._mainFrame = {
26 id: nextId(), 26 id: nextId(),
27 loaderId: nextId(), 27 loaderId: nextId(),
28 mimeType: 'text/html', 28 mimeType: 'text/html',
29 securityOrigin: this._url, 29 securityOrigin: this._url,
30 url: this._url 30 url: this._url
31 }; 31 };
32 32
33 this._executionContexts = []; 33 this._executionContexts = [];
34 this._executionContexts.push(this._createExecutionContext(this._mainFram e, false)); 34 this._executionContexts.push(this._createExecutionContext(this._mainFram e, false));
35 35
36 this._scripts = []; 36 this._scripts = [];
37 this._scriptContents = new Map(); 37 this._scriptContents = new Map();
38 38
39 this._childFrames = [];
40 this._pageGetResourceTreeHasRan = false;
41
39 this._dispatchMap = { 42 this._dispatchMap = {
40 'Debugger.enable': this._debuggerEnable, 43 'Debugger.enable': this._debuggerEnable,
41 'Debugger.getScriptSource': this._debuggerGetScriptSource, 44 'Debugger.getScriptSource': this._debuggerGetScriptSource,
42 'Debugger.setBlackboxPatterns': (id, params) => this._sendResponse(i d, {}), 45 'Debugger.setBlackboxPatterns': (id, params) => this._sendResponse(i d, {}),
43 'Runtime.enable': this._runtimeEnable, 46 'Runtime.enable': this._runtimeEnable,
44 'Page.enable': this._pageEnable, 47 'Page.enable': this._pageEnable,
45 'Page.getResourceTree': this._pageGetResourceTree, 48 'Page.getResourceTree': this._pageGetResourceTree,
49 'Network.enable': this._networkEnable
46 }; 50 };
47 } 51 }
48 52
49 capabilities() { 53 capabilities() {
50 return this._capabilities; 54 return this._capabilities;
51 } 55 }
52 56
53 disableDOMCapability() { 57 disableDOMCapability() {
54 this._capabilities = this._capabilities & (~SDK.Target.Capability.DOM); 58 this._capabilities = this._capabilities & (~SDK.Target.Capability.DOM);
55 } 59 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 this._fireEvent('Page.domContentEventFired', {timestamp: Date.now() / 10 00}); 114 this._fireEvent('Page.domContentEventFired', {timestamp: Date.now() / 10 00});
111 } 115 }
112 116
113 close() { 117 close() {
114 if (this._connection) { 118 if (this._connection) {
115 this._connection.disconnect(); 119 this._connection.disconnect();
116 this._connection = null; 120 this._connection = null;
117 } 121 }
118 } 122 }
119 123
124 /**
125 * @param {string} url
126 * @return {!Promise<string>} frameId
127 */
128 addFrame(url) {
129 console.assert(!this._pageGetResourceTreeHasRan, "Cannot add frames afte r getResourceTreeTree has ran.");
dgozman 2017/05/10 22:57:14 InspectorTest.completeTest here so we see the test
130 var frameId = nextId('page-');
131 this._childFrames.push({
132 id: frameId,
133 loaderId: this._mainFrame.loaderId,
dgozman 2017/05/10 22:57:14 loaderId should be unique.
134 url: url,
135 securityOrigin: url,
136 mimeType: "text/html"
137 });
138 return frameId;
139 }
140
141
142 /**
143 * @param {string} url
144 * @param {!SDK.ResourceTreeFrame=} frame
145 * @return {!Promise<string>}
dgozman 2017/05/10 22:57:14 Why return Promise? I think we should waitForReque
146 */
147 requestWillBeSent(url, frame) {
dgozman 2017/05/10 22:57:14 Let's call this sendXHR and issue responseReceived
148 var requestId = nextId('network-');
149 return this._fireEvent('Network.requestWillBeSent', {
150 requestId: requestId,
151 frameId: (frame || this._mainFrame).id,
152 loaderId: this._mainFrame.loaderId,
153 documentURL: this._mainFrame.url,
154 request: {
155 url: url,
156 method: 'GET',
157 headers: {},
158 initialPriority: 'Medium',
159 referrerPolicy: 'origin'
160 },
161 timestamp: 0,
162 initiator: {
163 name: 'other'
164 },
165 type: "Other"
166 }).then(() => requestId);
167 }
168
169
120 _createExecutionContext(frame, isContentScript) { 170 _createExecutionContext(frame, isContentScript) {
121 return { 171 return {
122 id: nextId(), 172 id: nextId(),
123 auxData: {isDefault: !isContentScript, frameId: frame.id }, 173 auxData: {isDefault: !isContentScript, frameId: frame.id },
124 origin: frame.securityOrigin, 174 origin: frame.securityOrigin,
125 name: '', 175 name: '',
126 }; 176 };
127 } 177 }
128 178
129 // ------------------------------------------------------------------------- 179 // -------------------------------------------------------------------------
130 // Command Handlers 180 // Command Handlers
131 // ------------------------------------------------------------------------- 181 // -------------------------------------------------------------------------
132 182
183 _networkEnable(id, params) {
184 this._enabledDomains.add('Network');
185 this._sendResponse(id, {});
186 }
187
133 _debuggerEnable(id, params) { 188 _debuggerEnable(id, params) {
134 this._enabledDomains.add('Debugger'); 189 this._enabledDomains.add('Debugger');
135 this._sendResponse(id, {}); 190 this._sendResponse(id, {});
136 for (var script of this._scripts) 191 for (var script of this._scripts)
137 this._fireEvent('Debugger.scriptParsed', script); 192 this._fireEvent('Debugger.scriptParsed', script);
138 } 193 }
139 194
140 _debuggerGetScriptSource(id, params) { 195 _debuggerGetScriptSource(id, params) {
141 if (!this._scriptContents.has(params.scriptId)) { 196 if (!this._scriptContents.has(params.scriptId)) {
142 this._sendResponse(id, undefined, { 197 this._sendResponse(id, undefined, {
(...skipping 12 matching lines...) Expand all
155 for (var context of this._executionContexts) 210 for (var context of this._executionContexts)
156 this._fireEvent('Runtime.executionContextCreated', {context: context }); 211 this._fireEvent('Runtime.executionContextCreated', {context: context });
157 } 212 }
158 213
159 _pageEnable(id, params) { 214 _pageEnable(id, params) {
160 this._enabledDomains.add('Page'); 215 this._enabledDomains.add('Page');
161 this._sendResponse(id, {}); 216 this._sendResponse(id, {});
162 } 217 }
163 218
164 _pageGetResourceTree(id, params) { 219 _pageGetResourceTree(id, params) {
220 this._pageGetResourceTreeHasRan = true;
221 var childFrames = this._childFrames.map(frame => ({frame: frame, resourc es: []}));
165 var result = { 222 var result = {
166 frameTree: { 223 frameTree: {
167 frame: this._mainFrame, 224 frame: this._mainFrame,
225 childFrames: childFrames,
168 resources: [] 226 resources: []
dgozman 2017/05/10 22:57:14 Let's report html resource. Or add TODO for it.
169 } 227 }
170 } 228 }
171 this._sendResponse(id, result); 229 this._sendResponse(id, result);
172 } 230 }
173 231
174 _isSupportedDomain(methodName) { 232 _isSupportedDomain(methodName) {
175 var domain = methodName.split('.')[0]; 233 var domain = methodName.split('.')[0];
176 if (domain === 'Page') 234 if (domain === 'Page')
177 return !!(this._capabilities & SDK.Target.Capability.DOM); 235 return !!(this._capabilities & SDK.Target.Capability.DOM);
178 return true; 236 return true;
179 } 237 }
180 238
181 _dispatch(id, methodName, params, message) { 239 _dispatch(id, methodName, params, message) {
182 var handler = this._isSupportedDomain(methodName) ? this._dispatchMap[me thodName] : null; 240 var handler = this._isSupportedDomain(methodName) ? this._dispatchMap[me thodName] : null;
183 if (handler) 241 if (handler)
184 return handler.call(this, id, params); 242 return handler.call(this, id, params);
185 this._sendResponse(id, undefined, { 243 this._sendResponse(id, undefined, {
186 message: 'Can\'t handle command ' + methodName, 244 message: 'Can\'t handle command ' + methodName,
187 code: Protocol.InspectorBackend.DevToolsStubErrorCode, 245 code: Protocol.InspectorBackend.DevToolsStubErrorCode,
188 }); 246 });
189 } 247 }
190 248
191 _sendResponse(id, result, error) { 249 _sendResponse(id, result, error) {
192 var message = { 250 var message = {
193 id: id, 251 id: id,
194 result: result, 252 result: result,
195 error: error 253 error: error
196 }; 254 };
197 this._connection.sendMessageToDevTools(message); 255 return this._connection.sendMessageToDevTools(message);
198 } 256 }
199 257
200 _fireEvent(methodName, params) { 258 _fireEvent(methodName, params) {
201 var domain = methodName.split('.')[0]; 259 var domain = methodName.split('.')[0];
202 // Non-enabled domains can't send events. 260 // Non-enabled domains can't send events.
203 if (!this._enabledDomains.has(domain)) 261 if (!this._enabledDomains.has(domain))
204 return; 262 return;
205 var message = { 263 var message = {
206 method: methodName, 264 method: methodName,
207 params: params 265 params: params
208 }; 266 };
209 this._connection.sendMessageToDevTools(message); 267 return this._connection.sendMessageToDevTools(message);
210 } 268 }
211 } 269 }
212 270
213 var MockPageConnection = class { 271 var MockPageConnection = class {
214 /** 272 /**
215 * @param {!Protocol.InspectorBackend.Connection.Params} params 273 * @param {!Protocol.InspectorBackend.Connection.Params} params
216 */ 274 */
217 constructor(page, params) { 275 constructor(page, params) {
218 this._page = page; 276 this._page = page;
219 this._onMessage = params.onMessage; 277 this._onMessage = params.onMessage;
220 this._onDisconnect = params.onDisconnect; 278 this._onDisconnect = params.onDisconnect;
221 } 279 }
222 280
223 sendMessageToDevTools(message) { 281 sendMessageToDevTools(message) {
224 setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0) ; 282 return Promise.resolve().then(() => this._onMessage.call(null, JSON.stri ngify(message)));
allada 2017/05/10 22:25:01 These were in non-deterministic order, so I went t
225 } 283 }
226 284
227 /** 285 /**
228 * @override 286 * @override
229 * @param {string} message 287 * @param {string} message
230 */ 288 */
231 sendMessage(message) { 289 sendMessage(message) {
232 var json = JSON.parse(message); 290 var json = JSON.parse(message);
233 this._page._dispatch(json.id, json.method, json.params, message); 291 this._page._dispatch(json.id, json.method, json.params, message);
234 } 292 }
235 293
236 /** 294 /**
237 * @override 295 * @override
238 * @return {!Promise} 296 * @return {!Promise}
239 */ 297 */
240 disconnect() { 298 disconnect() {
241 this._onDisconnect.call(null, 'force disconnect'); 299 this._onDisconnect.call(null, 'force disconnect');
242 this._onDisconnect = null; 300 this._onDisconnect = null;
243 this._onMessage = null; 301 this._onMessage = null;
244 return Promise.resolve(); 302 return Promise.resolve();
245 } 303 }
246 }; 304 };
247 305
248 }; 306 };
249 307
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698