Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / debugger / async-callstack-promises.html
1 <html>
2 <head>
3 <script src="../../http/tests/inspector/inspector-test.js"></script>
4 <script src="../../http/tests/inspector/debugger-test.js"></script>
5 <script>
6
7 function timeoutPromise(value, ms)
8 {
9     return new Promise(function promiseCallback(resolve, reject) {
10         function resolvePromise()
11         {
12             resolve(value);
13         }
14         function rejectPromise()
15         {
16             reject(value);
17         }
18         if (value instanceof Error)
19             setTimeout(rejectPromise, ms || 0);
20         else
21             setTimeout(resolvePromise, ms || 0);
22     });
23 }
24
25 function settledPromise(value)
26 {
27     function resolveCallback(resolve, reject)
28     {
29         resolve(value);
30     }
31     function rejectCallback(resolve, reject)
32     {
33         reject(value);
34     }
35     if (value instanceof Error)
36         return new Promise(rejectCallback);
37     else
38         return new Promise(resolveCallback);
39 }
40
41 function testFunction()
42 {
43     setTimeout(testFunctionTimeout, 0);
44 }
45
46 function testFunctionTimeout()
47 {
48     var functions = [doTestPromiseConstructor, doTestSettledPromises, doTestChainedPromises, doTestPromiseAll, doTestThrowFromChain, doTestPromiseResolveAndReject];
49     for (var i = 0; i < functions.length; ++i)
50         functions[i]();
51 }
52
53 function thenCallback(value)
54 {
55     debugger;
56 }
57
58 function errorCallback(error)
59 {
60     debugger;
61 }
62
63 function doTestPromiseConstructor()
64 {
65     new Promise(function promiseCallback(resolve, reject) {
66         resolve(1);
67         debugger;
68     });
69 }
70
71 function doTestSettledPromises()
72 {
73     settledPromise("resolved").then(thenCallback, errorCallback);
74     settledPromise(Error("rejected")).then(thenCallback, errorCallback);
75 }
76
77 function doTestChainedPromises()
78 {
79     timeoutPromise(1).then(function chained1() {
80         debugger;
81         return timeoutPromise(2);
82     }).then(function chained2() {
83         debugger;
84         return 3;
85     }).then(function chained3() {
86         debugger;
87         return settledPromise(4);
88     }).then(function chained4() {
89         debugger;
90         return timeoutPromise(5);
91     }).then(thenCallback, errorCallback);
92
93     timeoutPromise(1)
94         .then(JSON.stringify)
95         .then(JSON.parse)
96         .then(function afterJSONStringifyAndParse() {
97             debugger;
98         });
99 }
100
101 function doTestPromiseAll()
102 {
103     Promise.all([11, 22, 33, 44, 55].map(timeoutPromise))
104         .then(thenCallback, errorCallback);
105 }
106
107 function doTestThrowFromChain()
108 {
109     timeoutPromise(1).then(function chained1() {
110         return timeoutPromise(2);
111     }).then(function chained2() {
112         return settledPromise(3);
113     }).then(function chained3() {
114         throw Error("thrown from chained3");
115     }).then(function chained4() {
116         return timeoutPromise(5);
117     }).catch(function catchCallback() {
118         debugger;
119     });
120
121     timeoutPromise(1).then(function chained1() {
122         return timeoutPromise(2);
123     }).then(function chained2() {
124         return timeoutPromise(3);
125     }).then(function chained3() {
126         return timeoutPromise(Error(4));
127     }).then(function chained4() {
128         return timeoutPromise(5);
129     }).catch(function catchCallback() {
130         debugger;
131     });
132 }
133
134 function doTestPromiseResolveAndReject()
135 {
136     Promise.resolve(1).then(thenCallback, errorCallback);
137     Promise.reject(Error("2")).then(thenCallback, errorCallback);
138 }
139
140 var test = function()
141 {
142     var totalDebuggerStatements = 14;
143     var maxAsyncCallStackDepth = 4;
144     InspectorTest.runAsyncCallStacksTest(totalDebuggerStatements, maxAsyncCallStackDepth);
145 }
146
147 </script>
148 </head>
149
150 <body onload="runTest()">
151 <p>
152 Tests asynchronous call stacks for Promises.
153 </p>
154
155 </body>
156 </html>