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