testexecfw/symbianunittestfw/sutfw/sutfwexamples/sutfwracecarexample/tsrc/src/ut_racecar.cpp
changeset 0 3e07fef1e154
child 1 bbd31066657e
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     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     ADD_SUT( UT_FuelL )
       
    66     ADD_SUT( UT_SteeringL )
       
    67     ADD_SUT( UT_SpeedL ) 
       
    68     // Setup and teardown functions can be changed for each test function
       
    69     // Usually this is not needed, but this is possible.
       
    70     ADD_SUT_WITH_SETUP_AND_TEARDOWN( SetupL, UT_ColorL, Teardown )
       
    71 
       
    72     ADD_SUT( UT_TimeoutL )
       
    73     ADD_SUT(UT_CrashL ) 
       
    74     ADD_SUT(UT_QuitL ) 
       
    75 
       
    76     
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 UT_CRaceCar::~UT_CRaceCar()
       
    84     {
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void UT_CRaceCar::SetupL()
       
    92     {
       
    93     iRaceCar = CRaceCar::NewL( KRed, KInitialFuel );
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void UT_CRaceCar::Teardown()
       
   101     {
       
   102     delete iRaceCar;
       
   103     iRaceCar = NULL; 
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void UT_CRaceCar::UT_FuelL()
       
   111     {
       
   112     // Test the initial fuel
       
   113     SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar->FuelTankSize() )
       
   114     SUT_ASSERT_EQUALS( 0, iRaceCar->FuelLeft() )
       
   115 
       
   116     // Add a proper amount of fuel
       
   117     SUT_ASSERT_EQUALS( 0, iRaceCar->AddFuel( KInitialFuel ) )
       
   118     SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar->FuelLeft() )
       
   119 
       
   120     // Add too much fuel
       
   121     SUT_ASSERT_EQUALS( 2, iRaceCar->AddFuel( 1 ) )  //expect to leave
       
   122     SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar->FuelLeft() )
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void UT_CRaceCar::UT_SteeringL()
       
   130     {
       
   131     // Try to steer beyond the allowed values
       
   132     SUT_ASSERT( iRaceCar->Steer( -( KMaxTyreAngle + 1 ) ) != KErrNone )
       
   133     SUT_ASSERT_EQUALS( 0, iRaceCar->Direction() )
       
   134     SUT_ASSERT( iRaceCar->Steer( KMaxTyreAngle + 1 ) != KErrNone )
       
   135     SUT_ASSERT_EQUALS( 0, iRaceCar->Direction() )
       
   136 
       
   137     // Steer with the an allowed value 
       
   138     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Steer( KMaxTyreAngle ) )
       
   139     SUT_ASSERT_EQUALS( KMaxTyreAngle, iRaceCar->Direction() )
       
   140     
       
   141     // Try beyond the allowed value
       
   142     SUT_ASSERT( iRaceCar->Steer( 1 ) != KErrNone )
       
   143     SUT_ASSERT_EQUALS( KMaxTyreAngle, iRaceCar->Direction() )
       
   144 
       
   145     // Steer to the opposite maximum
       
   146     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Steer( -2 * KMaxTyreAngle ) )
       
   147     SUT_ASSERT_EQUALS( -KMaxTyreAngle, iRaceCar->Direction() )
       
   148 
       
   149     // Try beyond the allowed value
       
   150     SUT_ASSERT( iRaceCar->Steer( -1 ) != KErrNone )
       
   151     SUT_ASSERT_EQUALS( -KMaxTyreAngle, iRaceCar->Direction() )
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void UT_CRaceCar::UT_SpeedL()
       
   159     {
       
   160     // Try to accelerate without fuel
       
   161     SUT_ASSERT_EQUALS( 0, iRaceCar->Speed() )
       
   162     SUT_ASSERT( iRaceCar->Accelerate( 1 ) != KErrNone )
       
   163     
       
   164     // Add fuel and accelerate
       
   165     iRaceCar->AddFuel( 2 );
       
   166     SUT_ASSERT_EQUALS( 2, iRaceCar->FuelLeft() )
       
   167     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Accelerate( 100 ) )
       
   168     SUT_ASSERT_EQUALS( 100, iRaceCar->Speed() )
       
   169     SUT_ASSERT_EQUALS( 1, iRaceCar->FuelLeft() )
       
   170     SUT_ASSERT_EQUALS( KErrNone, iRaceCar->Accelerate( 50 ) )
       
   171     SUT_ASSERT_EQUALS( 150, iRaceCar->Speed() )
       
   172     SUT_ASSERT_EQUALS( 0, iRaceCar->FuelLeft() )
       
   173 
       
   174     // Try braking
       
   175     iRaceCar->Brake( 70 );
       
   176     SUT_ASSERT_EQUALS( 80, iRaceCar->Speed() )
       
   177     iRaceCar->Brake( 90 );
       
   178     SUT_ASSERT_EQUALS( 0, iRaceCar->Speed() )    
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void UT_CRaceCar::UT_ColorL()
       
   186     {
       
   187     // Check the initial color
       
   188     SUT_ASSERT_EQUALS( KRed, iRaceCar->Color() )
       
   189 
       
   190     // Try to paint the car, color allowed
       
   191     iRaceCar->ChangeColorL( KBlue );
       
   192     SUT_ASSERT_EQUALS( KBlue, iRaceCar->Color() )
       
   193     
       
   194     // Try to paint the car, color not allowed
       
   195     SUT_ASSERT_LEAVE( iRaceCar->ChangeColorL( KNullDesC ) )    
       
   196     
       
   197     SUT_ASSERT_LEAVE_WITH( 
       
   198         iRaceCar->ChangeColorL( KNullDesC ), KErrArgument )
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 void UT_CRaceCar::UT_TimeoutL()
       
   206     {
       
   207     //the car is too slow (:
       
   208     User::After(7 * 1000000);
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // 
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void UT_CRaceCar::UT_CrashL()
       
   216     {
       
   217     //the car is totaled (:
       
   218     User::Panic(_L("Crash"), -100);
       
   219     }
       
   220 
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // 
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 void UT_CRaceCar::UT_QuitL()
       
   227     {
       
   228     User::Leave(-100);
       
   229     }