add dist files builder and tester to Gruntfile
[mootools.git] / Specs / Browser / Browser.js
blob111df39de8712d5ea135d0d2a7e300c867286802
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(){
79         it('should think it is executed in a browser', function(){
80                 var isPhantomJS = !!navigator.userAgent.match(/phantomjs/i);
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                         var isPhantomJS = !!navigator.userAgent.match(/phantomjs/i);
88                         expect(isPhantomJS || Browser.ie || Browser.safari || Browser.chrome || Browser.firefox || Browser.opera).toEqual(true);
89                 }
90         });
92         it('should not assign a Browser[Browser.name] property for IE v11 or higher', function(){
93                 if (Browser.name == 'ie' && Browser.version >= 11){
94                         expect(Browser.ie || Browser.safari || Browser.chrome || Browser.firefox || Browser.opera).toBeUndefined();
95                 }
96         });
97 //</1.4compat>
99         it('should assume the IE version is emulated by the documentMode (X-UA-Compatible)', function(){
100                 if (Browser.name == 'ie' && document.documentMode) expect(Browser.version).toEqual(document.documentMode);
101         });
105 describe('Browser.parseUA', function(){
107         var parse = Browser.parseUA;
108         var userAgents = {
109                 ie6: {
110                         desc: 'Internet Explorer 6',
111                         string: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Win64; x64; SV1; .NET CLR 2.0.50727)',
112                         expect: {
113                                 name: 'ie',
114                                 version: 6
115                         }
116                 },
117                 ie7: {
118                         desc: 'Internet Explorer 7',
119                         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)',
120                         expect: {
121                                 name: 'ie',
122                                 version: 7
123                         }
124                 },
125                 ie8: {
126                         desc: 'Internet Explorer 8',
127                         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)',
128                         expect: {
129                                 name: 'ie',
130                                 version: 8
131                         }
132                 },
133                 ie9: {
134                         desc: 'Internet Explorer 9',
135                         string: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
136                         expect: {
137                                 name: 'ie',
138                                 version: 9
139                         }
140                 },
141                 ie10: {
142                         desc: 'Internet Explorer 10',
143                         string: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)',
144                         expect: {
145                                 name: 'ie',
146                                 version: 10
147                         }
148                 },
149                 ie11: {
150                         desc: 'Internet Explorer 11',
151                         string: 'Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko',
152                         expect: {
153                                 name: 'ie',
154                                 version: 11
155                         }
156                 },
157                 ie11v2: {
158                         desc: 'Internet Explorer 11 v2',
159                         string: 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv 11.0) like Gecko',
160                         expect: {
161                                 name: 'ie',
162                                 version: 11
163                         }
164                 },
165                 ieCompat: {
166                         desc: 'Internet Explorer 10 in IE7 compatibility',
167                         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)',
168                         expect: {
169                                 name: 'ie',
170                                 version: 7
171                         }
172                 },
173                 firefox: {
174                         desc: 'Firefox v24',
175                         string: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Firefox/24.0',
176                         expect: {
177                                 name: 'firefox',
178                                 version: 24
179                         }
180                 },
181                 opera10: {
182                         desc: 'Opera 10',
183                         string: 'Opera/9.80 (Windows NT 5.1; U; cs) Presto/2.2.15 Version/10.00',
184                         expect: {
185                                 name: 'opera',
186                                 version: 10
187                         }
188                 },
189                 opera11: {
190                         desc: 'Opera 11.62',
191                         string: 'Opera/9.80 (Windows NT 6.1; WOW64; U; pt) Presto/2.10.229 Version/11.62',
192                         expect: {
193                                 name: 'opera',
194                                 version: 11.62
195                         }
196                 },
197                 opera12: {
198                         desc: 'Opera 12.14',
199                         string: 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14',
200                         expect: {
201                                 name: 'opera',
202                                 version: 12.14
203                         }
204                 },
205                 safari: {
206                         desc: 'Safari 6.1',
207                         string: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.71 (KHTML, like Gecko) Version/6.1 Safari/537.71',
208                         expect: {
209                                 name: 'safari',
210                                 version: 6.1
211                         }
212                 },
213                 chrome: {
214                         desc: 'Chrome 31',
215                         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',
216                         expect: {
217                                 name: 'chrome',
218                                 version: 31
219                         }
220                 },
221                 chromeios: {
222                         desc: 'Chrome 33 on iOS',
223                         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',
224                         expect: {
225                                 name: 'chrome',
226                                 version: 33
227                         }
228                 }
229         };
231         var testUA = function(ua){
232                 return function(){
233                         var browser = parse(ua.string, '');
234                         Object.forEach(ua.expect, runExpects, browser);
235                 }
236         }
238         var runExpects = function(val, key){
239                 expect(this[key]).toEqual(val);
240         }
242         Object.forEach(userAgents, function(obj){
243                 it('should parse ' + obj.desc + ' user agent string', testUA(obj));
244         });