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 /** |
| 65 * Map from keys to layout specific key ids. This only contains a small subset |
| 66 * of the keys which are used in testing. The ids are based on the XKB layouts |
| 67 * in GoogleKeyboardInput-xkb.crx. |
| 68 */ |
| 69 var KEY_IDS = { |
| 70 'a' : { |
| 71 'us' : '101kbd-k-29', |
| 72 'us.compact' : 'compactkbd-k-key-10', |
| 73 }, |
| 74 'c' : { |
| 75 'us' : '101kbd-k-44', |
| 76 'us.compact' : 'compactkbd-k-key-21', |
| 77 |
| 78 }, |
| 79 'd' : { |
| 80 'us' : '101kbd-k-31', |
| 81 'us.compact' : 'compactkbd-k-key-12', |
| 82 |
| 83 }, |
| 84 'l' : { |
| 85 'us' : '101kbd-k-37', |
| 86 'us.compact' : 'compactkbd-k-key-18', |
| 87 |
| 88 }, |
| 89 'p' : { |
| 90 'us' : '101kbd-k-24', |
| 91 'us.compact' : 'compactkbd-k-key-9', |
| 92 }, |
| 93 'leftshift' : { |
| 94 'us' : '101kbd-k-41', |
| 95 'us.compact' : 'compactkbd-k-21', |
| 96 }, |
| 97 "capslock" : { |
| 98 'us' : '101kbd-k-28', |
| 99 } |
| 100 }; |
| 101 |
| 102 /** |
| 103 * Gets the key id of the specified character. |
| 104 * @param {string} layout The current keyboard layout. |
| 105 * @param {char} char The character to press. |
| 106 */ |
| 107 var getKeyId_ = function(layout, char) { |
| 108 var lower = char.toLowerCase(); |
| 109 assertTrue(!!KEY_IDS[lower], "Cannot find cached key id: " + char); |
| 110 assertTrue(!!KEY_IDS[lower][layout], |
| 111 "Cannot find cached key id: " + char + " in " + layout); |
| 112 return KEY_IDS[lower][layout]; |
| 113 } |
| 114 |
| 115 /** |
| 116 * Returns the current layout id. |
| 117 * @return {string} |
| 118 */ |
| 119 var getLayoutId_ = function() { |
| 120 // TODO(rsadam@): Generalize this. |
| 121 var id = window.location.search.split("id=")[1]; |
| 122 assertTrue(!!id, "No layout found."); |
| 123 return id; |
| 124 } |
| 125 |
| 126 /** |
| 127 * Returns the key object corresponding to the character. |
| 128 * @return {string} char The character. |
| 129 */ |
| 130 var getKey_ = function(char) { |
| 131 var layoutId = getLayoutId(); |
| 132 var key = document.getElementById(getKeyId_(layoutId, char)); |
| 133 assertTrue(!!key, "Key not present in layout: " + char); |
| 134 return key; |
| 135 } |
| 136 |
| 137 exports.getKey = getKey_; |
| 138 exports.getLayoutId = getLayoutId_; |
| 139 })(this); |
| 140 |
75 /** | 141 /** |
76 * Generates a mouse event and dispatches it on the target. | 142 * Generates a mouse event and dispatches it on the target. |
77 * @param target {Object} The target of the event. | 143 * @param target {Object} The target of the event. |
78 * @param type {String} The type of the mouse event. | 144 * @param type {String} The type of the mouse event. |
79 */ | 145 */ |
80 function generateMouseEvent(target, type) { | 146 function generateMouseEvent(target, type) { |
81 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); | 147 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); |
82 target.dispatchEvent(e); | 148 target.dispatchEvent(e); |
83 } | 149 } |
84 | 150 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 * @param type {String} The type of the touch event. | 182 * @param type {String} The type of the touch event. |
117 */ | 183 */ |
118 function generateTouchEvent(target, type) { | 184 function generateTouchEvent(target, type) { |
119 var e = document.createEvent('UIEvents'); | 185 var e = document.createEvent('UIEvents'); |
120 e.initEvent(type, true, true); | 186 e.initEvent(type, true, true); |
121 target.dispatchEvent(e); | 187 target.dispatchEvent(e); |
122 } | 188 } |
123 | 189 |
124 /** | 190 /** |
125 * Mocks a character type using touch. | 191 * Mocks a character type using touch. |
126 * @param {String} char The character to type. | 192 * @param {String} char The expected character. |
127 */ | 193 */ |
128 function mockTouchType(char) { | 194 function mockTouchType(char) { |
129 var send = chrome.input.ime.commitText; | 195 var send = chrome.input.ime.commitText; |
130 send.addExpectation({ | 196 send.addExpectation({ |
131 contextId: DEFAULT_CONTEXT_ID, | 197 contextId: DEFAULT_CONTEXT_ID, |
132 text: char, | 198 text: char, |
133 }); | 199 }); |
134 var key = getKey(char); | 200 var key = getKey(char); |
135 generateTouchEvent(key, 'touchstart'); | 201 generateTouchEvent(key, 'touchstart'); |
136 generateTouchEvent(key, 'touchend'); | 202 generateTouchEvent(key, 'touchend'); |
137 } | 203 } |
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 |