| 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 }; |
| 12 | 23 |
| 13 /** | 24 /** |
| 14 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API | 25 * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API |
| 15 * calls must set expectations for call signatures. | 26 * calls must set expectations for call signatures. |
| 16 */ | 27 */ |
| 17 function setUp() { | 28 function setUp() { |
| 18 mockController = new MockController(); | 29 mockController = new MockController(); |
| 19 mockTimer = new MockTimer(); | 30 mockTimer = new MockTimer(); |
| 31 mockTimer.install(); |
| 20 | 32 |
| 21 mockTimer.install(); | |
| 22 mockController.createFunctionMock(chrome.input.ime, 'commitText'); | 33 mockController.createFunctionMock(chrome.input.ime, 'commitText'); |
| 23 | |
| 24 var validateCommit = function(index, expected, observed) { | 34 var validateCommit = function(index, expected, observed) { |
| 25 // Only consider the first argument, the details object. | 35 // Only consider the first argument, the details object. |
| 26 var expectedEvent = expected[0]; | 36 var expectedEvent = expected[0]; |
| 27 var observedEvent = observed[0]; | 37 var observedEvent = observed[0]; |
| 28 assertEquals(expectedEvent.text, | 38 assertEquals(expectedEvent.text, |
| 29 observedEvent.text, | 39 observedEvent.text, |
| 30 'Mismatched commit text.'); | 40 'Mismatched commit text.'); |
| 31 }; | 41 }; |
| 32 chrome.input.ime.commitText.validateCall = validateCommit; | 42 chrome.input.ime.commitText.validateCall = validateCommit; |
| 43 |
| 33 setComposition = chrome.input.ime.setComposition; | 44 setComposition = chrome.input.ime.setComposition; |
| 34 // Mocks setComposition manually to immediately callback. The mock controller | 45 // Mocks setComposition manually to immediately callback. The mock controller |
| 35 // does not support callback functions. | 46 // does not support callback functions. |
| 36 chrome.input.ime.setComposition = function(obj, callback) { | 47 chrome.input.ime.setComposition = function(obj, callback) { |
| 37 callback(); | 48 callback(); |
| 38 } | 49 } |
| 39 window.setContext({'contextID': DEFAULT_CONTEXT_ID, 'type': 'text'}); | 50 window.setContext({'contextID': DEFAULT_CONTEXT_ID, 'type': 'text'}); |
| 40 // TODO(rsadam): Mock additional extension API calls as required. | 51 // TODO(rsadam): Mock additional extension API calls as required. |
| 41 } | 52 } |
| 42 | 53 |
| 43 /** | 54 /** |
| 44 * Verify that API calls match expectations. | 55 * Verify that API calls match expectations. |
| 45 */ | 56 */ |
| 46 function tearDown() { | 57 function tearDown() { |
| 47 mockController.verifyMocks(); | 58 mockController.verifyMocks(); |
| 48 mockController.reset(); | 59 mockController.reset(); |
| 49 mockTimer.uninstall(); | 60 mockTimer.uninstall(); |
| 50 chrome.input.ime.setComposition = setComposition; | 61 chrome.input.ime.setComposition = setComposition; |
| 51 } | 62 } |
| 52 | 63 |
| 53 /** | 64 /** |
| 54 * Retrieves the key from the current keyset. | 65 * Retrieves the key from the current keyset. |
| 55 * @param {String} char The character of the key. | 66 * @param {String} char The character of the key. |
| 56 * @return {Object} The key. | 67 * @return {Object} The key. |
| 57 */ | 68 */ |
| 58 function getKey(char) { | 69 function getKey(char) { |
| 59 return document.querySelector('#Key' + char.toUpperCase()) | 70 var key = document.querySelector('#Key' + char.toUpperCase()); |
| 71 assertTrue(!!key, "Cannot find key: " + char); |
| 72 return key; |
| 60 } | 73 } |
| 61 | 74 |
| 62 /** | 75 /** |
| 63 * Generates a mouse event and dispatches it on the target. | 76 * Generates a mouse event and dispatches it on the target. |
| 64 * @param target {Object} The target of the event. | 77 * @param target {Object} The target of the event. |
| 65 * @param type {String} The type of the mouse event. | 78 * @param type {String} The type of the mouse event. |
| 66 */ | 79 */ |
| 67 function generateMouseEvent(target, type) { | 80 function generateMouseEvent(target, type) { |
| 68 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); | 81 var e = new MouseEvent(type, {bubbles:true, cancelable:true}); |
| 69 target.dispatchEvent(e); | 82 target.dispatchEvent(e); |
| 70 } | 83 } |
| 71 | 84 |
| 72 /** | 85 /** |
| 73 * Mocks a character type using the mouse. | 86 * Mocks a key type using the mouse. |
| 87 * @param {Object} key The key to click on. |
| 88 */ |
| 89 function mockMouseTypeOnKey(key) { |
| 90 generateMouseEvent(key, 'mouseover'); |
| 91 generateMouseEvent(key, 'mousedown'); |
| 92 generateMouseEvent(key, 'mouseup'); |
| 93 generateMouseEvent(key, 'click'); |
| 94 generateMouseEvent(key, 'mouseover'); |
| 95 generateMouseEvent(key, 'mouseout'); |
| 96 } |
| 97 |
| 98 /** |
| 99 * Mocks a character type using the mouse. Expects the character will be |
| 100 * committed. |
| 74 * @param {String} char The character to type. | 101 * @param {String} char The character to type. |
| 75 */ | 102 */ |
| 76 function mockMouseType(char) { | 103 function mockMouseType(char) { |
| 77 var send = chrome.input.ime.commitText; | 104 var send = chrome.input.ime.commitText; |
| 78 send.addExpectation({ | 105 send.addExpectation({ |
| 79 contextId: DEFAULT_CONTEXT_ID, | 106 contextId: DEFAULT_CONTEXT_ID, |
| 80 text: char, | 107 text: char, |
| 81 }); | 108 }); |
| 82 var key = getKey(char); | 109 var key = getKey(char); |
| 83 if (!key) { | 110 mockMouseTypeOnKey(key); |
| 84 console.error("Cannot find key: " + char); | 111 } |
| 85 return; | 112 |
| 113 /** |
| 114 * Generates a touch event and dispatches it on the target. |
| 115 * @param target {Object} The target of the event. |
| 116 * @param type {String} The type of the touch event. |
| 117 */ |
| 118 function generateTouchEvent(target, type) { |
| 119 var e = document.createEvent('UIEvents'); |
| 120 e.initEvent(type, true, true); |
| 121 target.dispatchEvent(e); |
| 122 } |
| 123 |
| 124 /** |
| 125 * Mocks a character type using touch. |
| 126 * @param {String} char The character to type. |
| 127 */ |
| 128 function mockTouchType(char) { |
| 129 var send = chrome.input.ime.commitText; |
| 130 send.addExpectation({ |
| 131 contextId: DEFAULT_CONTEXT_ID, |
| 132 text: char, |
| 133 }); |
| 134 var key = getKey(char); |
| 135 generateTouchEvent(key, 'touchstart'); |
| 136 generateTouchEvent(key, 'touchend'); |
| 137 } |
| 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; |
| 86 } | 155 } |
| 87 generateMouseEvent(key, 'mouseover'); | 156 assertTrue(!!id, "Invalid shift alignment option: " + align); |
| 88 generateMouseEvent(key, 'mousedown'); | 157 var shift = document.querySelector('#' + id); |
| 89 generateMouseEvent(key, 'mouseup'); | 158 assertTrue(!!shift, "Cannot find shift key with alignment: " + align); |
| 90 generateMouseEvent(key, 'click'); | 159 return shift; |
| 91 generateMouseEvent(key, 'mouseover'); | |
| 92 generateMouseEvent(key, 'mouseout'); | |
| 93 } | 160 } |
| OLD | NEW |