Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / inspector / network-test.js
1 // This goes before everything else to keep console message line number invariant.
2 var lastXHRIndex = 0;
3 function xhrLoadedCallback()
4 {
5     // We need to make sure the console message text is unique so that we don't end up with repeat count update only.
6     console.log("XHR loaded: " + (++lastXHRIndex));
7 }
8
9 var initialize_NetworkTest = function() {
10
11 InspectorTest.dumpNetworkRequests = function()
12 {
13     var requests = WebInspector.inspectorView.panel("network").requests.slice();
14     requests.sort(function(a, b) {return a.url.localeCompare(b.url);});
15     InspectorTest.addResult("resources count = " + requests.length);
16     for (i = 0; i < requests.length; i++)
17         InspectorTest.addResult(requests[i].url);
18 }
19
20 InspectorTest.resetInspectorResourcesData = function(callback)
21 {
22     InspectorTest.evaluateInPage("resetInspectorResourcesData()", nextStep);
23
24     function nextStep(result)
25     {
26         if (!result) {
27             InspectorTest.addResult("This test can not be run as window.internals is not available.");
28             Inspector.completeTest();
29         } else
30             callback();
31     }
32 }
33
34 InspectorTest.makeSimpleXHR = function(method, url, async, callback)
35 {
36     InspectorTest.makeXHR(method, url, async, undefined, undefined, [], false, undefined, undefined, callback);
37 }
38
39 InspectorTest.makeSimpleXHRWithPayload = function(method, url, async, payload, callback)
40 {
41     InspectorTest.makeXHR(method, url, async, undefined, undefined, [], false, payload, undefined, callback);
42 }
43
44 InspectorTest.makeXHR = function(method, url, async, user, password, headers, withCredentials, payload, type, callback)
45 {
46     var args = {};
47     args.method = method;
48     args.url = url;
49     args.async = async;
50     args.user = user;
51     args.password = password;
52     args.headers = headers;
53     args.withCredentials = withCredentials;
54     args.payload = payload;
55     args.type = type;
56     var jsonArgs = JSON.stringify(args).replace(/\"/g, "\\\"");
57
58     function innerCallback(msg)
59     {
60         if (msg.consoleMessage().messageText.indexOf("XHR loaded") !== -1)
61             callback();
62         else
63             InspectorTest.addConsoleSniffer(innerCallback);
64     }
65
66     InspectorTest.addConsoleSniffer(innerCallback);
67     InspectorTest.evaluateInPage("makeXHRForJSONArguments(\"" + jsonArgs + "\")");
68 }
69
70 };
71
72 function makeSimpleXHR(method, url, async, callback)
73 {
74     makeSimpleXHRWithPayload(method, url, async, null, callback);
75 }
76
77 function makeSimpleXHRWithPayload(method, url, async, payload, callback)
78 {
79     makeXHR(method, url, async, undefined, undefined, [], false, payload, callback)
80 }
81
82 function makeXHR(method, url, async, user, password, headers, withCredentials, payload, type, callback)
83 {
84     var xhr = new XMLHttpRequest();
85     xhr.responseType = type;
86     xhr.onreadystatechange = function()
87     {
88         if (xhr.readyState === XMLHttpRequest.DONE) {
89             if (typeof(callback) === "function")
90                 callback();
91         }
92     }
93     xhr.open(method, url, async, user, password);
94     xhr.withCredentials = withCredentials;
95     for (var  i = 0; i < headers.length; ++i)
96         xhr.setRequestHeader(headers[i][0], headers[i][1]);
97     xhr.send(payload);
98 }
99
100 function makeXHRForJSONArguments(jsonArgs)
101 {
102     var args = JSON.parse(jsonArgs);
103     makeXHR(args.method, args.url, args.async, args.user, args.password, args.headers || [], args.withCredentials, args.payload, args.type, xhrLoadedCallback);
104 }
105
106
107 function resetInspectorResourcesData()
108 {
109     if (!window.internals)
110         return false;
111
112     internals.setInspectorResourcesDataSizeLimits(10 * 1000 * 1000, 1000 * 1000);
113     return true;
114 }
115