Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 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 -- Util -- Theme Service - Unit Tests |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 19 | */ |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 20 | describe('factory: fw/util/theme.js', function() { |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 21 | var ts, $log, fs; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | 812aa5a | 2016-04-19 18:12:45 -0700 | [diff] [blame] | 23 | beforeEach(module('onosUtil', 'onosRemote')); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 24 | |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 25 | beforeEach(inject(function (ThemeService, _$log_, FnService) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 26 | ts = ThemeService; |
| 27 | $log = _$log_; |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 28 | fs = FnService; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 29 | ts.init(); |
| 30 | })); |
| 31 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 32 | it('should define ThemeService', function () { |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 33 | expect(ts).toBeDefined(); |
| 34 | }); |
| 35 | |
| 36 | it('should define api functions', function () { |
| 37 | expect(fs.areFunctions(ts, [ |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 38 | 'init', 'theme', 'toggleTheme', 'addListener', 'removeListener', |
| 39 | 'spriteColor' |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 40 | ])).toBeTruthy(); |
| 41 | }); |
| 42 | |
| 43 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 44 | 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 Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 86 | 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 HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 89 | 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 Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 97 | |
| 98 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 99 | // === Unit Tests for listeners |
| 100 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 101 | 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 Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 118 | var cb = jasmine.createSpy('cb'); |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 119 | expect(cb.calls.count()).toEqual(0); |
| 120 | |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 121 | // make sure previous tests don't affect our theme state... |
| 122 | ts.theme('light'); |
| 123 | expect(ts.theme()).toEqual('light'); |
| 124 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 125 | ts.toggleTheme(); // -> dark |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 126 | expect(cb.calls.count()).toEqual(0); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 127 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 128 | ts.addListener(cb); |
| 129 | expect(cb.calls.count()).toEqual(0); |
| 130 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 131 | ts.toggleTheme(); // -> light |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 132 | expect(cb.calls.count()).toEqual(1); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 133 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 134 | ts.theme('light'); // (still light - no event) |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 135 | expect(cb.calls.count()).toEqual(1); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 136 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 137 | ts.theme('dark'); // -> dark |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 138 | expect(cb.calls.count()).toEqual(2); |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 139 | |
| 140 | ts.removeListener(cb); |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 141 | expect(cb.calls.count()).toEqual(2); |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 142 | |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 143 | ts.toggleTheme(); // -> light (but we weren't registered to hear it) |
| 144 | expect(cb.calls.count()).toEqual(2); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 145 | }); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 146 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 147 | }); |