Upstream version 9.38.198.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.networkRequests = function() {
12     return WebInspector.networkLog.requests.slice();
13 }
14
15 InspectorTest.dumpNetworkRequests = function()
16 {
17     var requests = InspectorTest.networkRequests();
18     requests.sort(function(a, b) {return a.url.localeCompare(b.url);});
19     InspectorTest.addResult("resources count = " + requests.length);
20     for (i = 0; i < requests.length; i++)
21         InspectorTest.addResult(requests[i].url);
22 }
23
24 InspectorTest.resetInspectorResourcesData = function(callback)
25 {
26     InspectorTest.evaluateInPage("resetInspectorResourcesData()", nextStep);
27
28     function nextStep(result)
29     {
30         if (!result) {
31             InspectorTest.addResult("This test can not be run as window.internals is not available.");
32             Inspector.completeTest();
33         } else
34             callback();
35     }
36 }
37
38 InspectorTest.makeSimpleXHR = function(method, url, async, callback)
39 {
40     InspectorTest.makeXHR(method, url, async, undefined, undefined, [], false, undefined, undefined, callback);
41 }
42
43 InspectorTest.makeSimpleXHRWithPayload = function(method, url, async, payload, callback)
44 {
45     InspectorTest.makeXHR(method, url, async, undefined, undefined, [], false, payload, undefined, callback);
46 }
47
48 InspectorTest.makeXHR = function(method, url, async, user, password, headers, withCredentials, payload, type, callback)
49 {
50     var args = {};
51     args.method = method;
52     args.url = url;
53     args.async = async;
54     args.user = user;
55     args.password = password;
56     args.headers = headers;
57     args.withCredentials = withCredentials;
58     args.payload = payload;
59     args.type = type;
60     var jsonArgs = JSON.stringify(args).replace(/\"/g, "\\\"");
61
62     function innerCallback(msg)
63     {
64         if (msg.consoleMessage().messageText.indexOf("XHR loaded") !== -1)
65             callback();
66         else
67             InspectorTest.addConsoleSniffer(innerCallback);
68     }
69
70     InspectorTest.addConsoleSniffer(innerCallback);
71     InspectorTest.evaluateInPage("makeXHRForJSONArguments(\"" + jsonArgs + "\")");
72 }
73
74 };
75
76 function makeSimpleXHR(method, url, async, callback)
77 {
78     makeSimpleXHRWithPayload(method, url, async, null, callback);
79 }
80
81 function makeSimpleXHRWithPayload(method, url, async, payload, callback)
82 {
83     makeXHR(method, url, async, undefined, undefined, [], false, payload, callback)
84 }
85
86 function makeXHR(method, url, async, user, password, headers, withCredentials, payload, type, callback)
87 {
88     var xhr = new XMLHttpRequest();
89     xhr.responseType = type;
90     xhr.onreadystatechange = function()
91     {
92         if (xhr.readyState === XMLHttpRequest.DONE) {
93             if (typeof(callback) === "function")
94                 callback();
95         }
96     }
97     xhr.open(method, url, async, user, password);
98     xhr.withCredentials = withCredentials;
99     for (var  i = 0; i < headers.length; ++i)
100         xhr.setRequestHeader(headers[i][0], headers[i][1]);
101     xhr.send(payload);
102 }
103
104 function makeXHRForJSONArguments(jsonArgs)
105 {
106     var args = JSON.parse(jsonArgs);
107     makeXHR(args.method, args.url, args.async, args.user, args.password, args.headers || [], args.withCredentials, args.payload, args.type, xhrLoadedCallback);
108 }
109
110
111 function resetInspectorResourcesData()
112 {
113     if (!window.internals)
114         return false;
115
116     internals.setInspectorResourcesDataSizeLimits(10 * 1000 * 1000, 1000 * 1000);
117     return true;
118 }
119