Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / about_tracing / mock_request_handler.js
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.
4
5 'use strict';
6
7 tvcm.exportTo('about_tracing', function() {
8   function MockRequestHandler() {
9     this.requests = [];
10     this.nextRequestIndex = 0;
11     this.allowLooping = false;
12   }
13
14   MockRequestHandler.prototype = {
15     expectRequest: function(method, pathRegex, generateResponse) {
16       var generateResponseCb;
17       if (typeof generateResponse === 'function') {
18         generateResponseCb = generateResponse;
19       } else {
20         generateResponseCb = function() {
21           return generateResponse;
22         };
23       }
24
25       this.requests.push({
26         method: method,
27         pathRegex: pathRegex,
28         generateResponseCb: generateResponseCb});
29     },
30
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++;
38         } else {
39           this.nextRequestIndex = (this.nextRequestIndex + 1) %
40               this.requests.length;
41         }
42
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);
48       }.bind(this));
49     },
50
51     assertAllRequestsHandled: function() {
52       if (this.allowLooping)
53         throw new Error('Incompatible with allowLooping');
54       assertTrue(this.nextRequestIndex == this.requests.length);
55     }
56   };
57
58   return {
59     MockRequestHandler: MockRequestHandler
60   };
61 });