blob: 7d53c23f287555222dfb0c42d633705f2ced44f2 [file] [log] [blame]
Simon Hunt420691a2014-12-16 20:16:28 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 -- Key Handler Service - Unit Tests
19
20 @author Simon Hunt
21 */
22describe('factory: fw/lib/keys.js', function() {
23 var ks,
24 fs,
25 d3Elem;
26
27 beforeEach(module('onosApp'));
28
29 beforeEach(inject(function (KeyService, FnService) {
30 ks = KeyService;
31 fs = FnService;
32 d3Elem = d3.select('body').append('p').attr('id', 'ptest');
33 ks.installOn(d3Elem);
34 }));
35
36 afterEach(function () {
37 d3.select('#ptest').remove();
38 });
39
40 it('should have injected stuff defined', function () {
41 expect(ONOS).toBeDefined();
42 expect(ks).toBeDefined();
43 expect(fs).toBeDefined();
44 });
45
46 // NOTE: kinda messy, but it seems to get the job done.
47 function jsKeyDown(element, code) {
48 var ev = document.createEvent('KeyboardEvent');
49
50 // Chromium Hack
51 if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
52 Object.defineProperty(ev, 'keyCode', {
53 get: function () { return this.keyCodeVal; }
54 });
55 Object.defineProperty(ev, 'which', {
56 get: function () { return this.keyCodeVal; }
57 });
58 }
59
60 if (ev.initKeyboardEvent) {
61 ev.initKeyboardEvent('keydown', true, true, document.defaultView,
62 false, false, false, false, code, code);
63 } else {
64 ev.initKeyEvent('keydown', true, true, document.defaultView,
65 false, false, false, false, code, 0);
66 }
67
68 ev.keyCodeVal = code;
69
70 if (ev.keyCode !== code) {
71 console.warn("keyCode mismatch " + ev.keyCode +
72 "(" + ev.which + ") -> "+ code);
73 }
74 element.dispatchEvent(ev);
75 }
76
77 it('should start in light theme', function () {
78 expect(ks.theme()).toEqual('light');
79 });
80 it('should toggle to dark theme', function () {
81 jsKeyDown(d3Elem.node(), 84); // 'T'
82 expect(ks.theme()).toEqual('dark');
83 });
84
85 // key code lookups
86 // NOTE: should be injecting keydown events, rather than exposing whatKey()
87 it('whatKey: 13', function () {
88 expect(ks.whatKey(13)).toEqual('enter');
89 });
90 it('whatKey: 16', function () {
91 expect(ks.whatKey(16)).toEqual('shift');
92 });
93 it('whatKey: 40', function () {
94 expect(ks.whatKey(40)).toEqual('downArrow');
95 });
96 it('whatKey: 65', function () {
97 expect(ks.whatKey(65)).toEqual('A');
98 });
99 it('whatKey: 84', function () {
100 expect(ks.whatKey(84)).toEqual('T');
101 });
102 it('whatKey: 49', function () {
103 expect(ks.whatKey(49)).toEqual('1');
104 });
105 it('whatKey: 55', function () {
106 expect(ks.whatKey(55)).toEqual('7');
107 });
108 it('whatKey: 112', function () {
109 expect(ks.whatKey(112)).toEqual('F1');
110 });
111 it('whatKey: 123', function () {
112 expect(ks.whatKey(123)).toEqual('F12');
113 });
114 it('whatKey: 1', function () {
115 expect(ks.whatKey(1)).toEqual('.');
116 });
117
118});