idlefw/tsrc/ai_utilities_api/src/mt_apiaicontentItemarrayiterator.cpp
branchRCL_3
changeset 111 053c6c7c14f3
equal deleted inserted replaced
110:2c7f27287390 111:053c6c7c14f3
       
     1 /*
       
     2 * Copyright (c) 2009 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 // CLASS UNDER TEST
       
    19 #include <aiutility.h>
       
    20 #include <aicontentmodel.h>
       
    21 
       
    22 #include <mt_apiaicontentitemarrayiterator.h>
       
    23 #include <digia/eunit/eunitmacros.h>
       
    24 
       
    25 const TInt KNonExistentItemId = -1;
       
    26 _LIT(KNonExistentItemCid, "NonExistentCid");
       
    27 
       
    28 
       
    29 
       
    30 // CONSTRUCTION
       
    31 MT_ApiAiContentItemArrayIterator* MT_ApiAiContentItemArrayIterator::NewL()
       
    32     {
       
    33     MT_ApiAiContentItemArrayIterator* self = MT_ApiAiContentItemArrayIterator::NewLC();
       
    34     CleanupStack::Pop( self );
       
    35     return self;
       
    36     }
       
    37 
       
    38 MT_ApiAiContentItemArrayIterator* MT_ApiAiContentItemArrayIterator::NewLC()
       
    39     {
       
    40     MT_ApiAiContentItemArrayIterator* self = new( ELeave ) MT_ApiAiContentItemArrayIterator();
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     return self;
       
    44     }
       
    45 
       
    46 // Destructor (virtual by CBase)
       
    47 MT_ApiAiContentItemArrayIterator::~MT_ApiAiContentItemArrayIterator()
       
    48     {
       
    49 #if _BullseyeCoverage
       
    50     cov_write();
       
    51 #endif
       
    52     }
       
    53 
       
    54 // Default constructor
       
    55 MT_ApiAiContentItemArrayIterator::MT_ApiAiContentItemArrayIterator()
       
    56     {
       
    57     }
       
    58 
       
    59 // Second phase construct
       
    60 void MT_ApiAiContentItemArrayIterator::ConstructL()
       
    61     {
       
    62     // The ConstructL from the base class CEUnitTestSuiteClass must be called.
       
    63     // It generates the test case table.
       
    64     CEUnitTestSuiteClass::ConstructL();
       
    65     }
       
    66 
       
    67 //  METHODS
       
    68 
       
    69 
       
    70 
       
    71 void MT_ApiAiContentItemArrayIterator::SetupL(  )
       
    72     {
       
    73     }
       
    74 
       
    75 void MT_ApiAiContentItemArrayIterator::Teardown(  )
       
    76     {
       
    77     }
       
    78 
       
    79 void MT_ApiAiContentItemArrayIterator::TestEmptyL(  )
       
    80     {
       
    81     MAiContentItemIterator* iter = NULL;
       
    82     // Test that Release(NULL) does not crash
       
    83     Release(iter);
       
    84     
       
    85     // Test that the iterator works correctly with an empty (NULL) array
       
    86     iter = AiUtility::CreateContentItemArrayIteratorL(NULL, 0);
       
    87     CleanupReleasePushL(iter);
       
    88 
       
    89     // Make two passes through the iterator to test also Reset
       
    90     for (TInt round = 1; round <= 2; ++round)
       
    91         {
       
    92         EUNIT_ASSERT( !iter->HasNext() );
       
    93         EUNIT_ASSERT_SPECIFIC_LEAVE( iter->NextL(), KErrOverflow );
       
    94         EUNIT_ASSERT_SPECIFIC_LEAVE( iter->ItemL(0), KErrNotFound );
       
    95         EUNIT_ASSERT_SPECIFIC_LEAVE( iter->ItemL(KNullDesC), KErrNotFound );
       
    96         iter->Reset();
       
    97         }
       
    98 
       
    99     CleanupStack::PopAndDestroy(iter);
       
   100     
       
   101     // Array with negative count should work as an empty array
       
   102     iter = AiUtility::CreateContentItemArrayIteratorL(NULL, -1);
       
   103     CleanupReleasePushL(iter);
       
   104 
       
   105     // Make two passes through the iterator to test also Reset
       
   106     for (TInt round = 1; round <= 2; ++round)
       
   107         {
       
   108         EUNIT_ASSERT( !iter->HasNext() );
       
   109         EUNIT_ASSERT_SPECIFIC_LEAVE( iter->NextL(), KErrOverflow );
       
   110         EUNIT_ASSERT_SPECIFIC_LEAVE( iter->ItemL(0), KErrNotFound );
       
   111         EUNIT_ASSERT_SPECIFIC_LEAVE( iter->ItemL(KNullDesC), KErrNotFound );
       
   112         iter->Reset();
       
   113         }
       
   114 
       
   115     CleanupStack::PopAndDestroy(iter);
       
   116     }
       
   117 
       
   118 TBool operator==(const TAiContentItem& aLhs, const TAiContentItem& aRhs)
       
   119     {
       
   120     // Use pointer comparison as CAiContentItemArrayIterator iterates through
       
   121     // the original items
       
   122     return (&aLhs == &aRhs);
       
   123     }
       
   124 
       
   125 void DoTestOneL
       
   126         ( const TAiContentItem& aRefItem, MAiContentItemIterator& aIter )
       
   127     {
       
   128     // Make two passes through the iterator to test also Reset
       
   129     for (TInt pass = 1; pass <= 2; ++pass)
       
   130         {
       
   131         EUNIT_ASSERT( aIter.HasNext() );
       
   132         EUNIT_ASSERT_EQUALS( aRefItem, aIter.NextL() );
       
   133         EUNIT_ASSERT( !aIter.HasNext() );
       
   134 
       
   135         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.NextL(), KErrOverflow );
       
   136         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemId), KErrNotFound );
       
   137         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemCid), KErrNotFound );
       
   138 
       
   139         EUNIT_ASSERT_EQUALS( aRefItem, aIter.ItemL(aRefItem.id) );
       
   140         EUNIT_ASSERT_EQUALS( aRefItem, aIter.ItemL(ContentCid(aRefItem)) );
       
   141 
       
   142         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemId), KErrNotFound );
       
   143         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemCid), KErrNotFound );
       
   144 
       
   145         aIter.Reset();
       
   146         }
       
   147     }
       
   148 
       
   149 void MT_ApiAiContentItemArrayIterator::TestOneL(  )
       
   150     {
       
   151     const TInt KTestId = 42;
       
   152     static const wchar_t KTestCid[] = L"testcid";
       
   153     static const char KTestType[] = "testtype";
       
   154     static const TAiContentItem KOneItem[] = { { KTestId, KTestCid, KTestType } };
       
   155     
       
   156     // Test iterator created with NewL
       
   157     MAiContentItemIterator* iter =
       
   158         AiUtility::CreateContentItemArrayIteratorL(KOneItem, 1);
       
   159     CleanupReleasePushL(iter);
       
   160     DoTestOneL( KOneItem[0], *iter );
       
   161     CleanupStack::PopAndDestroy(iter);
       
   162     
       
   163     // Test iterator created with create template helper
       
   164     iter = AiUtility::CreateContentItemArrayIteratorL(KOneItem);
       
   165     CleanupReleasePushL(iter);
       
   166     DoTestOneL( KOneItem[0], *iter );
       
   167     CleanupStack::PopAndDestroy(iter);
       
   168     }
       
   169 
       
   170 void DoTestManyL
       
   171         ( const TAiContentItem& aRefItem, MAiContentItemIterator& aIter, TInt aCount )
       
   172     {
       
   173     // Make two passes through the iterator to test also Reset
       
   174     for (TInt pass = 1; pass <= 2; ++pass)
       
   175         {
       
   176         TBool refItemIndex = -1;
       
   177         for ( TInt count = 0; count < aCount; ++count )
       
   178             {
       
   179             EUNIT_ASSERT( aIter.HasNext() );
       
   180             if (aIter.NextL() == aRefItem)
       
   181                 {
       
   182                 refItemIndex = count;
       
   183                 }
       
   184             }
       
   185         EUNIT_ASSERT( refItemIndex >= 0 );
       
   186         EUNIT_ASSERT( !aIter.HasNext() );
       
   187         
       
   188         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.NextL(), KErrOverflow );
       
   189         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemId), KErrNotFound );
       
   190         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemCid), KErrNotFound );
       
   191         
       
   192         EUNIT_ASSERT_EQUALS( aRefItem, aIter.ItemL(aRefItem.id) );
       
   193         EUNIT_ASSERT_EQUALS( aRefItem, aIter.ItemL(ContentCid(aRefItem)) );
       
   194 
       
   195         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemId), KErrNotFound );
       
   196         EUNIT_ASSERT_SPECIFIC_LEAVE( aIter.ItemL(KNonExistentItemCid), KErrNotFound );
       
   197 
       
   198         aIter.Reset();
       
   199         }
       
   200     }
       
   201 
       
   202 void MT_ApiAiContentItemArrayIterator::TestManyL(  )
       
   203     {
       
   204     const TInt KTestId1 = 42;
       
   205     static const wchar_t KTestCid1[] = L"testcid1";
       
   206     static const char KTestType1[] = "testtype1";
       
   207     const TInt KTestId2 = 53;
       
   208     static const wchar_t KTestCid2[] = L"testcid2";
       
   209     static const char KTestType2[] = "testtype2";
       
   210     static const TAiContentItem KTwoItems[] = 
       
   211         { 
       
   212         { KTestId1, KTestCid1, KTestType1 },
       
   213         { KTestId2, KTestCid2, KTestType2 } 
       
   214         };
       
   215     
       
   216     // Test iterator created with NewL
       
   217     MAiContentItemIterator* iter =
       
   218         AiUtility::CreateContentItemArrayIteratorL(KTwoItems, 2);
       
   219     CleanupReleasePushL(iter);
       
   220     DoTestManyL( KTwoItems[0], *iter, 2 );
       
   221     DoTestManyL( KTwoItems[1], *iter, 2 );
       
   222     CleanupStack::PopAndDestroy(iter);
       
   223     
       
   224     // Test iterator created with template helper
       
   225     iter = AiUtility::CreateContentItemArrayIteratorL(KTwoItems);
       
   226     CleanupReleasePushL(iter);
       
   227     DoTestManyL( KTwoItems[0], *iter, 2 );
       
   228     DoTestManyL( KTwoItems[1], *iter, 2 );
       
   229     CleanupStack::PopAndDestroy(iter);
       
   230     }
       
   231 
       
   232 //  TEST TABLE
       
   233 
       
   234 EUNIT_BEGIN_TEST_TABLE(
       
   235     MT_ApiAiContentItemArrayIterator,
       
   236     "Unit test suite for MAiContentItemIterator implementation returned from AiUtility::CreateContentItemArrayIteratorL",
       
   237     "CAiContentItemArrayIterator" )
       
   238 
       
   239 EUNIT_TEST(
       
   240     "Test empty (NULL) array",
       
   241     "MAiContentItemIterator",
       
   242     "All",
       
   243     "FUNCTIONALITY",
       
   244     SetupL, TestEmptyL, Teardown)
       
   245 
       
   246 EUNIT_TEST(
       
   247     "Test array with one item",
       
   248     "MAiContentItemIterator",
       
   249     "All",
       
   250     "FUNCTIONALITY",
       
   251     SetupL, TestOneL, Teardown)
       
   252 
       
   253 EUNIT_TEST(
       
   254     "Test array with multiple items",
       
   255     "MAiContentItemIteratore",
       
   256     "All",
       
   257     "FUNCTIONALITY",
       
   258     SetupL, TestManyL, Teardown)
       
   259 
       
   260 EUNIT_END_TEST_TABLE
       
   261 
       
   262 //  END OF FILE