blob: 7e7dae563c054491ca03922316227dd3d5b4a61e [file] [log] [blame]
Simon Huntf3069722014-12-16 18:15:37 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Simon Huntf3069722014-12-16 18:15:37 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Huntdc6362a2014-12-18 19:55:23 -080018 ONOS GUI -- Util -- General Purpose Functions - Unit Tests
Simon Huntf3069722014-12-16 18:15:37 -080019 */
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080020describe('factory: fw/util/fn.js', function() {
Simon Hunta11b4eb2015-01-28 16:20:50 -080021 var $window,
22 fs,
Simon Huntf3069722014-12-16 18:15:37 -080023 someFunction = function () {},
24 someArray = [1, 2, 3],
25 someObject = { foo: 'bar'},
26 someNumber = 42,
27 someString = 'xyyzy',
Simon Hunt59df0b22014-12-17 10:32:25 -080028 someDate = new Date(),
29 stringArray = ['foo', 'bar'];
Simon Huntf3069722014-12-16 18:15:37 -080030
Simon Huntdc6362a2014-12-18 19:55:23 -080031 beforeEach(module('onosUtil'));
Simon Huntf3069722014-12-16 18:15:37 -080032
Simon Hunt31bb01f2015-03-31 16:50:41 -070033 var mockWindow = {
34 innerWidth: 400,
35 innerHeight: 200,
36 navigator: {
37 userAgent: 'defaultUA'
38 }
39 };
40
41 beforeEach(function () {
42 module(function ($provide) {
43 $provide.value('$window', mockWindow);
44 });
45 });
46
Simon Hunta11b4eb2015-01-28 16:20:50 -080047 beforeEach(inject(function (_$window_, FnService) {
48 $window = _$window_;
Simon Huntf3069722014-12-16 18:15:37 -080049 fs = FnService;
50 }));
51
Simon Huntf3069722014-12-16 18:15:37 -080052 // === Tests for isF()
53 it('isF(): null for undefined', function () {
54 expect(fs.isF(undefined)).toBeNull();
55 });
56 it('isF(): null for null', function () {
57 expect(fs.isF(null)).toBeNull();
58 });
59 it('isF(): the reference for function', function () {
60 expect(fs.isF(someFunction)).toBe(someFunction);
61 });
62 it('isF(): null for string', function () {
63 expect(fs.isF(someString)).toBeNull();
64 });
65 it('isF(): null for number', function () {
66 expect(fs.isF(someNumber)).toBeNull();
67 });
68 it('isF(): null for Date', function () {
69 expect(fs.isF(someDate)).toBeNull();
70 });
71 it('isF(): null for array', function () {
72 expect(fs.isF(someArray)).toBeNull();
73 });
74 it('isF(): null for object', function () {
75 expect(fs.isF(someObject)).toBeNull();
76 });
77
78
79 // === Tests for isA()
80 it('isA(): null for undefined', function () {
81 expect(fs.isA(undefined)).toBeNull();
82 });
83 it('isA(): null for null', function () {
84 expect(fs.isA(null)).toBeNull();
85 });
86 it('isA(): null for function', function () {
87 expect(fs.isA(someFunction)).toBeNull();
88 });
89 it('isA(): null for string', function () {
90 expect(fs.isA(someString)).toBeNull();
91 });
92 it('isA(): null for number', function () {
93 expect(fs.isA(someNumber)).toBeNull();
94 });
95 it('isA(): null for Date', function () {
96 expect(fs.isA(someDate)).toBeNull();
97 });
98 it('isA(): the reference for array', function () {
99 expect(fs.isA(someArray)).toBe(someArray);
100 });
101 it('isA(): null for object', function () {
102 expect(fs.isA(someObject)).toBeNull();
103 });
104
105
106 // === Tests for isS()
107 it('isS(): null for undefined', function () {
108 expect(fs.isS(undefined)).toBeNull();
109 });
110 it('isS(): null for null', function () {
111 expect(fs.isS(null)).toBeNull();
112 });
113 it('isS(): null for function', function () {
114 expect(fs.isS(someFunction)).toBeNull();
115 });
116 it('isS(): the reference for string', function () {
117 expect(fs.isS(someString)).toBe(someString);
118 });
119 it('isS(): null for number', function () {
120 expect(fs.isS(someNumber)).toBeNull();
121 });
122 it('isS(): null for Date', function () {
123 expect(fs.isS(someDate)).toBeNull();
124 });
125 it('isS(): null for array', function () {
126 expect(fs.isS(someArray)).toBeNull();
127 });
128 it('isS(): null for object', function () {
129 expect(fs.isS(someObject)).toBeNull();
130 });
131
132
133 // === Tests for isO()
134 it('isO(): null for undefined', function () {
135 expect(fs.isO(undefined)).toBeNull();
136 });
137 it('isO(): null for null', function () {
138 expect(fs.isO(null)).toBeNull();
139 });
140 it('isO(): null for function', function () {
141 expect(fs.isO(someFunction)).toBeNull();
142 });
143 it('isO(): null for string', function () {
144 expect(fs.isO(someString)).toBeNull();
145 });
146 it('isO(): null for number', function () {
147 expect(fs.isO(someNumber)).toBeNull();
148 });
149 it('isO(): null for Date', function () {
150 expect(fs.isO(someDate)).toBeNull();
151 });
152 it('isO(): null for array', function () {
153 expect(fs.isO(someArray)).toBeNull();
154 });
155 it('isO(): the reference for object', function () {
156 expect(fs.isO(someObject)).toBe(someObject);
157 });
Simon Hunt59df0b22014-12-17 10:32:25 -0800158
159 // === Tests for contains()
160 it('contains(): false for improper args', function () {
161 expect(fs.contains()).toBeFalsy();
162 });
163 it('contains(): false for non-array', function () {
164 expect(fs.contains(null, 1)).toBeFalsy();
165 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800166 it('contains(): true for contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800167 expect(fs.contains(someArray, 1)).toBeTruthy();
168 expect(fs.contains(stringArray, 'bar')).toBeTruthy();
169 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800170 it('contains(): false for non-contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800171 expect(fs.contains(someArray, 109)).toBeFalsy();
172 expect(fs.contains(stringArray, 'zonko')).toBeFalsy();
173 });
Simon Hunt51fc40b2015-01-06 13:56:12 -0800174
175 // === Tests for areFunctions()
176 it('areFunctions(): false for non-array', function () {
177 expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy();
178 });
179 it('areFunctions(): true for empty-array', function () {
180 expect(fs.areFunctions({}, [])).toBeTruthy();
181 });
182 it('areFunctions(): true for some api', function () {
183 expect(fs.areFunctions({
184 a: function () {},
185 b: function () {}
186 }, ['b', 'a'])).toBeTruthy();
187 });
188 it('areFunctions(): false for some other api', function () {
189 expect(fs.areFunctions({
190 a: function () {},
191 b: 'not-a-function'
192 }, ['b', 'a'])).toBeFalsy();
193 });
Simon Hunt48e61672015-01-30 14:48:25 -0800194 it('areFunctions(): extraneous stuff NOT ignored', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -0800195 expect(fs.areFunctions({
196 a: function () {},
197 b: function () {},
198 c: 1,
199 d: 'foo'
Simon Hunt48e61672015-01-30 14:48:25 -0800200 }, ['a', 'b'])).toBeFalsy();
201 });
202 it('areFunctions(): extraneous stuff ignored (alternate fn)', function () {
203 expect(fs.areFunctionsNonStrict({
204 a: function () {},
205 b: function () {},
206 c: 1,
207 d: 'foo'
Simon Hunt51fc40b2015-01-06 13:56:12 -0800208 }, ['a', 'b'])).toBeTruthy();
209 });
210
Simon Hunt36c936b2015-01-30 16:07:18 -0800211 // == use the now-tested areFunctions() on our own api:
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700212 it('should define api functions', function () {
Simon Hunt48e61672015-01-30 14:48:25 -0800213 expect(fs.areFunctions(fs, [
214 'isF', 'isA', 'isS', 'isO', 'contains',
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700215 'areFunctions', 'areFunctionsNonStrict', 'windowSize',
216 'isMobile', 'isChrome', 'isSafari', 'isFirefox',
217 'debugOn', 'debug',
218 'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'sameObjProps', 'containsObj', 'cap',
219 'eecode', 'noPx', 'noPxStyle', 'endsWith', 'parseBitRate', 'addToTrie', 'removeFromTrie', 'trieLookup'
Simon Hunt48e61672015-01-30 14:48:25 -0800220 ])).toBeTruthy();
221 });
222
223
Simon Hunta11b4eb2015-01-28 16:20:50 -0800224 // === Tests for windowSize()
225 it('windowSize(): noargs', function () {
226 var dim = fs.windowSize();
227 expect(dim.width).toEqual(400);
228 expect(dim.height).toEqual(200);
229 });
230
231 it('windowSize(): adjust height', function () {
232 var dim = fs.windowSize(50);
233 expect(dim.width).toEqual(400);
234 expect(dim.height).toEqual(150);
235 });
236
237 it('windowSize(): adjust width', function () {
238 var dim = fs.windowSize(0, 50);
239 expect(dim.width).toEqual(350);
240 expect(dim.height).toEqual(200);
241 });
242
243 it('windowSize(): adjust width and height', function () {
244 var dim = fs.windowSize(101, 201);
245 expect(dim.width).toEqual(199);
246 expect(dim.height).toEqual(99);
247 });
Simon Hunt48e61672015-01-30 14:48:25 -0800248
Simon Hunt31bb01f2015-03-31 16:50:41 -0700249 // === Tests for isMobile()
250 var uaMap = {
251 chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " +
252 "AppleWebKit/537.36 (KHTML, like Gecko) " +
253 "Chrome/41.0.2272.89 Safari/537.36",
254
255 iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " +
256 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
257 "Mobile/11A465 Safari/9537.53",
258
259 iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " +
260 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
261 "Mobile/11A465 Safari/9537.53"
262 };
263
264 function setUa(key) {
265 var str = uaMap[key];
266 expect(str).toBeTruthy();
267 mockWindow.navigator.userAgent = str;
268 }
269
270 it('isMobile(): should be false for Chrome on Mac OS X', function () {
271 setUa('chrome');
272 expect(fs.isMobile()).toBe(false);
273 });
274 it('isMobile(): should be true for Safari on iPad', function () {
275 setUa('iPad');
276 expect(fs.isMobile()).toBe(true);
277 });
278 it('isMobile(): should be true for Safari on iPhone', function () {
279 setUa('iPhone');
280 expect(fs.isMobile()).toBe(true);
281 });
Simon Hunt48e61672015-01-30 14:48:25 -0800282
Simon Hunt36c936b2015-01-30 16:07:18 -0800283 // === Tests for find()
284 var dataset = [
285 { id: 'foo', name: 'Furby'},
286 { id: 'bar', name: 'Barbi'},
287 { id: 'baz', name: 'Basil'},
288 { id: 'goo', name: 'Gabby'},
289 { id: 'zoo', name: 'Zevvv'}
290 ];
291
292 it('should not find ooo', function () {
293 expect(fs.find('ooo', dataset)).toEqual(-1);
294 });
295 it('should find foo', function () {
296 expect(fs.find('foo', dataset)).toEqual(0);
297 });
298 it('should find zoo', function () {
299 expect(fs.find('zoo', dataset)).toEqual(4);
300 });
301
302 it('should not find Simon', function () {
303 expect(fs.find('Simon', dataset, 'name')).toEqual(-1);
304 });
305 it('should find Furby', function () {
306 expect(fs.find('Furby', dataset, 'name')).toEqual(0);
307 });
308 it('should find Zevvv', function () {
309 expect(fs.find('Zevvv', dataset, 'name')).toEqual(4);
310 });
Simon Hunt205099e2015-02-07 13:12:01 -0800311
312
313 // === Tests for inArray()
314 var objRef = { x:1, y:2 },
315 array = [1, 3.14, 'hey', objRef, 'there', true],
316 array2 = ['b', 'a', 'd', 'a', 's', 's'];
317
318 it('should return -1 on non-arrays', function () {
319 expect(fs.inArray(1, {x:1})).toEqual(-1);
320 });
321 it('should not find HOO', function () {
322 expect(fs.inArray('HOO', array)).toEqual(-1);
323 });
324 it('should find 1', function () {
325 expect(fs.inArray(1, array)).toEqual(0);
326 });
327 it('should find pi', function () {
328 expect(fs.inArray(3.14, array)).toEqual(1);
329 });
330 it('should find hey', function () {
331 expect(fs.inArray('hey', array)).toEqual(2);
332 });
333 it('should find the object', function () {
334 expect(fs.inArray(objRef, array)).toEqual(3);
335 });
336 it('should find there', function () {
337 expect(fs.inArray('there', array)).toEqual(4);
338 });
339 it('should find true', function () {
340 expect(fs.inArray(true, array)).toEqual(5);
341 });
342
343 it('should find the first occurrence A', function () {
344 expect(fs.inArray('a', array2)).toEqual(1);
345 });
346 it('should find the first occurrence S', function () {
347 expect(fs.inArray('s', array2)).toEqual(4);
348 });
349 it('should not find X', function () {
350 expect(fs.inArray('x', array2)).toEqual(-1);
351 });
352
353 // === Tests for removeFromArray()
354 it('should ignore non-arrays', function () {
355 expect(fs.removeFromArray(1, {x:1})).toBe(false);
356 });
357 it('should keep the array the same, for non-match', function () {
358 var array = [1, 2, 3];
359 expect(fs.removeFromArray(4, array)).toBe(false);
360 expect(array).toEqual([1, 2, 3]);
361 });
362 it('should remove a value', function () {
363 var array = [1, 2, 3];
364 expect(fs.removeFromArray(2, array)).toBe(true);
365 expect(array).toEqual([1, 3]);
366 });
367 it('should remove the first occurrence', function () {
368 var array = ['x', 'y', 'z', 'z', 'y'];
369 expect(fs.removeFromArray('y', array)).toBe(true);
370 expect(array).toEqual(['x', 'z', 'z', 'y']);
371 expect(fs.removeFromArray('x', array)).toBe(true);
372 expect(array).toEqual(['z', 'z', 'y']);
373 });
374
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700375 // === Tests for isEmptyObject()
376 it('should return true if an object is empty', function () {
377 expect(fs.isEmptyObject({})).toBe(true);
378 });
379 it('should return false if an object is not empty', function () {
380 expect(fs.isEmptyObject({foo: 'bar'})).toBe(false);
381 });
382
Simon Hunt27a5cc82015-02-19 14:49:55 -0800383 // === Tests for cap()
384 it('should ignore non-alpha', function () {
385 expect(fs.cap('123')).toEqual('123');
386 });
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700387 it('should capitalize first char', function () {
Simon Hunt27a5cc82015-02-19 14:49:55 -0800388 expect(fs.cap('Foo')).toEqual('Foo');
389 expect(fs.cap('foo')).toEqual('Foo');
390 expect(fs.cap('foo bar')).toEqual('Foo bar');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700391 expect(fs.cap('FOO BAR')).toEqual('Foo bar');
392 expect(fs.cap('foo Bar')).toEqual('Foo bar');
Simon Hunt27a5cc82015-02-19 14:49:55 -0800393 });
394
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700395 // === Tests for noPx()
396 it('should return the value without px suffix', function () {
397 expect(fs.noPx('10px')).toBe(10);
398 expect(fs.noPx('500px')).toBe(500);
399 expect(fs.noPx('-80px')).toBe(-80);
400 });
401
402 // === Tests for noPxStyle()
403 it("should give a style's property without px suffix", function () {
404 var d3Elem = d3.select('body')
405 .append('div')
406 .attr('id', 'fooElem')
407 .style({
408 width: '500px',
409 height: '200px',
410 'font-size': '12px'
411 });
412 expect(fs.noPxStyle(d3Elem, 'width')).toBe(500);
413 expect(fs.noPxStyle(d3Elem, 'height')).toBe(200);
414 expect(fs.noPxStyle(d3Elem, 'font-size')).toBe(12);
415 d3.select('#fooElem').remove();
416 });
417
Simon Hunt96641372015-06-02 15:53:25 -0700418 // === Tests for endsWith()
419 it('should return true if string ends with foo', function () {
420 expect(fs.endsWith("barfoo", "foo")).toBe(true);
421 });
422
423 it('should return false if string doesnt end with foo', function () {
424 expect(fs.endsWith("barfood", "foo")).toBe(false);
425 });
426
427 // === Tests for parseBitRate()
428 it('should return 5 - a', function () {
429 expect(fs.parseBitRate('5.47 KBps')).toBe(5);
430 });
431
432 it('should return 5 - b', function () {
433 expect(fs.parseBitRate('5. KBps')).toBe(5);
434 });
435
436 it('should return 5 - c', function () {
437 expect(fs.parseBitRate('5 KBps')).toBe(5);
438 });
439
440 it('should return 5 - d', function () {
441 expect(fs.parseBitRate('5 Kbps')).toBe(5);
442 });
443
444 it('should return 2001', function () {
445 expect(fs.parseBitRate('2,001.59 Gbps')).toBe(2001);
446 });
Simon Huntf3069722014-12-16 18:15:37 -0800447});
Simon Hunt96641372015-06-02 15:53:25 -0700448