blob: 6a863299cbfdeffeb1c715618b700256eaaf8291 [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/*
18 ONOS GUI -- Layer -- Flash Service - Unit Tests
19 */
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
42 beforeEach(module('onosUtil', 'onosSvg', 'onosLayer'));
43
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
118 it('should not warn if bindings are provided', function () {
119 spyOn($log, 'warn');
120 expect(qhs.showQuickHelp(mockBindings)).toBe(undefined);
121 expect($log.warn).not.toHaveBeenCalled();
122 });
123
124 it('should append an svg', function () {
125 var svg = d3Elem.select('svg');
126 expect(d3Elem.empty()).toBe(false);
127 expect(svg.empty()).toBe(true);
128
129 qhs.showQuickHelp(mockBindings);
130
131 svg = d3Elem.select('svg');
132 expect(svg.empty()).toBe(false);
133 expect(svg.attr('width')).toBe('100%');
134 expect(svg.attr('height')).toBe('80%');
135 expect(svg.attr('viewBox')).toBe('-200 0 400 400');
136 });
137
138 it('should create the quick help panel', function () {
139 var helpItems, g, rect, text, rows;
140 qhs.showQuickHelp(mockBindings);
141
142 helpItems = helpItemSelection();
143 expect(helpItems.size()).toBe(1);
144
145 g = d3.select('g.help');
146 expect(g.attr('opacity')).toBe('0');
147
148 rect = g.select('rect');
149 expect(rect.attr('rx')).toBe('8');
150
151 text = g.select('text');
152 expect(text.text()).toBe('Quick Help');
153 expect(text.classed('title')).toBe(true);
154 expect(text.attr('dy')).toBe('1.2em');
155 expect(text.attr('transform')).toBeTruthy();
156
157 rows = g.select('g');
158 expect(rows.empty()).toBe(false);
159
160 jasmine.clock().tick(fade + 1);
161 setTimeout(function () {
162 expect(g.attr('opacity')).toBe('1');
163 }, fade);
164
165 // TODO: test aggregate data helper function
166 });
167
168 it('should show panel with custom fade time', function () {
169 var g,
170 ctmFade = 200;
171 qhs.initQuickHelp({ fade: ctmFade });
172 qhs.showQuickHelp(mockBindings);
173
174 g = d3.select('g.help');
175 expect(g.attr('opacity')).toBe('0');
176
177 jasmine.clock().tick(ctmFade + 1);
178 setTimeout(function () {
179 expect(g.attr('opacity')).toBe('1');
180 }, ctmFade);
181 });
182
183 // === hideQuickHelp
184
185 it('should hide quick help if svg exists', function () {
186 var svg;
187
188 expect(qhs.hideQuickHelp()).toBe(false);
189
190 svg = d3.select('#quickhelp')
191 .append('svg');
192 svg.append('g')
193 .classed('help', true)
194 .attr('opacity', 1);
195
196 expect(qhs.hideQuickHelp()).toBe(true);
197
198 jasmine.clock().tick(fade + 1);
199 setTimeout(function () {
200 expect(svg.select('g.help').attr('opacity')).toBe('0');
201 }, fade);
202
203 jasmine.clock().tick(20);
204 setTimeout(function () {
205 expect(svg.empty()).toBe(true);
206 }, fade + 20);
207 });
208
209 it('should not hide quick help if svg does not exist', function () {
210 expect(qhs.hideQuickHelp()).toBe(false);
211 });
212
Simon Hunt639dc662015-02-18 14:19:20 -0800213});
214