- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / input_ime / main.js
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 'use strict';
5
6 /**
7  * This class is a base class of each input method implementation.
8  * @constructor
9  */
10 var IMEBase = function() {};
11 IMEBase.prototype = {
12   onActivate: function() {},
13   onDeactivated: function() {},
14   onFocus: function(context) {},
15   onBlur: function(contextID) {},
16   onInputContextUpdate: function(context) {},
17   onKeyEvent: function(context, engine, keyData) { return false; },
18   onCandidateClicked: function(candidateID, button) {},
19   onMenuItemActivated: function(name) {},
20   onSurroundingTextChanged: function(text, focus, anchor) {},
21   onReset: function(engineID) {}
22 };
23
24 /**
25  * This class provides simple identity input methods.
26  * @constructor
27  **/
28 var IdentityIME = function() {};
29 IdentityIME.prototype = new IMEBase();
30
31 /**
32  * This class provides an IME which capitalize given character.
33  * @constructor
34  */
35 var ToUpperIME = function() {};
36 ToUpperIME.prototype = new IMEBase();
37
38 /**
39  * @param {Object} context A context object passed from input.ime.onFocus.
40  * @param {string} engine An engine ID.
41  * @param {Object} keyData A keyevent object passed from input.ime.onKeyEvent.
42  * @return {boolean} True on the key event is consumed.
43  **/
44 ToUpperIME.prototype.onKeyEvent = function(context, engine, keyData) {
45   if (keyData.type == 'keydown' && /^[a-zA-Z]$/.test(keyData.key)) {
46     chrome.input.ime.commitText({
47       contextID: context.contextID,
48       text: keyData.key.toUpperCase()
49     }, function() {});
50     return true;
51   }
52   return false;
53 };
54
55 /**
56  * This class provide an IME which sneds message with API argument.
57  * @constructor
58  */
59 var APIArgumentIME = function() {};
60 APIArgumentIME.prototype = new IMEBase();
61
62 /**
63  * @param {Object} context A context object passed from input.ime.onFocus.
64  * @param {string} engine An engine ID.
65  * @param {Object} keyData A keyevent object passed from input.ime.onKeyEvent.
66  * @return {boolean} True on the key event is consumed.
67  **/
68 APIArgumentIME.prototype.onKeyEvent = function(context, engine, keyData) {
69   chrome.test.sendMessage('onKeyEvent:' +
70                           keyData.type + ':' +
71                           keyData.key + ':' +
72                           keyData.code + ':' +
73                           keyData.ctrlKey + ':' +
74                           keyData.altKey + ':' +
75                           keyData.shiftKey + ':' +
76                           keyData.capsLock);
77   return false;
78 };
79
80 /**
81  * This class listens the event from chrome.input.ime and forwards it to the
82  * activated engine.
83  * @constructor
84  **/
85 var EngineBridge = function() {};
86 EngineBridge.prototype = {
87
88   /**
89    * Map from engineID to actual engine instance.
90    * @type {Object}
91    * @private
92    **/
93   engineInstance_: {},
94
95   /**
96    * A current active engineID.
97    * @type {string}
98    * @private
99    **/
100   activeEngine_: null,
101
102   /**
103    * A input context currently focused.
104    * @type {string}
105    * @private
106    **/
107   focusedContext_: null,
108
109   /**
110    * Called from chrome.input.ime.onActivate.
111    * @private
112    * @this EngineBridge
113    **/
114   onActivate_: function(engineID) {
115     this.activeEngine_ = engineID;
116     this.engineInstance_[engineID].onActivate();
117     chrome.test.sendMessage('onActivate');
118   },
119
120   /**
121    * Called from chrome.input.ime.onDeactivated.
122    * @private
123    * @this EngineBridge
124    **/
125   onDeactivated_: function(engineID) {
126     if (this.engineInstance_[engineID])
127       this.engineInstance_[engineID].onDeactivated();
128     this.activeEngine_ = null;
129     chrome.test.sendMessage('onDeactivated');
130   },
131
132   /**
133    * Called from chrome.input.ime.onFocus.
134    * @private
135    * @this EngineBridge
136    **/
137   onFocus_: function(context) {
138     this.focusedContext_ = context;
139     if (this.activeEngine_)
140       this.engineInstance_[this.activeEngine_].onFocus(context);
141     chrome.test.sendMessage('onFocus');
142   },
143
144   /**
145    * Called from chrome.input.ime.onBlur.
146    * @private
147    * @this EngineBridge
148    **/
149   onBlur_: function(contextID) {
150     if (this.activeEngine_)
151       this.engineInstance_[this.activeEngine_].onBlur(contextID);
152     this.focusedContext_ = null;
153     chrome.test.sendMessage('onBlur');
154   },
155
156   /**
157    * Called from chrome.input.ime.onInputContextUpdate.
158    * @private
159    * @this EngineBridge
160    **/
161   onInputContextUpdate_: function(context) {
162     this.focusedContext_ = context;
163     if (this.activeEngine_)
164       this.engineInstance_[this.activeEngine_].onInputContextUpdate(context);
165     chrome.test.sendMessage('onInputContextUpdate');
166   },
167
168   /**
169    * Called from chrome.input.ime.onKeyEvent.
170    * @private
171    * @this EngineBridge
172    * @return {boolean} True on the key event is consumed.
173    **/
174   onKeyEvent_: function(engineID, keyData) {
175     chrome.test.sendMessage('onKeyEvent');
176     if (this.engineInstance_[engineID])
177       return this.engineInstance_[engineID].onKeyEvent(
178           this.focusedContext_, this.activeEngine_, keyData);
179     return false;
180   },
181
182   /**
183    * Called from chrome.input.ime.onCandidateClicked.
184    * @private
185    * @this EngineBridge
186    **/
187   onCandidateClicked_: function(engineID, candidateID, button) {
188     if (this.engineInstance_[engineID])
189       this.engineInstance_[engineID].onCandidateClicked(candidateID, button);
190     chrome.test.sendMessage('onCandidateClicked');
191   },
192
193   /**
194    * Called from chrome.input.ime.onMenuItemActivated.
195    * @private
196    * @this EngineBridge
197    **/
198   onMenuItemActivated_: function(engineID, name) {
199     this.engineInstance_[engineID].onMenuItemActivated(name);
200     chrome.test.sendMessage('onMenuItemActivated');
201   },
202
203   /**
204    * Called from chrome.input.ime.onSurroundingTextChanged.
205    * @private
206    * @this EngineBridge
207    **/
208   onSurroundingTextChanged_: function(engineID, object) {
209     this.engineInstance_[engineID].onSurroundingTextChanged(
210         object.text, object.focus, object.anchor);
211     chrome.test.sendMessage('onSurroundingTextChanged');
212   },
213
214   /**
215    * Called from chrome.input.ime.onReset.
216    * @private
217    * @this EngineBridge
218    **/
219   onReset_: function(engineID) {
220     this.engineInstance_[engineID].onReset(engineID);
221     chrome.test.sendMessage('onReset');
222   },
223
224   /**
225    * Add engine instance for |engineID|.
226    * @this EngineBridge
227    **/
228   addEngine: function(engineID, engine) {
229     this.engineInstance_[engineID] = engine;
230   },
231
232   /**
233    * Returns active engine ID. Returns null if there is no active engine.
234    * @this EngineBridge
235    * @return {string} An string which identify the engine.
236    **/
237   getActiveEngineID: function() {
238     return this.activeEngine_;
239   },
240
241   /**
242    * Returns currently focused context ID. Returns null if there is no focused
243    * context.
244    * @this EngineBridge
245    * @return {strine} An string which identify the context.
246    **/
247   getFocusedContextID: function () {
248     return this.focusedContext_;
249   },
250
251   /**
252    * Initialize EngineBridge by binding with chrome event.
253    * @this EngineBridge
254    **/
255   Initialize: function() {
256     chrome.input.ime.onActivate.addListener(this.onActivate_.bind(this));
257     chrome.input.ime.onDeactivated.addListener(this.onDeactivated_.bind(this));
258     chrome.input.ime.onFocus.addListener(this.onFocus_.bind(this));
259     chrome.input.ime.onBlur.addListener(this.onBlur_.bind(this));
260     chrome.input.ime.onInputContextUpdate.addListener(
261         this.onInputContextUpdate_.bind(this));
262     chrome.input.ime.onKeyEvent.addListener(this. onKeyEvent_.bind(this));
263     chrome.input.ime.onCandidateClicked.addListener(
264         this.onCandidateClicked_.bind(this));
265     chrome.input.ime.onMenuItemActivated.addListener(
266         this.onMenuItemActivated_.bind(this));
267     chrome.input.ime.onSurroundingTextChanged.addListener(
268         this.onSurroundingTextChanged_.bind(this));
269     chrome.input.ime.onReset.addListener(this.onReset_.bind(this));
270   }
271 };
272
273 var engineBridge = new EngineBridge();
274 engineBridge.Initialize();
275 engineBridge.addEngine('IdentityIME', new IdentityIME());
276 engineBridge.addEngine('ToUpperIME', new ToUpperIME());
277 engineBridge.addEngine('APIArgumentIME', new APIArgumentIME());
278 chrome.test.sendMessage('ReadyToUseImeEvent');