blob: c887c790eda7fdea41d33df1743975d6c9dcc9e5 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 Hunt7ac7be92015-01-06 10:47:56 -080019 */
20describe('factory: fw/svg/icon.js', function() {
Simon Hunt7f172cc2015-01-16 17:43:00 -080021 var is, d3Elem;
22
23 var viewBox = '0 0 50 50',
24 glyphSize = '50',
25 iconSize = '20';
26
Simon Hunt7ac7be92015-01-06 10:47:56 -080027
28 beforeEach(module('onosSvg'));
29
30 beforeEach(inject(function (IconService) {
31 is = IconService;
Simon Hunt7f172cc2015-01-16 17:43:00 -080032 d3Elem = d3.select('body').append('div').attr('id', 'myDiv');
Simon Hunt7ac7be92015-01-06 10:47:56 -080033 }));
34
Simon Hunt7f172cc2015-01-16 17:43:00 -080035 afterEach(function () {
36 d3.select('#myDiv').remove();
37 });
38
Simon Hunt7ac7be92015-01-06 10:47:56 -080039 it('should define IconService', function () {
40 expect(is).toBeDefined();
41 });
42
Simon Hunt7f172cc2015-01-16 17:43:00 -080043 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 Hunt97225382015-01-19 13:33:09 -080066 expect(rect.attr('rx')).toEqual('5');
Simon Hunt7f172cc2015-01-16 17:43:00 -080067
Bri Prebilic Coledee46622015-02-03 16:36:11 -080068 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 Hunt7f172cc2015-01-16 17:43:00 -080074 }
75
Simon Hunt7f172cc2015-01-16 17:43:00 -080076 it('should load an icon into a div', function () {
77 expect(d3Elem.html()).toEqual('');
78 is.loadIcon(d3Elem, 'deviceOnline');
79 verifyIconStructure('deviceOnline', '#checkMark');
80 });
81
82 it('should allow us to specify the icon size', function () {
83 expect(d3Elem.html()).toEqual('');
84 is.loadIcon(d3Elem, 'deviceOffline', 32);
85 verifyIconStructure('deviceOffline', '#xMark', '32');
86 });
87
Bri Prebilic Coledee46622015-02-03 16:36:11 -080088 it('should verify triangleUp icon', function () {
89 expect(d3Elem.html()).toEqual('');
90 is.loadIcon(d3Elem, 'tableColSortAsc', 10);
91 verifyIconStructure('tableColSortAsc', '#triangleUp', '10');
92 });
93
94 it('should verify triangleDown icon', function () {
95 expect(d3Elem.html()).toEqual('');
96 is.loadIcon(d3Elem, 'tableColSortDesc', 10);
97 verifyIconStructure('tableColSortDesc', '#triangleDown', '10');
98 });
99
100 it('should verify no icon is displayed', function () {
101 expect(d3Elem.html()).toEqual('');
102 is.loadIcon(d3Elem, 'tableColSortNone', 10);
103 verifyIconStructure('tableColSortNone', null, '10');
104 });
105
Simon Hunt7ac7be92015-01-06 10:47:56 -0800106});