]>
Commit | Line | Data |
---|---|---|
d21cd6c1 SR |
1 | /* jshint expr: true */ |
2 | ||
3 | var assert = chai.assert; | |
4 | var expect = chai.expect; | |
5 | ||
dfae3209 SR |
6 | import * as Log from '../core/util/logging.js'; |
7 | import l10nGet, { l10n } from '../core/util/localization.js'; | |
8 | ||
9 | import 'sinon'; | |
afb621d5 | 10 | import sinonChai from '../node_modules/sinon-chai/lib/sinon-chai.js'; |
dfae3209 SR |
11 | chai.use(sinonChai); |
12 | ||
d21cd6c1 SR |
13 | describe('Utils', function() { |
14 | "use strict"; | |
15 | ||
d21cd6c1 SR |
16 | describe('logging functions', function () { |
17 | beforeEach(function () { | |
18 | sinon.spy(console, 'log'); | |
0ae5b50a | 19 | sinon.spy(console, 'debug'); |
d21cd6c1 SR |
20 | sinon.spy(console, 'warn'); |
21 | sinon.spy(console, 'error'); | |
e4fef7be | 22 | sinon.spy(console, 'info'); |
d21cd6c1 SR |
23 | }); |
24 | ||
25 | afterEach(function () { | |
26 | console.log.restore(); | |
0ae5b50a | 27 | console.debug.restore(); |
d21cd6c1 SR |
28 | console.warn.restore(); |
29 | console.error.restore(); | |
e4fef7be | 30 | console.info.restore(); |
d21cd6c1 SR |
31 | }); |
32 | ||
33 | it('should use noop for levels lower than the min level', function () { | |
dfae3209 SR |
34 | Log.init_logging('warn'); |
35 | Log.Debug('hi'); | |
36 | Log.Info('hello'); | |
d21cd6c1 SR |
37 | expect(console.log).to.not.have.been.called; |
38 | }); | |
39 | ||
0ae5b50a | 40 | it('should use console.debug for Debug', function () { |
dfae3209 SR |
41 | Log.init_logging('debug'); |
42 | Log.Debug('dbg'); | |
0ae5b50a | 43 | expect(console.debug).to.have.been.calledWith('dbg'); |
e4fef7be | 44 | }); |
c464f47e | 45 | |
e4fef7be | 46 | it('should use console.info for Info', function () { |
dfae3209 SR |
47 | Log.init_logging('debug'); |
48 | Log.Info('inf'); | |
e4fef7be | 49 | expect(console.info).to.have.been.calledWith('inf'); |
d21cd6c1 SR |
50 | }); |
51 | ||
52 | it('should use console.warn for Warn', function () { | |
dfae3209 SR |
53 | Log.init_logging('warn'); |
54 | Log.Warn('wrn'); | |
d21cd6c1 SR |
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 () { | |
dfae3209 SR |
60 | Log.init_logging('error'); |
61 | Log.Error('err'); | |
d21cd6c1 SR |
62 | expect(console.error).to.have.been.called; |
63 | expect(console.error).to.have.been.calledWith('err'); | |
64 | }); | |
65 | }); | |
66 | ||
3cdc603a | 67 | describe('language selection', function () { |
77bc3a55 SM |
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"); | |
6ec3af22 PO |
74 | if (origNavigator === undefined) { |
75 | // Object.getOwnPropertyDescriptor() doesn't work | |
76 | // properly in any version of IE | |
77 | this.skip(); | |
78 | } | |
79 | ||
77bc3a55 | 80 | Object.defineProperty(window, "navigator", {value: {}}); |
6ec3af22 PO |
81 | if (window.navigator.languages !== undefined) { |
82 | // Object.defineProperty() doesn't work properly in old | |
83 | // versions of Chrome | |
84 | this.skip(); | |
85 | } | |
86 | ||
77bc3a55 SM |
87 | window.navigator.languages = []; |
88 | }); | |
89 | afterEach(function () { | |
90 | Object.defineProperty(window, "navigator", origNavigator); | |
91 | }); | |
92 | ||
3cdc603a | 93 | it('should use English by default', function() { |
dfae3209 | 94 | expect(l10n.language).to.equal('en'); |
3cdc603a PO |
95 | }); |
96 | it('should use English if no user language matches', function() { | |
97 | window.navigator.languages = ["nl", "de"]; | |
dfae3209 SR |
98 | l10n.setup(["es", "fr"]); |
99 | expect(l10n.language).to.equal('en'); | |
3cdc603a PO |
100 | }); |
101 | it('should use the most preferred user language', function() { | |
102 | window.navigator.languages = ["nl", "de", "fr"]; | |
dfae3209 SR |
103 | l10n.setup(["es", "fr", "de"]); |
104 | expect(l10n.language).to.equal('de'); | |
3cdc603a PO |
105 | }); |
106 | it('should prefer sub-languages languages', function() { | |
107 | window.navigator.languages = ["pt-BR"]; | |
dfae3209 SR |
108 | l10n.setup(["pt", "pt-BR"]); |
109 | expect(l10n.language).to.equal('pt-BR'); | |
3cdc603a PO |
110 | }); |
111 | it('should fall back to language "parents"', function() { | |
112 | window.navigator.languages = ["pt-BR"]; | |
dfae3209 SR |
113 | l10n.setup(["fr", "pt", "de"]); |
114 | expect(l10n.language).to.equal('pt'); | |
3cdc603a PO |
115 | }); |
116 | it('should not use specific language when user asks for a generic language', function() { | |
117 | window.navigator.languages = ["pt", "de"]; | |
dfae3209 SR |
118 | l10n.setup(["fr", "pt-BR", "de"]); |
119 | expect(l10n.language).to.equal('de'); | |
3cdc603a PO |
120 | }); |
121 | it('should handle underscore as a separator', function() { | |
122 | window.navigator.languages = ["pt-BR"]; | |
dfae3209 SR |
123 | l10n.setup(["pt_BR"]); |
124 | expect(l10n.language).to.equal('pt_BR'); | |
3cdc603a PO |
125 | }); |
126 | it('should handle difference in case', function() { | |
127 | window.navigator.languages = ["pt-br"]; | |
dfae3209 SR |
128 | l10n.setup(["pt-BR"]); |
129 | expect(l10n.language).to.equal('pt-BR'); | |
3cdc603a PO |
130 | }); |
131 | }); | |
132 | ||
d21cd6c1 SR |
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 | |
ee7d4c61 | 137 | // TODO(directxman12): figure out how to test the browser detection functions properly |
d21cd6c1 SR |
138 | // (we can't really test them against the browsers, except for Gecko |
139 | // via PhantomJS, the default test driver) | |
d21cd6c1 | 140 | }); |