describe('DeviceDetector', function(){

    var Request = require('../../lib/net/Request.js').Request;
    var DeviceDetector = null;

    it('require DeviceDetector', function(){
    	DeviceDetector = require('../../lib/util/DeviceDetector.js').DeviceDetector;
    	expect(DeviceDetector).toBeTruthy();

        beforeEach(function(){
            DeviceDetector = require('../../lib/util/DeviceDetector.js').DeviceDetector;
        });
	});

    it('getUserAgent #1', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('USER-AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });

    it('getUserAgent #2', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('USER-AGENT', 'XXX', true);
        req.setHeader('X-OPERAMINI-PHONE-UA', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });

    it('getUserAgent #3', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('USER-AGENT', 'XXX', true);
        req.setHeader('X-SKYFIRE-PHONE', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });

    it('getUserAgent #3', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('USER-AGENT', 'XXX', true);
        req.setHeader('X-SKYFIRE-PHONE', 'XXX', true);
        req.setHeader('X-OPERAMINI-PHONE-UA', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });

    it('getUserAgent #4', function () {
        var req = new Request();
        expect(DeviceDetector.getUserAgent(req)).toBeFalsy();
    });

    it('isFromDesktop #1', function () {
        var req = new Request();
        // firefox
        var ua = 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0';
        req.setHeader('USER-AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
    });

    it('isFromDesktop #2', function () {
        var req = new Request();
        // iphone
        var ua = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16';
        req.setHeader('USER-AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
    });

    it('isFromDesktop #3', function () {
        var req = new Request();
        // googlebot
        var ua = 'Googlebot/2.1 ( http://www.googlebot.com/bot.html)';
        req.setHeader('USER-AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.BOT);
    });

    it('isFromDesktop List', function () {
        var mobileUA = require('fs').readFileSync('./tests/util/devices.txt', 'utf8').split('\n');
        var fails = 0;
        for(var i=0,max=mobileUA.length;i<max;i++) {
            var req = new Request();
            req.setHeader('USER-AGENT', mobileUA[i], true);
            expect(DeviceDetector.getUserAgent(req)).toBe(mobileUA[i]);
            if (DeviceDetector.isDesktop(req) == true) {
                //console.log('FALSE POSITIVE: ' + mobileUA[i]);
                fails += 1;
            }
        }
        console.log('\nUA MISC DETECT FAILS: ' + fails + '/' + mobileUA.length, ((fails/mobileUA.length)*100) + '%');

    });


    it('isFromDesktop WURFL', function () {
        var mobileUA = require('fs').readFileSync('./tests/util/wurfl-2.3.3.xml', 'utf8');
        var regexp = /user_agent="(.*?)"/gi
        var result = mobileUA.match(regexp);
        var fails = 0;
        var count = 0;

        for(var i=0,max=result.length;i<max;i++) {
            // nie matchujemy genericow
            if (result[i].indexOf('DO_NOT_MATCH') > -1) {
                continue;
            }
            count += 1;
            var req = new Request();
            req.setHeader('USER-AGENT', result[i], true);
            expect(DeviceDetector.getUserAgent(req)).toBe(result[i]);
            if (DeviceDetector.isDesktop(req) == true) {
                //console.log('FALSE POSITIVE: ' + result[i]);
                fails += 1;
            }
        }
        expect(fails).toBe(237);
        expect(count).toBe(17729);

        console.log('\nUA MOBILE DETECT FAILS: ' + fails + '/' + count, Math.round((fails/count)*10000)/100 + '%');
    });

    it('isFromTablet WURFL', function () {
        var mobileUA = require('fs').readFileSync('./tests/util/wurfl-2.3.3.xml', 'utf8');
        var regexp = /user_agent="(.*?)"/gi
        var result = mobileUA.match(regexp);
        var fails = 0;
        var count = 0;

        for(var i=0,max=result.length;i<max;i++) {
            // nie matchujemy genericow
            if (result[i].indexOf('DO_NOT_MATCH') > -1) {
                continue;
            }
            count += 1;
            var req = new Request();
            req.setHeader('USER-AGENT', result[i], true);
            expect(DeviceDetector.getUserAgent(req)).toBe(result[i]);
            if (DeviceDetector.isTablet(req) == true) {
                //console.log('FALSE POSITIVE: ' + result[i]);
                fails += 1;
            }
        }
        expect(fails).toBe(405);
        expect(count).toBe(17729);

        console.log('\nUA TABLET DETECT FAILS: ' + fails + '/' + count, Math.round((fails/count)*10000)/100 + '%');
    });

    it('isFromDesktop Browser Detect', function () {
        var mobileUA = require('fs').readFileSync('./tests/util/useragentstring.browserlist.txt', 'utf8');
        var regexp = /<div id='liste'>([\s\S]+)<\/div>/gi;
        var uaRegexp = /<a.*?>(.*?)<\/a>/gi;
        var uaRegexpSingle = /<a.*?>(.*?)<\/a>/i;
        var result = mobileUA.match(regexp)
        result = result[0].match(uaRegexp);

        var fails = 0;
        var count = 0;

        for(var i=0,max=result.length;i<max;i++) {
            result[i] = result[i].match(uaRegexpSingle)[1];

            // nie matchujemy genericow
            if (result[i].indexOf('More ') == 0) {
                continue;
            }

            count += 1;
            var req = new Request();

            req.setHeader('USER-AGENT', result[i], true);
            expect(DeviceDetector.getUserAgent(req)).toBe(result[i]);
            if (DeviceDetector.isDesktop(req) == false) {
                //console.log('FALSE POSITIVE: ' + result[i]);
                fails += 1;
            }
        }
        console.log('\nUA BIG BROWSERS FAILS: ' + fails + '/' + count, Math.round((fails/count)*10000)/100 + '%');
    });

    it('Google - shoud be DESKTOP/BOT', function () {
        var req = new Request();
        var ua = 'MediapartnersGoogle';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
        expect(DeviceDetector.isBot(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.BOT);
    });

    it('cURL - shoud be DESKTOP/BOT', function () {
        var req = new Request();
        var ua = 'Perl interface for libcURL';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
        expect(DeviceDetector.isBot(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.BOT);
    });

    it('libWWW - shoud be DESKTOP/BOT', function () {
        var req = new Request();
        var ua = 'libwww-perl/5.834';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
        expect(DeviceDetector.isBot(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.BOT);
    });

    it('Windows Phone - shoud be MOBILE', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 800) ';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

    it('iPad 1 - shoud be TABLET', function() {
        var req = new Request();
        var ua = 'Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.isTablet(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

    it('iPad 2 - shoud be TABLET', function() {
        var req = new Request();
        var ua = 'Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.0.2 Mobile/9A5248d Safari/6533.18.5';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.isTablet(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

    it('iPad 3 - shoud be TABLET', function() {
        var req = new Request();
        var ua = 'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.isTablet(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

    it('iPad Mini - shoud be TABLET', function() {
        var req = new Request();
        var ua = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A406 Safari/8536.25';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.isTablet(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

    it('Motorola Xoom - should be TABLET', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.isTablet(req)).toBe(true);
        expect(DeviceDetector.isMobile(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

    it('HTC Desire X - should be MOBILE', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Linux; Android 4.0.4; HTC Desire X Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.isTablet(req)).toBe(false);
        expect(DeviceDetector.isMobile(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

    it('Kindle Fire - shoud be MOBILE', function() {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Kindle Fire Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

    it('Kindle Fire in Silk mode - shoud be MOBILE', function() {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

    it('monochrome Kindle - shoud be MOBILE', function() {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Linux; U; en-US) AppleWebKit/528.5+ (KHTML, like Gecko, Safari/528.5+) Version/4.0 Kindle/3.0 (screen 600×800; rotate)';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

    it('Chromebook - should be DESKTOP', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (X11; CrOS i686 1193.117.0) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.66 Safari/535.7';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.DESKTOP);
    });


	it('Opera Mini iPadV1 - should be Tablet', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (iPad; U; CPU iPhone OS 5_1_1 like Mac OS X; pl)';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
		expect(DeviceDetector.isTablet(req)).toBe(true);

		//expect(DeviceDetector.isMobile(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

	it('Samsung Galaxy Tab 10.1 - should be Tablet', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; sdk Build/HONEYCOMB) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
		expect(DeviceDetector.isTablet(req)).toBe(true);

		//expect(DeviceDetector.isMobile(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

	it('Firefox Tablet - should be Tablet', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Android; Tablet; rv:13.0) Gecko/13.0 Firefox/13.0';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
		expect(DeviceDetector.isTablet(req)).toBe(true);
		//expect(DeviceDetector.isMobile(req)).toBe(false);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.TABLET);
    });

	it('Firefox Mobile - should be Mobile', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
		expect(DeviceDetector.isTablet(req)).toBe(false);
		expect(DeviceDetector.isMobile(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

	it('Opera Mobile on HTC Desire X - should be Mobile', function () {
        var req = new Request();
        var ua = 'Opera/9.80 (Android 4.0.4; Linux; Opera Mobi/ADR-1301080958) Presto/2.11.355 Version/12.10';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
		expect(DeviceDetector.isTablet(req)).toBe(false);
		expect(DeviceDetector.isMobile(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

        it('BlackBerry 10 - should be Mobile', function () {
        var req = new Request();
        var ua = 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
		expect(DeviceDetector.isTablet(req)).toBe(false);
		expect(DeviceDetector.isMobile(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE);
    });

        it('GoogleBot Mobile - should be Mobile bot', function () {
        var req = new Request();
        var ua = 'SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)';
        req.setHeader('User-Agent', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
		expect(DeviceDetector.isTablet(req)).toBe(false);
		expect(DeviceDetector.isMobile(req)).toBe(true);
        expect(DeviceDetector.getNormalizedUserAgent(req)).toBe(DeviceDetector.NUA.MOBILE_BOT);
    });

});

