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