Upstream version 5.34.104.0
[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.extensionId || '') + ':' +
71                           keyData.type + ':' +
72                           keyData.key + ':' +
73                           keyData.code + ':' +
74                           keyData.ctrlKey + ':' +
75                           keyData.altKey + ':' +
76                           keyData.shiftKey + ':' +
77                           keyData.capsLock);
78   return false;
79 };
80
81 /**
82  * This class listens the event from chrome.input.ime and forwards it to the
83  * activated engine.
84  * @constructor
85  **/
86 var EngineBridge = function() {};
87 EngineBridge.prototype = {
88
89   /**
90    * Map from engineID to actual engine instance.
91    * @type {Object}
92    * @private
93    **/
94   engineInstance_: {},
95
96   /**
97    * A current active engineID.
98    * @type {string}
99    * @private
100    **/
101   activeEngine_: null,
102
103   /**
104    * A input context currently focused.
105    * @type {string}
106    * @private
107    **/
108   focusedContext_: null,
109
110   /**
111    * Called from chrome.input.ime.onActivate.
112    * @private
113    * @this EngineBridge
114    **/
115   onActivate_: function(engineID) {
116     this.activeEngine_ = engineID;
117     this.engineInstance_[engineID].onActivate();
118     chrome.test.sendMessage('onActivate');
119   },
120
121   /**
122    * Called from chrome.input.ime.onDeactivated.
123    * @private
124    * @this EngineBridge
125    **/
126   onDeactivated_: function(engineID) {
127     if (this.engineInstance_[engineID])
128       this.engineInstance_[engineID].onDeactivated();
129     this.activeEngine_ = null;
130     chrome.test.sendMessage('onDeactivated');
131   },
132
133   /**
134    * Called from chrome.input.ime.onFocus.
135    * @private
136    * @this EngineBridge
137    **/
138   onFocus_: function(context) {
139     this.focusedContext_ = context;
140     if (this.activeEngine_)
141       this.engineInstance_[this.activeEngine_].onFocus(context);
142     chrome.test.sendMessage('onFocus:' + context.type);
143   },
144
145   /**
146    * Called from chrome.input.ime.onBlur.
147    * @private
148    * @this EngineBridge
149    **/
150   onBlur_: function(contextID) {
151     if (this.activeEngine_)
152       this.engineInstance_[this.activeEngine_].onBlur(contextID);
153     this.focusedContext_ = null;
154     chrome.test.sendMessage('onBlur');
155   },
156
157   /**
158    * Called from chrome.input.ime.onInputContextUpdate.
159    * @private
160    * @this EngineBridge
161    **/
162   onInputContextUpdate_: function(context) {
163     this.focusedContext_ = context;
164     if (this.activeEngine_)
165       this.engineInstance_[this.activeEngine_].onInputContextUpdate(context);
166     chrome.test.sendMessage('onInputContextUpdate');
167   },
168
169   /**
170    * Called from chrome.input.ime.onKeyEvent.
171    * @private
172    * @this EngineBridge
173    * @return {boolean} True on the key event is consumed.
174    **/
175   onKeyEvent_: function(engineID, keyData) {
176     chrome.test.sendMessage('onKeyEvent');
177     if (this.engineInstance_[engineID])
178       return this.engineInstance_[engineID].onKeyEvent(
179           this.focusedContext_, this.activeEngine_, keyData);
180     return false;
181   },
182
183   /**
184    * Called from chrome.input.ime.onCandidateClicked.
185    * @private
186    * @this EngineBridge
187    **/
188   onCandidateClicked_: function(engineID, candidateID, button) {
189     if (this.engineInstance_[engineID])
190       this.engineInstance_[engineID].onCandidateClicked(candidateID, button);
191     chrome.test.sendMessage('onCandidateClicked');
192   },
193
194   /**
195    * Called from chrome.input.ime.onMenuItemActivated.
196    * @private
197    * @this EngineBridge
198    **/
199   onMenuItemActivated_: function(engineID, name) {
200     this.engineInstance_[engineID].onMenuItemActivated(name);
201     chrome.test.sendMessage('onMenuItemActivated');
202   },
203
204   /**
205    * Called from chrome.input.ime.onSurroundingTextChanged.
206    * @private
207    * @this EngineBridge
208    **/
209   onSurroundingTextChanged_: function(engineID, object) {
210     this.engineInstance_[engineID].onSurroundingTextChanged(
211         object.text, object.focus, object.anchor);
212     chrome.test.sendMessage('onSurroundingTextChanged');
213   },
214
215   /**
216    * Called from chrome.input.ime.onReset.
217    * @private
218    * @this EngineBridge
219    **/
220   onReset_: function(engineID) {
221     this.engineInstance_[engineID].onReset(engineID);
222     chrome.test.sendMessage('onReset');
223   },
224
225   /**
226    * Add engine instance for |engineID|.
227    * @this EngineBridge
228    **/
229   addEngine: function(engineID, engine) {
230     this.engineInstance_[engineID] = engine;
231   },
232
233   /**
234    * Returns active engine ID. Returns null if there is no active engine.
235    * @this EngineBridge
236    * @return {string} An string which identify the engine.
237    **/
238   getActiveEngineID: function() {
239     return this.activeEngine_;
240   },
241
242   /**
243    * Returns currently focused context ID. Returns null if there is no focused
244    * context.
245    * @this EngineBridge
246    * @return {strine} An string which identify the context.
247    **/
248   getFocusedContextID: function () {
249     return this.focusedContext_;
250   },
251
252   /**
253    * Initialize EngineBridge by binding with chrome event.
254    * @this EngineBridge
255    **/
256   Initialize: function() {
257     chrome.input.ime.onActivate.addListener(this.onActivate_.bind(this));
258     chrome.input.ime.onDeactivated.addListener(this.onDeactivated_.bind(this));
259     chrome.input.ime.onFocus.addListener(this.onFocus_.bind(this));
260     chrome.input.ime.onBlur.addListener(this.onBlur_.bind(this));
261     chrome.input.ime.onInputContextUpdate.addListener(
262         this.onInputContextUpdate_.bind(this));
263     chrome.input.ime.onKeyEvent.addListener(this. onKeyEvent_.bind(this));
264     chrome.input.ime.onCandidateClicked.addListener(
265         this.onCandidateClicked_.bind(this));
266     chrome.input.ime.onMenuItemActivated.addListener(
267         this.onMenuItemActivated_.bind(this));
268     chrome.input.ime.onSurroundingTextChanged.addListener(
269         this.onSurroundingTextChanged_.bind(this));
270     chrome.input.ime.onReset.addListener(this.onReset_.bind(this));
271   }
272 };
273
274 var engineBridge = new EngineBridge();
275 engineBridge.Initialize();
276 engineBridge.addEngine('IdentityIME', new IdentityIME());
277 engineBridge.addEngine('ToUpperIME', new ToUpperIME());
278 engineBridge.addEngine('APIArgumentIME', new APIArgumentIME());
279 chrome.test.sendMessage('ReadyToUseImeEvent');