blob: bc434cac907d0defe313b9ccb16848872c973047 [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',
Steven Burrows86af4352016-11-16 18:19:12 -0600215 'areFunctions', 'areFunctionsNonStrict', 'windowSize',
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700216 'isMobile', 'isChrome', 'isSafari', 'isFirefox',
217 'debugOn', 'debug',
218 'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'sameObjProps', 'containsObj', 'cap',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100219 'eecode', 'noPx', 'noPxStyle', 'endsWith', 'parseBitRate', 'addToTrie', 'removeFromTrie', 'trieLookup',
Steven Burrows86af4352016-11-16 18:19:12 -0600220 'classNames', 'extend'
Simon Hunt48e61672015-01-30 14:48:25 -0800221 ])).toBeTruthy();
222 });
223
224
Simon Hunta11b4eb2015-01-28 16:20:50 -0800225 // === Tests for windowSize()
226 it('windowSize(): noargs', function () {
227 var dim = fs.windowSize();
228 expect(dim.width).toEqual(400);
229 expect(dim.height).toEqual(200);
230 });
231
232 it('windowSize(): adjust height', function () {
233 var dim = fs.windowSize(50);
234 expect(dim.width).toEqual(400);
235 expect(dim.height).toEqual(150);
236 });
237
238 it('windowSize(): adjust width', function () {
239 var dim = fs.windowSize(0, 50);
240 expect(dim.width).toEqual(350);
241 expect(dim.height).toEqual(200);
242 });
243
244 it('windowSize(): adjust width and height', function () {
245 var dim = fs.windowSize(101, 201);
246 expect(dim.width).toEqual(199);
247 expect(dim.height).toEqual(99);
248 });
Simon Hunt48e61672015-01-30 14:48:25 -0800249
Simon Hunt31bb01f2015-03-31 16:50:41 -0700250 // === Tests for isMobile()
251 var uaMap = {
252 chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " +
253 "AppleWebKit/537.36 (KHTML, like Gecko) " +
254 "Chrome/41.0.2272.89 Safari/537.36",
255
256 iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " +
257 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
258 "Mobile/11A465 Safari/9537.53",
259
260 iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " +
261 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
262 "Mobile/11A465 Safari/9537.53"
263 };
264
265 function setUa(key) {
266 var str = uaMap[key];
267 expect(str).toBeTruthy();
268 mockWindow.navigator.userAgent = str;
269 }
270
271 it('isMobile(): should be false for Chrome on Mac OS X', function () {
272 setUa('chrome');
273 expect(fs.isMobile()).toBe(false);
274 });
275 it('isMobile(): should be true for Safari on iPad', function () {
276 setUa('iPad');
277 expect(fs.isMobile()).toBe(true);
278 });
279 it('isMobile(): should be true for Safari on iPhone', function () {
280 setUa('iPhone');
281 expect(fs.isMobile()).toBe(true);
282 });
Simon Hunt48e61672015-01-30 14:48:25 -0800283
Simon Hunt36c936b2015-01-30 16:07:18 -0800284 // === Tests for find()
285 var dataset = [
286 { id: 'foo', name: 'Furby'},
287 { id: 'bar', name: 'Barbi'},
288 { id: 'baz', name: 'Basil'},
289 { id: 'goo', name: 'Gabby'},
290 { id: 'zoo', name: 'Zevvv'}
291 ];
292
293 it('should not find ooo', function () {
294 expect(fs.find('ooo', dataset)).toEqual(-1);
295 });
296 it('should find foo', function () {
297 expect(fs.find('foo', dataset)).toEqual(0);
298 });
299 it('should find zoo', function () {
300 expect(fs.find('zoo', dataset)).toEqual(4);
301 });
302
303 it('should not find Simon', function () {
304 expect(fs.find('Simon', dataset, 'name')).toEqual(-1);
305 });
306 it('should find Furby', function () {
307 expect(fs.find('Furby', dataset, 'name')).toEqual(0);
308 });
309 it('should find Zevvv', function () {
310 expect(fs.find('Zevvv', dataset, 'name')).toEqual(4);
311 });
Simon Hunt205099e2015-02-07 13:12:01 -0800312
313
314 // === Tests for inArray()
315 var objRef = { x:1, y:2 },
316 array = [1, 3.14, 'hey', objRef, 'there', true],
317 array2 = ['b', 'a', 'd', 'a', 's', 's'];
318
319 it('should return -1 on non-arrays', function () {
320 expect(fs.inArray(1, {x:1})).toEqual(-1);
321 });
322 it('should not find HOO', function () {
323 expect(fs.inArray('HOO', array)).toEqual(-1);
324 });
325 it('should find 1', function () {
326 expect(fs.inArray(1, array)).toEqual(0);
327 });
328 it('should find pi', function () {
329 expect(fs.inArray(3.14, array)).toEqual(1);
330 });
331 it('should find hey', function () {
332 expect(fs.inArray('hey', array)).toEqual(2);
333 });
334 it('should find the object', function () {
335 expect(fs.inArray(objRef, array)).toEqual(3);
336 });
337 it('should find there', function () {
338 expect(fs.inArray('there', array)).toEqual(4);
339 });
340 it('should find true', function () {
341 expect(fs.inArray(true, array)).toEqual(5);
342 });
343
344 it('should find the first occurrence A', function () {
345 expect(fs.inArray('a', array2)).toEqual(1);
346 });
347 it('should find the first occurrence S', function () {
348 expect(fs.inArray('s', array2)).toEqual(4);
349 });
350 it('should not find X', function () {
351 expect(fs.inArray('x', array2)).toEqual(-1);
352 });
353
354 // === Tests for removeFromArray()
355 it('should ignore non-arrays', function () {
356 expect(fs.removeFromArray(1, {x:1})).toBe(false);
357 });
358 it('should keep the array the same, for non-match', function () {
359 var array = [1, 2, 3];
360 expect(fs.removeFromArray(4, array)).toBe(false);
361 expect(array).toEqual([1, 2, 3]);
362 });
363 it('should remove a value', function () {
364 var array = [1, 2, 3];
365 expect(fs.removeFromArray(2, array)).toBe(true);
366 expect(array).toEqual([1, 3]);
367 });
368 it('should remove the first occurrence', function () {
369 var array = ['x', 'y', 'z', 'z', 'y'];
370 expect(fs.removeFromArray('y', array)).toBe(true);
371 expect(array).toEqual(['x', 'z', 'z', 'y']);
372 expect(fs.removeFromArray('x', array)).toBe(true);
373 expect(array).toEqual(['z', 'z', 'y']);
374 });
375
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700376 // === Tests for isEmptyObject()
377 it('should return true if an object is empty', function () {
378 expect(fs.isEmptyObject({})).toBe(true);
379 });
380 it('should return false if an object is not empty', function () {
381 expect(fs.isEmptyObject({foo: 'bar'})).toBe(false);
382 });
383
Simon Hunt27a5cc82015-02-19 14:49:55 -0800384 // === Tests for cap()
385 it('should ignore non-alpha', function () {
386 expect(fs.cap('123')).toEqual('123');
387 });
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700388 it('should capitalize first char', function () {
Simon Hunt27a5cc82015-02-19 14:49:55 -0800389 expect(fs.cap('Foo')).toEqual('Foo');
390 expect(fs.cap('foo')).toEqual('Foo');
391 expect(fs.cap('foo bar')).toEqual('Foo bar');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700392 expect(fs.cap('FOO BAR')).toEqual('Foo bar');
393 expect(fs.cap('foo Bar')).toEqual('Foo bar');
Simon Hunt27a5cc82015-02-19 14:49:55 -0800394 });
395
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700396 // === Tests for noPx()
397 it('should return the value without px suffix', function () {
398 expect(fs.noPx('10px')).toBe(10);
399 expect(fs.noPx('500px')).toBe(500);
400 expect(fs.noPx('-80px')).toBe(-80);
401 });
402
403 // === Tests for noPxStyle()
404 it("should give a style's property without px suffix", function () {
405 var d3Elem = d3.select('body')
406 .append('div')
407 .attr('id', 'fooElem')
408 .style({
409 width: '500px',
410 height: '200px',
411 'font-size': '12px'
412 });
413 expect(fs.noPxStyle(d3Elem, 'width')).toBe(500);
414 expect(fs.noPxStyle(d3Elem, 'height')).toBe(200);
415 expect(fs.noPxStyle(d3Elem, 'font-size')).toBe(12);
416 d3.select('#fooElem').remove();
417 });
418
Simon Hunt96641372015-06-02 15:53:25 -0700419 // === Tests for endsWith()
420 it('should return true if string ends with foo', function () {
421 expect(fs.endsWith("barfoo", "foo")).toBe(true);
422 });
423
424 it('should return false if string doesnt end with foo', function () {
425 expect(fs.endsWith("barfood", "foo")).toBe(false);
426 });
427
428 // === Tests for parseBitRate()
429 it('should return 5 - a', function () {
430 expect(fs.parseBitRate('5.47 KBps')).toBe(5);
431 });
432
433 it('should return 5 - b', function () {
434 expect(fs.parseBitRate('5. KBps')).toBe(5);
435 });
436
437 it('should return 5 - c', function () {
438 expect(fs.parseBitRate('5 KBps')).toBe(5);
439 });
440
441 it('should return 5 - d', function () {
442 expect(fs.parseBitRate('5 Kbps')).toBe(5);
443 });
444
445 it('should return 2001', function () {
446 expect(fs.parseBitRate('2,001.59 Gbps')).toBe(2001);
447 });
Simon Huntf3069722014-12-16 18:15:37 -0800448});
Simon Hunt96641372015-06-02 15:53:25 -0700449