[go: nahoru, domu]

Skip to content

Commit

Permalink
Move localization.js to app
Browse files Browse the repository at this point in the history
Since it is no longer used in core. Also splits localization tests into
a separate file.
  • Loading branch information
samhed committed Nov 14, 2017
1 parent 51dab6d commit e48dd94
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 68 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/* global window, document.getElementById, Util, WebUtil, RFB, Display */

import * as Log from '../core/util/logging.js';
import _, { l10n } from '../core/util/localization.js';
import _, { l10n } from './localization.js';
import { isTouchDevice } from '../core/util/browsers.js';
import { setCapture, getPointerEvent } from '../core/util/events.js';
import KeyTable from "../core/input/keysym.js";
Expand Down
2 changes: 2 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = function(config) {
files: [
{ pattern: 'vendor/sinon.js', included: false },
{ pattern: 'node_modules/sinon-chai/lib/sinon-chai.js', included: false },
{ pattern: 'app/localization.js', included: false },
{ pattern: 'core/**/*.js', included: false },
{ pattern: 'vendor/pako/**/*.js', included: false },
{ pattern: 'tests/test.*.js', included: false },
Expand Down Expand Up @@ -90,6 +91,7 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'app/localization.js': ['babel'],
'core/**/*.js': ['babel'],
'tests/test.*.js': ['babel'],
'tests/fake.*.js': ['babel'],
Expand Down
76 changes: 76 additions & 0 deletions tests/test.localization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* jshint expr: true */

var assert = chai.assert;
var expect = chai.expect;

import l10nGet, { l10n } from '../app/localization.js';

describe('Localization', function() {
"use strict";

describe('language selection', function () {
var origNavigator;
beforeEach(function () {
// window.navigator is a protected read-only property in many
// environments, so we need to redefine it whilst running these
// tests.
origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
if (origNavigator === undefined) {
// Object.getOwnPropertyDescriptor() doesn't work
// properly in any version of IE
this.skip();
}

Object.defineProperty(window, "navigator", {value: {}});
if (window.navigator.languages !== undefined) {
// Object.defineProperty() doesn't work properly in old
// versions of Chrome
this.skip();
}

window.navigator.languages = [];
});
afterEach(function () {
Object.defineProperty(window, "navigator", origNavigator);
});

it('should use English by default', function() {
expect(l10n.language).to.equal('en');
});
it('should use English if no user language matches', function() {
window.navigator.languages = ["nl", "de"];
l10n.setup(["es", "fr"]);
expect(l10n.language).to.equal('en');
});
it('should use the most preferred user language', function() {
window.navigator.languages = ["nl", "de", "fr"];
l10n.setup(["es", "fr", "de"]);
expect(l10n.language).to.equal('de');
});
it('should prefer sub-languages languages', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt", "pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
it('should fall back to language "parents"', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["fr", "pt", "de"]);
expect(l10n.language).to.equal('pt');
});
it('should not use specific language when user asks for a generic language', function() {
window.navigator.languages = ["pt", "de"];
l10n.setup(["fr", "pt-BR", "de"]);
expect(l10n.language).to.equal('de');
});
it('should handle underscore as a separator', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt_BR"]);
expect(l10n.language).to.equal('pt_BR');
});
it('should handle difference in case', function() {
window.navigator.languages = ["pt-br"];
l10n.setup(["pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
});
});
67 changes: 0 additions & 67 deletions tests/test.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var assert = chai.assert;
var expect = chai.expect;

import * as Log from '../core/util/logging.js';
import l10nGet, { l10n } from '../core/util/localization.js';

import sinon from '../vendor/sinon.js';

Expand Down Expand Up @@ -63,72 +62,6 @@ describe('Utils', function() {
});
});

describe('language selection', function () {
var origNavigator;
beforeEach(function () {
// window.navigator is a protected read-only property in many
// environments, so we need to redefine it whilst running these
// tests.
origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
if (origNavigator === undefined) {
// Object.getOwnPropertyDescriptor() doesn't work
// properly in any version of IE
this.skip();
}

Object.defineProperty(window, "navigator", {value: {}});
if (window.navigator.languages !== undefined) {
// Object.defineProperty() doesn't work properly in old
// versions of Chrome
this.skip();
}

window.navigator.languages = [];
});
afterEach(function () {
Object.defineProperty(window, "navigator", origNavigator);
});

it('should use English by default', function() {
expect(l10n.language).to.equal('en');
});
it('should use English if no user language matches', function() {
window.navigator.languages = ["nl", "de"];
l10n.setup(["es", "fr"]);
expect(l10n.language).to.equal('en');
});
it('should use the most preferred user language', function() {
window.navigator.languages = ["nl", "de", "fr"];
l10n.setup(["es", "fr", "de"]);
expect(l10n.language).to.equal('de');
});
it('should prefer sub-languages languages', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt", "pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
it('should fall back to language "parents"', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["fr", "pt", "de"]);
expect(l10n.language).to.equal('pt');
});
it('should not use specific language when user asks for a generic language', function() {
window.navigator.languages = ["pt", "de"];
l10n.setup(["fr", "pt-BR", "de"]);
expect(l10n.language).to.equal('de');
});
it('should handle underscore as a separator', function() {
window.navigator.languages = ["pt-BR"];
l10n.setup(["pt_BR"]);
expect(l10n.language).to.equal('pt_BR');
});
it('should handle difference in case', function() {
window.navigator.languages = ["pt-br"];
l10n.setup(["pt-BR"]);
expect(l10n.language).to.equal('pt-BR');
});
});

// TODO(directxman12): test the conf_default and conf_defaults methods
// TODO(directxman12): test decodeUTF8
// TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
Expand Down

0 comments on commit e48dd94

Please sign in to comment.