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.
10 beforeEach: function() {
11 sinon
.stub(chrome
.i18n
, 'getMessage');
13 afterEach: function() {
14 $testStub(chrome
.i18n
.getMessage
).restore();
18 QUnit
.test('getTranslationOrError(tag) should return tag on error',
20 var translation
= l10n
.getTranslationOrError('non_existent_tag');
21 assert
.equal(translation
, 'non_existent_tag');
24 QUnit
.test('localizeElementFromTag() should replace innerText by default',
26 var element
= document
.createElement('div');
27 $testStub(chrome
.i18n
.getMessage
).withArgs('tag')
28 .returns('<b>Hello World</b>');
30 l10n
.localizeElementFromTag(element
, 'tag');
32 assert
.equal(element
.innerHTML
, '<b>Hello World</b>');
35 QUnit
.test('localizeElementFromTag() should replace innerHTML if flag is set',
37 var element
= document
.createElement('div');
38 $testStub(chrome
.i18n
.getMessage
).withArgs('tag')
39 .returns('<b>Hello World</b>');
41 l10n
.localizeElementFromTag(element
, 'tag', null, true);
43 assert
.equal(element
.innerHTML
, '<b>Hello World</b>');
47 'localizeElement() should replace innerText using the "i18n-content" ' +
48 'attribute as the tag',
50 var element
= document
.createElement('div');
51 element
.setAttribute('i18n-content', 'tag');
52 $testStub(chrome
.i18n
.getMessage
).withArgs('tag')
53 .returns('<b>Hello World</b>');
55 l10n
.localizeElement(element
);
57 assert
.equal(element
.innerHTML
, '<b>Hello World</b>');
61 'localize() should replace element title using the "i18n-title" ' +
62 'attribute as the tag',
64 var fixture
= document
.getElementById('qunit-fixture');
65 fixture
.innerHTML
= '<div class="target" i18n-title="tag"></div>';
66 $testStub(chrome
.i18n
.getMessage
)
68 .returns('localized title');
72 var target
= document
.querySelector('.target');
73 assert
.equal(target
.title
, 'localized title');
76 QUnit
.test('localize() should support string substitutions', function(assert
) {
77 var fixture
= document
.getElementById('qunit-fixture');
79 '<div class="target" ' +
80 'i18n-content="tag" ' +
81 'i18n-value-1="param1" ' +
82 'i18n-value-2="param2">' +
85 $testStub(chrome
.i18n
.getMessage
).withArgs('tag', ['param1', 'param2'])
86 .returns('localized');
90 var target
= document
.querySelector('.target');
91 assert
.equal(target
.innerText
, 'localized');
94 QUnit
.test('localize() should support tag substitutions', function(assert
) {
95 var fixture
= document
.getElementById('qunit-fixture');
97 '<div class="target" i18n-content="tag"' +
98 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>';
100 $testStub(chrome
.i18n
.getMessage
).withArgs('tag1').returns('param1');
101 $testStub(chrome
.i18n
.getMessage
).withArgs('tag2').returns('param2');
102 $testStub(chrome
.i18n
.getMessage
)
103 .withArgs('tag', ['param1', 'param2'])
104 .returns('localized');
108 var target
= document
.querySelector('.target');
109 assert
.equal(target
.innerText
, 'localized');