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