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