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