OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 /** | 6 /** |
7 * @fileoverview Testing stub for messages. | 7 * @fileoverview Testing stub for messages. |
8 */ | 8 */ |
9 | 9 |
10 goog.provide('cvox.TestMsgs'); | 10 goog.provide('cvox.TestMsgs'); |
(...skipping 25 matching lines...) Expand all Loading... | |
36 | 36 |
37 /** | 37 /** |
38 * Returns the message with the given message id from the ChromeVox namespace. | 38 * Returns the message with the given message id from the ChromeVox namespace. |
39 * | 39 * |
40 * @param {string} message_id The id. | 40 * @param {string} message_id The id. |
41 * @param {Array.<string>} opt_subs Substitution strings. | 41 * @param {Array.<string>} opt_subs Substitution strings. |
42 * @return {string} The message. | 42 * @return {string} The message. |
43 */ | 43 */ |
44 cvox.TestMsgs.prototype.getMsg = function(message_id, opt_subs) { | 44 cvox.TestMsgs.prototype.getMsg = function(message_id, opt_subs) { |
45 if (!message_id) { | 45 if (!message_id) { |
46 var e = new Error(); | 46 throw Error('Message id required'); |
47 e.message = 'Message id required'; | |
48 throw e; | |
49 } | 47 } |
50 var message = cvox.TestMessages['chromevox_' + message_id]; | 48 var message = cvox.TestMessages[('chromevox_' + message_id).toUpperCase()]; |
51 if (message == undefined) { | 49 if (message == undefined) { |
52 var e = new Error(); | 50 throw Error('missing-msg: ' + message_id); |
53 e.message = 'missing-msg: ' + message_id; | |
54 throw e; | |
55 } | 51 } |
56 | 52 |
57 var messageString = message.message; | 53 var messageString = message.message; |
58 if (opt_subs) { | 54 if (opt_subs) { |
59 // Unshift a null to make opt_subs and message.placeholders line up. | 55 // Unshift a null to make opt_subs and message.placeholders line up. |
60 for (var i = 0; i < opt_subs.length; i++) { | 56 for (var i = 0; i < opt_subs.length; i++) { |
61 var placeholderObject = message.placeholders ? | 57 messageString = messageString.replace('$' + (i + 1), opt_subs[i]); |
62 message.placeholders[i + 1] : undefined; | |
63 if (!placeholderObject) { | |
64 // Allow tests to substitute the string 'dummy' if they don't know | |
dmazzoni
2014/06/24 05:54:10
FWIW, I think we will need to add this feature bac
| |
65 // how many substitutions a message has. | |
66 if (opt_subs[i] == 'dummy') { | |
67 continue; | |
68 } | |
69 var e = new Error(); | |
70 e.message = 'Bad placeholder ' + i + ' for message id ' + message_id; | |
71 throw e; | |
72 } | |
73 var placeholder = message.placeholders[i + 1].content; | |
74 messageString = messageString.replace(placeholder, opt_subs[i]); | |
75 } | 58 } |
76 } | 59 } |
77 return messageString; | 60 return messageString; |
78 }; | 61 }; |
79 | 62 |
80 | 63 |
81 /** | 64 /** |
82 * Retuns a number formatted correctly. | 65 * Retuns a number formatted correctly. |
83 * | 66 * |
84 * @param {number} num The number. | 67 * @param {number} num The number. |
85 * @return {string} The number in the correct locale. | 68 * @return {string} The number in the correct locale. |
86 */ | 69 */ |
87 cvox.TestMsgs.prototype.getNumber = function(num) { | 70 cvox.TestMsgs.prototype.getNumber = function(num) { |
88 return '' + num; | 71 return '' + num; |
89 }; | 72 }; |
90 | 73 |
91 /** | 74 /** |
92 * Cosntructor for the host factory. | 75 * Cosntructor for the host factory. |
93 */ | 76 */ |
94 cvox.HostFactory.msgsConstructor = cvox.TestMsgs; | 77 cvox.HostFactory.msgsConstructor = cvox.TestMsgs; |
OLD | NEW |