/*
* Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  test case
*
*/


#include <w32std.h>
#include <coecntrl.h>
#include <eikenv.h>
#include <ptikeymappings.h>
#include <ptikeymapdata.h>

#include "BCTestKeymap.h"
#include "BCTestPtiEngcontainer.h"
#include "BCTestPtiEng.hrh"

// ======== MEMBER FUNCTIONS ========

// ---------------------------------------------------------------------------
// Symbian 2nd static Constructor
// ---------------------------------------------------------------------------
//
CBCTestKeymap* CBCTestKeymap::NewL()
    {
    CBCTestKeymap* self = new( ELeave ) CBCTestKeymap();
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop( self );
    return self;
    }
    
// ---------------------------------------------------------------------------
// C++ default constructor
// ---------------------------------------------------------------------------
//
CBCTestKeymap::CBCTestKeymap():iMap(NULL)
    {    
    }
    
// ---------------------------------------------------------------------------
// Destructor
// ---------------------------------------------------------------------------
//
CBCTestKeymap::~CBCTestKeymap()
    {
    Teardown();
    }
    
// ---------------------------------------------------------------------------
// Symbian 2nd Constructor
// ---------------------------------------------------------------------------
//
void CBCTestKeymap::ConstructL()
    {    
    BuildScriptL();    
    }
    
// ---------------------------------------------------------------------------
// CBCTestKeymap::BuildScriptL
// ---------------------------------------------------------------------------
//
void CBCTestKeymap::BuildScriptL()
    { 
    // Add script as your need.   
    AddTestL( LeftCBA, 
        REP(Down, 2),
        KeyOK, TEND );    
    }
    
// ---------------------------------------------------------------------------
// CBCTestKeymap::RunL
// ---------------------------------------------------------------------------
//
void CBCTestKeymap::RunL( TInt aCmd )
    {
    if ( aCmd != EBCTestCmdPtiKeymap)
        {
        return;
        }
    
    TestFunctionL();
    }

// ---------------------------------------------------------------------------
// CBCTestKeymap::TestLayoutDecorator
// ---------------------------------------------------------------------------
//    
void CBCTestKeymap::TestFunctionL()
    {
    TestCreateL();
    SetupL();    
    if ( iMap )
    	{
        TestMapping();
        TestReplaceMappingL();
        TestReverseMapping();
        TestKeyData();
    	}
    }
    

void CBCTestKeymap::SetupL()
    {
    
    RArray<TInt> dataImpl;
    CPtiKeyMapDataFactory::ListImplementationsL(dataImpl);

    TUid uid = TUid::Uid(dataImpl[0]);
    CPtiKeyMapDataFactory* keymapDatafactory =
    CPtiKeyMapDataFactory::CreateImplementationL(uid);
    CleanupStack::PushL(keymapDatafactory);

    dataImpl.Close();

    iKeymapData =static_cast<CPtiKeyMapData*>(
    keymapDatafactory->KeyMapDataForLanguageL(01));
    iMap = CPtiKeyMappings::NewL(iKeymapData);

    CleanupStack::PopAndDestroy(keymapDatafactory); //keymapDatafactory     
    AssertTrueL(ETrue, _L("CPtiKeyMappings::NewL()"));    
    }

void CBCTestKeymap::Teardown()
    {
    delete iMap;
	iMap = NULL;
	delete iKeymapData;
	iKeymapData = NULL;	
    }

void CBCTestKeymap::TestCreateL()
    {
    }

void CBCTestKeymap::TestMapping()
    {
    TestStartMapping();
    TestNextKey();
    }

void CBCTestKeymap::TestStartMapping()
    {
    AssertIntL(
        TUint('a'), iMap->StartMapping(/*TPtiKey::*/EPtiKey2, /*TPtiTextCase::*/EPtiCaseLower),
        _L("press 2 got 'a'"));
    AssertIntL(
        TUint('A'), iMap->StartMapping(/*TPtiKey::*/EPtiKey2, /*TPtiTextCase::*/EPtiCaseUpper),
        _L("press Shift 2 got 'A'"));
    }

void CBCTestKeymap::TestNextKey()
    {
    TBool isAppend = ETrue;
    iMap->StartMapping(EPtiKey3, EPtiCaseLower);
    AssertIntL( TUint16('e'), iMap->NextKey(EPtiKey3, isAppend, EPtiCaseLower),
        _L("press 3, press 3 got 'e'"));

    AssertTrueL( !isAppend, _L("and 'e' is not append"));

    AssertIntL( TUint16('F'), iMap->NextKey(EPtiKey3, isAppend, EPtiCaseUpper),
        _L("then, press Shift 3 got 'F'"));

    AssertIntL( TUint16('g'), iMap->NextKey(EPtiKey4, isAppend, EPtiCaseLower),
        _L("then press 4 as next key, got 'g'"));
    }

void CBCTestKeymap::TestReplaceMappingL()
    {
    TBool isAppend = ETrue;
    _LIT(KNewMapLower, "!@#$");
    _LIT(KNewMapUpper, "%:=&");
    TBuf<5> lowerMap(KNewMapLower);
    TBuf<5> upperMap(KNewMapUpper);

    AssertIntL( KErrNone, iMap->ReplaceKeyMapL(EPtiKey1, lowerMap, EPtiCaseLower),
        _L("Lower case mapping for Key 1 change to !@#$"));
    AssertIntL( KErrNone, iMap->ReplaceKeyMapL(EPtiKey1, upperMap, EPtiCaseUpper),
        _L("Upper case mapping for Key 1 change to %:=&"));
    
    iMap->StartMapping(EPtiKey1, EPtiCaseLower);
    AssertTrueL(ETrue,_L("press 1 got '!'"));
    
    iMap->NextKey(EPtiKey1, isAppend, EPtiCaseLower);
    AssertTrueL(ETrue,_L("press 1, again got '@'"));
    
    iMap->NextKey(EPtiKey1, isAppend, EPtiCaseUpper);
    AssertTrueL(ETrue,_L("then, press Shift 1 got '@'"));
    
    iMap->NextKey(EPtiKey2, isAppend, EPtiCaseLower);
    AssertTrueL(ETrue,_L("then press 2 as next key, got 'a'"));
    }

void CBCTestKeymap::TestReverseMapping()
    {
    AssertIntL( EPtiKey2, iMap->KeyForCharacter('c'), 
        _L("'c' is mapped by key 2"));
    AssertIntL( EPtiKey9, iMap->KeyForCharacter('X'),
        _L("'X' is mapped by key 9"));
    }

void CBCTestKeymap::TestKeyData()
    {
    TBuf<8> mapStr;

    iMap->GetDataForKey(EPtiKey1, mapStr, EPtiCaseLower);
    AssertTrueL( ETrue, _L("Lower case data for key 1 is !@#$"));

    iMap->GetDataForKey(EPtiKey1, mapStr, EPtiCaseUpper);
    AssertTrueL( ETrue, _L("Upper case data for key 1 is %:=&"));
    }

//static
MPtiKeyMappings* CBCTestKeymap::CreateKeyMapL()
    {
    return NULL;
    }
