Chromium Code Reviews| 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 chrome.i18n = chrome.i18n || {}; | |
| 12 chrome.i18n.getMessage = chrome.i18n.getMessage || function() {}; | |
|
Jamie
2014/07/18 02:04:50
The use of || here doesn't feel right. It's genera
kelvinp
2014/07/21 17:46:22
Done.
| |
| 13 sinon.stub(chrome.i18n, 'getMessage'); | |
| 14 }, | |
| 15 teardown: function() { | |
| 16 chrome.i18n.getMessage.restore(); | |
| 17 } | |
| 18 }); | |
| 19 | |
| 20 test('getTranslationOrError(tag) should return tag on error', function() { | |
| 21 var translation = l10n.getTranslationOrError('not_existing_tag'); | |
|
Jamie
2014/07/18 02:04:51
s/no_existing/non_existent/?
kelvinp
2014/07/21 17:46:22
Done.
| |
| 22 equal(translation, 'not_existing_tag'); | |
| 23 }); | |
| 24 | |
| 25 test('localizeElementFromTag() should replace innerText by default', | |
| 26 function() { | |
| 27 var element = document.createElement('div'); | |
| 28 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>'); | |
| 29 | |
| 30 l10n.localizeElementFromTag(element, 'tag'); | |
| 31 | |
| 32 equal(element.innerText, '<b>Hello World</b>'); | |
| 33 equal(element.innerHTML, '<b>Hello World</b>'); | |
|
Jamie
2014/07/18 02:04:51
Why test both of these? Doesn't the first test imp
kelvinp
2014/07/21 17:46:22
Done.
| |
| 34 }); | |
| 35 | |
| 36 test('localizeElementFromTag() should replace innerHTML if flag is set', | |
| 37 function() { | |
| 38 var element = document.createElement('div'); | |
| 39 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>'); | |
| 40 | |
| 41 l10n.localizeElementFromTag(element, 'tag', null, true); | |
| 42 | |
| 43 equal(element.innerText, 'Hello World'); | |
| 44 equal(element.innerHTML, '<b>Hello World</b>'); | |
| 45 }); | |
| 46 | |
| 47 test( | |
| 48 'localizeElement() should replace innerText using the "i18n-content" ' + | |
| 49 'attribute as the tag', | |
| 50 function() { | |
| 51 var element = document.createElement('div'); | |
| 52 element.setAttribute('i18n-content', 'tag'); | |
| 53 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>'); | |
| 54 | |
| 55 l10n.localizeElement(element); | |
| 56 | |
| 57 equal(element.innerText, '<b>Hello World</b>'); | |
| 58 equal(element.innerHTML, '<b>Hello World</b>'); | |
| 59 }); | |
| 60 | |
| 61 test( | |
| 62 'localize() should replace element title using the "i18n-title" ' + | |
| 63 'attribute as the tag', | |
| 64 function() { | |
| 65 var fixture = document.getElementById('qunit-fixture'); | |
| 66 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>'; | |
| 67 chrome.i18n.getMessage.withArgs('tag').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'); | |
|
Jamie
2014/07/18 02:04:51
Nit: indentation.
kelvinp
2014/07/21 17:46:22
Done.
| |
| 77 fixture.innerHTML = '<div class="target" i18n-content="tag"' + | |
| 78 ' i18n-value-1="param1" i18n-value-2="param2"></div>'; | |
|
Jamie
2014/07/18 02:04:50
For inline HTML that's too long for one line, I th
kelvinp
2014/07/21 17:46:22
Done.
| |
| 79 | |
| 80 chrome.i18n.getMessage.withArgs('tag', ['param1', 'param2']) | |
| 81 .returns('localized'); | |
| 82 | |
| 83 l10n.localize(); | |
| 84 | |
| 85 var target = document.querySelector('.target'); | |
| 86 equal(target.innerText, 'localized'); | |
| 87 }); | |
| 88 | |
| 89 test('localize() should support tag substitutions', function() { | |
| 90 var fixture = document.getElementById('qunit-fixture'); | |
| 91 fixture.innerHTML = | |
| 92 '<div class="target" i18n-content="tag"' + | |
| 93 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>'; | |
| 94 | |
| 95 var getMessage = chrome.i18n.getMessage; | |
| 96 getMessage.withArgs('tag1').returns('param1'); | |
| 97 getMessage.withArgs('tag2').returns('param2'); | |
| 98 getMessage.withArgs('tag', ['param1', 'param2']).returns('localized'); | |
| 99 | |
| 100 l10n.localize(); | |
| 101 | |
| 102 var target = document.querySelector('.target'); | |
| 103 equal(target.innerText, 'localized'); | |
| 104 }); | |
| 105 | |
| 106 })(); | |
| OLD | NEW |