| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function() { | 5 (function() { |
| 6 | 6 |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 module('l10n', { | 9 module('l10n', { |
| 10 setup: function() { | 10 setup: function() { |
| 11 sinon.stub(chrome.i18n, 'getMessage'); | 11 sinon.$setupStub(chrome.i18n, 'getMessage'); |
| 12 }, | 12 }, |
| 13 teardown: function() { | 13 teardown: function() { |
| 14 chrome.i18n.getMessage.restore(); | 14 chrome.i18n.getMessage.$testStub.restore(); |
| 15 } | 15 } |
| 16 }); | 16 }); |
| 17 | 17 |
| 18 test('getTranslationOrError(tag) should return tag on error', function() { | 18 test('getTranslationOrError(tag) should return tag on error', function() { |
| 19 var translation = l10n.getTranslationOrError('non_existent_tag'); | 19 var translation = l10n.getTranslationOrError('non_existent_tag'); |
| 20 equal(translation, 'non_existent_tag'); | 20 equal(translation, 'non_existent_tag'); |
| 21 }); | 21 }); |
| 22 | 22 |
| 23 test('localizeElementFromTag() should replace innerText by default', | 23 test('localizeElementFromTag() should replace innerText by default', |
| 24 function() { | 24 function() { |
| 25 var element = document.createElement('div'); | 25 var element = document.createElement('div'); |
| 26 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>'); | 26 chrome.i18n.getMessage.$testStub.withArgs('tag') |
| 27 .returns('<b>Hello World</b>'); |
| 27 | 28 |
| 28 l10n.localizeElementFromTag(element, 'tag'); | 29 l10n.localizeElementFromTag(element, 'tag'); |
| 29 | 30 |
| 30 equal(element.innerHTML, '<b>Hello World</b>'); | 31 equal(element.innerHTML, '<b>Hello World</b>'); |
| 31 }); | 32 }); |
| 32 | 33 |
| 33 test('localizeElementFromTag() should replace innerHTML if flag is set', | 34 test('localizeElementFromTag() should replace innerHTML if flag is set', |
| 34 function() { | 35 function() { |
| 35 var element = document.createElement('div'); | 36 var element = document.createElement('div'); |
| 36 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>'); | 37 chrome.i18n.getMessage.$testStub.withArgs('tag') |
| 38 .returns('<b>Hello World</b>'); |
| 37 | 39 |
| 38 l10n.localizeElementFromTag(element, 'tag', null, true); | 40 l10n.localizeElementFromTag(element, 'tag', null, true); |
| 39 | 41 |
| 40 equal(element.innerHTML, '<b>Hello World</b>'); | 42 equal(element.innerHTML, '<b>Hello World</b>'); |
| 41 }); | 43 }); |
| 42 | 44 |
| 43 test( | 45 test( |
| 44 'localizeElement() should replace innerText using the "i18n-content" ' + | 46 'localizeElement() should replace innerText using the "i18n-content" ' + |
| 45 'attribute as the tag', | 47 'attribute as the tag', |
| 46 function() { | 48 function() { |
| 47 var element = document.createElement('div'); | 49 var element = document.createElement('div'); |
| 48 element.setAttribute('i18n-content', 'tag'); | 50 element.setAttribute('i18n-content', 'tag'); |
| 49 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>'); | 51 chrome.i18n.getMessage.$testStub.withArgs('tag') |
| 52 .returns('<b>Hello World</b>'); |
| 50 | 53 |
| 51 l10n.localizeElement(element); | 54 l10n.localizeElement(element); |
| 52 | 55 |
| 53 equal(element.innerHTML, '<b>Hello World</b>'); | 56 equal(element.innerHTML, '<b>Hello World</b>'); |
| 54 }); | 57 }); |
| 55 | 58 |
| 56 test( | 59 test( |
| 57 'localize() should replace element title using the "i18n-title" ' + | 60 'localize() should replace element title using the "i18n-title" ' + |
| 58 'attribute as the tag', | 61 'attribute as the tag', |
| 59 function() { | 62 function() { |
| 60 var fixture = document.getElementById('qunit-fixture'); | 63 var fixture = document.getElementById('qunit-fixture'); |
| 61 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>'; | 64 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>'; |
| 62 chrome.i18n.getMessage.withArgs('tag').returns('localized title'); | 65 chrome.i18n.getMessage.$testStub.withArgs('tag').returns('localized title'); |
| 63 | 66 |
| 64 l10n.localize(); | 67 l10n.localize(); |
| 65 | 68 |
| 66 var target = document.querySelector('.target'); | 69 var target = document.querySelector('.target'); |
| 67 equal(target.title, 'localized title'); | 70 equal(target.title, 'localized title'); |
| 68 }); | 71 }); |
| 69 | 72 |
| 70 test('localize() should support string substitutions', function() { | 73 test('localize() should support string substitutions', function() { |
| 71 var fixture = document.getElementById('qunit-fixture'); | 74 var fixture = document.getElementById('qunit-fixture'); |
| 72 fixture.innerHTML = | 75 fixture.innerHTML = |
| 73 '<div class="target" ' + | 76 '<div class="target" ' + |
| 74 'i18n-content="tag" ' + | 77 'i18n-content="tag" ' + |
| 75 'i18n-value-1="param1" ' + | 78 'i18n-value-1="param1" ' + |
| 76 'i18n-value-2="param2">' + | 79 'i18n-value-2="param2">' + |
| 77 '</div>'; | 80 '</div>'; |
| 78 | 81 |
| 79 chrome.i18n.getMessage.withArgs('tag', ['param1', 'param2']) | 82 chrome.i18n.getMessage.$testStub.withArgs('tag', ['param1', 'param2']) |
| 80 .returns('localized'); | 83 .returns('localized'); |
| 81 | 84 |
| 82 l10n.localize(); | 85 l10n.localize(); |
| 83 | 86 |
| 84 var target = document.querySelector('.target'); | 87 var target = document.querySelector('.target'); |
| 85 equal(target.innerText, 'localized'); | 88 equal(target.innerText, 'localized'); |
| 86 }); | 89 }); |
| 87 | 90 |
| 88 test('localize() should support tag substitutions', function() { | 91 test('localize() should support tag substitutions', function() { |
| 89 var fixture = document.getElementById('qunit-fixture'); | 92 var fixture = document.getElementById('qunit-fixture'); |
| 90 fixture.innerHTML = | 93 fixture.innerHTML = |
| 91 '<div class="target" i18n-content="tag"' + | 94 '<div class="target" i18n-content="tag"' + |
| 92 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>'; | 95 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>'; |
| 93 | 96 |
| 94 var getMessage = chrome.i18n.getMessage; | 97 var getMessage = chrome.i18n.getMessage.$testStub; |
| 95 getMessage.withArgs('tag1').returns('param1'); | 98 getMessage.withArgs('tag1').returns('param1'); |
| 96 getMessage.withArgs('tag2').returns('param2'); | 99 getMessage.withArgs('tag2').returns('param2'); |
| 97 getMessage.withArgs('tag', ['param1', 'param2']).returns('localized'); | 100 getMessage.withArgs('tag', ['param1', 'param2']).returns('localized'); |
| 98 | 101 |
| 99 l10n.localize(); | 102 l10n.localize(); |
| 100 | 103 |
| 101 var target = document.querySelector('.target'); | 104 var target = document.querySelector('.target'); |
| 102 equal(target.innerText, 'localized'); | 105 equal(target.innerText, 'localized'); |
| 103 }); | 106 }); |
| 104 | 107 |
| 105 })(); | 108 })(); |
| OLD | NEW |