]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.util.js
4ab2944f62e1b440822f001218d931ec5557427a
[mirror_novnc.git] / tests / test.util.js
1 /* jshint expr: true */
2
3 var assert = chai.assert;
4 var expect = chai.expect;
5
6 import * as Log from '../core/util/logging.js';
7 import l10nGet, { l10n } from '../core/util/localization.js';
8
9 import sinon from '../vendor/sinon.js';
10
11 describe('Utils', function() {
12 "use strict";
13
14 describe('logging functions', function () {
15 beforeEach(function () {
16 sinon.spy(console, 'log');
17 sinon.spy(console, 'debug');
18 sinon.spy(console, 'warn');
19 sinon.spy(console, 'error');
20 sinon.spy(console, 'info');
21 });
22
23 afterEach(function () {
24 console.log.restore();
25 console.debug.restore();
26 console.warn.restore();
27 console.error.restore();
28 console.info.restore();
29 });
30
31 it('should use noop for levels lower than the min level', function () {
32 Log.init_logging('warn');
33 Log.Debug('hi');
34 Log.Info('hello');
35 expect(console.log).to.not.have.been.called;
36 });
37
38 it('should use console.debug for Debug', function () {
39 Log.init_logging('debug');
40 Log.Debug('dbg');
41 expect(console.debug).to.have.been.calledWith('dbg');
42 });
43
44 it('should use console.info for Info', function () {
45 Log.init_logging('debug');
46 Log.Info('inf');
47 expect(console.info).to.have.been.calledWith('inf');
48 });
49
50 it('should use console.warn for Warn', function () {
51 Log.init_logging('warn');
52 Log.Warn('wrn');
53 expect(console.warn).to.have.been.called;
54 expect(console.warn).to.have.been.calledWith('wrn');
55 });
56
57 it('should use console.error for Error', function () {
58 Log.init_logging('error');
59 Log.Error('err');
60 expect(console.error).to.have.been.called;
61 expect(console.error).to.have.been.calledWith('err');
62 });
63 });
64
65 describe('language selection', function () {
66 var origNavigator;
67 beforeEach(function () {
68 // window.navigator is a protected read-only property in many
69 // environments, so we need to redefine it whilst running these
70 // tests.
71 origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
72 if (origNavigator === undefined) {
73 // Object.getOwnPropertyDescriptor() doesn't work
74 // properly in any version of IE
75 this.skip();
76 }
77
78 Object.defineProperty(window, "navigator", {value: {}});
79 if (window.navigator.languages !== undefined) {
80 // Object.defineProperty() doesn't work properly in old
81 // versions of Chrome
82 this.skip();
83 }
84
85 window.navigator.languages = [];
86 });
87 afterEach(function () {
88 Object.defineProperty(window, "navigator", origNavigator);
89 });
90
91 it('should use English by default', function() {
92 expect(l10n.language).to.equal('en');
93 });
94 it('should use English if no user language matches', function() {
95 window.navigator.languages = ["nl", "de"];
96 l10n.setup(["es", "fr"]);
97 expect(l10n.language).to.equal('en');
98 });
99 it('should use the most preferred user language', function() {
100 window.navigator.languages = ["nl", "de", "fr"];
101 l10n.setup(["es", "fr", "de"]);
102 expect(l10n.language).to.equal('de');
103 });
104 it('should prefer sub-languages languages', function() {
105 window.navigator.languages = ["pt-BR"];
106 l10n.setup(["pt", "pt-BR"]);
107 expect(l10n.language).to.equal('pt-BR');
108 });
109 it('should fall back to language "parents"', function() {
110 window.navigator.languages = ["pt-BR"];
111 l10n.setup(["fr", "pt", "de"]);
112 expect(l10n.language).to.equal('pt');
113 });
114 it('should not use specific language when user asks for a generic language', function() {
115 window.navigator.languages = ["pt", "de"];
116 l10n.setup(["fr", "pt-BR", "de"]);
117 expect(l10n.language).to.equal('de');
118 });
119 it('should handle underscore as a separator', function() {
120 window.navigator.languages = ["pt-BR"];
121 l10n.setup(["pt_BR"]);
122 expect(l10n.language).to.equal('pt_BR');
123 });
124 it('should handle difference in case', function() {
125 window.navigator.languages = ["pt-br"];
126 l10n.setup(["pt-BR"]);
127 expect(l10n.language).to.equal('pt-BR');
128 });
129 });
130
131 // TODO(directxman12): test the conf_default and conf_defaults methods
132 // TODO(directxman12): test decodeUTF8
133 // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
134 // TODO(directxman12): figure out a good way to test getPosition and getEventPosition
135 // TODO(directxman12): figure out how to test the browser detection functions properly
136 // (we can't really test them against the browsers, except for Gecko
137 // via PhantomJS, the default test driver)
138 });