| OLD | NEW |
| (Empty) |
| 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 (function() { | |
| 6 | |
| 7 'use strict'; | |
| 8 | |
| 9 QUnit.module('l10n', { | |
| 10 beforeEach: function() { | |
| 11 sinon.stub(chrome.i18n, 'getMessage'); | |
| 12 }, | |
| 13 afterEach: function() { | |
| 14 $testStub(chrome.i18n.getMessage).restore(); | |
| 15 } | |
| 16 }); | |
| 17 | |
| 18 QUnit.test('getTranslationOrError(tag) should return tag on error', | |
| 19 function(assert) { | |
| 20 var translation = l10n.getTranslationOrError('non_existent_tag'); | |
| 21 assert.equal(translation, 'non_existent_tag'); | |
| 22 }); | |
| 23 | |
| 24 QUnit.test('localizeElementFromTag() should replace innerText by default', | |
| 25 function(assert) { | |
| 26 var element = document.createElement('div'); | |
| 27 $testStub(chrome.i18n.getMessage).withArgs('tag') | |
| 28 .returns('<b>Hello World</b>'); | |
| 29 | |
| 30 l10n.localizeElementFromTag(element, 'tag'); | |
| 31 | |
| 32 assert.equal(element.innerHTML, '<b>Hello World</b>'); | |
| 33 }); | |
| 34 | |
| 35 QUnit.test('localizeElementFromTag() should replace innerHTML if flag is set', | |
| 36 function(assert) { | |
| 37 var element = document.createElement('div'); | |
| 38 $testStub(chrome.i18n.getMessage).withArgs('tag') | |
| 39 .returns('<b>Hello World</b>'); | |
| 40 | |
| 41 l10n.localizeElementFromTag(element, 'tag', null, true); | |
| 42 | |
| 43 assert.equal(element.innerHTML, '<b>Hello World</b>'); | |
| 44 }); | |
| 45 | |
| 46 QUnit.test( | |
| 47 'localizeElement() should replace innerText using the "i18n-content" ' + | |
| 48 'attribute as the tag', | |
| 49 function(assert) { | |
| 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>'); | |
| 54 | |
| 55 l10n.localizeElement(element); | |
| 56 | |
| 57 assert.equal(element.innerHTML, '<b>Hello World</b>'); | |
| 58 }); | |
| 59 | |
| 60 QUnit.test( | |
| 61 'localize() should replace element title using the "i18n-title" ' + | |
| 62 'attribute as the tag', | |
| 63 function(assert) { | |
| 64 var fixture = document.getElementById('qunit-fixture'); | |
| 65 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>'; | |
| 66 $testStub(chrome.i18n.getMessage) | |
| 67 .withArgs('tag') | |
| 68 .returns('localized title'); | |
| 69 | |
| 70 l10n.localize(); | |
| 71 | |
| 72 var target = document.querySelector('.target'); | |
| 73 assert.equal(target.title, 'localized title'); | |
| 74 }); | |
| 75 | |
| 76 QUnit.test('localize() should support string substitutions', function(assert) { | |
| 77 var fixture = document.getElementById('qunit-fixture'); | |
| 78 fixture.innerHTML = | |
| 79 '<div class="target" ' + | |
| 80 'i18n-content="tag" ' + | |
| 81 'i18n-value-1="param1" ' + | |
| 82 'i18n-value-2="param2">' + | |
| 83 '</div>'; | |
| 84 | |
| 85 $testStub(chrome.i18n.getMessage).withArgs('tag', ['param1', 'param2']) | |
| 86 .returns('localized'); | |
| 87 | |
| 88 l10n.localize(); | |
| 89 | |
| 90 var target = document.querySelector('.target'); | |
| 91 assert.equal(target.innerText, 'localized'); | |
| 92 }); | |
| 93 | |
| 94 QUnit.test('localize() should support tag substitutions', function(assert) { | |
| 95 var fixture = document.getElementById('qunit-fixture'); | |
| 96 fixture.innerHTML = | |
| 97 '<div class="target" i18n-content="tag"' + | |
| 98 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>'; | |
| 99 | |
| 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'); | |
| 105 | |
| 106 l10n.localize(); | |
| 107 | |
| 108 var target = document.querySelector('.target'); | |
| 109 assert.equal(target.innerText, 'localized'); | |
| 110 }); | |
| 111 | |
| 112 })(); | |
| OLD | NEW |