• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6/**
7 * @fileoverview Testing stub for messages.
8 */
9
10goog.provide('cvox.TestMsgs');
11
12goog.require('cvox.AbstractMsgs');
13goog.require('cvox.HostFactory');
14goog.require('cvox.TestMessages');
15
16
17
18/**
19 * @constructor
20 * @extends {cvox.AbstractMsgs}
21 */
22cvox.TestMsgs = function() {
23  cvox.AbstractMsgs.call(this);
24};
25goog.inherits(cvox.TestMsgs, cvox.AbstractMsgs);
26
27
28/**
29 * Return the current locale.
30 * @return {string} The locale.
31 */
32cvox.TestMsgs.prototype.getLocale = function() {
33  return 'testing';
34};
35
36
37/**
38 * Returns the message with the given message id from the ChromeVox namespace.
39 *
40 * @param {string} message_id The id.
41 * @param {Array.<string>} opt_subs Substitution strings.
42 * @return {string} The message.
43 */
44cvox.TestMsgs.prototype.getMsg = function(message_id, opt_subs) {
45  if (!message_id) {
46    var e = new Error();
47    e.message = 'Message id required';
48    throw e;
49  }
50  var message = cvox.TestMessages['chromevox_' + message_id];
51  if (message == undefined) {
52    var e = new Error();
53    e.message = 'missing-msg: ' + message_id;
54    throw e;
55  }
56
57  var messageString = message.message;
58  if (opt_subs) {
59    // Unshift a null to make opt_subs and message.placeholders line up.
60    for (var i = 0; i < opt_subs.length; i++) {
61      var placeholderObject = message.placeholders ?
62          message.placeholders[i + 1] : undefined;
63      if (!placeholderObject) {
64        // Allow tests to substitute the string 'dummy' if they don't know
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    }
76  }
77  return messageString;
78};
79
80
81/**
82 * Retuns a number formatted correctly.
83 *
84 * @param {number} num The number.
85 * @return {string} The number in the correct locale.
86 */
87cvox.TestMsgs.prototype.getNumber = function(num) {
88  return '' + num;
89};
90
91/**
92 * Cosntructor for the host factory.
93 */
94cvox.HostFactory.msgsConstructor = cvox.TestMsgs;
95