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


function testSimpleCall(){
    var numberToCall = "8005551212";

    var showActiveCall_orig = Mojo.Controller.getAppController().assistant.showActiveCall;
    var numberCalled = undefined;
    Mojo.Controller.getAppController().assistant.showActiveCall = function(number, pauseWaitDigits){
        numberCalled = number;
    }

    new DialpadAssistant(numberToCall);
    assertEquals(numberToCall, numberCalled);
    
    Mojo.Controller.getAppController().assistant.showActiveCall = showActiveCall_orig;
}

function testCallVoicemail(){
    var vmNumber = "1231231234";
    
    var vmq_orig = TelephonyCommands.voicemailNumberQuery;
    TelephonyCommands.voicemailNumberQuery = function(callback){
        callback(true, vmNumber);
    }

    // simulate launcher voicemail action (need "voicemail" query param)
    var tqp_orig = String.prototype.toQueryParams;
    String.prototype.toQueryParams = function(separator){
        return {"voicemail": "something"};
    }

    var showActiveCall_orig = Mojo.Controller.getAppController().assistant.showActiveCall;
    var numberCalled = undefined;
    Mojo.Controller.getAppController().assistant.showActiveCall = function(number, pauseWaitDigits){
        numberCalled = number;
    }

    new DialpadAssistant(undefined);    // voicemail number is not provided here
    assertEquals(vmNumber, numberCalled);
    
    TelephonyCommands.voicemailNumberQuery = vmq_orig;
    String.prototype.toQueryParams = tqp_orig;
    Mojo.Controller.getAppController().assistant.showActiveCall = showActiveCall_orig;
}


function testActionHandlingCDMA(){
    var pushScene_orig = Mojo.Controller.stageController.pushScene;
    var actionMsg = undefined;
    Mojo.Controller.stageController.pushScene = function(msg){
        actionMsg = msg;
    }

    var isGSM_orig = PlatformType.isGSM;
    PlatformType.isGSM = function(){
        return false;
    }
    
    var showProgrammingDialog_orig = DialpadAssistant.prototype.showProgrammingDialog;
    DialpadAssistant.prototype.showProgrammingDialog = function(showPrev, showAkey){
        actionMsg = showPrev.toString() + showAkey.toString();
    }
    
    var showActivationDialog_orig = DialpadAssistant.prototype.showActivationDialog;
    DialpadAssistant.prototype.showActivationDialog = function(showPrev, showAkey){
        actionMsg = "activation dialog shown";
    }
    
    // make any 6-digit MSL URL work
    var verifyMsl_orig = TelephonyCommands.verifyMsl;
    TelephonyCommands.verifyMsl = function(url, callback){
        callback(true);
    }

    var cdmaUrls = [
        [DialStringParser.PP_TTY, "poundTTY"],
        [DialStringParser.PP_AKEY, "falsetrue"],
        [DialStringParser.PP_EVDO, "poundEvdo"],
        [DialStringParser.PP_PREV, "truefalse"],
        [DialStringParser.PP_GPS, "poundGPS"],
        ["000000", "activation dialog shown"]  // MSL (any 6 digit URL will work for this test)
    ];

    for (var i = 0; i < cdmaUrls.length; i++) {
        new DialpadAssistant("##" + cdmaUrls[i][0] + "#");
        assertEquals(cdmaUrls[i][0], actionMsg, cdmaUrls[i][1]);
        actionMsg = undefined;
    }

    DialpadAssistant.prototype.showProgrammingDialog = showProgrammingDialog_orig;
    DialpadAssistant.prototype.showActivationDialog = showActivationDialog_orig;
    TelephonyCommands.verifyMsl = verifyMsl_orig;   
    PlatformType.isGSM = isGSM_orig;
    Mojo.Controller.stageController.pushScene = pushScene_orig;
}

function testActionHandlingGSM(){
    var pushScene_orig = Mojo.Controller.stageController.pushScene;
    var actionMsg = undefined;
    Mojo.Controller.stageController.pushScene = function(msg){
        actionMsg = msg;
    }

    var isGSM_orig = PlatformType.isGSM;
    PlatformType.isGSM = function(){
        return true;
    }
    
    var showMmiPending_orig = DialpadAssistant.prototype.showMmiPending;
    var showMmiPending_called = undefined;
    DialpadAssistant.prototype.showMmiPending = function(){
        showMmiPending_called = true;
    }
    
    var mmiDialString = "**04*0*1*1*#";    // change pin1 from 0 to 1
    new DialpadAssistant(mmiDialString);
    assertTrue(showMmiPending_called);    
    
    PlatformType.isGSM = isGSM_orig;
    Mojo.Controller.stageController.pushScene = pushScene_orig;
}

function testDialFailure(){
    var numberToCall = "8005551212";    // we'll force this to fail by overriding TelephonyCommands.dial()

    var showActiveCall_orig = Mojo.Controller.getAppController().assistant.showActiveCall;
    var numberCalled = "";
    Mojo.Controller.getAppController().assistant.showActiveCall = function(number, pauseWaitDigits){
        numberCalled = number;
    }
       
    var showScene_orig = WindowLauncher.showScene;
    var failureMsg = "";
    var showScene_called = false;
    WindowLauncher.showScene = function(msg0, msg1){
        showScene_called = true;
        if (failureMsg[0] && failureMsg[1]) {
            assertEquals(failureMsg[0], msg0);
            assertEquals(failureMsg[1], msg1);
        }
        else{
            fail("showScene should not be called when failureMsg[0,1] are undefined");
        }
    }
    
    var announceDroppedCall_orig = Mojo.Controller.getAppController().assistant.announcer.announceDroppedCall;
    var announceDroppedCall_msg = "";
    Mojo.Controller.getAppController().assistant.announcer.announceDroppedCall = function(msg){
        announceDroppedCall_msg = msg;
    }
    
    var dial_orig = TelephonyCommands.dial;
    var resultObject = "";
    TelephonyCommands.dial = function(number, blockId, failureCb){
        failureCb(resultObject);  // force a failure
    }
    
    var resultObjects = [
        {"errorCode": 7},
        {"errorCode": 8},
        {"errorText": "blahblah"}
    ];
    var failureMsgs = [
        ["puk1LockWindow", "pin1lock"],
        ["puk1LockWindow", "puk1lock"],
        [resultObjects[2].errorText, undefined]
    ];
    assertEquals(resultObjects.length, failureMsgs.length); // just ensure these are the same length for the for-loop

    for (var i = 0; i < resultObjects.length; i++) {
        resultObject = resultObjects[i];
        failureMsg = failureMsgs[i];
        new DialpadAssistant(numberToCall);
        assertEquals(numberToCall, numberCalled);
        if (failureMsg[0] && failureMsg[1]) {   // showScene is called
            assertTrue(showScene_called);
        }
        else{                                   // announceDroppedCall is called
            assertTrue(announceDroppedCall_msg.include(failureMsg[0]));
        }
        numberCalled = "";
        showScene_called = false;
        announceDroppedCall_msg = "";
    }

    Mojo.Controller.getAppController().assistant.showActiveCall = showActiveCall_orig;
    WindowLauncher.showScene = showScene_orig;
    Mojo.Controller.getAppController().assistant.announcer.announceDroppedCall = announceDroppedCall_orig;
    TelephonyCommands.dial = dial_orig;
}














