OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 The Chromium Authors. All rights reserved. | 2 * Copyright 2014 The Chromium Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 var mockController; | 7 var mockController; |
8 var mockTimer; | 8 var mockTimer; |
9 var setComposition; | 9 var setComposition; |
10 | 10 |
11 var DEFAULT_CONTEXT_ID = 1; | 11 var DEFAULT_CONTEXT_ID = 1; |
12 var CAPSLOCK_ID = "OsLeft"; | |
13 | |
14 /** | |
15 * Key alignments. | |
16 * @enum {string} | |
17 */ | |
18 var Alignment = { | |
19 LEFT: 'left', | |
20 RIGHT: 'right', | |
21 CENTER: 'center' | |
22 }; | |
23 | 12 |
24 /** | 13 /** |
25 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API | 14 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API |
26 * calls must set expectations for call signatures. | 15 * calls must set expectations for call signatures. |
27 */ | 16 */ |
28 function setUp() { | 17 function setUp() { |
29 mockController = new MockController(); | 18 mockController = new MockController(); |
30 mockTimer = new MockTimer(); | 19 mockTimer = new MockTimer(); |
31 mockTimer.install(); | 20 mockTimer.install(); |
32 | 21 |
(...skipping 22 matching lines...) Expand all Loading... | |
55 * Verify that API calls match expectations. | 44 * Verify that API calls match expectations. |
56 */ | 45 */ |
57 function tearDown() { | 46 function tearDown() { |
58 mockController.verifyMocks(); | 47 mockController.verifyMocks(); |
59 mockController.reset(); | 48 mockController.reset(); |
60 mockTimer.uninstall(); | 49 mockTimer.uninstall(); |
61 chrome.input.ime.setComposition = setComposition; | 50 chrome.input.ime.setComposition = setComposition; |
62 } | 51 } |
63 | 52 |
64 /** | 53 /** |
65 * Retrieves the key from the current keyset. | 54 * Checks whether the element is currently being displayed on screen. |
66 * @param {String} char The character of the key. | 55 * @param {Object} The object to check. |
67 * @return {Object} The key. | 56 * @return {boolean} |
68 */ | 57 */ |
69 function getKey(char) { | 58 function isActive(el) { |
70 var key = document.querySelector('#Key' + char.toUpperCase()); | 59 return window.getComputedStyle(el).display != "none"; |
71 assertTrue(!!key, "Cannot find key: " + char); | |
72 return key; | |
73 } | 60 } |
74 | 61 |
62 (function(exports) { | |
63 | |
64 var KEY_IDS = { | |
bshe
2014/05/12 13:24:27
Could you document why only these characters have
rsadam
2014/05/12 20:23:46
Done.
| |
65 'a' : { | |
66 'us' : '101kbd-k-29', | |
67 'us.compact' : 'compactkbd-k-key-10', | |
68 }, | |
69 'c' : { | |
70 'us' : '101kbd-k-44', | |
71 'us.compact' : 'compactkbd-k-key-21', | |
72 | |
73 }, | |
74 'd' : { | |
75 'us' : '101kbd-k-31', | |
76 'us.compact' : 'compactkbd-k-key-12', | |
77 | |
78 }, | |
79 'l' : { | |
80 'us' : '101kbd-k-37', | |
81 'us.compact' : 'compactkbd-k-key-18', | |
82 | |
83 }, | |
84 'p' : { | |
85 'us' : '101kbd-k-24', | |
86 'us.compact' : 'compactkbd-k-key-9', | |
87 }, | |
88 'leftshift' : { | |
89 'us' : '101kbd-k-41', | |
90 'us.compact' : 'compactkbd-k-21', | |
91 }, | |
92 "capslock" : { | |
93 'us' : '101kbd-k-28', | |
94 } | |
95 }; | |
96 | |
97 /** | |
98 * Gets the key id of the specified character. | |
99 * @param {string} layout The current keyboard layout. | |
100 * @param {char} char The character to press. | |
101 */ | |
102 var getKeyId_ = function(layout, char) { | |
103 var lower = char.toLowerCase(); | |
104 assertTrue(!!KEY_IDS[lower], "Cannot find cached key id: " + char); | |
105 assertTrue(!!KEY_IDS[lower][layout], | |
106 "Cannot find cached key id: " + char + " in " + layout); | |
107 return KEY_IDS[lower][layout]; | |
108 } | |
109 | |
110 /** | |
111 * Returns the current layout id. | |
112 * @return {string} | |
113 */ | |
114 var getLayoutId_ = function() { | |
115 // TODO(rsadam@): Generalize this. | |
116 var id = window.location.search.split("id=")[1]; | |
117 assertTrue(!!id, "No layout found."); | |
118 return id; | |
119 } | |
120 | |
121 /** | |
122 * Returns the key object corresponding to the character. | |
123 * @return {string} char The character. | |
124 */ | |
125 var getKey_ = function(char) { | |
126 var layoutId = getLayoutId(); | |
127 var key = document.getElementById(getKeyId_(layoutId, char)); | |
128 assertTrue(!!key, "Key not present in layout: " + char); | |
129 return key; | |
130 } | |
131 | |
132 exports.getKey = getKey_; | |
133 exports.getLayoutId = getLayoutId_; | |
134 })(this); | |
135 | |
75 /** | 136 /** |
76 * Generates a mouse event and dispatches it on the target. | 137 * Generates a mouse event and dispatches it on the target. |
77 * @param target {Object} The target of the event. | 138 * @param target {Object} The target of the event. |
78 * @param type {String} The type of the mouse event. | 139 * @param type {String} The type of the mouse event. |
79 */ | 140 */ |
80 function generateMouseEvent(target, type) { | 141 function generateMouseEvent(target, type) { |
81 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); | 142 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); |
82 target.dispatchEvent(e); | 143 target.dispatchEvent(e); |
83 } | 144 } |
84 | 145 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 * @param type {String} The type of the touch event. | 177 * @param type {String} The type of the touch event. |
117 */ | 178 */ |
118 function generateTouchEvent(target, type) { | 179 function generateTouchEvent(target, type) { |
119 var e = document.createEvent('UIEvents'); | 180 var e = document.createEvent('UIEvents'); |
120 e.initEvent(type, true, true); | 181 e.initEvent(type, true, true); |
121 target.dispatchEvent(e); | 182 target.dispatchEvent(e); |
122 } | 183 } |
123 | 184 |
124 /** | 185 /** |
125 * Mocks a character type using touch. | 186 * Mocks a character type using touch. |
126 * @param {String} char The character to type. | 187 * @param {String} char The expected character. |
127 */ | 188 */ |
128 function mockTouchType(char) { | 189 function mockTouchType(char) { |
129 var send = chrome.input.ime.commitText; | 190 var send = chrome.input.ime.commitText; |
130 send.addExpectation({ | 191 send.addExpectation({ |
131 contextId: DEFAULT_CONTEXT_ID, | 192 contextId: DEFAULT_CONTEXT_ID, |
132 text: char, | 193 text: char, |
133 }); | 194 }); |
134 var key = getKey(char); | 195 var key = getKey(char); |
135 generateTouchEvent(key, 'touchstart'); | 196 generateTouchEvent(key, 'touchstart'); |
136 generateTouchEvent(key, 'touchend'); | 197 generateTouchEvent(key, 'touchend'); |
137 } | 198 } |
138 | |
139 /** | |
140 * Retrieves the shift key from the current keyset. | |
141 * @param {Alignment} align The alignment of the shift key. | |
142 * @return {Object} The key. | |
143 */ | |
144 function getShiftKey(align) { | |
145 var id; | |
146 switch(align) { | |
147 case Alignment.LEFT: | |
148 id = 'ShiftLeft'; | |
149 break; | |
150 case Alignment.RIGHT: | |
151 id = 'ShiftRight'; | |
152 break; | |
153 default: | |
154 break; | |
155 } | |
156 assertTrue(!!id, "Invalid shift alignment option: " + align); | |
157 var shift = document.querySelector('#' + id); | |
158 assertTrue(!!shift, "Cannot find shift key with alignment: " + align); | |
159 return shift; | |
160 } | |
OLD | NEW |