Fixes #2715 - Adding Microsoft Edge UA string support to Browser.
[mootools.git] / Specs / Browser / Browser.js
blob7e786bf994c1d784dd43fe7e2d5fd60c840a265d
1 /*
2 ---
3 name: Browser
4 requires: ~
5 provides: ~
6 ...
7 */
9 //<1.2compat>
10 describe('$exec', function(){
12         it('should evaluate on global scope', function(){
13                 $exec.call($exec, 'var execSpec = 42');
14                 expect(window.execSpec).toEqual(42);
15         });
17         it('should return the evaluated script', function(){
18                 expect($exec('$empty();')).toEqual('$empty();');
19         });
21 });
22 //</1.2compat>
24 describe('Browser.exec', function(){
26         it('should evaluate on global scope', function(){
27                 Browser.exec.call(Browser.exec, 'var execSpec = 42');
28                 expect(window.execSpec).toEqual(42);
29         });
31         it('should return the evaluated script', function(){
32                 expect(Browser.exec('function test(){}')).toEqual('function test(){}');
33         });
35 });
37 // String.stripScripts
39 describe('String.stripScripts', function(){
41         it('should strip all script tags from a string', function(){
42                 expect('<div><script type="text/javascript" src="file.js"></script></div>'.stripScripts()).toEqual('<div></div>');
43         });
45         it('should execute the stripped tags from the string', function(){
46                 expect('<div><script type="text/javascript"> var stripScriptsSpec = 42; </script></div>'.stripScripts(true)).toEqual('<div></div>');
47                 expect(window.stripScriptsSpec).toEqual(42);
48                 expect('<div><script>\n// <!--\nvar stripScriptsSpec = 24;\n//-->\n</script></div>'.stripScripts(true)).toEqual('<div></div>');
49                 expect(window.stripScriptsSpec).toEqual(24);
50                 expect('<div><script>\n/*<![CDATA[*/\nvar stripScriptsSpec = 4242;\n/*]]>*/</script></div>'.stripScripts(true)).toEqual('<div></div>');
51                 expect(window.stripScriptsSpec).toEqual(4242);
52         });
54 });
57 describe('Document', function(){
59         it('should hold the parent window', function(){
60                 expect(document.window).toEqual(window);
61         });
63         it('should hold the head element', function(){
64                 expect(document.head.tagName.toLowerCase()).toEqual('head');
65         });
67 });
69 describe('Window', function(){
71         it('should set the Element prototype', function(){
72                 expect(window.Element.prototype).toBeDefined();
73         });
75 });
77 describe('Browser', function(){
78         var isPhantomJS = !!navigator.userAgent.match(/phantomjs/i);
80         it('should think it is executed in a browser', function(){
81                 if (!isPhantomJS) expect(['ie', 'safari', 'chrome', 'firefox', 'opera']).toContain(Browser.name);
82         });
84 //<1.4compat>
85         it('should assign a Browser[Browser.name] property for all browsers, except IE v11 or higher', function(){
86                 if (Browser.name != 'ie' || Browser.version < 11){
87                         expect(isPhantomJS || Browser.ie || Browser.safari || Browser.chrome || Browser.firefox || Browser.opera).toEqual(true);
88                 }
89         });
91         it('should not assign a Browser[Browser.name] property for IE v11 or higher', function(){
92                 if (Browser.name == 'ie' && Browser.version >= 11){
93                         expect(Browser.ie || Browser.safari || Browser.chrome || Browser.firefox || Browser.opera).toBeUndefined();
94                 }
95         });
96 //</1.4compat>
98         it('should assume the IE version is emulated by the documentMode (X-UA-Compatible)', function(){
99                 if (Browser.name == 'ie' && document.documentMode) expect(Browser.version).toEqual(document.documentMode);
100         });
101         it('should find a browser version', function(){
102                 expect(Browser.version || isPhantomJS).toBeTruthy();
103                 expect(typeof Browser.version).toEqual('number');
104         });
108 describe('Browser.parseUA', function(){
110         var parse = Browser.parseUA;
111         var userAgents = {
112                 ie6: {
113                         desc: 'Internet Explorer 6',
114                         string: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Win64; x64; SV1; .NET CLR 2.0.50727)',
115                         expect: {
116                                 name: 'ie',
117                                 version: 6
118                         }
119                 },
120                 ie7: {
121                         desc: 'Internet Explorer 7',
122                         string: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)',
123                         expect: {
124                                 name: 'ie',
125                                 version: 7
126                         }
127                 },
128                 ie8: {
129                         desc: 'Internet Explorer 8',
130                         string: 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)',
131                         expect: {
132                                 name: 'ie',
133                                 version: 8
134                         }
135                 },
136                 ie9: {
137                         desc: 'Internet Explorer 9',
138                         string: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
139                         expect: {
140                                 name: 'ie',
141                                 version: 9
142                         }
143                 },
144                 ie10: {
145                         desc: 'Internet Explorer 10',
146                         string: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)',
147                         expect: {
148                                 name: 'ie',
149                                 version: 10
150                         }
151                 },
152                 ie11: {
153                         desc: 'Internet Explorer 11',
154                         string: 'Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko',
155                         expect: {
156                                 name: 'ie',
157                                 version: 11
158                         }
159                 },
160                 ie11v2: {
161                         desc: 'Internet Explorer 11 v2',
162                         string: 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv 11.0) like Gecko',
163                         expect: {
164                                 name: 'ie',
165                                 version: 11
166                         }
167                 },
168                 ieCompat: {
169                         desc: 'Internet Explorer 10 in IE7 compatibility',
170                         string: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)',
171                         expect: {
172                                 name: 'ie',
173                                 version: 7
174                         }
175                 },
176                 firefox: {
177                         desc: 'Firefox v24',
178                         string: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Firefox/24.0',
179                         expect: {
180                                 name: 'firefox',
181                                 version: 24
182                         }
183                 },
184                 opera10: {
185                         desc: 'Opera 10',
186                         string: 'Opera/9.80 (Windows NT 5.1; U; cs) Presto/2.2.15 Version/10.00',
187                         expect: {
188                                 name: 'opera',
189                                 version: 10
190                         }
191                 },
192                 opera11: {
193                         desc: 'Opera 11.62',
194                         string: 'Opera/9.80 (Windows NT 6.1; WOW64; U; pt) Presto/2.10.229 Version/11.62',
195                         expect: {
196                                 name: 'opera',
197                                 version: 11.62
198                         }
199                 },
200                 opera12: {
201                         desc: 'Opera 12.14',
202                         string: 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14',
203                         expect: {
204                                 name: 'opera',
205                                 version: 12.14
206                         }
207                 },
208                 safari: {
209                         desc: 'Safari 6.1',
210                         string: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.71 (KHTML, like Gecko) Version/6.1 Safari/537.71',
211                         expect: {
212                                 name: 'safari',
213                                 version: 6.1
214                         }
215                 },
216                 chrome: {
217                         desc: 'Chrome 31',
218                         string: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36',
219                         expect: {
220                                 name: 'chrome',
221                                 version: 31
222                         }
223                 },
224                 chromeios: {
225                         desc: 'Chrome 33 on iOS',
226                         string: 'Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/33.0.1750.21 Mobile/11B554a Safari/9537.53',
227                         expect: {
228                                 name: 'chrome',
229                                 version: 33
230                         }
231                 },
232                 edge12: {
233                         desc: 'Edge 12',
234                         string: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0',
235                         expect: {
236                                 name: 'edge',
237                                 version: 12,
238                                 platform: 'windows'
239                         }
240                 }
241         };
243         var testUA = function(ua){
244                 return function(){
245                         var browser = parse(ua.string, '');
246                         Object.forEach(ua.expect, runExpects, browser);
247                 };
248         };
250         var runExpects = function(val, key){
251                 expect(this[key]).toEqual(val);
252         };
254         Object.forEach(userAgents, function(obj){
255                 it('should parse ' + obj.desc + ' user agent string', testUA(obj));
256         });