Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 3 | * |
| 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 | /* |
| 18 | ONOS GUI -- SVG -- Icon Service - Unit Tests |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 19 | */ |
| 20 | describe('factory: fw/svg/icon.js', function() { |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 21 | var is, d3Elem; |
| 22 | |
| 23 | var viewBox = '0 0 50 50', |
| 24 | glyphSize = '50', |
| 25 | iconSize = '20'; |
| 26 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 27 | |
| 28 | beforeEach(module('onosSvg')); |
| 29 | |
| 30 | beforeEach(inject(function (IconService) { |
| 31 | is = IconService; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 32 | d3Elem = d3.select('body').append('div').attr('id', 'myDiv'); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 33 | })); |
| 34 | |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 35 | afterEach(function () { |
| 36 | d3.select('#myDiv').remove(); |
| 37 | }); |
| 38 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 39 | it('should define IconService', function () { |
| 40 | expect(is).toBeDefined(); |
| 41 | }); |
| 42 | |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 43 | function checkElemSize(elem, dim) { |
| 44 | expect(elem.attr('width')).toEqual(dim); |
| 45 | expect(elem.attr('height')).toEqual(dim); |
| 46 | } |
| 47 | |
| 48 | function verifyIconStructure(iconClass, useHref, iSize, vBox, gSize) { |
| 49 | var isz = iSize || iconSize, |
| 50 | vbx = vBox || viewBox, |
| 51 | gsz = gSize || glyphSize; |
| 52 | |
| 53 | var svg = d3Elem.selectAll('svg'); |
| 54 | expect(svg.size()).toBe(1); |
| 55 | checkElemSize(svg, isz); |
| 56 | expect(svg.attr('viewBox')).toEqual(vbx); |
| 57 | |
| 58 | var g = svg.selectAll('g'); |
| 59 | expect(g.size()).toBe(1); |
| 60 | expect(g.classed('icon')).toBeTruthy(); |
| 61 | expect(g.classed(iconClass)).toBeTruthy(); |
| 62 | |
| 63 | var rect = g.select('rect'); |
| 64 | expect(rect.size()).toBe(1); |
| 65 | checkElemSize(rect, gsz); |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 66 | expect(rect.attr('rx')).toEqual('5'); |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 67 | |
Bri Prebilic Cole | dee4662 | 2015-02-03 16:36:11 -0800 | [diff] [blame] | 68 | if (useHref) { |
| 69 | var use = g.select('use'); |
| 70 | expect(use.classed('glyph')).toBeTruthy(); |
| 71 | expect(use.attr('xlink:href')).toEqual(useHref); |
| 72 | checkElemSize(use, gsz); |
| 73 | } |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 76 | it('should load an icon into a div', function () { |
| 77 | expect(d3Elem.html()).toEqual(''); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 78 | is.loadIconByClass(d3Elem, 'active'); |
| 79 | verifyIconStructure('active', '#checkMark'); |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 80 | }); |
| 81 | |
| 82 | it('should allow us to specify the icon size', function () { |
| 83 | expect(d3Elem.html()).toEqual(''); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 84 | is.loadIconByClass(d3Elem, 'inactive', 32); |
| 85 | verifyIconStructure('inactive', '#xMark', '32'); |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 86 | }); |
| 87 | |
Bri Prebilic Cole | dee4662 | 2015-02-03 16:36:11 -0800 | [diff] [blame] | 88 | it('should verify triangleUp icon', function () { |
| 89 | expect(d3Elem.html()).toEqual(''); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 90 | is.loadIconByClass(d3Elem, 'upArrow', 10); |
| 91 | verifyIconStructure('upArrow', '#triangleUp', '10'); |
Bri Prebilic Cole | dee4662 | 2015-02-03 16:36:11 -0800 | [diff] [blame] | 92 | }); |
| 93 | |
| 94 | it('should verify triangleDown icon', function () { |
| 95 | expect(d3Elem.html()).toEqual(''); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 96 | is.loadIconByClass(d3Elem, 'downArrow', 10); |
| 97 | verifyIconStructure('downArrow', '#triangleDown', '10'); |
Bri Prebilic Cole | dee4662 | 2015-02-03 16:36:11 -0800 | [diff] [blame] | 98 | }); |
| 99 | |
| 100 | it('should verify no icon is displayed', function () { |
| 101 | expect(d3Elem.html()).toEqual(''); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 102 | is.loadIconByClass(d3Elem, 'tableColSortNone', 10); |
Bri Prebilic Cole | dee4662 | 2015-02-03 16:36:11 -0800 | [diff] [blame] | 103 | verifyIconStructure('tableColSortNone', null, '10'); |
| 104 | }); |
| 105 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 106 | }); |