Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / resources / leak-check.js
1 // include resources/js-test.js before this file.
2
3 function getCounterValues(callback) {
4     testRunner.resetTestHelperControllers();
5     asyncGC(function() {
6         var ret = {'numberOfLiveDocuments': window.internals.numberOfLiveDocuments()};
7
8         var refCountedInstances = JSON.parse(window.internals.dumpRefCountedInstanceCounts());
9         for (typename in refCountedInstances)
10             ret['numberOfInstances-'+typename] = refCountedInstances[typename];
11
12         callback(ret);
13     });
14
15 }
16
17 function compareValues(countersBefore, countersAfter, tolerance) {
18     for (type in tolerance) {
19         var before = countersBefore[type];
20         var after = countersAfter[type];
21
22         if (after - before <= tolerance[type])
23             testPassed('The difference of counter "'+type+'" before and after the cycle is under the threshold of '+tolerance[type]+'.');
24         else
25             testFailed('counter "'+type+'" was '+before+' before and now '+after+' after the cycle. This exceeds the threshold of '+tolerance[type]+'.');
26     }
27 }
28
29 function doLeakTest(src, tolerance) {
30     var frame = document.createElement('iframe');
31     document.body.appendChild(frame);
32     function loadSourceIntoIframe(src, callback) {
33         var originalSrc = frame.src;
34
35         frame.onload = function() {
36             if (frame.src === originalSrc)
37                 return true;
38
39             callback();
40             return true;
41         };
42         frame.src = src;
43     }
44
45     jsTestIsAsync = true;
46     if (!window.internals) {
47         debug("This test only runs on DumpRenderTree, as it requires existence of window.internals and cross-domain resource access check disabled.");
48         finishJSTest();
49     }
50
51     loadSourceIntoIframe('about:blank', function() {
52         // blank document loaded...
53         getCounterValues(function(countersBefore) {
54             loadSourceIntoIframe(src, function() {
55                 // target document loaded...
56
57                 loadSourceIntoIframe('about:blank', function() {
58                     // target document unloaded...
59
60                     // Measure counter values on next timer event. This is needed
61                     // to correctly handle deref cycles for some ActiveDOMObjects
62                     // such as XMLHttpRequest.
63                     setTimeout(function() {
64                         getCounterValues(function(countersAfter) {
65                             compareValues(countersBefore, countersAfter, tolerance);
66                             finishJSTest();
67                         });
68                     }, 0);
69                 });
70             });
71         });
72     });
73 }
74
75 function htmlToUrl(html) {
76     return 'data:text/html;charset=utf-8,' + html;
77 }
78
79 function grabScriptText(id) {
80     return document.getElementById(id).innerText;
81 }
82
83 // include fast/js/resources/js-test-post.js after this file.