Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / resources / worker-test-helpers.js
1 // Subset of testharness.js API for use in Service Worker tests.
2 // FIXME: Support async tests, possibly by just pulling in testharness.js
3
4 // Example:
5 //   test(function() {
6 //     assert_true(n > 1, 'n should be greater than 1');
7 //     assert_false(n < 1, 'n should not be less than 1');
8 //     assert_equals(n, 1, 'n should be equal to 1');
9 //     assert_array_equals(a, [1,2,3], 'arrays should match');
10 //     assert_throws({name: 'TypeError'}, function() { "123".call(); }, 'Should throw');
11 //     if (n < 0) {
12 //       assert_unreached('This should not happen');
13 //     }
14 //   }, 'Test description here');
15
16 (function(global) {
17
18     function assert(expected_true, error, description) {
19         if (!expected_true) {
20             var message = description ? error + ': ' + description : error;
21             throw message;
22         }
23     }
24
25     function assert_true(actual, m) {
26         assert(actual === true, 'expected true, got ' + actual, m);
27     }
28
29     function assert_false(actual, m) {
30         assert(actual === false, 'expected false, got ' + actual, m);
31     }
32
33
34     // Same as Object.is() from ES6
35     function is(a, b) {
36         if (a === b)
37             return (a !== 0) || (1 / a === 1 / b); // -0
38         return a !== a && b !== b; // NaN
39     }
40     assert(is(NaN, NaN), 'NaN should be equal to itself');
41     assert(!is(0, -0), '0 and -0 should not be equal');
42
43     function assert_equals(actual, expected, m) {
44         assert(is(actual, expected),
45                'expected (' + typeof expected + ') ' + expected +
46                ' but got (' + typeof actual + ') ' + actual, m);
47     }
48
49     function assert_array_equals(actual, expected, m) {
50         assert(actual.length === expected.length,
51                'lengths differ, expected ' + expected.length + ' got ' + actual.length, m);
52         for (var i = 0; i < expected.length; ++i) {
53             var ai = actual[i], ei = expected[i];
54             assert(is(ai, ei),
55                    'index ' + i + ', expected (' + typeof ei + ') ' + ei +
56                    ' but got (' + typeof ai + ') ' + ai, m);
57         }
58     }
59
60     function assert_throws(example, func, m) {
61         try {
62             func();
63         } catch (ex) {
64             if (example)
65                 assert(ex.name === example.name,
66                        String(func) + ' threw ' + ex.name + ' expected ' + example.name, m);
67             return;
68         }
69         assert(false, String(func) + ' did not throw', m);
70     }
71
72     function assert_unreached(m) {
73         assert(false, 'Reached unreachable code', m);
74     }
75
76     function test(func, name) {
77         tests.push({func:func, name:name});
78     }
79
80     // This assumes the test is started via 'service_worker_test' in test-helpers.js
81     var tests = [];
82     self.addEventListener('message', function(e) {
83         if (e.ports.length) {
84             var port = e.ports[0];
85             try {
86                 tests.forEach(function(test) { test.func(); });
87                 port.postMessage('pass');
88             } catch (ex) {
89                 port.postMessage(ex);
90             }
91         }
92     });
93
94     // Exports
95     global.assert_true = assert_true;
96     global.assert_false = assert_false;
97     global.assert_equals = assert_equals;
98     global.assert_array_equals = assert_array_equals;
99     global.assert_throws = assert_throws;
100     global.assert_unreached = assert_unreached;
101     global.test = test;
102
103 }(self));