blob: a7bda70ef6cd8304990bec4c21f174d8dd1ae6e6 [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,
Simon Hunt0fe05d62017-05-17 16:42:32 -070022 $log,
Simon Hunta11b4eb2015-01-28 16:20:50 -080023 fs,
Simon Huntf3069722014-12-16 18:15:37 -080024 someFunction = function () {},
25 someArray = [1, 2, 3],
26 someObject = { foo: 'bar'},
27 someNumber = 42,
28 someString = 'xyyzy',
Simon Hunt59df0b22014-12-17 10:32:25 -080029 someDate = new Date(),
30 stringArray = ['foo', 'bar'];
Simon Huntf3069722014-12-16 18:15:37 -080031
Simon Huntdc6362a2014-12-18 19:55:23 -080032 beforeEach(module('onosUtil'));
Simon Huntf3069722014-12-16 18:15:37 -080033
Simon Hunt31bb01f2015-03-31 16:50:41 -070034 var mockWindow = {
35 innerWidth: 400,
36 innerHeight: 200,
37 navigator: {
38 userAgent: 'defaultUA'
39 }
40 };
41
Simon Hunt0fe05d62017-05-17 16:42:32 -070042 var mockLog = {
43 debug: function () {},
44 info: function () {},
45 warn: function () {},
46 error: function () {}
47 };
48
Simon Hunt31bb01f2015-03-31 16:50:41 -070049 beforeEach(function () {
50 module(function ($provide) {
51 $provide.value('$window', mockWindow);
Simon Hunt0fe05d62017-05-17 16:42:32 -070052 $provide.value('$log', mockLog);
Simon Hunt31bb01f2015-03-31 16:50:41 -070053 });
54 });
55
Simon Hunt0fe05d62017-05-17 16:42:32 -070056 beforeEach(inject(function (_$log_, _$window_, FnService) {
57 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -080058 $window = _$window_;
Simon Huntf3069722014-12-16 18:15:37 -080059 fs = FnService;
60 }));
61
Simon Huntf3069722014-12-16 18:15:37 -080062 // === Tests for isF()
63 it('isF(): null for undefined', function () {
64 expect(fs.isF(undefined)).toBeNull();
65 });
66 it('isF(): null for null', function () {
67 expect(fs.isF(null)).toBeNull();
68 });
69 it('isF(): the reference for function', function () {
70 expect(fs.isF(someFunction)).toBe(someFunction);
71 });
72 it('isF(): null for string', function () {
73 expect(fs.isF(someString)).toBeNull();
74 });
75 it('isF(): null for number', function () {
76 expect(fs.isF(someNumber)).toBeNull();
77 });
78 it('isF(): null for Date', function () {
79 expect(fs.isF(someDate)).toBeNull();
80 });
81 it('isF(): null for array', function () {
82 expect(fs.isF(someArray)).toBeNull();
83 });
84 it('isF(): null for object', function () {
85 expect(fs.isF(someObject)).toBeNull();
86 });
87
88
89 // === Tests for isA()
90 it('isA(): null for undefined', function () {
91 expect(fs.isA(undefined)).toBeNull();
92 });
93 it('isA(): null for null', function () {
94 expect(fs.isA(null)).toBeNull();
95 });
96 it('isA(): null for function', function () {
97 expect(fs.isA(someFunction)).toBeNull();
98 });
99 it('isA(): null for string', function () {
100 expect(fs.isA(someString)).toBeNull();
101 });
102 it('isA(): null for number', function () {
103 expect(fs.isA(someNumber)).toBeNull();
104 });
105 it('isA(): null for Date', function () {
106 expect(fs.isA(someDate)).toBeNull();
107 });
108 it('isA(): the reference for array', function () {
109 expect(fs.isA(someArray)).toBe(someArray);
110 });
111 it('isA(): null for object', function () {
112 expect(fs.isA(someObject)).toBeNull();
113 });
114
115
116 // === Tests for isS()
117 it('isS(): null for undefined', function () {
118 expect(fs.isS(undefined)).toBeNull();
119 });
120 it('isS(): null for null', function () {
121 expect(fs.isS(null)).toBeNull();
122 });
123 it('isS(): null for function', function () {
124 expect(fs.isS(someFunction)).toBeNull();
125 });
126 it('isS(): the reference for string', function () {
127 expect(fs.isS(someString)).toBe(someString);
128 });
129 it('isS(): null for number', function () {
130 expect(fs.isS(someNumber)).toBeNull();
131 });
132 it('isS(): null for Date', function () {
133 expect(fs.isS(someDate)).toBeNull();
134 });
135 it('isS(): null for array', function () {
136 expect(fs.isS(someArray)).toBeNull();
137 });
138 it('isS(): null for object', function () {
139 expect(fs.isS(someObject)).toBeNull();
140 });
141
142
143 // === Tests for isO()
144 it('isO(): null for undefined', function () {
145 expect(fs.isO(undefined)).toBeNull();
146 });
147 it('isO(): null for null', function () {
148 expect(fs.isO(null)).toBeNull();
149 });
150 it('isO(): null for function', function () {
151 expect(fs.isO(someFunction)).toBeNull();
152 });
153 it('isO(): null for string', function () {
154 expect(fs.isO(someString)).toBeNull();
155 });
156 it('isO(): null for number', function () {
157 expect(fs.isO(someNumber)).toBeNull();
158 });
159 it('isO(): null for Date', function () {
160 expect(fs.isO(someDate)).toBeNull();
161 });
162 it('isO(): null for array', function () {
163 expect(fs.isO(someArray)).toBeNull();
164 });
165 it('isO(): the reference for object', function () {
166 expect(fs.isO(someObject)).toBe(someObject);
167 });
Simon Hunt59df0b22014-12-17 10:32:25 -0800168
169 // === Tests for contains()
170 it('contains(): false for improper args', function () {
171 expect(fs.contains()).toBeFalsy();
172 });
173 it('contains(): false for non-array', function () {
174 expect(fs.contains(null, 1)).toBeFalsy();
175 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800176 it('contains(): true for contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800177 expect(fs.contains(someArray, 1)).toBeTruthy();
178 expect(fs.contains(stringArray, 'bar')).toBeTruthy();
179 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800180 it('contains(): false for non-contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800181 expect(fs.contains(someArray, 109)).toBeFalsy();
182 expect(fs.contains(stringArray, 'zonko')).toBeFalsy();
183 });
Simon Hunt51fc40b2015-01-06 13:56:12 -0800184
185 // === Tests for areFunctions()
186 it('areFunctions(): false for non-array', function () {
187 expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy();
188 });
189 it('areFunctions(): true for empty-array', function () {
190 expect(fs.areFunctions({}, [])).toBeTruthy();
191 });
192 it('areFunctions(): true for some api', function () {
193 expect(fs.areFunctions({
194 a: function () {},
195 b: function () {}
196 }, ['b', 'a'])).toBeTruthy();
197 });
198 it('areFunctions(): false for some other api', function () {
199 expect(fs.areFunctions({
200 a: function () {},
201 b: 'not-a-function'
202 }, ['b', 'a'])).toBeFalsy();
203 });
Simon Hunt48e61672015-01-30 14:48:25 -0800204 it('areFunctions(): extraneous stuff NOT ignored', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -0800205 expect(fs.areFunctions({
206 a: function () {},
207 b: function () {},
208 c: 1,
209 d: 'foo'
Simon Hunt48e61672015-01-30 14:48:25 -0800210 }, ['a', 'b'])).toBeFalsy();
211 });
212 it('areFunctions(): extraneous stuff ignored (alternate fn)', function () {
213 expect(fs.areFunctionsNonStrict({
214 a: function () {},
215 b: function () {},
216 c: 1,
217 d: 'foo'
Simon Hunt51fc40b2015-01-06 13:56:12 -0800218 }, ['a', 'b'])).toBeTruthy();
219 });
220
Simon Hunt36c936b2015-01-30 16:07:18 -0800221 // == use the now-tested areFunctions() on our own api:
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700222 it('should define api functions', function () {
Simon Hunt48e61672015-01-30 14:48:25 -0800223 expect(fs.areFunctions(fs, [
224 'isF', 'isA', 'isS', 'isO', 'contains',
Steven Burrows86af4352016-11-16 18:19:12 -0600225 'areFunctions', 'areFunctionsNonStrict', 'windowSize',
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700226 'isMobile', 'isChrome', 'isSafari', 'isFirefox',
227 'debugOn', 'debug',
228 'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'sameObjProps', 'containsObj', 'cap',
Simon Hunt69855172017-03-31 09:48:25 -0700229 'eecode', 'noPx', 'noPxStyle', 'endsWith', 'addToTrie', 'removeFromTrie', 'trieLookup',
Simon Hunt0fe05d62017-05-17 16:42:32 -0700230 'classNames', 'extend', 'sanitize'
Simon Hunt48e61672015-01-30 14:48:25 -0800231 ])).toBeTruthy();
232 });
233
234
Simon Hunta11b4eb2015-01-28 16:20:50 -0800235 // === Tests for windowSize()
236 it('windowSize(): noargs', function () {
237 var dim = fs.windowSize();
238 expect(dim.width).toEqual(400);
239 expect(dim.height).toEqual(200);
240 });
241
242 it('windowSize(): adjust height', function () {
243 var dim = fs.windowSize(50);
244 expect(dim.width).toEqual(400);
245 expect(dim.height).toEqual(150);
246 });
247
248 it('windowSize(): adjust width', function () {
249 var dim = fs.windowSize(0, 50);
250 expect(dim.width).toEqual(350);
251 expect(dim.height).toEqual(200);
252 });
253
254 it('windowSize(): adjust width and height', function () {
255 var dim = fs.windowSize(101, 201);
256 expect(dim.width).toEqual(199);
257 expect(dim.height).toEqual(99);
258 });
Simon Hunt48e61672015-01-30 14:48:25 -0800259
Simon Hunt31bb01f2015-03-31 16:50:41 -0700260 // === Tests for isMobile()
261 var uaMap = {
262 chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " +
263 "AppleWebKit/537.36 (KHTML, like Gecko) " +
264 "Chrome/41.0.2272.89 Safari/537.36",
265
266 iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " +
267 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
268 "Mobile/11A465 Safari/9537.53",
269
270 iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " +
271 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
272 "Mobile/11A465 Safari/9537.53"
273 };
274
275 function setUa(key) {
276 var str = uaMap[key];
277 expect(str).toBeTruthy();
278 mockWindow.navigator.userAgent = str;
279 }
280
281 it('isMobile(): should be false for Chrome on Mac OS X', function () {
282 setUa('chrome');
283 expect(fs.isMobile()).toBe(false);
284 });
285 it('isMobile(): should be true for Safari on iPad', function () {
286 setUa('iPad');
287 expect(fs.isMobile()).toBe(true);
288 });
289 it('isMobile(): should be true for Safari on iPhone', function () {
290 setUa('iPhone');
291 expect(fs.isMobile()).toBe(true);
292 });
Simon Hunt48e61672015-01-30 14:48:25 -0800293
Simon Hunt36c936b2015-01-30 16:07:18 -0800294 // === Tests for find()
295 var dataset = [
296 { id: 'foo', name: 'Furby'},
297 { id: 'bar', name: 'Barbi'},
298 { id: 'baz', name: 'Basil'},
299 { id: 'goo', name: 'Gabby'},
300 { id: 'zoo', name: 'Zevvv'}
301 ];
302
303 it('should not find ooo', function () {
304 expect(fs.find('ooo', dataset)).toEqual(-1);
305 });
306 it('should find foo', function () {
307 expect(fs.find('foo', dataset)).toEqual(0);
308 });
309 it('should find zoo', function () {
310 expect(fs.find('zoo', dataset)).toEqual(4);
311 });
312
313 it('should not find Simon', function () {
314 expect(fs.find('Simon', dataset, 'name')).toEqual(-1);
315 });
316 it('should find Furby', function () {
317 expect(fs.find('Furby', dataset, 'name')).toEqual(0);
318 });
319 it('should find Zevvv', function () {
320 expect(fs.find('Zevvv', dataset, 'name')).toEqual(4);
321 });
Simon Hunt205099e2015-02-07 13:12:01 -0800322
323
324 // === Tests for inArray()
325 var objRef = { x:1, y:2 },
326 array = [1, 3.14, 'hey', objRef, 'there', true],
327 array2 = ['b', 'a', 'd', 'a', 's', 's'];
328
329 it('should return -1 on non-arrays', function () {
330 expect(fs.inArray(1, {x:1})).toEqual(-1);
331 });
332 it('should not find HOO', function () {
333 expect(fs.inArray('HOO', array)).toEqual(-1);
334 });
335 it('should find 1', function () {
336 expect(fs.inArray(1, array)).toEqual(0);
337 });
338 it('should find pi', function () {
339 expect(fs.inArray(3.14, array)).toEqual(1);
340 });
341 it('should find hey', function () {
342 expect(fs.inArray('hey', array)).toEqual(2);
343 });
344 it('should find the object', function () {
345 expect(fs.inArray(objRef, array)).toEqual(3);
346 });
347 it('should find there', function () {
348 expect(fs.inArray('there', array)).toEqual(4);
349 });
350 it('should find true', function () {
351 expect(fs.inArray(true, array)).toEqual(5);
352 });
353
354 it('should find the first occurrence A', function () {
355 expect(fs.inArray('a', array2)).toEqual(1);
356 });
357 it('should find the first occurrence S', function () {
358 expect(fs.inArray('s', array2)).toEqual(4);
359 });
360 it('should not find X', function () {
361 expect(fs.inArray('x', array2)).toEqual(-1);
362 });
363
364 // === Tests for removeFromArray()
365 it('should ignore non-arrays', function () {
366 expect(fs.removeFromArray(1, {x:1})).toBe(false);
367 });
368 it('should keep the array the same, for non-match', function () {
369 var array = [1, 2, 3];
370 expect(fs.removeFromArray(4, array)).toBe(false);
371 expect(array).toEqual([1, 2, 3]);
372 });
373 it('should remove a value', function () {
374 var array = [1, 2, 3];
375 expect(fs.removeFromArray(2, array)).toBe(true);
376 expect(array).toEqual([1, 3]);
377 });
378 it('should remove the first occurrence', function () {
379 var array = ['x', 'y', 'z', 'z', 'y'];
380 expect(fs.removeFromArray('y', array)).toBe(true);
381 expect(array).toEqual(['x', 'z', 'z', 'y']);
382 expect(fs.removeFromArray('x', array)).toBe(true);
383 expect(array).toEqual(['z', 'z', 'y']);
384 });
385
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700386 // === Tests for isEmptyObject()
387 it('should return true if an object is empty', function () {
388 expect(fs.isEmptyObject({})).toBe(true);
389 });
390 it('should return false if an object is not empty', function () {
391 expect(fs.isEmptyObject({foo: 'bar'})).toBe(false);
392 });
393
Simon Hunt27a5cc82015-02-19 14:49:55 -0800394 // === Tests for cap()
395 it('should ignore non-alpha', function () {
396 expect(fs.cap('123')).toEqual('123');
397 });
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700398 it('should capitalize first char', function () {
Simon Hunt27a5cc82015-02-19 14:49:55 -0800399 expect(fs.cap('Foo')).toEqual('Foo');
400 expect(fs.cap('foo')).toEqual('Foo');
401 expect(fs.cap('foo bar')).toEqual('Foo bar');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700402 expect(fs.cap('FOO BAR')).toEqual('Foo bar');
403 expect(fs.cap('foo Bar')).toEqual('Foo bar');
Simon Hunt27a5cc82015-02-19 14:49:55 -0800404 });
405
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700406 // === Tests for noPx()
407 it('should return the value without px suffix', function () {
408 expect(fs.noPx('10px')).toBe(10);
409 expect(fs.noPx('500px')).toBe(500);
410 expect(fs.noPx('-80px')).toBe(-80);
411 });
412
413 // === Tests for noPxStyle()
414 it("should give a style's property without px suffix", function () {
415 var d3Elem = d3.select('body')
416 .append('div')
417 .attr('id', 'fooElem')
418 .style({
419 width: '500px',
420 height: '200px',
421 'font-size': '12px'
422 });
423 expect(fs.noPxStyle(d3Elem, 'width')).toBe(500);
424 expect(fs.noPxStyle(d3Elem, 'height')).toBe(200);
425 expect(fs.noPxStyle(d3Elem, 'font-size')).toBe(12);
426 d3.select('#fooElem').remove();
427 });
428
Simon Hunt96641372015-06-02 15:53:25 -0700429 // === Tests for endsWith()
430 it('should return true if string ends with foo', function () {
431 expect(fs.endsWith("barfoo", "foo")).toBe(true);
432 });
433
434 it('should return false if string doesnt end with foo', function () {
435 expect(fs.endsWith("barfood", "foo")).toBe(false);
436 });
437
Simon Hunt0fe05d62017-05-17 16:42:32 -0700438 // === Tests for sanitize()
439 function chkSan(u, s) {
440 expect(fs.sanitize(u)).toEqual(s);
441 }
442 function chkGood(g) {
443 chkSan(g, g)
444 }
445 it('should return foo', function () {
446 chkGood('foo');
447 });
448 it('should retain < b > tags', function () {
449 chkGood('foo <b>bar</b> baz');
450 });
451 it('should retain < i > tags', function () {
452 chkGood('foo <i>bar</i> baz');
453 });
454 it('should retain < p > tags', function () {
455 chkGood('foo <p>bar</p> baz');
456 });
457 it('should retain < em > tags', function () {
458 chkGood('foo <em>bar</em> baz');
459 });
460 it('should retain < strong > tags', function () {
461 chkGood('foo <strong>bar</strong> baz');
462 });
463
464 it('should reject < a > tags', function () {
465 chkSan('test <a href="hah">something</a> this', 'test something this');
466 });
467
468 it('should log a warning for < script > tags', function () {
469 spyOn($log, 'warn');
470 chkSan('<script>alert("foo");</script>', '');
471 expect($log.warn).toHaveBeenCalledWith(
472 'Unsanitary HTML input -- <script> detected!'
473 );
474 });
475 it('should log a warning for < style > tags', function () {
476 spyOn($log, 'warn');
477 chkSan('<style> h1 {color:red;} </style>', '');
478 expect($log.warn).toHaveBeenCalledWith(
479 'Unsanitary HTML input -- <style> detected!'
480 );
481 });
482
483 it('should completely strip < script >, remove < a >, retain < i >', function () {
484 chkSan(
485 'Hey <i>this</i> is <script>alert("foo");</script> <a href="meh">cool</a>',
486 'Hey <i>this</i> is cool'
487 );
488 });
489
Simon Huntf3069722014-12-16 18:15:37 -0800490});
Simon Hunt96641372015-06-02 15:53:25 -0700491