/* Copyright 2009 Palm, Inc.  All rights reserved. */


const GSM = true;
const CDMA = false;
const regularNumber = "8005551212";

DialStringParser.interpretDialStringNoBounce = function(dialString, isGSM, dialFailCb, messageCb) {
	var ret = DialStringParser.interpretDialString(dialString, isGSM, dialFailCb, messageCb);
	DialStringParser.clearDebounceTimeout();
	return ret;
}

function DialStringParserTests() {
		
}

//dialString, isGSM, dialFailCb, messageCb

DialStringParserTests.prototype.testUndefinedNumber = function() {
    var result = DialStringParser.interpretDialStringNoBounce(null, GSM);
    Mojo.requireEqual(result.action, DialAction.UNKNOWN);
	return Mojo.Test.passed;
}

DialStringParserTests.prototype.testRegularNumber = function() {
    var dial_orig = TelephonyCommands.dial;
    var numberDialed;
    TelephonyCommands.dial = function(dialString, y, z) {numberDialed = dialString; };
	
	// GSM
    var result = DialStringParser.interpretDialStringNoBounce(regularNumber, GSM);
    Mojo.requireEqual(result.action, DialAction.DIAL, "Regular number action GSM case failed");
    Mojo.requireEqual(regularNumber, numberDialed, "Regular number dialed GSM case failed");
	
	// CDMA
	result = DialStringParser.interpretDialStringNoBounce(regularNumber, CDMA);
    Mojo.requireEqual(result.action, DialAction.DIAL, "Regular number action CDMA case failed");
    Mojo.requireEqual(regularNumber, numberDialed, "Regular number dialed CDMA case failed");
    
    TelephonyCommands.dial = dial_orig;
	return Mojo.Test.passed;
}

DialStringParserTests.prototype.testEmergencyNumber = function() {
    var dial_orig = TelephonyCommands.dial;
    TelephonyCommands.dial = function(dialString, y, z){ numberDialed = dialString; };
    var numberDialed;
	
	Mojo.Log.error("emer numbers %j",PlatformType.getListofEmergencyNumbers());
	
    var result = DialStringParser.interpretDialStringNoBounce("911", true);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, "911");
    
    TelephonyCommands.dial = dial_orig;
	return Mojo.Test.passed;
}

DialStringParserTests.prototype.testCDMAPrefixes = function() {
    var dial_orig = TelephonyCommands.dial;
    TelephonyCommands.dial = function(dialString, y, z){ numberDialed = dialString; };
	
	var codes = $H(DialStringParser.launchcodesCDMA);
	codes = codes.merge(DialStringParser.launchcodesCommon);
	
	codes.each(function(i){
		var url, result;
		
		// VALID
		url = "##" + i.key + "#"
		result = DialStringParser.interpretDialStringNoBounce(url, CDMA);
        Mojo.requireEqual(result.action, i.value, url + " failed");
		
		url = "#*#" + i.key + "#"
		result = DialStringParser.interpretDialStringNoBounce(url, CDMA);
        Mojo.requireEqual(result.action, i.value, url + " failed");
		
		url = "##" + i.key + ""
		result = DialStringParser.interpretDialStringNoBounce(url, CDMA);
        Mojo.requireEqual(result.action, i.value, url + " failed");
		
		// INVALID
		url = "#*" + i.key + "#"
		result = DialStringParser.interpretDialStringNoBounce(url, CDMA);
        Mojo.requireEqual(result.action, DialAction.DIAL, url + " failed");
		
		url = "##" + i.key + "*"
		result = DialStringParser.interpretDialStringNoBounce(url, CDMA);
        Mojo.requireEqual(result.action, DialAction.UNKNOWN, url + " failed");
	});
	
	TelephonyCommands.dial = dial_orig;
	return Mojo.Test.passed;
}

DialStringParserTests.prototype.testCdmaRegularNumber = function() {
    // Any dialed number that doesn't match ##X# or #*X#, where X is [0-9]*, should be dialed.

    // Dialed numbers whose length >= 3 or entirely contain [0-9\+]* are handled (dialed) by the "Optimization" block just before the CDMA (!isGSM) block in DialStringParser.js
    // Therefore, this should test other variations.
    
    var dial_orig = TelephonyCommands.dial;
    var numberDialed;
    TelephonyCommands.dial = function(dialString, y, z){ numberDialed = dialString; };
		
	// '00' - '09'
	for (var n = 0; n <= 9; n++) {
        var dialNumber = n.toPaddedString(2);
        var result = DialStringParser.interpretDialStringNoBounce(dialNumber, CDMA, null, null, null);
        Mojo.requireEqual(result.action, DialAction.DIAL, dialNumber + " action");
        Mojo.requireEqual(numberDialed, dialNumber, dialNumber);
    }
	
	// '1'
	var result = DialStringParser.interpretDialStringNoBounce("1", CDMA, null, null, null);
    Mojo.requireEqual(result.action, DialAction.VOICEMAIL, dialNumber + " action");
    Mojo.requireEqual(numberDialed, dialNumber, dialNumber);
	
	// '2' - '99'
    for (var n = 2; n <= 99; n++) {
        var dialNumber = n.toString();
        var result = DialStringParser.interpretDialStringNoBounce(dialNumber, CDMA, null, null, null);
        Mojo.requireEqual(result.action, DialAction.DIAL, dialNumber + " action");
        Mojo.requireEqual(numberDialed, dialNumber, dialNumber);
    }
	
    // whitespace
    var result = DialStringParser.interpretDialStringNoBounce("800 555 1212", CDMA, null, null, null);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, "800 555 1212", numberDialed);
    
    // dashes
    numberDialed = "";
    result = DialStringParser.interpretDialStringNoBounce("800-555-1212", CDMA, null, null, null);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, "800-555-1212", numberDialed);
    
    // letters
    numberDialed = "";
    result = DialStringParser.interpretDialStringNoBounce("800 JKL 1212", CDMA, null, null, null);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, "800 JKL 1212", numberDialed);
	
    TelephonyCommands.dial = dial_orig;
	return Mojo.Test.passed;
}


DialStringParserTests.prototype.testGsmShortActiveCall = function() {

    var disconnectAllHeld_orig = TelephonyCommands.disconnectAllHeld;
    var disconnectAllActive_orig = TelephonyCommands.disconnectAllActive;
    var swap_orig = TelephonyCommands.swap;
    var merge_orig = TelephonyCommands.merge;
    var sendUSSD_orig = TelephonyCommands.sendUSSD;
    var disconnectCallMmi_orig = TelephonyCommands.disconnectCallMmi;
    var extractMmi_orig = TelephonyCommands.extractMmi;
    var isCallConnected_orig = DialStringParser.isCallConnected;

    var called_disconnectAllHeld = false;
    TelephonyCommands.disconnectAllHeld = function(){ called_disconnectAllHeld = true; };
    var called_disconnectAllActive = false;
    TelephonyCommands.disconnectAllActive = function(){ called_disconnectAllActive = true; };
    var called_swap = false;
    TelephonyCommands.swap = function(){ called_swap = true; };
    var called_merge = false;
    TelephonyCommands.merge = function(){ called_merge = true; };
    var called_sendUSSD = false;
    TelephonyCommands.sendUSSD = function(x, y){ called_sendUSSD = true; };
    var called_disconnectCallMmi = false;
    TelephonyCommands.disconnectCallMmi = function(x){ called_disconnectCallMmi = true; };
    var called_extractMmi = false;
    TelephonyCommands.extractMmi = function(x){ called_extractMmi = true; };
    DialStringParser.isCallConnected = function(){ return true; };  // simulate call in-progress

    // 0, 1, 2, 3, 4 (default case)

    var result = DialStringParser.interpretDialStringNoBounce("0", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.INCALLMMI);
    Mojo.require(called_disconnectAllHeld);

    result = DialStringParser.interpretDialStringNoBounce("1", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.INCALLMMI);
    Mojo.require(called_disconnectAllActive);

    result = DialStringParser.interpretDialStringNoBounce("2", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.INCALLMMI);
    Mojo.require(called_swap);

    result = DialStringParser.interpretDialStringNoBounce("3", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.INCALLMMI);
    Mojo.require(called_merge);

    result = DialStringParser.interpretDialStringNoBounce("4", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.MMI);
    Mojo.require(called_sendUSSD);

    // 1x, 2x, 3x (default case); x is any single digit
    
    result = DialStringParser.interpretDialStringNoBounce("10", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.INCALLMMI);
    Mojo.require(called_disconnectCallMmi);

    result = DialStringParser.interpretDialStringNoBounce("20", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.INCALLMMI);
    Mojo.require(called_extractMmi);

    called_sendUSSD = false;
    result = DialStringParser.interpretDialStringNoBounce("30", GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.MMI);
    Mojo.require(called_sendUSSD); 

    TelephonyCommands.disconnectAllHeld = disconnectAllHeld_orig;
    TelephonyCommands.disconnectAllActive = disconnectAllActive_orig;
    TelephonyCommands.swap = swap_orig;
    TelephonyCommands.merge = merge_orig;
    TelephonyCommands.sendUSSD = sendUSSD_orig;
    TelephonyCommands.disconnectCallMmi = disconnectCallMmi_orig;
    TelephonyCommands.extractMmi = extractMmi_orig;
    DialStringParser.isCallConnected = isCallConnected_orig;

	return Mojo.Test.passed;
}


//*31#<number><SEND> to supress CLIR or #31#<number><SEND> to invoke CLIR
DialStringParserTests.prototype.testGsmClir = function() {
    var dial_orig = TelephonyCommands.dial;
    
    var numberDialed = "";
    TelephonyCommands.dial = function(dialString, y, z){ numberDialed = dialString; };
    
    var result = DialStringParser.interpretDialStringNoBounce("*31#" + regularNumber, GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, regularNumber, numberDialed);

    result = DialStringParser.interpretDialStringNoBounce("#31#" + regularNumber, GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, regularNumber, numberDialed);

    TelephonyCommands.dial = dial_orig;

	return Mojo.Test.passed;
}


DialStringParserTests.prototype.testGsmParsedNumber = function() {
    var dial_orig = TelephonyCommands.dial;
    
    var numberDialed = "";
    TelephonyCommands.dial = function(dialString, y, z){ numberDialed = dialString; };
    
    var number = "800-555-1212";
    var result = DialStringParser.interpretDialStringNoBounce(number, GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, number, numberDialed);
	
    var number = "+18005551212";
    var extraDigits = "p1";
    numberDialed = "";  
    var result = DialStringParser.interpretDialStringNoBounce(number + extraDigits, GSM, null, null, null);
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(result.pauseWaitDigits, extraDigits);
    Mojo.requireEqual(numberDialed, number, numberDialed);

    TelephonyCommands.dial = dial_orig;  

	return Mojo.Test.passed;  
}


DialStringParserTests.prototype.testGsmParsedAction = function() {
    var messageCb_msg = undefined;
    var messageCb = function(msg){
        messageCb_msg = msg;
    }
    
    // mmi responses for changing pin1
    var testData = [
        {pair: {"returnValue": "true"}, msg: "PIN change successful."},
        {pair: {"errorCode": 5}, msg: "PIN must be enabled before change is allowed."},
        {pair: {"attemptsRemaining": "7"}, msg: "Unable to change PIN. 7 attempts remaining."},
        {pair: {"serviceCode": "04"}, msg: "Unable to change PIN."},
        {pair: {"serviceCode": "lidvrtnhle"}, msg: undefined}
    ];
	
    var prefixes = ["*", "#", "**", "##", "*#"];
    var suffix = "04*0*1*1*#";    // change pin1 from 0 to 1
    
    lsr_orig = Mojo.Service.Request;
    
    for (var i=0; i < testData.length; i++){
        var pair = testData[i].pair;
        var msg = testData[i].msg;

        Mojo.Service.Request = Class.create({
            initialize: function(url, options) {
                options.onSuccess(pair);
            },
        });
        
        for (var j = 0; j < prefixes.length; j++) {
            var result = DialStringParser.interpretDialStringNoBounce(prefixes[j] + suffix, GSM, null, messageCb, null);
			
            if (prefixes[j] == "**") { // only register (**) will work with svc code 04
                Mojo.requireEqual(result.action, DialAction.MMI);
                Mojo.requireEqual(messageCb_msg, msg);
            }
            else {
                Mojo.requireEqual(result.action, DialAction.UNKNOWN);
                Mojo.require(messageCb_msg == undefined);
            }
			
            messageCb_msg = undefined;
        }
    }
    
    Mojo.Service.Request = lsr_orig;
	
	return Mojo.Test.passed;
}


// ensure CDMA codes are not processed by GSM, and vice versa
DialStringParserTests.prototype.testCrossNetworkCodes = function() {
    var dial_orig = TelephonyCommands.dial;
    var numberDialed = undefined;
    TelephonyCommands.dial = function(dialString, y, z){ numberDialed = dialString; };

    // GSM code, CDMA network
    var number = "**04*0*1*1*#";    // GSM: change pin1 from 0 to 1   
    var result = DialStringParser.interpretDialStringNoBounce(number, CDMA, null, null, null);    
    Mojo.requireEqual(result.action, DialAction.DIAL);
    Mojo.requireEqual(numberDialed, number, numberDialed);
    
    // CDMA code, GSM network
    var verifyMsl_orig = TelephonyCommands.verifyMsl;
    var verifyMsl_called = false;
    TelephonyCommands.verifyMsl = function(x, y) {
        verifyMsl_called = true;
    }
    var sendUSSD_orig = TelephonyCommands.sendUSSD;
    var sendUSSD_dialString = undefined;
    TelephonyCommands.sendUSSD = function(dialString, messageCb){
        sendUSSD_dialString = dialString;
    }
    var cdmaUrls = [
        DialAction.VPON,
        DialAction.VPOFF,
        DialAction.TTY,
        DialAction.AKEY,
        DialAction.EVDO,
        DialAction.PREV,
        DialAction.GPS
    ];
    var prefixes = ["##", "#*#"];
    numberDialed = undefined;
    for (var i = 0; i < cdmaUrls.length; i++) {
        for (var j = 0; j < prefixes.length; j++) {
            var url = prefixes[j] + cdmaUrls[i] + "#";
            var result = DialStringParser.interpretDialStringNoBounce(url, GSM, null, null, null);
            Mojo.requireFalse(verifyMsl_called);
            if (sendUSSD_dialString){
                Mojo.requireEqual(result.action, DialAction.MMI, "1: " + url);
                Mojo.requireEqual(sendUSSD_dialString, url, "2: " + url);
            }
            else{
                Mojo.requireEqual(result.action, DialAction.DIAL, "3: " + url);
                Mojo.requireEqual(numberDialed, url, "4: " + url);
            }            
            sendUSSD_dialString = undefined;
        }
    }
    
    TelephonyCommands.dial = dial_orig;
    TelephonyCommands.verifyMsl = verifyMsl_orig;
    TelephonyCommands.sendUSSD = sendUSSD_orig;

	return Mojo.Test.passed;
}


DialStringParserTests.prototype.testGsmUSSDBypass = function() {
	
	var gsmna_orig = PlatformType.isGSMNorthAmerican;
	var dial_orig = TelephonyCommands.dial;
	
	// helper
	function runtests_with_expected(expectedresult) {	
		var result = DialStringParser.interpretDialStringNoBounce("0", GSM);
	    Mojo.requireEqual(result.action, expectedresult, "0");
		
	    result = DialStringParser.interpretDialStringNoBounce("00", GSM);
	    Mojo.requireEqual(result.action, expectedresult, "00");
				
	    result = DialStringParser.interpretDialStringNoBounce("*0", GSM);
	    Mojo.requireEqual(result.action, expectedresult, "*0");
		
		result = DialStringParser.interpretDialStringNoBounce("#0", GSM);
	    Mojo.requireEqual(result.action, expectedresult, "#0");
		
		result = DialStringParser.interpretDialStringNoBounce("*8", GSM);
	    Mojo.requireEqual(result.action, expectedresult, "*8");
		
	}
	
	TelephonyCommands.dial = Mojo.doNothing;
	PlatformType.isGSMNorthAmerican = function() { return true; };
	
	runtests_with_expected(DialAction.DIAL);
	
	PlatformType.isGSMNorthAmerican = function() { return false; };
	
	runtests_with_expected(DialAction.MMI);
	
	PlatformType.isGSMNorthAmerican = gsmna_orig;
	TelephonyCommands.dial = dial_orig;    
	return Mojo.Test.passed;
}

