2011-03-03 Ilya Tikhonovsky <loislo@chromium.org>
[WebKit-https.git] / LayoutTests / http / tests / inspector / protocol-test.js
1 var initialize_ProtocolTest = function() {
2
3 InspectorTest.filterProps = function(something, nondeterministicProps)
4 {
5     if (something instanceof Object)
6         for (var prop in something)
7             if (prop in nondeterministicProps)
8                 something[prop] = "<" + typeof something[prop] + ">";
9             else
10                 something[prop] = this.filterProps(something[prop], nondeterministicProps);
11     else if (something instanceof Array)
12         for (var i = 0; i < something.length; ++i)
13             something[i] = this.filterProps(something[i], nondeterministicProps);
14     else if (typeof something === "number")
15         something = "<number>";
16     return something;
17 };
18
19 InspectorTest._dumpTestResult = function(callArguments)
20 {
21     var functionName = callArguments.shift();
22     this.filterProps(callArguments, this._nondeterministicProps);
23     var expression = JSON.stringify(callArguments);
24     expression = expression.slice(1, expression.length - 1).replace(/\"<number>\"/g, "<number>");;
25     var sentObject = JSON.parse(this._lastSentTestMessage);
26     var receivedObject = (typeof this._lastReceivedMessage === "string") ? JSON.parse(this._lastReceivedMessage) : this._lastReceivedMessage;
27
28     InspectorTest.addResult("-----------------------------------------------------------");
29     InspectorTest.addResult(this._agentName + "." + functionName + "(" + expression + ")");
30     InspectorTest.addResult("");
31     InspectorTest.addResult("request:");
32     InspectorTest.addObject(sentObject, this._nondeterministicProps);
33     InspectorTest.addResult("");
34     InspectorTest.addResult("response:");
35     InspectorTest.addObject(receivedObject, this._nondeterministicProps);
36     InspectorTest.addResult("");
37 };
38
39 InspectorTest._callback = function(callArguments, result)
40 {
41     this._dumpTestResult(callArguments);
42     this._runNextStep(result);
43 };
44
45 InspectorTest._runNextStep = function(result)
46 {
47     var step = ++this._step;
48     var nextTest = this._testSuite[step];
49     if (nextTest) {
50         var nextTestCopy = JSON.parse(JSON.stringify(nextTest));
51         nextTest.push(this._callback.bind(this, nextTestCopy));
52         var functionName = nextTest.shift();
53         this._agentCoverage[functionName] = "checked";
54         this._agent[functionName].apply(this._agent, nextTest);
55         this._lastSentTestMessage = this._lastSentMessage;
56     }
57     else {
58         InspectorTest.addResult("===========================================================");
59         InspectorTest.addResult("Coverage for " + this._agentName);
60         InspectorTest.addObject(this._agentCoverage);
61         InspectorBackend.dispatch = this._originalDispatch;
62         InspectorFrontendHost.sendMessageToBackend = this._originalSendMessageToBackend;
63         this.completeTest();
64     }
65 };
66
67 InspectorTest.runProtocolTestSuite = function(agentName, testSuite, nondeterministicProps)
68 {
69     this._agentName = agentName;
70     this._testSuite = testSuite;
71     this._nondeterministicProps = {};
72     for (var i = 0; i < nondeterministicProps.length; ++i)
73         this._nondeterministicProps[nondeterministicProps[i]] = true;
74     this._agent = window[agentName];
75     this._agentCoverage = {};
76     for (var key in this._agent)
77         this._agentCoverage[key] = "not checked";
78     this._step = -1;
79
80     this._originalDispatch = InspectorBackend.dispatch;
81     InspectorBackend.dispatch = function(message) { InspectorTest._lastReceivedMessage = message; InspectorTest._originalDispatch.apply(InspectorBackend, [message]); }
82
83     this._originalSendMessageToBackend = InspectorFrontendHost.sendMessageToBackend;
84     InspectorFrontendHost.sendMessageToBackend = function(message) { InspectorTest._lastSentMessage = message; InspectorTest._originalSendMessageToBackend.apply(InspectorFrontendHost, [message]); }
85
86     this._runNextStep();
87 };
88
89 };