1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.exportTo('about_tracing', function() {
8 function MockRequestHandler() {
10 this.nextRequestIndex = 0;
11 this.allowLooping = false;
14 MockRequestHandler.prototype = {
15 expectRequest: function(method, pathRegex, generateResponse) {
16 var generateResponseCb;
17 if (typeof generateResponse === 'function') {
18 generateResponseCb = generateResponse;
20 generateResponseCb = function() {
21 return generateResponse;
28 generateResponseCb: generateResponseCb});
31 tracingRequest: function(method, path, data) {
32 return new Promise(function(resolver) {
33 var requestIndex = this.nextRequestIndex;
34 if (requestIndex >= this.requests.length)
35 throw new Error('Unhandled request');
36 if (!this.allowLooping) {
37 this.nextRequestIndex++;
39 this.nextRequestIndex = (this.nextRequestIndex + 1) %
43 var req = this.requests[requestIndex];
44 assertTrue(req.method === method);
45 assertTrue(path.search(req.pathRegex) == 0);
46 var resp = req.generateResponseCb(data, path);
47 resolver.resolve(resp);
51 assertAllRequestsHandled: function() {
52 if (this.allowLooping)
53 throw new Error('Incompatible with allowLooping');
54 assertTrue(this.nextRequestIndex == this.requests.length);
59 MockRequestHandler: MockRequestHandler