Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox / testing / chromevox_unittest_base.js
1 // Copyright 2014 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
5 GEN_INCLUDE([
6     'chrome/browser/resources/chromeos/chromevox/testing/assert_additions.js']);
7 GEN_INCLUDE([
8   'chrome/browser/resources/chromeos/chromevox/testing/common.js']);
9
10 /**
11  * Base test fixture for ChromeVox unit tests.
12  *
13  * Note that while conceptually these are unit tests, these tests need
14  * to run in a full web page, so they're actually run as WebUI browser
15  * tests.
16  *
17  * @constructor
18  * @extends {testing.Test}
19  */
20 function ChromeVoxUnitTestBase() {}
21
22 ChromeVoxUnitTestBase.prototype = {
23   __proto__: testing.Test.prototype,
24
25   /** @override */
26   closureModuleDeps: [
27     'cvox.ChromeVoxTester',
28     'cvox.ChromeVoxUserCommands',
29     'cvox.SpokenListBuilder',
30   ],
31
32   /** @override */
33   browsePreload: DUMMY_URL,
34
35   /**
36    * @override
37    * It doesn't make sense to run the accessibility audit on these tests,
38    * since many of them are deliberately testing inaccessible html.
39    */
40   runAccessibilityChecks: false,
41
42   /**
43    * Loads some inlined html into the body of the current document, replacing
44    * whatever was there previously.
45    * @param {string} html The html to load as a string.
46    */
47   loadHtml: function(html) {
48     while (document.head.firstChild) {
49       document.head.removeChild(document.head.firstChild);
50     }
51     while (document.body.firstChild) {
52       document.body.removeChild(document.body.firstChild);
53     }
54     this.appendHtml(html);
55   },
56
57   /**
58    * Loads some inlined html into the current document, replacing
59    * whatever was there previously. This version takes the html
60    * encoded as a comment inside a function, so you can use it like this:
61    *
62    * this.loadDoc(function() {/*!
63    *     <p>Html goes here</p>
64    * * /});
65    *
66    * @param {Function} commentEncodedHtml The html to load, embedded as a
67    *     comment inside an anonymous function - see example, above.
68    */
69   loadDoc: function(commentEncodedHtml) {
70     var html =
71         TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
72     this.loadHtml(html);
73   },
74
75   /**
76    * Appends some inlined html into the current document, at the end of
77    * the body element. Takes the html encoded as a comment inside a function,
78    * so you can use it like this:
79    *
80    * this.appendDoc(function() {/*!
81    *     <p>Html goes here</p>
82    * * /});
83    *
84    * @param {Function} commentEncodedHtml The html to load, embedded as a
85    *     comment inside an anonymous function - see example, above.
86    */
87   appendDoc: function(commentEncodedHtml) {
88     var html =
89         TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
90     this.appendHtml(html);
91   },
92
93   /**
94    * Appends some inlined html into the current document, at the end of
95    * the body element.
96    * @param {string} html The html to load as a string.
97    */
98   appendHtml: function(html) {
99     var div = document.createElement('div');
100     div.innerHTML = html;
101     var fragment = document.createDocumentFragment();
102     while (div.firstChild) {
103       fragment.appendChild(div.firstChild);
104     }
105     document.body.appendChild(fragment);
106   },
107
108   /**
109    * Waits for the queued events in ChromeVoxEventWatcher to be
110    * handled, then calls a callback function with provided arguments
111    * in the test case scope. Very useful for asserting the results of events.
112    *
113    * @param {function()} func A function to call when ChromeVox is ready.
114    * @param {*} var_args Additional arguments to pass through to the function.
115    * @return {ChromeVoxUnitTestBase} this.
116    */
117   waitForCalm: function(func, var_args) {
118     var me = this;
119     var calmArguments = Array.prototype.slice.call(arguments);
120     calmArguments.shift();
121     cvox.ChromeVoxEventWatcher.addReadyCallback(function() {
122       func.apply(me, calmArguments);
123     });
124     return this; // for chaining.
125   },
126
127   /**
128    * Asserts the TTS engine spoke a certain string. Clears the TTS buffer.
129    * @param {string} expectedText The expected text.
130    * @return {ChromeVoxUnitTestBase} this.
131    */
132   assertSpoken: function(expectedText) {
133     assertEquals(expectedText,
134                  cvox.ChromeVoxTester.testTts().getUtterancesAsString());
135     cvox.ChromeVoxTester.clearUtterances();
136     return this; // for chaining.
137   },
138
139   /**
140    * Asserts a list of utterances are in the correct queue mode.
141    * @param {cvox.SpokenListBuilder|Array} expectedList A list
142    *     of [text, queueMode] tuples OR a SpokenListBuilder with the expected
143    *     utterances.
144    * @return {ChromeVoxUnitTestBase} this.
145    */
146   assertSpokenList: function(expectedList) {
147     if (expectedList instanceof cvox.SpokenListBuilder) {
148       expectedList = expectedList.build();
149     }
150
151     var ulist = cvox.ChromeVoxTester.testTts().getUtteranceInfoList();
152     for (var i = 0; i < expectedList.length; i++) {
153       var text = expectedList[i][0];
154       var queueMode = expectedList[i][1];
155       this.assertSingleUtterance_(text, queueMode,
156                                   ulist[i].text, ulist[i].queueMode);
157     }
158     cvox.ChromeVoxTester.clearUtterances();
159     return this; // for chaining.
160   },
161
162   assertSingleUtterance_: function(
163       expectedText, expectedQueueMode, text, queueMode) {
164     assertEquals(expectedQueueMode, queueMode);
165     assertEquals(expectedText, text);
166   },
167
168   /**
169    * Focuses an element.
170    * @param {string} id The id of the element to focus.
171    * @return {ChromeVoxUnitTestBase} this.
172    */
173   setFocus: function(id) {
174     $(id).focus();
175     return this; // for chaining.
176   },
177
178   /**
179    * Executes a ChromeVox user command.
180    * @param {string} command The name of the command to run.
181    * @return {ChromeVoxUnitTestBase} this.
182    */
183   userCommand: function(command) {
184     cvox.ChromeVoxUserCommands.commands[command]();
185     return this; // for chaining.
186   },
187
188   /**
189    * @return {cvox.SpokenListBuilder} A new builder.
190    */
191   spokenList: function() {
192     return new cvox.SpokenListBuilder();
193   }
194 };