commonappservices/alarmserver/Test/ASTstAlarmTest.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "ASTstAlarmTest.h"
       
    17 #include "testserver.h"
       
    18 
       
    19 #ifdef __WINS__ 
       
    20 const TUid KServerUid2={0x1000008D};
       
    21 const TUid KServerUid3={0x101F502A};
       
    22 #endif
       
    23 
       
    24 
       
    25 //
       
    26 // ----> ASTstAlarmTest (source)
       
    27 //
       
    28 
       
    29 //*************************************************************************************
       
    30 EXPORT_C ASTstAlarmTest::ASTstAlarmTest()
       
    31 : iTest(KAlarmTestCodeTitle)
       
    32 	{
       
    33 	iAlarmServer = RASCliSession();
       
    34 	iInitialHomeTime.HomeTime();
       
    35 	}
       
    36 
       
    37 
       
    38 //*************************************************************************************
       
    39 EXPORT_C void ASTstAlarmTest::Close()
       
    40 	{
       
    41 	ASTstAlarmTest* tlsSelf = reinterpret_cast<ASTstAlarmTest*>(Dll::Tls());
       
    42 	delete tlsSelf;
       
    43 	Dll::SetTls(NULL);
       
    44 	}
       
    45 
       
    46 
       
    47 //*************************************************************************************
       
    48 EXPORT_C ASTstAlarmTest& ASTstAlarmTest::Self()
       
    49 	{
       
    50 	ASTstAlarmTest* tlsSelf = reinterpret_cast<ASTstAlarmTest*>(Dll::Tls());
       
    51 	if	(!tlsSelf)
       
    52 		{
       
    53 		tlsSelf = new ASTstAlarmTest; 
       
    54 		ASSERT(tlsSelf != NULL);
       
    55 		Dll::SetTls(tlsSelf);
       
    56 		}
       
    57 	return *tlsSelf;
       
    58 	}
       
    59 
       
    60 
       
    61 //
       
    62 //
       
    63 //
       
    64 
       
    65 
       
    66 //*************************************************************************************
       
    67 EXPORT_C void ASTstAlarmTest::operator()(TInt aResult, TInt aLineNum, const TText* aFileName)
       
    68 	{
       
    69 	if (!aResult)
       
    70 		resetHomeTimeL();
       
    71 	iTest.operator()(aResult, aLineNum, aFileName);
       
    72 	}
       
    73 
       
    74 
       
    75 //*************************************************************************************
       
    76 EXPORT_C void ASTstAlarmTest::operator()(TInt aResult, TInt aLineNum)
       
    77 	{
       
    78 		
       
    79 	if (!aResult)
       
    80 		resetHomeTimeL();
       
    81 	iTest.operator()(aResult, aLineNum);
       
    82 	}
       
    83 
       
    84 
       
    85 //*************************************************************************************
       
    86 EXPORT_C void ASTstAlarmTest::operator()(TInt aResult)
       
    87 	{
       
    88 	if (!aResult)
       
    89 		resetHomeTimeL();
       
    90 	iTest.operator()(aResult);
       
    91 	}
       
    92 
       
    93 
       
    94 //*************************************************************************************
       
    95 EXPORT_C void ASTstAlarmTest::Title()
       
    96 	{
       
    97 	iTest.Title();
       
    98 	}
       
    99 
       
   100 
       
   101 //*************************************************************************************
       
   102 EXPORT_C void ASTstAlarmTest::Start(const TDesC& aHeading)
       
   103 	{
       
   104 	iTest.Start(aHeading);
       
   105 	}
       
   106 
       
   107 
       
   108 //*************************************************************************************
       
   109 EXPORT_C void ASTstAlarmTest::Next(const TDesC& aHeading)
       
   110 	{
       
   111 	iTest.Next(aHeading);
       
   112 	}
       
   113 
       
   114 
       
   115 //*************************************************************************************
       
   116 EXPORT_C void ASTstAlarmTest::EndL()
       
   117 	{
       
   118 	iTest.End();
       
   119 	resetHomeTimeL();
       
   120 	}
       
   121 
       
   122 //*************************************************************************************
       
   123 EXPORT_C void ASTstAlarmTest::UpLevel()
       
   124 	{
       
   125 	iTest.End();
       
   126 	}
       
   127 
       
   128 
       
   129 //
       
   130 //
       
   131 //
       
   132 
       
   133 
       
   134 //*************************************************************************************
       
   135 EXPORT_C void ASTstAlarmTest::TestClearStoreL()
       
   136 //
       
   137 //	Clear any settings that may have been stored
       
   138 //  Method could leave when trying to get list of all alarm categories in use within the alarm server.
       
   139 //
       
   140 	{
       
   141 	iTest(iAlarmServer.Handle() != KNullHandle);
       
   142 
       
   143 	// Delete all alarms
       
   144 	RArray<TAlarmCategory> categories;
       
   145 	iAlarmServer.GetAvailableCategoryListL(categories); 
       
   146 	TInt count = categories.Count();
       
   147 	for(TInt i=0; i<count; i++)
       
   148 		{
       
   149 		const TAlarmCategory category = categories[i];
       
   150 		iTest(iAlarmServer.AlarmDeleteAllByCategory(category, EFalse) == KErrNone);
       
   151 		}
       
   152 	categories.Close();
       
   153 
       
   154 	// Restore alarm sound
       
   155 	iAlarmServer.SetAlarmSoundState(EAlarmGlobalSoundStateOn);
       
   156 
       
   157 	// Check there aren't any queued alarms (not a particularly good
       
   158 	// test, but better than nothing).
       
   159 	count = iAlarmServer.NumberOfAlarmsActiveInQueue();
       
   160 	iTest(!count);
       
   161 	}
       
   162 
       
   163 
       
   164 //*************************************************************************************
       
   165 EXPORT_C void ASTstAlarmTest::WaitForNotificationBufferToBeEmptied(TRequestStatus& aStatus, TAlarmId& aAlarmId)
       
   166 	{
       
   167 	if	(aStatus == KRequestPending)
       
   168 		return;
       
   169 
       
   170 	iAlarmServer.NotifyChange(aStatus, aAlarmId);
       
   171 	User::After(KTimeToWait);
       
   172 	if	(aStatus == KRequestPending)
       
   173 		return;
       
   174 	User::WaitForRequest(aStatus);
       
   175 
       
   176 	// Because of event bufferring inside the Alarm Server
       
   177 	// We need to wait until there aren't any more events
       
   178 	// in the buffer, and then attempt to cancel the request.
       
   179 	//
       
   180 	// Trying to cancel a request when the server has
       
   181 	// already completed the message (because something
       
   182 	// was in the buffer) results in the server panicking
       
   183 	// the client.
       
   184 	FOREVER
       
   185 		{
       
   186 		iTest(aStatus >= EAlarmChangeEventState && aStatus <= EAlarmChangeEventLast);
       
   187 		iAlarmServer.NotifyChange(aStatus, aAlarmId);
       
   188 		//
       
   189 		// Wait a second so that we can tell if this really is
       
   190 		// waiting for an event, or just for the Alarm Server
       
   191 		// process to be given some scheduling time by the
       
   192 		// kernel
       
   193 		User::After(KTimeToWait);
       
   194 		
       
   195 		// If its still pending, then we should attempt
       
   196 		// to cancel the request. Otherwise, if its not
       
   197 		// pending, then the Alarm Server probably just
       
   198 		// completed the request based upon the contents
       
   199 		// of the event buffer.
       
   200 		if	(aStatus == KRequestPending)
       
   201 			break;
       
   202 		else
       
   203 			{
       
   204 			// Eat the request and try again
       
   205 			User::WaitForRequest(aStatus);
       
   206 			}
       
   207 		}
       
   208 	}
       
   209 
       
   210 
       
   211 //*************************************************************************************
       
   212 EXPORT_C void ASTstAlarmTest::WaitForEvent(TInt aEvent, TRequestStatus& aStatus, TAlarmId& aAlarmId)
       
   213 	{
       
   214 	FOREVER
       
   215 		{
       
   216 		iAlarmServer.NotifyChange(aStatus, aAlarmId);
       
   217 		User::WaitForRequest(aStatus);
       
   218 		if	(aStatus == aEvent)
       
   219 			return;
       
   220 		}
       
   221 	}
       
   222 
       
   223 
       
   224 //*************************************************************************************
       
   225 EXPORT_C void ASTstAlarmTest::TestEventBuffer(TInt aExpected, TRequestStatus& aStatus, TAlarmId& aAlarmId)
       
   226 //
       
   227 //	Check there the notification of this thread. Because notifications
       
   228 //	in the new Alarm Server are bufferred, then the first notification
       
   229 //	may not be the one we are looking for. Therefore we continue to
       
   230 //  request notifications until the buffer is exhausted, at which point,
       
   231 //	if we haven't yet seent the notification we were looking for, we
       
   232 //	panic.
       
   233 //
       
   234 	{
       
   235 	if	(aStatus != aExpected)
       
   236 		{
       
   237 		// Need to exhaust the event buffer
       
   238 
       
   239 		// If the buffer is empty (server side) then the current
       
   240 		// request will still be outstanding, therefore the test
       
   241 		// has failed.
       
   242 		User::After(KTimeToWait);
       
   243 		iTest(aStatus != KRequestPending);
       
   244 
       
   245 		// Eat current request
       
   246 		User::WaitForRequest(aStatus);
       
   247 
       
   248 		// Now go through each item in the buffer, requesting them
       
   249 		// one by one
       
   250 		FOREVER
       
   251 			{
       
   252 			iAlarmServer.NotifyChange(aStatus, aAlarmId);
       
   253 			User::After(KTimeToWait);
       
   254 			iTest(aStatus != KRequestPending);
       
   255 			User::WaitForRequest(aStatus);
       
   256 			if	(aStatus == aExpected)
       
   257 				break;
       
   258 			}
       
   259 		}
       
   260 
       
   261 	// Request another notification
       
   262 	if	(aStatus != KRequestPending)
       
   263 		{
       
   264 		// Eat all the rest
       
   265 		WaitForNotificationBufferToBeEmptied(aStatus, aAlarmId);
       
   266 		}
       
   267 	}
       
   268 
       
   269 
       
   270 //*************************************************************************************
       
   271 EXPORT_C TInt ASTstAlarmTest::CountOrphanedAlarmsL()
       
   272 //
       
   273 // Return the number of orphaned alarms
       
   274 // Method could leave while getting the list of unique identifiers of all the alarms in the alarm server.
       
   275 //
       
   276 	{
       
   277 	TASShdAlarm alarm;
       
   278 	//
       
   279 	RArray<TAlarmId> ids;
       
   280 	iAlarmServer.GetAlarmIdListL(ids);   // Method could leave while getting the list of unique identifiers of all the alarms in the alarm server.
       
   281 
       
   282 	TInt ret = 0;
       
   283 	const TInt count = ids.Count();
       
   284 	for(TInt i=0; i<count; i++)
       
   285 		{
       
   286 		const TAlarmId id = ids[i];
       
   287 		const TInt error = iAlarmServer.GetAlarmDetails(id, alarm);
       
   288 
       
   289 		// Alarm might have been deleted by another thread? Unlikely,
       
   290 		// but best to check
       
   291 		if	(error == KErrNone && alarm.HasBecomeOrphaned())
       
   292 			++ret;
       
   293 		}
       
   294 	ids.Close();
       
   295 	//
       
   296 	return ret;
       
   297 	}
       
   298 
       
   299 
       
   300 //*************************************************************************************
       
   301 EXPORT_C void ASTstAlarmTest::TestStartServers(TAny*
       
   302 #if defined (__WINS__)
       
   303 													 instructionSet
       
   304 #endif
       
   305 																   )
       
   306 	{
       
   307 
       
   308 #ifdef __WINS__ 
       
   309 
       
   310 const TUidType serverUid(KNullUid,KServerUid2,KServerUid3);
       
   311 
       
   312 	_LIT(KConsoleAlarmAlertServerImg,"ConsoleAlarmAlertServer");
       
   313 	RProcess server;
       
   314     TInt ret;
       
   315 	if ( instructionSet != NULL)
       
   316 	   {   
       
   317 		TBuf<256> cmdline;
       
   318 		RArray<TInt>* temp = static_cast<RArray<TInt>*>(instructionSet);
       
   319 	    TInt len = temp->Count();
       
   320 		for(TInt i=0;i<len;i++)
       
   321 		   {
       
   322 			cmdline.Append((*temp)[i]);
       
   323 		   }
       
   324         ret=server.Create(KConsoleAlarmAlertServerImg,cmdline,serverUid);
       
   325         iTest(ret == KErrNone);
       
   326 	   }
       
   327     else 
       
   328 		{
       
   329         ret=server.Create(KConsoleAlarmAlertServerImg,KNullDesC,serverUid);
       
   330         iTest(ret == KErrNone);
       
   331 		}
       
   332 
       
   333 	TRequestStatus stat;
       
   334 	server.Rendezvous(stat);
       
   335 
       
   336 	if (stat!=KRequestPending)
       
   337 		{
       
   338 		server.Kill(0);		// abort startup
       
   339 		}
       
   340 	else
       
   341 		{
       
   342 		server.Resume();	// logon OK - start the server
       
   343 	}
       
   344 #endif // __WINS__ 
       
   345 
       
   346 	// Start the alarm server
       
   347 	const TInt r = iAlarmServer.Connect();
       
   348 	if	(r != KErrNone)
       
   349 		{
       
   350 		_LIT(KStartupFailure, "Couldn't connect to AS");
       
   351 		User::Panic(KStartupFailure, 0);
       
   352 		}
       
   353 	}
       
   354 
       
   355 
       
   356 void ASTstAlarmTest::resetHomeTimeL()
       
   357 {
       
   358 	RPIMTestServer serv;
       
   359     User::LeaveIfError(serv.Connect()); 
       
   360     serv.SetHomeTime(iInitialHomeTime);
       
   361     serv.Close();
       
   362 }
       
   363 
       
   364 //
       
   365 // ----> Global Methods (source)
       
   366 //
       
   367 
       
   368 //*************************************************************************************