Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / inspector / sources / debugger / resources / framework.js
blob39b96659616915ec33ef95b102e17be06e39966c
1 // A framework for testing.
3 var Framework = {};
5 Framework.safeRun = function(callback, onSuccess, onException, breakOnUncaught)
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     }
19 Framework.throwFrameworkException = function(msg)
21     throw Error("FrameworkException" + (msg ? ": " + msg : ""));
24 Framework.breakInFramework = function()
26     debugger;
29 Framework.empty = function()
33 Framework.doSomeWork = function()
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     }
43 Framework.schedule = function(callback, delay)
45     setTimeout(callback, delay || 0);
48 Framework.willSchedule = function(callback, delay)
50     return function Framework_willSchedule() {
51         return Framework.schedule(callback, delay);
52     };
55 Framework.doSomeAsyncChainCalls = function(callback)
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);
70 Framework.appendChild = function(parent, child)
72     parent.appendChild(child);
75 Framework.sendXHR = function(url)
77     var request = new XMLHttpRequest();
78     request.open("GET", url, true);
79     try { request.send(); } catch (e) {}
82 Framework.addEventListener = function(element, eventType, listener, capture)
84     function Framework_eventListener()
85     {
86         var result = listener ? listener() : void 0;
87         return result;
88     }
90     function Framework_remover()
91     {
92         element.removeEventListener(eventType, Framework_eventListener, capture);
93     }
95     element.addEventListener(eventType, Framework_eventListener, capture);
96     return Framework_remover;
99 Framework.bind = function(func, thisObject, var_args)
101     var args = Array.prototype.slice.call(arguments, 2);
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;
114 Framework.throwInNative = function()
116     var wrongJson = "})";
117     window["dummy"] = JSON.parse(wrongJson);
120 Framework.throwInNativeAndCatch = function()
122     try {
123         Framework.throwInNative();
124     } catch(e) {
125     }
128 Framework.throwFrameworkExceptionAndCatch = function()
130     try {
131         Framework.throwFrameworkException();
132     } catch(e) {
133     }
136 Framework.scheduleUntilDone = function(callback, delay)
138     Framework.schedule(Framework_scheduleUntilDone, delay);
140     function Framework_scheduleUntilDone()
141     {
142         if (callback && callback())
143             return;
144         Framework.schedule(Framework_scheduleUntilDone, delay);
145     }
148 Framework.doSomeWorkDoNotChangeTopCallFrame = function()
150     const numberOfSteps = 5000;
151     for (var i = 0; i < numberOfSteps; ++i) {
152         if (window["dummy property should not exist!" + i]) // Prevent optimizations.
153             return i;
154     }
155     return -1;
158 Framework.assert = function(var_args)
160     var args = Array.prototype.slice.call(arguments, 0);
161     return console.assert.apply(console, args);
164 Framework.createButtonWithEventListenersAndClick = function(eventListener)
166     var button = document.createElement("input");
167     button.type = "button";
168     Framework.addEventListener(button, "click", Framework.empty, true);
169     Framework.addEventListener(button, "click", Framework.bind(Framework.empty, null), false);
170     Framework.addEventListener(button, "click", Framework.bind(Framework.safeRun, null, Framework.empty, Framework.empty, Framework.empty), true);
171     if (eventListener)
172         Framework.addEventListener(button, "click", eventListener, true);
173     button.click();
174     return button;