Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / js / Promise-bindings-check-exception.html
1 <!DOCTYPE html>
2 <script src="../../resources/js-test.js"></script>
3 <div id="description"></div>
4 <div id="console"></div>
5 <script>
6 // http://heycam.github.io/webidl/#es-operations
7 description('Operations that return a Promise type should handle exceptions ' +
8             'by returning a rejected Promise with the exception.')
9
10 window.jsTestIsAsync = true;
11
12 var reason;
13
14 function shouldBeRejected(promise, message) {
15     return promise.then(function() {
16         testFailed('Resolved unexpectedly: ' + message);
17     }, function(e) {
18         reason = e;
19         testPassed('Rejected as expected: ' + message);
20         shouldBeTrue('reason instanceof Error');
21         debug(e);
22     });
23 }
24
25 function shouldBeResolved(promise, message) {
26     return promise.then(function() {
27         testPassed('Resolved as expected: ' + message);
28     }, function(e) {
29         testFailed('Rejected unexpectedly: ' + message);
30         reason = e;
31         shouldBeTrue('reason instanceof Error');
32         debug(e);
33     });
34 }
35
36 var check = internals.promiseCheck.bind(internals);
37 var check2 = internals.promiseCheckWithoutExceptionState.bind(internals);
38 var check3 = internals.promiseCheckRange.bind(internals);
39 var check4 = internals.promiseCheckOverload.bind(internals);
40
41 Promise.resolve().then(function() {
42     return shouldBeRejected(check(), 'no arguments');
43 }).then(function() {
44     return shouldBeResolved(check(3, true, {}, '', ['']), 'valid arguments');
45 }).then(function() {
46     return shouldBeResolved(check(null, true, {}, '', []), 'convert(long)');
47 }).then(function() {
48     return shouldBeResolved(check(3, {}, {}, '', []), 'convert(boolean)');
49 }).then(function() {
50     return shouldBeRejected(check(3, true, 3, '', []), 'type error(Dictionary)');
51 }).then(function() {
52     return shouldBeResolved(check(3, true, {}, {}, []), 'convert(String)');
53 }).then(function() {
54     var x = {
55         toString: function() {
56             throw Error('Thrown from toString');
57         }
58     };
59     return shouldBeRejected(check(3, true, {}, x, []), 'conversion error(toString)');
60 }).then(function() {
61     var x = {
62         toString: function() {
63             throw Error('Thrown from toString');
64         }
65     };
66     return shouldBeRejected(check(3, true, {}, '', [x]), 'conversion error([String])');
67 }).then(function() {
68     return shouldBeRejected(check(3, false, {}, '', []), 'rejected by the impl');
69 }).then(function() {
70     return shouldBeRejected(check2(), 'no arguments');
71 }).then(function() {
72     return shouldBeResolved(check2({}, '', '', ''), 'valid arguments');
73 }).then(function() {
74     return shouldBeResolved(check2({}, ''), 'valid arguments');
75 }).then(function() {
76     return shouldBeRejected(check2(3, ''), 'type error(Dictionary)');
77 }).then(function() {
78     return shouldBeResolved(check2({}, '', {}, 3), 'convert(String...)');
79 }).then(function() {
80     var x = {
81         toString: function() {
82             throw Error('Thrown from toString');
83         }
84     };
85     return shouldBeRejected(check2({}, '', '', x), 'conversion error(String...)');
86 }).then(function() {
87     return shouldBeRejected(check3(-1), 'range error(octet)');
88 }).then(function() {
89     return shouldBeResolved(check4(location), 'valid argument (Location)');
90 }).then(function() {
91     return shouldBeResolved(check4(document), 'valid argument (Document)');
92 }).then(function() {
93     return shouldBeResolved(check4(location, 0, 0), 'valid argument (Location, long, long)');
94 }).then(function() {
95     return shouldBeRejected(check4({}), 'type error (Object)');
96 }).then(function() {
97     return shouldBeRejected(check4(location, 0), 'type error (Location, long)');
98 }).then(undefined, function(e) {
99     testFailed('An exception is thrown from a method');
100     debug(e);
101 }).then(finishJSTest, finishJSTest);
102 </script>