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