Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / chromeos / virtual_keyboard / inputview / test_base.js
1 /*
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
4  * found in the LICENSE file.
5  */
6
7 var mockController;
8 var mockTimer;
9 var setComposition;
10
11 var DEFAULT_CONTEXT_ID = 1;
12
13 /**
14  * Create mocks for the virtualKeyboardPrivate API. Any tests that trigger API
15  * calls must set expectations for call signatures.
16  */
17 function setUp() {
18   mockController = new MockController();
19   mockTimer = new MockTimer();
20
21   mockTimer.install();
22   mockController.createFunctionMock(chrome.input.ime, 'commitText');
23
24   var validateCommit = function(index, expected, observed) {
25     // Only consider the first argument, the details object.
26     var expectedEvent = expected[0];
27     var observedEvent = observed[0];
28     assertEquals(expectedEvent.text,
29                  observedEvent.text,
30                  'Mismatched commit text.');
31   };
32   chrome.input.ime.commitText.validateCall = validateCommit;
33   setComposition = chrome.input.ime.setComposition;
34   // Mocks setComposition manually to immediately callback. The mock controller
35   // does not support callback functions.
36   chrome.input.ime.setComposition = function(obj, callback) {
37     callback();
38   }
39   window.setContext({'contextID': DEFAULT_CONTEXT_ID, 'type': 'text'});
40   // TODO(rsadam): Mock additional extension API calls as required.
41 }
42
43 /**
44  * Verify that API calls match expectations.
45  */
46 function tearDown() {
47   mockController.verifyMocks();
48   mockController.reset();
49   mockTimer.uninstall();
50   chrome.input.ime.setComposition = setComposition;
51 }
52
53 /**
54  * Retrieves the key from the current keyset.
55  * @param {String} char The character of the key.
56  * @return {Object} The key.
57  */
58 function getKey(char) {
59   return document.querySelector('#Key' + char.toUpperCase())
60 }
61
62 /**
63  * Generates a mouse event and dispatches it on the target.
64  * @param target {Object} The target of the event.
65  * @param type {String} The type of the mouse event.
66  */
67 function generateMouseEvent(target, type) {
68   var e = new MouseEvent(type, {bubbles:true, cancelable:true});
69   target.dispatchEvent(e);
70 }
71
72 /**
73  * Mocks a character type using the mouse.
74  * @param {String} char The character to type.
75  */
76 function mockMouseType(char) {
77   var send = chrome.input.ime.commitText;
78   send.addExpectation({
79     contextId: DEFAULT_CONTEXT_ID,
80     text: char,
81   });
82   var key = getKey(char);
83   if (!key) {
84     console.error("Cannot find key: " + char);
85     return;
86   }
87   generateMouseEvent(key, 'mouseover');
88   generateMouseEvent(key, 'mousedown');
89   generateMouseEvent(key, 'mouseup');
90   generateMouseEvent(key, 'click');
91   generateMouseEvent(key, 'mouseover');
92   generateMouseEvent(key, 'mouseout');
93 }