blob: 8f79e5cffdec3154631bf1a57331010a3696d8ec [file] [log] [blame]
Simon Hunt639dc662015-02-18 14:19:20 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt639dc662015-02-18 14:19:20 -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 Hunta6ab9f02017-07-11 11:45:50 -070018 ONOS GUI -- Layer -- Quick Help Service - Unit Tests
Simon Hunt639dc662015-02-18 14:19:20 -080019 */
20describe('factory: fw/layer/quickhelp.js', function () {
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -070021 var $log, fs, qhs, d3Elem,
22 fade = 500,
23 noop = function () {},
24 mockBindings = {
25 globalKeys: {
26 slash: [noop, 'Show / hide Quick Help'],
27 T: [noop, 'Toggle Theme']
28 },
29 globalFormat: ['slash', 'T'],
30 viewKeys: {
31 H: [noop, 'Show / hide hosts'],
32 I: [noop, 'Toggle instances panel']
33 },
34 viewGestures: []
35 };
36
37 // list of needed bindings to use in aggregateData
38 var neededBindings = [
39 'globalKeys', 'globalFormat', 'viewKeys', 'viewGestures'
40 ];
Simon Hunt639dc662015-02-18 14:19:20 -080041
Simon Hunta6ab9f02017-07-11 11:45:50 -070042 beforeEach(module('onosUtil', 'onosSvg', 'onosLayer', 'onosRemote'));
Simon Hunt639dc662015-02-18 14:19:20 -080043
Bri Prebilic Coleef091902015-04-28 16:53:47 -070044 beforeEach(inject(function (_$log_, FnService, QuickHelpService) {
Simon Hunt639dc662015-02-18 14:19:20 -080045 $log = _$log_;
Simon Hunt639dc662015-02-18 14:19:20 -080046 fs = FnService;
47 qhs = QuickHelpService;
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -070048
Bri Prebilic Coleef091902015-04-28 16:53:47 -070049 jasmine.clock().install();
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -070050 d3Elem = d3.select('body').append('div').attr('id', 'quickhelp');
51 qhs.initQuickHelp();
Simon Hunt639dc662015-02-18 14:19:20 -080052 }));
53
54 afterEach(function () {
Bri Prebilic Coleef091902015-04-28 16:53:47 -070055 jasmine.clock().uninstall();
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -070056 d3.select('#quickhelp').remove();
Simon Hunt639dc662015-02-18 14:19:20 -080057 });
58
59 function helpItemSelection() {
60 return d3Elem.selectAll('.help');
61 }
62
63 it('should define QuickHelpService', function () {
64 expect(qhs).toBeDefined();
65 });
66
67 it('should define api functions', function () {
68 expect(fs.areFunctions(qhs, [
69 'initQuickHelp', 'showQuickHelp', 'hideQuickHelp'
70 ])).toBeTruthy();
71 });
72
73 it('should have no items to start', function () {
74 expect(helpItemSelection().size()).toBe(0);
75 });
76
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -070077 // === showQuickHelp
78
79 it('should warn if bad bindings are provided', function () {
80 var warning =
81 'Quickhelp Service: showQuickHelp(), invalid bindings object';
82 spyOn($log, 'warn');
83
84 expect(qhs.showQuickHelp()).toBeNull();
85 expect($log.warn).toHaveBeenCalledWith(warning);
86
87 expect(qhs.showQuickHelp({})).toBeNull();
88 expect($log.warn).toHaveBeenCalledWith(warning);
89
90 expect(qhs.showQuickHelp([1, 2, 3])).toBeNull();
91 expect($log.warn).toHaveBeenCalledWith(warning);
Simon Hunt639dc662015-02-18 14:19:20 -080092 });
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -070093
94 it('should warn if not all needed bindings are provided', function () {
95 var warning =
96 'Quickhelp Service: showQuickHelp(),' +
97 ' needed bindings for help panel not provided:';
98 spyOn($log, 'warn');
99
100 expect(qhs.showQuickHelp({
101 foo: 'foo', bar: 'bar'
102 })).toBeNull();
103 expect($log.warn).toHaveBeenCalledWith(warning, neededBindings);
104
105 expect(qhs.showQuickHelp({
106 globalKeys: {}
107 })).toBeNull();
108 expect($log.warn).toHaveBeenCalledWith(warning, neededBindings);
109
110 expect(qhs.showQuickHelp({
111 globalKeys: {},
112 globalFormat: {},
113 viewKeys: {}
114 })).toBeNull();
115 expect($log.warn).toHaveBeenCalledWith(warning, neededBindings);
116 });
117
Simon Hunta6ab9f02017-07-11 11:45:50 -0700118 it('should not warn if bindings are provided (except lion)', function () {
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700119 spyOn($log, 'warn');
120 expect(qhs.showQuickHelp(mockBindings)).toBe(undefined);
Simon Hunta6ab9f02017-07-11 11:45:50 -0700121 expect($log.warn).toHaveBeenCalledWith(
122 'No lion bundle registered:', 'core.fw.QuickHelp'
123 );
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700124 });
125
Simon Hunta6ab9f02017-07-11 11:45:50 -0700126 // TODO: consider testing localization of text
127
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700128 it('should append an svg', function () {
129 var svg = d3Elem.select('svg');
130 expect(d3Elem.empty()).toBe(false);
131 expect(svg.empty()).toBe(true);
132
133 qhs.showQuickHelp(mockBindings);
134
135 svg = d3Elem.select('svg');
136 expect(svg.empty()).toBe(false);
137 expect(svg.attr('width')).toBe('100%');
138 expect(svg.attr('height')).toBe('80%');
139 expect(svg.attr('viewBox')).toBe('-200 0 400 400');
140 });
141
142 it('should create the quick help panel', function () {
143 var helpItems, g, rect, text, rows;
144 qhs.showQuickHelp(mockBindings);
145
146 helpItems = helpItemSelection();
147 expect(helpItems.size()).toBe(1);
148
149 g = d3.select('g.help');
150 expect(g.attr('opacity')).toBe('0');
151
152 rect = g.select('rect');
153 expect(rect.attr('rx')).toBe('8');
154
155 text = g.select('text');
Simon Hunta6ab9f02017-07-11 11:45:50 -0700156 // NOTE: we aren't mocking localization, so should get %...% key tag
157 expect(text.text()).toBe('%qh_title%');
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700158 expect(text.classed('title')).toBe(true);
159 expect(text.attr('dy')).toBe('1.2em');
160 expect(text.attr('transform')).toBeTruthy();
161
162 rows = g.select('g');
163 expect(rows.empty()).toBe(false);
164
165 jasmine.clock().tick(fade + 1);
166 setTimeout(function () {
167 expect(g.attr('opacity')).toBe('1');
168 }, fade);
169
170 // TODO: test aggregate data helper function
171 });
172
173 it('should show panel with custom fade time', function () {
174 var g,
175 ctmFade = 200;
176 qhs.initQuickHelp({ fade: ctmFade });
177 qhs.showQuickHelp(mockBindings);
178
179 g = d3.select('g.help');
180 expect(g.attr('opacity')).toBe('0');
181
182 jasmine.clock().tick(ctmFade + 1);
183 setTimeout(function () {
184 expect(g.attr('opacity')).toBe('1');
185 }, ctmFade);
186 });
187
188 // === hideQuickHelp
189
190 it('should hide quick help if svg exists', function () {
191 var svg;
192
193 expect(qhs.hideQuickHelp()).toBe(false);
194
195 svg = d3.select('#quickhelp')
196 .append('svg');
197 svg.append('g')
198 .classed('help', true)
199 .attr('opacity', 1);
200
201 expect(qhs.hideQuickHelp()).toBe(true);
202
203 jasmine.clock().tick(fade + 1);
204 setTimeout(function () {
205 expect(svg.select('g.help').attr('opacity')).toBe('0');
206 }, fade);
207
208 jasmine.clock().tick(20);
209 setTimeout(function () {
210 expect(svg.empty()).toBe(true);
211 }, fade + 20);
212 });
213
214 it('should not hide quick help if svg does not exist', function () {
215 expect(qhs.hideQuickHelp()).toBe(false);
216 });
217
Simon Hunt639dc662015-02-18 14:19:20 -0800218});
219