blob: 256b064200c788d31a6572e83047e94e80c67b80 [file] [log] [blame]
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -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 -- Util -- Theme Service - Unit Tests
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080019 */
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070020xdescribe('factory: fw/util/theme.js', function() {
Simon Hunt245a88e2015-02-02 13:26:04 -080021 var ts, $log, fs;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080022
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070023 beforeEach(module('onosUtil', 'onosRemote'));
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080024
Simon Hunt245a88e2015-02-02 13:26:04 -080025 beforeEach(inject(function (ThemeService, _$log_, FnService) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080026 ts = ThemeService;
27 $log = _$log_;
Simon Hunt245a88e2015-02-02 13:26:04 -080028 fs = FnService;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080029 ts.init();
30 }));
31
Simon Hunt3a6eec02015-02-09 21:16:43 -080032 it('should define ThemeService', function () {
Simon Hunt245a88e2015-02-02 13:26:04 -080033 expect(ts).toBeDefined();
34 });
35
36 it('should define api functions', function () {
37 expect(fs.areFunctions(ts, [
38 'init', 'theme', 'toggleTheme', 'addListener', 'removeListener'
39 ])).toBeTruthy();
40 });
41
42
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080043 function verifyBodyClass(yes, no) {
44 function bodyHasClass(c) {
45 return d3.select('body').classed(c);
46 }
47 expect(bodyHasClass(yes)).toBeTruthy();
48 expect(bodyHasClass(no)).toBeFalsy();
49 }
50
51 it('should default to light theme', function () {
52 expect(ts.theme()).toEqual('light');
53 });
54
55 it('should toggle to dark, then to light again', function () {
56 // Note: re-work this once theme-change listeners are implemented
57 spyOn($log, 'debug');
58
59 expect(ts.toggleTheme()).toEqual('dark');
60 expect(ts.theme()).toEqual('dark');
61 expect($log.debug).toHaveBeenCalledWith('Theme-Change-(toggle): dark');
62 verifyBodyClass('dark', 'light');
63
64 expect(ts.toggleTheme()).toEqual('light');
65 expect(ts.theme()).toEqual('light');
66 expect($log.debug).toHaveBeenCalledWith('Theme-Change-(toggle): light');
67 verifyBodyClass('light', 'dark');
68 });
69
70 it('should let us set the theme by name', function () {
71 // Note: re-work this once theme-change listeners are implemented
72 spyOn($log, 'debug');
73
74 expect(ts.theme()).toEqual('light');
75 ts.theme('dark');
76 expect(ts.theme()).toEqual('dark');
77 expect($log.debug).toHaveBeenCalledWith('Theme-Change-(set): dark');
78 verifyBodyClass('dark', 'light');
79 });
80
81 it('should ignore unknown theme names', function () {
82 // Note: re-work this once theme-change listeners are implemented
83 spyOn($log, 'debug');
84
85 expect(ts.theme()).toEqual('light');
86 verifyBodyClass('light', 'dark');
87
88 ts.theme('turquoise');
89 expect(ts.theme()).toEqual('light');
90 expect($log.debug).not.toHaveBeenCalled();
91 verifyBodyClass('light', 'dark');
92 });
Simon Hunt245a88e2015-02-02 13:26:04 -080093
94
Simon Huntb72b1192015-02-02 14:42:06 -080095 // === Unit Tests for listeners
96
97 it('should report lack of callback', function () {
98 spyOn($log, 'error');
99 var list = ts.addListener();
100 expect($log.error).toHaveBeenCalledWith(
101 'ThemeService.addListener(): callback not a function'
102 );
103 expect(list.error).toEqual('No callback defined');
104 });
105
106 it('should report non-functional callback', function () {
107 spyOn($log, 'error');
108 var list = ts.addListener(['some array']);
109 expect($log.error).toHaveBeenCalledWith(
110 'ThemeService.addListener(): callback not a function'
111 );
112 expect(list.error).toEqual('No callback defined');
113 });
114
115 it('should invoke our callback with an event', function () {
116 var event;
117
118 function cb(ev) {
119 event = ev;
120 }
121
122 expect(event).toBeUndefined();
123 ts.addListener(cb);
124 ts.theme('dark');
125 expect(event).toEqual({
126 event: 'themeChange',
127 value: 'dark'
128 });
129 });
130
131 it('should invoke our callback at appropriate times', function () {
132 var calls = [],
133 phase,
134 listener;
135
136 function cb() {
137 calls.push(phase);
138 }
139
140 expect(calls).toEqual([]);
141
142 phase = 'pre';
143 ts.toggleTheme(); // -> dark
144
145 phase = 'added';
146 listener = ts.addListener(cb);
147 ts.toggleTheme(); // -> light
148
149 phase = 'same';
150 ts.theme('light'); // (still light - no event)
151
152 phase = 'diff';
153 ts.theme('dark'); // -> dark
154
155 phase = 'post';
156 ts.removeListener(listener);
157 ts.toggleTheme(); // -> light
158
159 expect(calls).toEqual(['added', 'diff']);
160 });
Simon Hunt245a88e2015-02-02 13:26:04 -0800161
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800162});