Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / unittest / assertions_test.html
1 <!DOCTYPE html>
2 <!--
3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
6 -->
7 <link rel="import" href="/tvcm/unittest/assertions.html">
8 <link rel="import" href="/tvcm/quad.html">
9 <link rel="import" href="/tvcm/rect.html">
10 <link rel="import" href="/tvcm/gl_matrix.html">
11 <script>
12 'use strict';
13
14 tvcm.unittest.testSuite(function() {
15   function assertionTestSetup() {
16     global.rawAssertThrows = function(fn) {
17       try {
18         fn();
19       } catch (e) {
20         if (e instanceof tvcm.unittest.TestError)
21           return;
22         throw new Error('Unexpected error from <' + fn + '>: ' + e);
23       }
24       throw new Error('Expected <' + fn + '> to throw');
25     };
26
27     global.rawAssertNotThrows = function(fn) {
28       try {
29         fn();
30       } catch (e) {
31         throw new Error('Expected <' + fn + '> to not throw: ' + e.message);
32       }
33     };
34   }
35
36   function assertionTestTeardown() {
37     global.rawAssertThrows = undefined;
38     global.rawAssertNotThrows = undefined;
39   }
40
41   function assertionTest(name, testFn) {
42     test(name, testFn, {
43       setUp: assertionTestSetup,
44       tearDown: assertionTestTeardown
45     });
46   }
47
48   assertionTest('assertTrue', function() {
49     rawAssertThrows(function() {
50       assertTrue(false);
51     });
52     rawAssertNotThrows(function() {
53       assertTrue(true);
54     });
55   });
56
57   assertionTest('assertFalse', function() {
58     rawAssertThrows(function() {
59       assertFalse(true);
60     });
61     rawAssertNotThrows(function() {
62       assertFalse(false);
63     });
64   });
65
66   assertionTest('assertUndefined', function() {
67     rawAssertThrows(function() {
68       assertUndefined('');
69     });
70     rawAssertNotThrows(function() {
71       assertUndefined(undefined);
72     });
73   });
74
75   assertionTest('assertNotUndefined', function() {
76     rawAssertThrows(function() {
77       assertNotUndefined(undefined);
78     });
79     rawAssertNotThrows(function() {
80       assertNotUndefined('');
81     });
82   });
83
84   assertionTest('assertNull', function() {
85     rawAssertThrows(function() {
86       assertNull('');
87     });
88     rawAssertNotThrows(function() {
89       assertNull(null);
90     });
91   });
92
93   assertionTest('assertNotNull', function() {
94     rawAssertThrows(function() {
95       assertNotNull(null);
96     });
97     rawAssertNotThrows(function() {
98       assertNotNull('');
99     });
100   });
101
102   assertionTest('assertEquals', function() {
103     rawAssertThrows(function() {
104       assertEquals(1, 2);
105     });
106     rawAssertNotThrows(function() {
107       assertEquals(1, 1);
108     });
109
110     try {
111       var f = {};
112       f.foo = f;
113       assertEquals(1, f);
114       throw new tvcm.unittest.TestError('Failed to throw');
115     } catch (e) {
116       assertNotEquals('Converting circular structure to JSON', e.message);
117     }
118
119     try {
120       var f = {};
121       f.foo = f;
122       assertEquals(f, 1);
123       throw new tvcm.unittest.TestError('Failed to throw');
124     } catch (e) {
125       assertNotEquals('Converting circular structure to JSON', e.message);
126     }
127   });
128
129   assertionTest('assertNotEquals', function() {
130     rawAssertThrows(function() {
131       assertNotEquals(1, 1);
132     });
133     rawAssertNotThrows(function() {
134       assertNotEquals(1, 2);
135     });
136   });
137
138   assertionTest('assertArrayEquals', function() {
139     rawAssertThrows(function() {
140       assertArrayEquals([2, 3], [2, 4]);
141     });
142     rawAssertThrows(function() {
143       assertArrayEquals([1], [1, 2]);
144     });
145     rawAssertNotThrows(function() {
146       assertArrayEquals(['a', 'b'], ['a', 'b']);
147     });
148   });
149
150   assertionTest('assertArrayEqualsShallow', function() {
151     rawAssertThrows(function() {
152       assertArrayShallowEquals([2, 3], [2, 4]);
153     });
154     rawAssertThrows(function() {
155       assertArrayShallowEquals([1], [1, 2]);
156     });
157     rawAssertNotThrows(function() {
158       assertArrayShallowEquals(['a', 'b'], ['a', 'b']);
159     });
160   });
161
162   assertionTest('assertAlmostEquals', function() {
163     rawAssertThrows(function() {
164       assertAlmostEquals(1, 0);
165     });
166     rawAssertThrows(function() {
167       assertAlmostEquals(1, 1.000011);
168     });
169
170     rawAssertNotThrows(function() {
171       assertAlmostEquals(1, 1);
172     });
173     rawAssertNotThrows(function() {
174       assertAlmostEquals(1, 1.000001);
175     });
176     rawAssertNotThrows(function() {
177       assertAlmostEquals(1, 1 - 0.000001);
178     });
179   });
180
181   assertionTest('assertVec2Equals', function() {
182     rawAssertThrows(function() {
183       assertVec2Equals(vec2.fromValues(0, 1), vec2.fromValues(0, 2));
184     });
185     rawAssertThrows(function() {
186       assertVec2Equals(vec2.fromValues(1, 2), vec2.fromValues(2, 2));
187     });
188     rawAssertNotThrows(function() {
189       assertVec2Equals(vec2.fromValues(1, 1), vec2.fromValues(1, 1));
190     });
191   });
192
193   assertionTest('assertVec3Equals', function() {
194     rawAssertThrows(function() {
195       assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(0, 1, 3));
196     });
197     rawAssertThrows(function() {
198       assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(0, 3, 2));
199     });
200     rawAssertThrows(function() {
201       assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(3, 1, 2));
202     });
203     rawAssertNotThrows(function() {
204       assertVec3Equals(vec3.fromValues(1, 2, 3), vec3.fromValues(1, 2, 3));
205     });
206   });
207
208   assertionTest('assertQuadEquals', function() {
209     rawAssertThrows(function() {
210       assertQuadEquals(
211           tvcm.Quad.fromXYWH(1, 1, 2, 2), tvcm.Quad.fromXYWH(1, 1, 2, 3));
212     });
213     rawAssertNotThrows(function() {
214       assertQuadEquals(
215           tvcm.Quad.fromXYWH(1, 1, 2, 2), tvcm.Quad.fromXYWH(1, 1, 2, 2));
216     });
217   });
218
219   assertionTest('assertRectEquals', function() {
220     rawAssertThrows(function() {
221       assertRectEquals(
222           tvcm.Rect.fromXYWH(1, 1, 2, 2), tvcm.Rect.fromXYWH(1, 1, 2, 3));
223     });
224     rawAssertNotThrows(function() {
225       assertRectEquals(
226           tvcm.Rect.fromXYWH(1, 1, 2, 2), tvcm.Rect.fromXYWH(1, 1, 2, 2));
227     });
228   });
229
230   assertionTest('assertObjectEquals', function() {
231     rawAssertThrows(function() {
232       assertObjectEquals({a: 1}, {a: 2});
233     });
234     rawAssertThrows(function() {
235       assertObjectEquals({a: 1}, []);
236     });
237     rawAssertThrows(function() {
238       assertObjectEquals({a: 1, b: {}}, {a: 1, c: {}, b: {}});
239     });
240     rawAssertNotThrows(function() {
241       assertObjectEquals({}, {});
242     });
243     rawAssertNotThrows(function() {
244       assertObjectEquals({a: 1}, {a: 1});
245     });
246   });
247
248   assertionTest('assertThrows', function() {
249     rawAssertThrows(function() {
250       assertThrows(function() {
251       });
252     });
253     rawAssertNotThrows(function() {
254       assertThrows(function() {
255         throw new Error('expected_error');
256       });
257     });
258   });
259
260   assertionTest('assertDoesNotThrow', function() {
261     rawAssertThrows(function() {
262       assertDoesNotThrow(function() {
263         throw new Error('expected_error');
264       });
265     });
266     rawAssertNotThrows(function() {
267       assertDoesNotThrow(function() {
268       });
269     });
270   });
271
272   assertionTest('assertApproxEquals', function() {
273     rawAssertThrows(function() {
274       assertApproxEquals(1, 5, 0.5);
275     });
276     rawAssertNotThrows(function() {
277       assertApproxEquals(1, 2, 1);
278     });
279   });
280
281   assertionTest('assertVisible', function() {
282     rawAssertThrows(function() {
283       assertVisible({});
284     });
285     rawAssertThrows(function() {
286       assertVisible({offsetHeight: 0});
287     });
288     rawAssertThrows(function() {
289       assertVisible({offsetWidth: 0});
290     });
291     rawAssertNotThrows(function() {
292       assertVisible({offsetWidth: 1, offsetHeight: 1});
293     });
294   });
295 });
296 </script>