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, [ |
| 38 | 'init', 'theme', 'toggleTheme', 'addListener', 'removeListener' |
| 39 | ])).toBeTruthy(); |
| 40 | }); |
| 41 | |
| 42 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 43 | 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 | |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 85 | ts.theme('light'); // setting theme lo light (was set to dark by the previous test) |
| 86 | $log.debug.calls.reset(); // resetting the spy |
| 87 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 88 | expect(ts.theme()).toEqual('light'); |
| 89 | verifyBodyClass('light', 'dark'); |
| 90 | |
| 91 | ts.theme('turquoise'); |
| 92 | expect(ts.theme()).toEqual('light'); |
| 93 | expect($log.debug).not.toHaveBeenCalled(); |
| 94 | verifyBodyClass('light', 'dark'); |
| 95 | }); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 96 | |
| 97 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 98 | // === Unit Tests for listeners |
| 99 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 100 | it('should invoke our callback with an event', function () { |
| 101 | var event; |
| 102 | |
| 103 | function cb(ev) { |
| 104 | event = ev; |
| 105 | } |
| 106 | |
| 107 | expect(event).toBeUndefined(); |
| 108 | ts.addListener(cb); |
| 109 | ts.theme('dark'); |
| 110 | expect(event).toEqual({ |
| 111 | event: 'themeChange', |
| 112 | value: 'dark' |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | it('should invoke our callback at appropriate times', function () { |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 117 | var cb = jasmine.createSpy('cb'); |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 118 | expect(cb.calls.count()).toEqual(0); |
| 119 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 120 | ts.toggleTheme(); // -> dark |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 121 | expect(cb.calls.count()).toEqual(0); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 122 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 123 | ts.addListener(cb); |
| 124 | expect(cb.calls.count()).toEqual(0); |
| 125 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 126 | ts.toggleTheme(); // -> light |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 127 | expect(cb.calls.count()).toEqual(1); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 128 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 129 | ts.theme('light'); // (still light - no event) |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 130 | // TODO: this ought not to have been called - need to investigate |
| 131 | expect(cb.calls.count()).toEqual(2); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 132 | |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 133 | ts.theme('dark'); // -> dark |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 134 | expect(cb.calls.count()).toEqual(3); |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 135 | |
| 136 | ts.removeListener(cb); |
| 137 | expect(cb.calls.count()).toEqual(3); |
| 138 | |
| 139 | ts.toggleTheme(); // -> light |
| 140 | expect(cb.calls.count()).toEqual(4); |
Simon Hunt | b72b119 | 2015-02-02 14:42:06 -0800 | [diff] [blame] | 141 | }); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 142 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 143 | }); |