Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / sources / debugger / resources / framework.js
1 // A framework for testing.
2
3 var Framework = {};
4
5 Framework.safeRun = function(callback, onSuccess, onException, breakOnUncaught)
6 {
7     try {
8         callback();
9         if (onSuccess)
10             Framework.safeRun(onSuccess, undefined, onException, breakOnUncaught);
11     } catch (e) {
12         if (onException)
13             Framework.safeRun(onException, undefined, breakOnUncaught ? Framework.breakInFramework : undefined);
14         else if (breakOnUncaught)
15             Framework.breakInFramework();
16     }
17 }
18
19 Framework.throwFrameworkException = function(msg)
20 {
21     throw Error("FrameworkException" + (msg ? ": " + msg : ""));
22 }
23
24 Framework.breakInFramework = function()
25 {
26     debugger;
27 }
28
29 Framework.empty = function()
30 {
31 }
32
33 Framework.doSomeWork = function()
34 {
35     const numberOfSteps = 50;
36     for (var i = 0; i < numberOfSteps; ++i) {
37         if (window["dummy property should not exist!" + i]) // Prevent optimizations.
38             return i;
39         Framework.safeRun(Framework.empty, Framework.empty, Framework.empty, true);
40     }
41 }
42
43 Framework.schedule = function(callback, delay)
44 {
45     setTimeout(callback, delay || 0);
46 }
47
48 Framework.willSchedule = function(callback, delay)
49 {
50     return function Framework_willSchedule() {
51         return Framework.schedule(callback, delay);
52     };
53 }
54
55 Framework.doSomeAsyncChainCalls = function(callback)
56 {
57     var func1 = Framework.willSchedule(function Framework_inner1() {
58         if (callback)
59             callback();
60     });
61     var func2 = Framework.willSchedule(function Framework_inner2() {
62         if (window.callbackFromFramework)
63             window.callbackFromFramework(func1);
64         else
65             func1();
66     });
67     Framework.schedule(func2);
68 }
69
70 Framework.appendChild = function(parent, child)
71 {
72     parent.appendChild(child);
73 }
74
75 Framework.sendXHR = function(url)
76 {
77     var request = new XMLHttpRequest();
78     request.open("GET", url, true);
79     try { request.send(); } catch (e) {}
80 }
81
82 Framework.addEventListener = function(element, eventType, listener, capture)
83 {
84     function Framework_eventListener()
85     {
86         if (listener)
87             listener();
88     }
89
90     function Framework_remover()
91     {
92         element.removeEventListener(eventType, Framework_eventListener, capture);
93     }
94
95     element.addEventListener(eventType, Framework_eventListener, capture);
96     return Framework_remover;
97 }
98
99 Framework.bind = function(func, thisObject, var_args)
100 {
101     var args = Array.prototype.slice.call(arguments, 2);
102
103     function Framework_bound(var_args)
104     {
105         return func.apply(thisObject, args.concat(Array.prototype.slice.call(arguments)));
106     }
107     Framework_bound.toString = function()
108     {
109         return "Framework_bound: " + func;
110     };
111     return Framework_bound;
112 }
113
114 Framework.throwInNative = function()
115 {
116     var wrongJson = "})";
117     window["dummy"] = JSON.parse(wrongJson);
118 }
119
120 Framework.throwInNativeAndCatch = function()
121 {
122     try {
123         Framework.throwInNative();
124     } catch(e) {
125     }
126 }
127
128 Framework.throwFrameworkExceptionAndCatch = function()
129 {
130     try {
131         Framework.throwFrameworkException();
132     } catch(e) {
133     }
134 }