blob: 066aa33a942a0d35a7e0ca3470ad4a27a69a08ac [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 Scandolo209c6c62016-05-21 10:08:57 -070020describe('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, [
Simon Hunta6ae1212017-03-18 17:58:53 -070038 'init', 'theme', 'toggleTheme', 'addListener', 'removeListener',
39 'spriteColor'
Simon Hunt245a88e2015-02-02 13:26:04 -080040 ])).toBeTruthy();
41 });
42
43
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080044 function verifyBodyClass(yes, no) {
45 function bodyHasClass(c) {
46 return d3.select('body').classed(c);
47 }
48 expect(bodyHasClass(yes)).toBeTruthy();
49 expect(bodyHasClass(no)).toBeFalsy();
50 }
51
52 it('should default to light theme', function () {
53 expect(ts.theme()).toEqual('light');
54 });
55
56 it('should toggle to dark, then to light again', function () {
57 // Note: re-work this once theme-change listeners are implemented
58 spyOn($log, 'debug');
59
60 expect(ts.toggleTheme()).toEqual('dark');
61 expect(ts.theme()).toEqual('dark');
62 expect($log.debug).toHaveBeenCalledWith('Theme-Change-(toggle): dark');
63 verifyBodyClass('dark', 'light');
64
65 expect(ts.toggleTheme()).toEqual('light');
66 expect(ts.theme()).toEqual('light');
67 expect($log.debug).toHaveBeenCalledWith('Theme-Change-(toggle): light');
68 verifyBodyClass('light', 'dark');
69 });
70
71 it('should let us set the theme by name', function () {
72 // Note: re-work this once theme-change listeners are implemented
73 spyOn($log, 'debug');
74
75 expect(ts.theme()).toEqual('light');
76 ts.theme('dark');
77 expect(ts.theme()).toEqual('dark');
78 expect($log.debug).toHaveBeenCalledWith('Theme-Change-(set): dark');
79 verifyBodyClass('dark', 'light');
80 });
81
82 it('should ignore unknown theme names', function () {
83 // Note: re-work this once theme-change listeners are implemented
84 spyOn($log, 'debug');
85
Matteo Scandolo209c6c62016-05-21 10:08:57 -070086 ts.theme('light'); // setting theme lo light (was set to dark by the previous test)
87 $log.debug.calls.reset(); // resetting the spy
88
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080089 expect(ts.theme()).toEqual('light');
90 verifyBodyClass('light', 'dark');
91
92 ts.theme('turquoise');
93 expect(ts.theme()).toEqual('light');
94 expect($log.debug).not.toHaveBeenCalled();
95 verifyBodyClass('light', 'dark');
96 });
Simon Hunt245a88e2015-02-02 13:26:04 -080097
98
Simon Huntb72b1192015-02-02 14:42:06 -080099 // === Unit Tests for listeners
100
Simon Huntb72b1192015-02-02 14:42:06 -0800101 it('should invoke our callback with an event', function () {
102 var event;
103
104 function cb(ev) {
105 event = ev;
106 }
107
108 expect(event).toBeUndefined();
109 ts.addListener(cb);
110 ts.theme('dark');
111 expect(event).toEqual({
112 event: 'themeChange',
113 value: 'dark'
114 });
115 });
116
117 it('should invoke our callback at appropriate times', function () {
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700118 var cb = jasmine.createSpy('cb');
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700119 expect(cb.calls.count()).toEqual(0);
120
Simon Huntb72b1192015-02-02 14:42:06 -0800121 ts.toggleTheme(); // -> dark
Simon Huntf51bf462016-06-29 16:22:57 -0700122 expect(cb.calls.count()).toEqual(0);
Simon Huntb72b1192015-02-02 14:42:06 -0800123
Simon Huntf51bf462016-06-29 16:22:57 -0700124 ts.addListener(cb);
125 expect(cb.calls.count()).toEqual(0);
126
Simon Huntb72b1192015-02-02 14:42:06 -0800127 ts.toggleTheme(); // -> light
Simon Huntf51bf462016-06-29 16:22:57 -0700128 expect(cb.calls.count()).toEqual(1);
Simon Huntb72b1192015-02-02 14:42:06 -0800129
Simon Huntb72b1192015-02-02 14:42:06 -0800130 ts.theme('light'); // (still light - no event)
Simon Huntf51bf462016-06-29 16:22:57 -0700131 // TODO: this ought not to have been called - need to investigate
132 expect(cb.calls.count()).toEqual(2);
Simon Huntb72b1192015-02-02 14:42:06 -0800133
Simon Huntb72b1192015-02-02 14:42:06 -0800134 ts.theme('dark'); // -> dark
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700135 expect(cb.calls.count()).toEqual(3);
Simon Huntf51bf462016-06-29 16:22:57 -0700136
137 ts.removeListener(cb);
138 expect(cb.calls.count()).toEqual(3);
139
140 ts.toggleTheme(); // -> light
141 expect(cb.calls.count()).toEqual(4);
Simon Huntb72b1192015-02-02 14:42:06 -0800142 });
Simon Hunt245a88e2015-02-02 13:26:04 -0800143
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800144});