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