2013-12-13 12:17:10 +00:00
/ * *
* Loop through all entries in our user agents object and test everything .
*
* @ see src / useragents . js
* @ author hannes . diercks @ jimdo . com
* /
var g
, ua
, p
, assert = require ( 'assert' )
2015-07-26 02:14:31 +00:00
, browser = require ( '../src/bowser' )
2013-12-13 12:17:10 +00:00
, allUserAgents = require ( '../src/useragents' ) . useragents
/ * *
* Get the length of an object .
* http : //stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array
*
* @ param { Object } obj
* @ return { Number }
* /
function objLength ( obj ) {
var size = 0
, key
for ( key in obj ) {
if ( obj . hasOwnProperty ( key ) ) size ++
}
return size
}
2014-02-20 08:33:11 +00:00
function objKeys ( obj ) {
var keys = [ ]
, key
for ( key in obj ) {
if ( obj . hasOwnProperty ( key ) ) {
keys . push ( key )
}
}
keys . sort ( ) ;
return keys . join ( ', ' )
}
2013-12-13 12:17:10 +00:00
/* Groups */
for ( g in allUserAgents ) { ( function ( group , userAgents ) {
describe ( group , function ( ) {
/* User Agents */
for ( ua in userAgents ) { ( function ( userAgent , expections ) {
2014-02-20 08:33:11 +00:00
describe ( 'user agent "' + userAgent + '"' , function ( ) {
2013-12-13 12:17:10 +00:00
expections . name = group
/* Get the result from bowser. */
var result = browser . _detect ( userAgent )
/* At first, check if the result has the correct length. */
2014-02-20 08:33:11 +00:00
it ( 'should have ' + objLength ( expections ) + ' properties' , function ( ) {
assert . equal ( objKeys ( result ) , objKeys ( expections ) )
2013-12-13 12:17:10 +00:00
} )
/* Properties */
for ( p in expections ) { ( function ( property , value , resultValue ) {
/* Now ensure correctness of every property. */
it ( '\'s Property "' + property + '" should be ' + value , function ( ) {
assert . equal ( resultValue , value )
} )
} ) ( p , expections [ p ] , result [ p ] ) }
} )
} ) ( ua , userAgents [ ua ] ) }
} )
} ) ( g , allUserAgents [ g ] ) }
2016-06-16 09:18:35 +00:00
var comparisionsTasks = [
[ '9.0' , '10' , - 1 ] ,
[ '11' , '10' , 1 ] ,
[ '1.10.2.1' , '1.8.2.1.90' , 1 ] ,
[ '1.010.2.1' , '1.08.2.1.90' , 1 ] ,
[ '1.10.2.1' , '1.10.2.1' , 0 ] ,
[ '1.10.2.1' , '1.0800.2' , - 1 ] ,
2016-06-16 10:20:30 +00:00
[ '1.0.0-alpha' , '1.0.0-alpha.1' , - 1 ] ,
[ '1.0.0-alpha.1' , '1.0.0-alpha.beta' , - 1 ] ,
[ '1.0.0-alpha.beta' , '1.0.0-beta' , - 1 ] ,
[ '1.0.0-beta' , '1.0.0-beta.2' , - 1 ] ,
[ '1.0.0-beta.11' , '1.0.0-rc.1' , - 1 ] ,
[ '1.0.0-rc.1' , '1.0.0' , - 1 ]
2016-06-16 09:18:35 +00:00
] ;
describe ( 'Browser versions comparision' , function ( ) {
for ( g in comparisionsTasks ) {
var task = comparisionsTasks [ g ] ,
version = task [ 0 ] ,
version2 = task [ 1 ] ,
matching = task [ 2 ] === 0 ? ' == ' : ( task [ 2 ] > 0 ) ? ' > ' : ' < ' ;
it ( 'version ' + version + ' should be' + matching + 'version ' + version2 , function ( ) {
assert . equal ( browser . compareVersions ( [ version , version2 ] ) , task [ 2 ] ) ;
2016-06-16 10:41:53 +00:00
} ) ;
2016-06-16 09:18:35 +00:00
}
} ) ;
2016-06-16 13:02:07 +00:00
2017-08-26 04:23:35 +00:00
describe ( 'Unsupported browser check' , function ( ) {
2016-06-16 13:02:07 +00:00
2017-08-26 04:23:35 +00:00
before ( function ( ) {
2016-06-16 13:02:07 +00:00
this . ie10 _6 = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0" ;
} ) ;
2017-08-26 04:23:35 +00:00
it ( 'should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified' , function ( ) {
var unsupported = browser . isUnsupportedBrowser ( { msie : "10" } , this . ie10 _6 ) ;
2016-06-16 13:02:07 +00:00
assert . equal ( unsupported , false ) ;
} ) ;
2017-08-26 04:23:35 +00:00
it ( 'should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified in strict mode' , function ( ) {
var unsupported = browser . isUnsupportedBrowser ( { msie : "10" } , true , this . ie10 _6 ) ;
2016-07-06 21:01:02 +00:00
assert . equal ( unsupported , false ) ;
} ) ;
2017-08-26 04:23:35 +00:00
it ( 'should NOT be passed by #check for IE10.6 and for IE11 miminal version specified' , function ( ) {
var supported = browser . check ( { msie : "11" } , this . ie10 _6 ) ;
2016-06-16 13:02:07 +00:00
assert . equal ( supported , false ) ;
} ) ;
2017-08-26 04:23:35 +00:00
it ( 'should NOT be passed by #check for IE10.6 and for IE11 miminal version specified in strict mode' , function ( ) {
var supported = browser . check ( { msie : "11" } , true , this . ie10 _6 ) ;
2016-07-06 21:01:02 +00:00
assert . equal ( supported , false ) ;
} ) ;
2017-08-26 04:23:35 +00:00
it ( 'should throw an error when minVersion map has a number, but not a string' , function ( ) {
2016-10-31 09:07:15 +00:00
assert . throws ( ( ) => {
2017-08-26 04:23:35 +00:00
browser . check ( { msie : 11 } , this . ie10 _6 ) ;
2016-10-31 09:07:15 +00:00
} , /Browser version in the minVersion map should be a string/ ) ;
} ) ;
2017-08-26 04:23:35 +00:00
it ( 'should be passed by #check for IE10.6 when version was not specified' , function ( ) {
2016-06-16 13:02:07 +00:00
var supported = browser . check ( { } , this . ie10 _6 ) ;
assert . equal ( supported , true ) ;
} ) ;
2017-08-26 04:23:35 +00:00
it ( 'should NOT be passed by #check for IE10.6 when version was not specified in strict mode' , function ( ) {
2016-06-16 13:02:07 +00:00
var supported = browser . check ( { } , true , this . ie10 _6 ) ;
assert . equal ( supported , false ) ;
} ) ;
2017-08-26 04:23:35 +00:00
} ) ;
describe ( 'Android 8.0.0 OPR6 build type not confused for the Opera browser' , function ( ) {
before ( function ( ) {
this . android8 _opr6 _ua = "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 6P Build/OPR6.170623.013) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.107 Mobile Safari/537.36" ;
this . opera _ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 OPR/47.0.2631.55" ;
} ) ;
2016-06-16 13:02:07 +00:00
2017-08-26 04:23:35 +00:00
it ( 'Android 8 Chrome 60 user agent should be detected as Chrome 60' , function ( ) {
var flags = browser . _detect ( this . android8 _opr6 _ua ) ;
assert . equal ( flags . chrome , true ) ;
assert . equal ( flags . version , "60.0" ) ;
assert . equal ( flags . opera , undefined ) ;
} ) ;
it ( 'Opera user agent should be detected as Opera 47' , function ( ) {
var flags = browser . _detect ( this . opera _ua ) ;
assert . equal ( flags . opera , true ) ;
assert . equal ( flags . version , "47.0" ) ;
assert . equal ( flags . chrome , undefined ) ;
} ) ;
} ) ;