symbianunittestfw/sutfw/sutfwexamples/sutfwracecarexample/tsrc/src/ut_racecar.cpp
branchRCL_3
changeset 3 9397a16b6eb8
parent 1 6edeef394eb7
equal deleted inserted replaced
1:6edeef394eb7 3:9397a16b6eb8
     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 #include "ut_racecar.h"
       
    19 #include "racecar.h"
       
    20 #include <symbianunittestmacros.h>
       
    21 
       
    22 _LIT( KRed, "red" );
       
    23 _LIT( KBlue, "blue" );
       
    24 const TInt KInitialFuel( 100 );
       
    25 
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 UT_CRaceCar* UT_CRaceCar::NewL()
       
    32     {
       
    33     UT_CRaceCar* self = UT_CRaceCar::NewLC(); 
       
    34     CleanupStack::Pop( self );
       
    35     return self;
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 UT_CRaceCar* UT_CRaceCar::NewLC()
       
    43     {
       
    44     UT_CRaceCar* self = new( ELeave )UT_CRaceCar();
       
    45     CleanupStack::PushL( self );
       
    46     self->ConstructL();
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 UT_CRaceCar::UT_CRaceCar()
       
    55     {
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 void UT_CRaceCar::ConstructL()
       
    63     {
       
    64     BASE_CONSTRUCT
       
    65     
       
    66     DEFINE_TEST_CLASS( UT_CRaceCar )
       
    67     
       
    68     ADD_SUT( UT_FuelL )
       
    69     ADD_SUT( UT_SteeringL )
       
    70     ADD_SUT( UT_SpeedL ) 
       
    71     // Setup and teardown functions can be changed for each test function
       
    72     // Usually this is not needed, but this is possible.
       
    73     ADD_SUT_WITH_SETUP_AND_TEARDOWN( SetupL, UT_ColorL, Teardown )
       
    74 
       
    75     ADD_SUT( UT_TimeoutL )
       
    76     ADD_SUT(UT_CrashL ) 
       
    77     ADD_SUT(UT_QuitL ) 
       
    78 
       
    79     
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 UT_CRaceCar::~UT_CRaceCar()
       
    87     {
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void UT_CRaceCar::SetupL()
       
    95     {
       
    96     iRaceCar = CRaceCar::NewL( KRed, KInitialFuel );
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void UT_CRaceCar::Teardown()
       
   104     {
       
   105     delete iRaceCar;
       
   106     iRaceCar = NULL; 
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void UT_CRaceCar::UT_FuelL()
       
   114     {
       
   115     // Test the initial fuel
       
   116     SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar->FuelTankSize() )
       
   117     SUT_ASSERT_EQUALS( 0, iRaceCar->FuelLeft() )
       
   118 
       
   119     // Add a proper amount of fuel
       
   120     SUT_ASSERT_EQUALS( 0, iRaceCar->AddFuel( KInitialFuel ) )
       
   121     SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar->FuelLeft() )
       
   122 
       
   123     // Add too much fuel
       
   124     SUT_ASSERT_EQUALS( 2, iRaceCar->AddFuel( 1 ) )  //expect to leave
       
   125     SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar->FuelLeft() )
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void UT_CRaceCar::UT_SteeringL()
       
   133     {
       
   134     // Try to steer beyond the allowed values
       
   135     SUT_ASSERT( iRaceCar->Steer( -( KMaxTyreAngle + 1 ) ) != KErrNone )
       
   136     SUT_ASSERT_EQUALS( 0, iRaceCar->Direction() )
       
   137     SUT_ASSERT( iRaceCar->Steer( KMaxTyreAngle + 1 ) != KErrNone )
       
   138     SUT_ASSERT_EQUALS( 0, iRaceCar->Direction() )
       
   139 
       
   140     // Steer with the an allowed value 
       
   141     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Steer( KMaxTyreAngle ) )
       
   142     SUT_ASSERT_EQUALS( KMaxTyreAngle, iRaceCar->Direction() )
       
   143     
       
   144     // Try beyond the allowed value
       
   145     SUT_ASSERT( iRaceCar->Steer( 1 ) != KErrNone )
       
   146     SUT_ASSERT_EQUALS( KMaxTyreAngle, iRaceCar->Direction() )
       
   147 
       
   148     // Steer to the opposite maximum
       
   149     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Steer( -2 * KMaxTyreAngle ) )
       
   150     SUT_ASSERT_EQUALS( -KMaxTyreAngle, iRaceCar->Direction() )
       
   151 
       
   152     // Try beyond the allowed value
       
   153     SUT_ASSERT( iRaceCar->Steer( -1 ) != KErrNone )
       
   154     SUT_ASSERT_EQUALS( -KMaxTyreAngle, iRaceCar->Direction() )
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void UT_CRaceCar::UT_SpeedL()
       
   162     {
       
   163     // Try to accelerate without fuel
       
   164     SUT_ASSERT_EQUALS( 0, iRaceCar->Speed() )
       
   165     SUT_ASSERT( iRaceCar->Accelerate( 1 ) != KErrNone )
       
   166     
       
   167     // Add fuel and accelerate
       
   168     iRaceCar->AddFuel( 2 );
       
   169     SUT_ASSERT_EQUALS( 2, iRaceCar->FuelLeft() )
       
   170     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Accelerate( 100 ) )
       
   171     SUT_ASSERT_EQUALS( 100, iRaceCar->Speed() )
       
   172     SUT_ASSERT_EQUALS( 1, iRaceCar->FuelLeft() )
       
   173     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Accelerate( 50 ) )
       
   174     SUT_ASSERT_EQUALS( 150, iRaceCar->Speed() )
       
   175     SUT_ASSERT_EQUALS( 0, iRaceCar->FuelLeft() )
       
   176 
       
   177     // Try braking
       
   178     iRaceCar->Brake( 70 );
       
   179     SUT_ASSERT_EQUALS( 80, iRaceCar->Speed() )
       
   180     iRaceCar->Brake( 90 );
       
   181     SUT_ASSERT_EQUALS( 0, iRaceCar->Speed() )    
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void UT_CRaceCar::UT_ColorL()
       
   189     {
       
   190     // Check the initial color
       
   191     SUT_ASSERT_EQUALS( KRed, iRaceCar->Color() )
       
   192 
       
   193     // Try to paint the car, color allowed
       
   194     iRaceCar->ChangeColorL( KBlue );
       
   195     SUT_ASSERT_EQUALS( KBlue, iRaceCar->Color() )
       
   196     
       
   197     // Try to paint the car, color not allowed
       
   198     SUT_ASSERT_LEAVE( iRaceCar->ChangeColorL( KNullDesC ) )    
       
   199     
       
   200     SUT_ASSERT_LEAVE_WITH( 
       
   201         iRaceCar->ChangeColorL( KNullDesC ), KErrArgument )
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 void UT_CRaceCar::UT_TimeoutL()
       
   209     {
       
   210     //the car is too slow (:
       
   211     User::After(7 * 1000000);
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // 
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void UT_CRaceCar::UT_CrashL()
       
   219     {
       
   220     //the car is totaled (:
       
   221     User::Panic(_L("Crash"), -100);
       
   222     }
       
   223 
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // 
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void UT_CRaceCar::UT_QuitL()
       
   230     {
       
   231     User::Leave(-100);
       
   232     }