idlefw/tsrc/utility/mt_aiutils/MT_AiStrParser.cpp
branchRCL_3
changeset 27 2c7f27287390
equal deleted inserted replaced
25:9e077f9a342c 27:2c7f27287390
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  CLASS HEADER
       
    20 #include "MT_AiStrParser.h"
       
    21 
       
    22 //  EXTERNAL INCLUDES
       
    23 #include <digia/eunit/eunitmacros.h>
       
    24 #include <utf.h>
       
    25 
       
    26 //  INTERNAL INCLUDES
       
    27 #include <aistrparser.h>
       
    28 #include <aiutility.h>
       
    29 
       
    30 // CONSTANTS
       
    31 namespace
       
    32     {
       
    33     _LIT( KUnicode, "Text with unicode characters ä and \x20AC" );
       
    34     _LIT8( KUtf8, "Text" );
       
    35     }
       
    36 
       
    37 _LIT16( KHexPrefix16, "0x" );
       
    38 
       
    39 TInt ParseInt
       
    40         ( TInt32& aValue, const TDesC16& aStringValue )
       
    41     {
       
    42     const TInt pos = aStringValue.FindF( KHexPrefix16 );
       
    43     if (pos != KErrNotFound)
       
    44         {
       
    45         TLex16 lex( aStringValue.Mid( pos + KHexPrefix16().Length() ) );
       
    46         // Hex parsing needs unsigned int
       
    47         TUint32 value = 0;
       
    48         const TInt parseResult = lex.Val( value, EHex );
       
    49         if ( parseResult == KErrNone )
       
    50             {
       
    51             aValue = value;
       
    52             }
       
    53         return parseResult;
       
    54         }
       
    55     else
       
    56         {
       
    57         TLex16 lex( aStringValue );
       
    58         return lex.Val(aValue);
       
    59         }
       
    60     }
       
    61 
       
    62 
       
    63 // CONSTRUCTION
       
    64 MT_AiStrParser* MT_AiStrParser::NewL()
       
    65     {
       
    66     MT_AiStrParser* self = MT_AiStrParser::NewLC();
       
    67     CleanupStack::Pop();
       
    68 
       
    69     return self;
       
    70     }
       
    71 
       
    72 MT_AiStrParser* MT_AiStrParser::NewLC()
       
    73     {
       
    74     MT_AiStrParser* self = new( ELeave ) MT_AiStrParser();
       
    75     CleanupStack::PushL( self );
       
    76 
       
    77     self->ConstructL();
       
    78 
       
    79     return self;
       
    80     }
       
    81 
       
    82 // Destructor (virtual by CBase)
       
    83 MT_AiStrParser::~MT_AiStrParser()
       
    84     {
       
    85     }
       
    86 
       
    87 // Default constructor
       
    88 MT_AiStrParser::MT_AiStrParser()
       
    89     {
       
    90     }
       
    91 
       
    92 // Second phase construct
       
    93 void MT_AiStrParser::ConstructL()
       
    94     {
       
    95     // The ConstructL from the base class CEUnitTestSuiteClass must be called.
       
    96     // It generates the test case table.
       
    97     CEUnitTestSuiteClass::ConstructL();
       
    98     }
       
    99 
       
   100 //  METHODS
       
   101 
       
   102 
       
   103 void MT_AiStrParser::SetupL()
       
   104     {
       
   105     iStrParser = AiUtility::CreateStrParserL();
       
   106     }
       
   107 
       
   108 void MT_AiStrParser::SetupUtf8BufferL()
       
   109     {
       
   110     iStrParser = AiUtility::CreateStrParserL();
       
   111     iUtf8Buffer = HBufC8::NewL(0);    
       
   112     }
       
   113          
       
   114 void MT_AiStrParser::SetupUnicodeBufferL()
       
   115     {
       
   116     iStrParser = AiUtility::CreateStrParserL();
       
   117     iUtf8Buffer = HBufC8::NewL(0);
       
   118     }
       
   119          
       
   120 void MT_AiStrParser::SetupHexIntL()
       
   121     {
       
   122     iStrParser = AiUtility::CreateStrParserL();
       
   123     _LIT8( KTestValue, "0xbad" );
       
   124     iUtf8Buffer = KTestValue().AllocL();
       
   125     iTestValue = 0xbad;
       
   126     iTestResult = KErrNone;
       
   127     }
       
   128          
       
   129 void MT_AiStrParser::SetupHexIntUnicodeL()
       
   130     {
       
   131     iStrParser = AiUtility::CreateStrParserL();
       
   132     _LIT( KTestValue, "0xbad" );
       
   133     iUnicodeBuffer = KTestValue().AllocL();
       
   134     iTestValue = 0xbad;
       
   135     iTestResult = KErrNone;
       
   136     }
       
   137          
       
   138 void MT_AiStrParser::SetupIntL()
       
   139     {
       
   140     iStrParser = AiUtility::CreateStrParserL();
       
   141     _LIT8( KTestValue, "42" );
       
   142     iUtf8Buffer = KTestValue().AllocL();
       
   143     iTestValue = 42;
       
   144     iTestResult = KErrNone;
       
   145     }
       
   146 
       
   147 void MT_AiStrParser::SetupIntUnicodeL()
       
   148     {
       
   149     iStrParser = AiUtility::CreateStrParserL();
       
   150     _LIT( KTestValue, "42" );
       
   151     iUnicodeBuffer = KTestValue().AllocL();
       
   152     iTestValue = 42;
       
   153     iTestResult = KErrNone;
       
   154     }
       
   155 
       
   156 void MT_AiStrParser::SetupNoIntL()
       
   157     {
       
   158     SetupUtf8BufferL();
       
   159     iTestResult = KErrGeneral;
       
   160     }
       
   161     
       
   162 void MT_AiStrParser::SetupNoIntUnicodeL()
       
   163     {
       
   164     iStrParser = AiUtility::CreateStrParserL();
       
   165     iUnicodeBuffer = HBufC::NewL(0);
       
   166     iTestResult = KErrGeneral;
       
   167     }
       
   168     
       
   169 void MT_AiStrParser::Teardown(  )
       
   170     {
       
   171     delete iUnicodeBuffer;
       
   172     iUnicodeBuffer = NULL;
       
   173     
       
   174     delete iUtf8Buffer;
       
   175     iUtf8Buffer = NULL;
       
   176     
       
   177     Release( iStrParser );
       
   178     }
       
   179 
       
   180 void MT_AiStrParser::TestCopyUnicodeToUnicodeL()
       
   181     {
       
   182     iUnicodeBuffer = iStrParser->CopyToBufferL(iUnicodeBuffer, KUnicode );
       
   183     
       
   184     EUNIT_ASSERT( *iUnicodeBuffer == KUnicode );
       
   185     }
       
   186          
       
   187 void MT_AiStrParser::TestCopyUnicodeToUtf8L()
       
   188     {
       
   189     iUtf8Buffer = iStrParser->CopyToBufferL( iUtf8Buffer, KUnicode );
       
   190     
       
   191     HBufC8* reference = CnvUtfConverter::ConvertFromUnicodeToUtf8L( KUnicode );
       
   192     
       
   193     EUNIT_ASSERT_EQUALS( *iUtf8Buffer, *reference );
       
   194     
       
   195     delete reference;
       
   196     }
       
   197             
       
   198 void MT_AiStrParser::TestCopyUtf8ToUnicodeL()
       
   199     {
       
   200     iUnicodeBuffer = iStrParser->CopyToBufferL( iUnicodeBuffer, KUtf8 );
       
   201     
       
   202     HBufC* reference = CnvUtfConverter::ConvertToUnicodeFromUtf8L( KUtf8 );
       
   203     
       
   204     EUNIT_ASSERT_EQUALS( *iUnicodeBuffer, *reference );
       
   205     
       
   206     delete reference;
       
   207     }
       
   208                  
       
   209 void MT_AiStrParser::TestCopyUtf8ToUtf8L()
       
   210     {
       
   211     iUtf8Buffer = iStrParser->CopyToBufferL( iUtf8Buffer, KUtf8 );
       
   212     
       
   213     EUNIT_ASSERT_EQUALS( *iUtf8Buffer, KUtf8 );
       
   214     }
       
   215          
       
   216 void MT_AiStrParser::TestParseInt()
       
   217     {
       
   218     TInt32 value(0);
       
   219     
       
   220     TInt result = iStrParser->ParseInt( value, *iUtf8Buffer );
       
   221     
       
   222     EUNIT_ASSERT_EQUALS( result, iTestResult );
       
   223     
       
   224     if ( result == KErrNone )
       
   225         {
       
   226         EUNIT_ASSERT_EQUALS( value, iTestValue );
       
   227         }
       
   228     }
       
   229          
       
   230 void MT_AiStrParser::TestParseIntUnicode()
       
   231     {
       
   232     TInt32 value(0);
       
   233     
       
   234     TInt result = ParseInt( value, *iUnicodeBuffer );
       
   235     
       
   236     EUNIT_ASSERT_EQUALS( result, iTestResult );
       
   237     
       
   238     if ( result == KErrNone )
       
   239         {
       
   240         EUNIT_ASSERT_EQUALS( value, iTestValue );
       
   241         }
       
   242     }
       
   243          
       
   244 //  TEST TABLE
       
   245 
       
   246 EUNIT_BEGIN_TEST_TABLE(
       
   247     MT_AiStrParser,
       
   248     "Unit test suite for class XmlUiController utility functions",
       
   249     "UNIT" )
       
   250 
       
   251 EUNIT_TEST(
       
   252     "Copy unicode to unicode, no buffer",
       
   253     "AiXmlUiController",
       
   254     "CopyToBufferL( HBufC16*, const TDesC16& )",
       
   255     "FUNCTIONALITY",
       
   256     SetupL, TestCopyUnicodeToUnicodeL, Teardown)
       
   257     
       
   258 EUNIT_TEST(
       
   259     "Copy unicode to unicode, reallocate buffer",
       
   260     "AiXmlUiController",
       
   261     "CopyToBufferL( HBufC16*, const TDesC16& )",
       
   262     "FUNCTIONALITY",
       
   263     SetupUnicodeBufferL, TestCopyUnicodeToUnicodeL, Teardown)
       
   264 
       
   265 EUNIT_TEST(
       
   266     "Copy UTF8 to unicode, no buffer",
       
   267     "AiXmlUiController",
       
   268     "CopyToBufferL( HBufC16*, const TDesC8& )",
       
   269     "FUNCTIONALITY",
       
   270     SetupL, TestCopyUtf8ToUnicodeL, Teardown)
       
   271  
       
   272 
       
   273 EUNIT_TEST(
       
   274     "Copy UTF8 to unicode, reallocate buffer",
       
   275     "AiXmlUiController",
       
   276     "CopyToBufferL( HBufC16*, const TDesC8& )",
       
   277     "FUNCTIONALITY",
       
   278     SetupUnicodeBufferL, TestCopyUtf8ToUnicodeL, Teardown)
       
   279        
       
   280 EUNIT_TEST(
       
   281     "Copy unicode to UTF8, no buffer",
       
   282     "AiXmlUiController",
       
   283     "CopyToBufferL( HBufC8*, const TDesC16& )",
       
   284     "FUNCTIONALITY",
       
   285     SetupL, TestCopyUnicodeToUtf8L, Teardown)
       
   286     
       
   287 EUNIT_TEST(
       
   288     "Copy unicode to UTF8, reallocate buffer",
       
   289     "AiXmlUiController",
       
   290     "CopyToBufferL( HBufC8*, const TDesC16& )",
       
   291     "FUNCTIONALITY",
       
   292     SetupUtf8BufferL, TestCopyUnicodeToUtf8L, Teardown)
       
   293 
       
   294 EUNIT_TEST(
       
   295     "Copy UTF8 to UTF8, no buffer",
       
   296     "AiXmlUiController",
       
   297     "CopyToBufferL( HBufC8*, const TDesC8& )",
       
   298     "FUNCTIONALITY",
       
   299     SetupL, TestCopyUtf8ToUtf8L, Teardown)
       
   300     
       
   301 EUNIT_TEST(
       
   302     "Copy UTF8 to UTF8, reallocate buffer",
       
   303     "AiXmlUiController",
       
   304     "CopyToBufferL( HBufC8*, const TDesC8& )",
       
   305     "FUNCTIONALITY",
       
   306     SetupUtf8BufferL, TestCopyUtf8ToUtf8L, Teardown)
       
   307 
       
   308 EUNIT_TEST(
       
   309     "Parse hex integer",
       
   310     "AiXmlUiController",
       
   311     "ParseInt",
       
   312     "FUNCTIONALITY",
       
   313     SetupHexIntL, TestParseInt, Teardown)
       
   314 
       
   315 EUNIT_TEST(
       
   316     "Parse decimal integer",
       
   317     "AiXmlUiController",
       
   318     "ParseInt",
       
   319     "FUNCTIONALITY",
       
   320     SetupIntL, TestParseInt, Teardown)
       
   321 
       
   322 EUNIT_TEST(
       
   323     "Parse invalid buffer",
       
   324     "AiXmlUiController",
       
   325     "ParseInt",
       
   326     "ERROR HANDLING",
       
   327     SetupNoIntL, TestParseInt, Teardown)
       
   328 
       
   329 EUNIT_TEST(
       
   330     "Parse UNICODE hex integer",
       
   331     "AiXmlUiController",
       
   332     "ParseInt",
       
   333     "FUNCTIONALITY",
       
   334     SetupHexIntUnicodeL, TestParseIntUnicode, Teardown)
       
   335 
       
   336 EUNIT_TEST(
       
   337     "Parse UNICODE decimal integer",
       
   338     "AiXmlUiController",
       
   339     "ParseInt",
       
   340     "FUNCTIONALITY",
       
   341     SetupIntUnicodeL, TestParseIntUnicode, Teardown)
       
   342 
       
   343 EUNIT_TEST(
       
   344     "Parse UNICODE invalid buffer",
       
   345     "AiXmlUiController",
       
   346     "ParseInt",
       
   347     "ERROR HANDLING",
       
   348     SetupNoIntUnicodeL, TestParseIntUnicode, Teardown)
       
   349 
       
   350 EUNIT_END_TEST_TABLE
       
   351 
       
   352 //  END OF FILE