photosgallery/slideshow/engine/tsrc/t_cshweventrouter/t_cshweventrouter.cpp
changeset 0 4e91876724a2
child 18 bcb43dc84c44
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:   Test for scheduler for the slideshow
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 //  CLASS HEADER
       
    22 #include "T_CShwEventRouter.h"
       
    23 
       
    24 //  EXTERNAL INCLUDES
       
    25 #include <digia/eunit/eunitmacros.h>
       
    26 #include <digia/eunit/eunitdecorators.h>
       
    27 
       
    28 //  INTERNAL INCLUDES
       
    29 #include "shweventrouter.h"
       
    30 #include "shwevent.h"
       
    31 #include "shwslideshowenginepanic.h"
       
    32 
       
    33 // test state flags
       
    34 TBool gT_TestEventDestructorCalled = EFalse;
       
    35 TBool gT_TestEventCloneLCCalled = EFalse;
       
    36 TBool gT_PanicCalled = EFalse;
       
    37 
       
    38 // A test event class
       
    39 class T_TestEvent : public MShwEvent
       
    40 	{
       
    41 	public:
       
    42 		~T_TestEvent()
       
    43 			{
       
    44 			gT_TestEventDestructorCalled = ETrue;
       
    45 			}
       
    46 		MShwEvent* CloneLC()
       
    47 			{
       
    48 			gT_TestEventCloneLCCalled = ETrue;
       
    49 			MShwEvent* tmp = new( ELeave ) T_TestEvent;
       
    50 			CleanupStack::PushL( tmp );
       
    51 			return tmp;
       
    52 			}
       
    53 
       
    54 		/**
       
    55 		 * Helper to check that the event is of this class type
       
    56 		 */
       
    57 		TBool IsOfSameClass( MShwEvent* aLhs )
       
    58 			{
       
    59 			// here we have to compare agains NULL as TBool cannot be converted from a pointer
       
    60 			return dynamic_cast<T_TestEvent*>( aLhs ) != NULL;
       
    61 			}
       
    62 	};
       
    63 
       
    64 // CONSTRUCTION
       
    65 T_CShwEventRouter* T_CShwEventRouter::NewL()
       
    66 	{
       
    67 	T_CShwEventRouter* self = T_CShwEventRouter::NewLC();
       
    68 	CleanupStack::Pop();
       
    69 	
       
    70 	return self;
       
    71 	}
       
    72 
       
    73 T_CShwEventRouter* T_CShwEventRouter::NewLC()
       
    74 	{
       
    75 	T_CShwEventRouter* self = new( ELeave ) T_CShwEventRouter();
       
    76 	CleanupStack::PushL( self );
       
    77 	
       
    78 	self->ConstructL();
       
    79 	
       
    80 	return self;
       
    81 	}
       
    82 
       
    83 // Destructor (virtual by CBase)
       
    84 T_CShwEventRouter::~T_CShwEventRouter()
       
    85 	{
       
    86 	}
       
    87 
       
    88 // Default constructor
       
    89 T_CShwEventRouter::T_CShwEventRouter()
       
    90 	{
       
    91 	}
       
    92 
       
    93 // Second phase construct
       
    94 void T_CShwEventRouter::ConstructL()
       
    95 	{
       
    96 	// The ConstructL from the base class CEUnitTestSuiteClass must be called.
       
    97 	// It generates the test case table.
       
    98 	CEUnitTestSuiteClass::ConstructL();
       
    99 	}
       
   100 
       
   101 // METHODS
       
   102 
       
   103 void T_CShwEventRouter::NotifyL( MShwEvent* aEvent )
       
   104 	{
       
   105 	// we got the event
       
   106 	iEventReceiveCount++;
       
   107 	// assert that the event was correct one
       
   108 	EUNIT_ASSERT_DESC( iEventToReceive->IsOfSameClass( aEvent ), "Check that the event class is correct" );
       
   109 	// for OOM testing, lets make a memory allocation
       
   110 	TInt* intti = new( ELeave ) TInt;
       
   111 	delete intti;
       
   112 	// did the test want us to leave
       
   113 	if( iLeaveInNotify )
       
   114 		{
       
   115 		User::Leave( KErrCorrupt );
       
   116 		}
       
   117 	// did the test want us to send new events
       
   118 	if( iSendNewEventCount-- > 0 )
       
   119 		{
       
   120 		// allocate new event
       
   121 		T_TestEvent testEvent2;
       
   122 		// set the event object pointer so that the notify can verify the event object
       
   123 		iEventToReceive = &testEvent2;
       
   124 		// send the event,
       
   125 		iQueue->SendEventL( &testEvent2 );
       
   126 		}
       
   127 	}
       
   128 	
       
   129 void T_CShwEventRouter::SetEventQueue( MShwEventQueue* aQueue )
       
   130 	{
       
   131 	iQueue = aQueue;
       
   132 	}
       
   133 
       
   134 void T_CShwEventRouter::SetupL()
       
   135 	{
       
   136 	// reset information flags
       
   137 	gT_TestEventDestructorCalled = EFalse;
       
   138 	gT_TestEventCloneLCCalled = EFalse;
       
   139 	gT_PanicCalled = EFalse;
       
   140 	// reset test state
       
   141 	iLeaveInNotify = EFalse;
       
   142 	iSendNewEventCount = 0;
       
   143 	iEventReceiveCount = 0;
       
   144 	// construct the router
       
   145 	iRouter = CShwEventRouter::NewL();
       
   146 	}
       
   147 
       
   148 void T_CShwEventRouter::Teardown()
       
   149 	{
       
   150 	delete iRouter;
       
   151 	iRouter = NULL;
       
   152 	}
       
   153 
       
   154 void T_CShwEventRouter::TestBasicRoutingL()
       
   155 	{
       
   156 	// add us as observer and producer
       
   157 	iRouter->AddObserverL( this );
       
   158 	iRouter->AddProducer( this );
       
   159 
       
   160 	T_TestEvent testEvent;
       
   161 	// set the event object pointer so that the notify can verify the event object
       
   162 	iEventToReceive = &testEvent;
       
   163 	// send the event,
       
   164 	iQueue->SendEventL( &testEvent );
       
   165 	// check we got an event
       
   166 	EUNIT_ASSERT_EQUALS_DESC( iEventReceiveCount, 1, "one event expected" );
       
   167 	// check that our event class methods were correctly called
       
   168 	EUNIT_ASSERT_DESC( gT_TestEventCloneLCCalled, "check that clone was called" );
       
   169 	EUNIT_ASSERT_DESC( gT_TestEventDestructorCalled, "destructor was called" );
       
   170 	// there was no panic call
       
   171 	EUNIT_ASSERT( !gT_PanicCalled );
       
   172 	}
       
   173 
       
   174 void T_CShwEventRouter::TestNestedEventsL()
       
   175 	{
       
   176 	// we want the notify to add 2 events during the notify calls so 3 events overall
       
   177 	iSendNewEventCount = 2;
       
   178 	// add us as observer and producer
       
   179 	iRouter->AddObserverL( this );
       
   180 	iRouter->AddProducer( this );
       
   181 
       
   182 	T_TestEvent testEvent;
       
   183 	// set the event object pointer so that the notify can verify the event object
       
   184 	iEventToReceive = &testEvent;
       
   185 	// send the event,
       
   186 	iQueue->SendEventL( &testEvent );
       
   187 	// check we got events
       
   188 	EUNIT_ASSERT_EQUALS_DESC( iEventReceiveCount, 3, "three events expected" );
       
   189 	// check that our event class methods were correctly called
       
   190 	EUNIT_ASSERT_DESC( gT_TestEventCloneLCCalled, "check that clone was called" );
       
   191 	EUNIT_ASSERT_DESC( gT_TestEventDestructorCalled, "destructor was called" );
       
   192 	// there was no panic call
       
   193 	EUNIT_ASSERT( !gT_PanicCalled );
       
   194 	}
       
   195 
       
   196 void T_CShwEventRouter::TestNestedEventsAndMultipleObserversL()
       
   197 	{
       
   198 //	EUNIT_GET_ALLOC_DECORATOR_FAILCOUNT_D( failurecount );
       
   199 //	EUNIT_PRINT( _L("Memory alloc %d"), failurecount );
       
   200 
       
   201 	// we want the notify to add 2 events during the notify calls so 3 events overall
       
   202 	iSendNewEventCount = 2;
       
   203 	// add us as observer twice
       
   204 	RPointerArray<MShwEventObserver> observers;
       
   205 	CleanupClosePushL( observers );
       
   206 	observers.AppendL( this );
       
   207 	observers.AppendL( this );
       
   208 	// add the array of observers
       
   209 	iRouter->AddObserversL( observers.Array() );
       
   210 	CleanupStack::PopAndDestroy();
       
   211 	// add us as publisher
       
   212 	RPointerArray<MShwEventPublisher> publisher;
       
   213 	CleanupClosePushL( publisher );
       
   214 	publisher.AppendL( this );
       
   215 	iRouter->AddProducers( publisher.Array() );
       
   216 	CleanupStack::PopAndDestroy();
       
   217 
       
   218 	T_TestEvent testEvent;
       
   219 	// set the event object pointer so that the notify can verify the event object
       
   220 	iEventToReceive = &testEvent;
       
   221 	// send the event,
       
   222 	iQueue->SendEventL( &testEvent );
       
   223 	// check we got events
       
   224 	EUNIT_ASSERT_EQUALS_DESC( iEventReceiveCount, 6, "six events expected" );
       
   225 	// check that our event class methods were correctly called
       
   226 	EUNIT_ASSERT_DESC( gT_TestEventCloneLCCalled, "check that clone was called" );
       
   227 	EUNIT_ASSERT_DESC( gT_TestEventDestructorCalled, "destructor was called" );
       
   228 	// there was no panic call
       
   229 	EUNIT_ASSERT( !gT_PanicCalled );
       
   230 	}
       
   231 
       
   232 void T_CShwEventRouter::TestLeaveInNotifyL()
       
   233 	{
       
   234 	// we want the notify to leave
       
   235 	iLeaveInNotify = ETrue;
       
   236 	// add us as observer and producer
       
   237 	iRouter->AddObserverL( this );
       
   238 	iRouter->AddProducer( this );
       
   239 	
       
   240 	T_TestEvent testEvent;
       
   241 	// set the event object pointer so that the notify can verify the event object
       
   242 	iEventToReceive = &testEvent;
       
   243 	// send the event, TRAP since we need to get out in the event
       
   244 	EUNIT_TRAP_EXCEPT_ALLOC_D( error, 
       
   245 		{
       
   246 		iQueue->SendEventL( &testEvent );
       
   247 		} );
       
   248 	// there was no panic call
       
   249 	EUNIT_ASSERT( !gT_PanicCalled );
       
   250 	EUNIT_ASSERT_EQUALS_DESC( error, KErrCorrupt, "test that leave was correct code" );
       
   251 
       
   252 	// check we got an event
       
   253 	EUNIT_ASSERT_EQUALS_DESC( iEventReceiveCount, 1, "one event expected" );
       
   254 	EUNIT_ASSERT_DESC( gT_TestEventCloneLCCalled, "check that clone was called" );
       
   255 	EUNIT_ASSERT_DESC( !gT_TestEventDestructorCalled, "destructor was not yet called" );
       
   256 	
       
   257 	// delete router
       
   258 	Teardown();
       
   259 	// now the event gets deleted
       
   260 	EUNIT_ASSERT_DESC( gT_TestEventDestructorCalled, "destructor was called" );
       
   261 	}
       
   262 
       
   263 void T_CShwEventRouter::TestOOML()
       
   264 	{
       
   265 	// this test case is supposed to be run under alloc decorator
       
   266 	// check that we got alloc decorator on
       
   267 	if( !CEUnitTestCaseDecorator::ActiveTestCaseDecorator( KEUnitAllocTestCaseDecoratorName ) )
       
   268 		{
       
   269 		EUNIT_FAIL_TEST( "Test configuration failure, alloc decorator not on" );
       
   270 		}
       
   271 	
       
   272 	// add us as observer and producer
       
   273 	iRouter->AddObserverL( this );
       
   274 	iRouter->AddProducer( this );
       
   275 	
       
   276 	T_TestEvent testEvent;
       
   277 	// set the event object pointer so that the notify can verify the event object
       
   278 	iEventToReceive = &testEvent;
       
   279 	// send the event
       
   280 	iQueue->SendEventL( &testEvent );
       
   281 	
       
   282 	// get the alloc failure count from the alloc decorator
       
   283 	// in the first two alloc failures we dont get to the nofity method
       
   284 	EUNIT_GET_ALLOC_DECORATOR_FAILCOUNT_D( failurecount );
       
   285 	if( failurecount > 2 )
       
   286 		{
       
   287 		// ok we should get the event
       
   288 		// check we got an event
       
   289 		EUNIT_ASSERT_EQUALS_DESC( iEventReceiveCount, 1, "one event expected" );
       
   290 		// check that if clone was called, then also destructor is called
       
   291 		EUNIT_ASSERT_EQUALS( gT_TestEventDestructorCalled, gT_TestEventCloneLCCalled );
       
   292 		}
       
   293 	}
       
   294 	
       
   295 //  TEST TABLE
       
   296 
       
   297 EUNIT_BEGIN_TEST_TABLE(
       
   298     T_CShwEventRouter,
       
   299     "CShwEventRouter unit tests",
       
   300     "UNIT" )
       
   301 
       
   302 EUNIT_ALLOC_TEST(
       
   303     "Testing basic functionality",
       
   304     "CShwEventRouter",
       
   305     "SendEvent",
       
   306     "FUNCTIONALITY",
       
   307     SetupL, TestBasicRoutingL, Teardown)
       
   308 
       
   309 EUNIT_ALLOC_TEST(
       
   310     "Testing nested events",
       
   311     "CShwEventRouter",
       
   312     "SendEvent",
       
   313     "FUNCTIONALITY",
       
   314     SetupL, TestNestedEventsL, Teardown)
       
   315 
       
   316 EUNIT_ALLOC_TEST(
       
   317     "Testing multiple observers",
       
   318     "CShwEventRouter",
       
   319     "SendEvent",
       
   320     "FUNCTIONALITY",
       
   321     SetupL, TestNestedEventsAndMultipleObserversL, Teardown)
       
   322 
       
   323 EUNIT_ALLOC_TEST(
       
   324     "Testing error handling",
       
   325     "CShwEventRouter",
       
   326     "SendEvent",
       
   327     "ERRORHANDLING",
       
   328     SetupL, TestLeaveInNotifyL, Teardown)
       
   329 
       
   330 EUNIT_ALLOC_TEST(
       
   331     "Testing error handling in OOM",
       
   332     "CShwEventRouter",
       
   333     "SendEvent",
       
   334     "ERRORHANDLING",
       
   335     SetupL, TestOOML, Teardown)
       
   336 
       
   337 
       
   338 EUNIT_END_TEST_TABLE
       
   339 
       
   340 //  END OF FILE