Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / remoting / webapp / unittests / l10n_unittest.js
blob6ac3680f7599228447419dae8ee02f16ef2fd341
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.
5 (function() {
7 'use strict';
9 module('l10n', {
10 setup: function() {
11 sinon.stub(chrome.i18n, 'getMessage');
13 teardown: function() {
14 chrome.i18n.getMessage.restore();
16 });
18 test('getTranslationOrError(tag) should return tag on error', function() {
19 var translation = l10n.getTranslationOrError('non_existent_tag');
20 equal(translation, 'non_existent_tag');
21 });
23 test('localizeElementFromTag() should replace innerText by default',
24 function() {
25 var element = document.createElement('div');
26 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
28 l10n.localizeElementFromTag(element, 'tag');
30 equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;');
31 });
33 test('localizeElementFromTag() should replace innerHTML if flag is set',
34 function() {
35 var element = document.createElement('div');
36 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
38 l10n.localizeElementFromTag(element, 'tag', null, true);
40 equal(element.innerHTML, '<b>Hello World</b>');
41 });
43 test(
44 'localizeElement() should replace innerText using the "i18n-content" ' +
45 'attribute as the tag',
46 function() {
47 var element = document.createElement('div');
48 element.setAttribute('i18n-content', 'tag');
49 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
51 l10n.localizeElement(element);
53 equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;');
54 });
56 test(
57 'localize() should replace element title using the "i18n-title" ' +
58 'attribute as the tag',
59 function() {
60 var fixture = document.getElementById('qunit-fixture');
61 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>';
62 chrome.i18n.getMessage.withArgs('tag').returns('localized title');
64 l10n.localize();
66 var target = document.querySelector('.target');
67 equal(target.title, 'localized title');
68 });
70 test('localize() should support string substitutions', function() {
71 var fixture = document.getElementById('qunit-fixture');
72 fixture.innerHTML =
73 '<div class="target" ' +
74 'i18n-content="tag" ' +
75 'i18n-value-1="param1" ' +
76 'i18n-value-2="param2">' +
77 '</div>';
79 chrome.i18n.getMessage.withArgs('tag', ['param1', 'param2'])
80 .returns('localized');
82 l10n.localize();
84 var target = document.querySelector('.target');
85 equal(target.innerText, 'localized');
86 });
88 test('localize() should support tag substitutions', function() {
89 var fixture = document.getElementById('qunit-fixture');
90 fixture.innerHTML =
91 '<div class="target" i18n-content="tag"' +
92 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>';
94 var getMessage = chrome.i18n.getMessage;
95 getMessage.withArgs('tag1').returns('param1');
96 getMessage.withArgs('tag2').returns('param2');
97 getMessage.withArgs('tag', ['param1', 'param2']).returns('localized');
99 l10n.localize();
101 var target = document.querySelector('.target');
102 equal(target.innerText, 'localized');
105 })();