Home | History | Annotate | Download | only in unittests
      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     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     chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
     27 
     28     l10n.localizeElementFromTag(element, 'tag');
     29 
     30     equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;');
     31 });
     32 
     33 test('localizeElementFromTag() should replace innerHTML if flag is set',
     34   function() {
     35     var element = document.createElement('div');
     36     chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
     37 
     38     l10n.localizeElementFromTag(element, 'tag', null, true);
     39 
     40     equal(element.innerHTML, '<b>Hello World</b>');
     41 });
     42 
     43 test(
     44   'localizeElement() should replace innerText using the "i18n-content" ' +
     45   'attribute as the tag',
     46   function() {
     47     var element = document.createElement('div');
     48     element.setAttribute('i18n-content', 'tag');
     49     chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
     50 
     51     l10n.localizeElement(element);
     52 
     53     equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;');
     54 });
     55 
     56 test(
     57   'localize() should replace element title using the "i18n-title" ' +
     58   'attribute as the tag',
     59   function() {
     60     var fixture = document.getElementById('qunit-fixture');
     61     fixture.innerHTML = '<div class="target" i18n-title="tag"></div>';
     62     chrome.i18n.getMessage.withArgs('tag').returns('localized title');
     63 
     64     l10n.localize();
     65 
     66     var target = document.querySelector('.target');
     67     equal(target.title, 'localized title');
     68 });
     69 
     70 test('localize() should support string substitutions', function() {
     71   var fixture = document.getElementById('qunit-fixture');
     72   fixture.innerHTML =
     73   '<div class="target" ' +
     74       'i18n-content="tag" ' +
     75       'i18n-value-1="param1" ' +
     76       'i18n-value-2="param2">' +
     77   '</div>';
     78 
     79   chrome.i18n.getMessage.withArgs('tag', ['param1', 'param2'])
     80       .returns('localized');
     81 
     82   l10n.localize();
     83 
     84   var target = document.querySelector('.target');
     85   equal(target.innerText, 'localized');
     86 });
     87 
     88 test('localize() should support tag substitutions', function() {
     89   var fixture = document.getElementById('qunit-fixture');
     90   fixture.innerHTML =
     91       '<div class="target" i18n-content="tag"' +
     92       ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>';
     93 
     94   var getMessage = chrome.i18n.getMessage;
     95   getMessage.withArgs('tag1').returns('param1');
     96   getMessage.withArgs('tag2').returns('param2');
     97   getMessage.withArgs('tag', ['param1', 'param2']).returns('localized');
     98 
     99   l10n.localize();
    100 
    101   var target = document.querySelector('.target');
    102   equal(target.innerText, 'localized');
    103 });
    104 
    105 })();
    106