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