Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / chromeos / virtual_keyboard / hide_keyboard_key_test.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 /**
8  * Class for testing hide keyboard key. Other than hide keyboard as the name
9  * suggested, hide keyboard key can also switch keyboard layout and lock/unlock
10  * keyboard.
11  * @param {string=} opt_layout Optional name of the initial layout.
12  * @constructor
13  */
14 function HideKeyboardKeyTester(layout) {
15   this.layout = layout;
16   this.subtasks = [];
17 }
18
19 HideKeyboardKeyTester.prototype = {
20   __proto__: SubtaskScheduler.prototype,
21
22   /**
23    * The keyboard overlay element.
24    * @type {kb-keyboard-overlay}
25    */
26   get overlay() {
27     var overlay =
28         $('keyboard').shadowRoot.querySelector('kb-keyboard-overlay');
29     assertTrue(!!overlay, 'Unable to find overlay container');
30     return overlay;
31   },
32
33   /**
34    * Mocks tap/click on hide keyboard key. It should pop up the overlay menu.
35    * @param {string} keysetId Initial keyset.
36    */
37   tapHideKeyboard: function(keysetId) {
38     var self = this;
39     var mockEvent = {pointerId: 1};
40     mockEvent.stopPropagation = function() {};
41     var fn = function() {
42       Debug('Mock tap on hide keyboard key.');
43
44       var hideKey =
45           $('keyboard').activeKeyset.querySelector('kb-hide-keyboard-key');
46       assertTrue(!!hideKey, 'Unable to find hide keyboard key.');
47
48       hideKey.down(mockEvent);
49       hideKey.up(mockEvent);
50     };
51     this.addWaitCondition(fn, keysetId);
52     this.addSubtask(fn);
53     this.verifyOverlayVisibilityChange(true);
54   },
55
56   /**
57    * Mocks tap/click on the overlay menu. It should hide the overlay menu.
58    * @param {string} keysetId Initial keyset.
59    */
60   tapOverlay: function(keysetId) {
61     var mockEvent = {pointerId: 1};
62     mockEvent.stopPropagation = function() {};
63     var self = this;
64     var fn = function() {
65       Debug('Mock tap on overlay.');
66       var overlay = self.overlay;
67       overlay.up(mockEvent);
68     };
69     this.addWaitCondition(fn, keysetId);
70     this.addSubtask(fn);
71     this.verifyOverlayVisibilityChange(false);
72   },
73
74   /**
75    * Mocks selection of lock/unlock button from the options menu.
76    * @param {string} keysetId Initial keyset.
77    * @param {boolean} expect Whether or not the keyboard should be locked.
78    */
79   selectLockUnlockButton: function(keysetId, expect) {
80     var mockEvent = {pointerId: 1};
81     mockEvent.stopPropagation = function() {};
82     var self = this;
83     var fn = function() {
84       Debug('mock keyup on lock/unlock button.');
85
86       var optionsMenu = self.overlay.shadowRoot.querySelector(
87           'kb-options-menu');
88       assertTrue(!!optionsMenu, 'Unable to find options menu.');
89       var lockUnlockButton = optionsMenu.shadowRoot.querySelector(
90           'kb-options-menu-toggle-lock-item');
91       assertTrue(!!lockUnlockButton, 'Unable to find lock/unlock button.');
92       // Should toggle lock.
93       chrome.virtualKeyboardPrivate.lockKeyboard.addExpectation(expect);
94       lockUnlockButton.up(mockEvent);
95       self.overlay.up(mockEvent);
96     };
97     this.addWaitCondition(fn, keysetId);
98     this.addSubtask(fn);
99     this.verifyOverlayVisibilityChange(false);
100   },
101
102   /**
103    * Mocks selection of hide from the options menu.
104    * @param {string} keysetId Initial keyset.
105    */
106   selectHide: function(keysetId) {
107     var mockEvent = {pointerId: 1};
108     mockEvent.stopPropagation = function() {};
109     var self = this;
110     var fn = function() {
111       Debug('mock keyup on hide button.');
112       var optionsMenu = self.overlay.shadowRoot.querySelector(
113           'kb-options-menu');
114       assertTrue(!!optionsMenu, 'Unable to find options menu.');
115       var button = optionsMenu.shadowRoot.querySelector(
116           'kb-options-menu-item[layout=none]');
117       assertTrue(!!button, 'Unable to find hide button.');
118       // Expect hideKeyboard to be called.
119       chrome.virtualKeyboardPrivate.hideKeyboard.addExpectation();
120       // hideKeyboard in api_adapter also unlocks the keyboard.
121       chrome.virtualKeyboardPrivate.lockKeyboard.addExpectation(false);
122       button.up(mockEvent);
123       self.overlay.up(mockEvent);
124     };
125     this.addWaitCondition(fn, keysetId);
126     this.addSubtask(fn);
127     this.verifyOverlayVisibilityChange(false);
128   },
129
130   /**
131    * Waits for the overlay to change visibility.
132    * @param {string} keysetId Initial keyset.
133    */
134   verifyOverlayVisibilityChange: function(visibility) {
135     var fn = function() {
136       if (visibility)
137         Debug('Validated that overlay is now visible.');
138       else
139         Debug('Validated that overlay is now hidden.');
140     };
141     fn.waitCondition = {
142       state: 'overlayVisibility',
143       value: visibility
144     };
145     this.addSubtask(fn);
146   }
147 };
148
149 /**
150  * Tests that the overlay menu appears when the hide keyboard button is clicked
151  * and disappears when the overlay is then clicked.
152  * @param {Function} testDoneCallback The callback function on completion.
153  */
154 function testOverlayMenu(testDoneCallback) {
155   var tester = new HideKeyboardKeyTester(Layout.DEFAULT);
156   tester.tapHideKeyboard(Keyset.LOWER);
157   tester.tapOverlay(Keyset.LOWER);
158   tester.scheduleTest('testOverlayMenu', testDoneCallback);
159 }
160
161 /**
162  * Tests that clicking hide in the overlay menu hides the keyboard.
163  * @param {Function} testDoneCallback The callback function on completion.
164  */
165 function testHideKeyboard(testDoneCallback) {
166   var tester = new HideKeyboardKeyTester(Layout.DEFAULT);
167   tester.tapHideKeyboard(Keyset.LOWER);
168   tester.selectHide(Keyset.LOWER);
169   tester.scheduleTest('testHideKeyboard', testDoneCallback);
170 }
171
172 /**
173  * Tests that the lock keyboard in the overlay menu toggles.
174  * @param {Function} testDoneCallback The callback function on completion.
175  */
176 function testLockKeyboard(testDoneCallback) {
177   var tester = new HideKeyboardKeyTester(Layout.DEFAULT);
178   tester.tapHideKeyboard(Keyset.LOWER);
179   tester.selectLockUnlockButton(Keyset.LOWER, true);
180
181   tester.tapHideKeyboard(Keyset.LOWER);
182   tester.selectLockUnlockButton(Keyset.LOWER, false);
183
184   tester.scheduleTest('testLockKeyboard', testDoneCallback);
185 }