commonappservices/alarmserver/Test/TOomTest.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 
       
    18 // System includes
       
    19 #include <asclisoundplay.h>
       
    20 
       
    21 // Globals
       
    22 static TTime time;
       
    23 static TAlarmId id;
       
    24 static TInt ret;
       
    25 
       
    26 // Type definitions
       
    27 typedef CArrayFixFlat<TASCliSoundPlayDefinition> CPlayIntervalArray;
       
    28 
       
    29 // This define appears in sa_mng.cpp - update it if it changes there!
       
    30 #define KDefaultAlarmCycleStartOffsetMinutes	{0,1,2,3,5,10,20,30,45,60,90,120,180,240,KErrNotFound};
       
    31 
       
    32 static TAlarmId AddAlarm(TTime aTime)
       
    33 	{
       
    34 	TASShdAlarm alarm;
       
    35 	alarm.Category() = KASCliCategoryClock;
       
    36 	alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatOnce;
       
    37 	alarm.NextDueTime() = aTime;
       
    38 	//
       
    39 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
    40 	const TAlarmId id = alarm.Id();
       
    41 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
    42 
       
    43 	return id;
       
    44 	}
       
    45 
       
    46 static TAlarmId AddUtcAlarm(TTime aTime)
       
    47 	{
       
    48 	TASShdAlarm alarm;
       
    49 	alarm.Category()=KASCliCategoryClock;
       
    50 	alarm.RepeatDefinition()=EAlarmRepeatDefintionRepeatOnce;
       
    51 	alarm.SetUtcNextDueTime(aTime);
       
    52 	//
       
    53 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
    54 	const TAlarmId id = alarm.Id();
       
    55 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
    56 
       
    57 	return id;
       
    58 	}
       
    59 
       
    60 
       
    61 
       
    62 static void TestDefaultSoundInfoL()
       
    63 //
       
    64 //	Get default sound info details
       
    65 //
       
    66 	{
       
    67 	TheAlarmTest.Test().Next(_L("Test the default sound play intervals"));
       
    68 	// When the server is started, the alarm sound intervals should currently be
       
    69 	// set to their defaults defined in KDefaultAlarmCycleStartOffsetMinutes
       
    70 
       
    71 	// Initialise sound array with default values
       
    72 	CPlayIntervalArray* array = new(ELeave) CPlayIntervalArray(20);
       
    73 	CleanupStack::PushL(array);
       
    74 
       
    75 	// Get default sound intervals
       
    76 	TheAlarmTest.Session().GetAlarmPlayIntervalsL(*array);
       
    77 
       
    78 	// Check that the default values are correct
       
    79 	const TInt minuteCycle[]=KDefaultAlarmCycleStartOffsetMinutes;
       
    80 	TInt i = 0;
       
    81 	TInt offset = minuteCycle[i];
       
    82 	while (offset != KErrNotFound)
       
    83 		{
       
    84 		const TASCliSoundPlayDefinition& item = array->At(i);
       
    85 		TheAlarmTest(item.Offset().Int() == offset, __LINE__);
       
    86 		TheAlarmTest(item.Duration().Int() == KDefaultSoundPlayDurationInSeconds, __LINE__);
       
    87 		offset = minuteCycle[++i];
       
    88 		}
       
    89 	CleanupStack::PopAndDestroy();
       
    90 	}
       
    91 
       
    92 	
       
    93 static void TestAlarmFunctionalityL()
       
    94 	{
       
    95 	TheAlarmTest.Test().Next(_L("Test alarm functionality"));
       
    96 
       
    97 	TheAlarmTest.TestClearStoreL();
       
    98 
       
    99 	time.HomeTime();
       
   100 	time += TTimeIntervalMinutes(1);
       
   101 	id = AddAlarm(time);
       
   102 	TASShdAlarm alarm;
       
   103 	ret = TheAlarmTest.Session().GetAlarmDetails(id, alarm);
       
   104 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   105 	TAlarmCategory category;
       
   106 	ret = TheAlarmTest.Session().GetAlarmCategory(id, category);
       
   107 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   108 	TheAlarmTest(category == KASCliCategoryClock, __LINE__);
       
   109 	ret = TheAlarmTest.Session().SetAlarmStatus(id, EAlarmStatusDisabled);
       
   110 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   111 	TAlarmStatus status;
       
   112 	ret = TheAlarmTest.Session().GetAlarmStatus(id, status);
       
   113 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   114 	TheAlarmTest(status == EAlarmStatusDisabled, __LINE__);
       
   115 	}
       
   116 
       
   117 
       
   118 	
       
   119 static void TestAlarmDataFunctionalityL()
       
   120 	{
       
   121 	TheAlarmTest.Test().Next(_L("Test alarm data functionality"));
       
   122 
       
   123 	time.HomeTime();
       
   124 	time += TTimeIntervalMinutes(1);
       
   125 	id = AddAlarm(time);
       
   126 
       
   127 	_LIT8(KAlarmData, "some 8-bit data to attach to the alarm");
       
   128 
       
   129 	ret = TheAlarmTest.Session().AlarmDataAttachL(id, KAlarmData());
       
   130 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   131 	ret = TheAlarmTest.Session().AlarmDataSize(id);
       
   132 	TheAlarmTest(ret > 0, __LINE__);
       
   133 
       
   134 	HBufC8* temp = HBufC8::NewLC(KAlarmData().Size());
       
   135 	TPtr8 pData(temp->Des());
       
   136 	ret = TheAlarmTest.Session().GetAlarmData(id, pData);
       
   137 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   138 	TheAlarmTest(pData.CompareC(KAlarmData()) == 0, __LINE__);
       
   139 	CleanupStack::PopAndDestroy(temp);
       
   140 	}
       
   141 
       
   142 
       
   143 static void TestOtherFunctionalityL()
       
   144 	{
       
   145 	TheAlarmTest.Test().Next(_L("Test other functionality"));
       
   146 
       
   147 	TheAlarmTest.TestClearStoreL();
       
   148 
       
   149 	time.HomeTime();
       
   150 	time += TTimeIntervalMinutes(1);
       
   151 	id = AddAlarm(time);
       
   152 	time -= TTimeIntervalSeconds(30);
       
   153 	id = AddAlarm(time);
       
   154 
       
   155 	ret = TheAlarmTest.Session().SetAlarmStatusByCategory(KASCliCategoryClock, EAlarmStatusEnabled);
       
   156 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   157 
       
   158 	ret = TheAlarmTest.Session().GetAlarmCountForCategory(KASCliCategoryClock);
       
   159 	TheAlarmTest(ret == 2, __LINE__);
       
   160 
       
   161 	RArray<TAlarmCategory> categories;
       
   162 	TheAlarmTest.Session().GetAvailableCategoryListL(categories);
       
   163 	TheAlarmTest(categories.Count() == 1, __LINE__);
       
   164 	categories.Reset();
       
   165 
       
   166 	RArray<TAlarmId> ids;
       
   167 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, ids);
       
   168 	TheAlarmTest(ids.Count() == 2, __LINE__);
       
   169 	ids.Reset();
       
   170 
       
   171 	ret = TheAlarmTest.Session().AlarmCountByState(EAlarmStateQueued);
       
   172 	TheAlarmTest(ret == 2, __LINE__);
       
   173 
       
   174 	TheAlarmTest.Session().GetAlarmIdListL(ids);
       
   175 	TheAlarmTest(ids.Count() == 2, __LINE__);
       
   176 	ids.Reset();
       
   177 
       
   178 	TAlarmId nextDueId;
       
   179 	ret = TheAlarmTest.Session().GetNextDueAlarmId(nextDueId);
       
   180 	TheAlarmTest(ret == KErrNone && nextDueId == id, __LINE__);
       
   181 
       
   182 	ret = TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   183 	TheAlarmTest(ret == 2, __LINE__);
       
   184 
       
   185 	ret = TheAlarmTest.Session().AlarmDeleteAllByCategory(KASCliCategoryClock, EFalse);
       
   186 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   187 	}
       
   188 
       
   189 static void TestUtcAlarmFunctionalityL()
       
   190 	{
       
   191 	TheAlarmTest.Test().Next(_L("Test UTC alarm functionality"));
       
   192 
       
   193 	TheAlarmTest.TestClearStoreL();
       
   194 
       
   195 	time.HomeTime();
       
   196 	time += TTimeIntervalMinutes(1);
       
   197 	id = AddUtcAlarm(time);
       
   198 	TASShdAlarm alarm;
       
   199 	ret = TheAlarmTest.Session().GetAlarmDetails(id, alarm);
       
   200 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   201 	TAlarmCategory category;
       
   202 	ret = TheAlarmTest.Session().GetAlarmCategory(id, category);
       
   203 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   204 	TheAlarmTest(category == KASCliCategoryClock, __LINE__);
       
   205 	ret = TheAlarmTest.Session().SetAlarmStatus(id, EAlarmStatusDisabled);
       
   206 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   207 	TAlarmStatus status;
       
   208 	ret = TheAlarmTest.Session().GetAlarmStatus(id, status);
       
   209 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   210 	TheAlarmTest(status == EAlarmStatusDisabled, __LINE__);
       
   211 	}
       
   212 
       
   213 
       
   214 	
       
   215 static void TestUtcAlarmDataFunctionalityL()
       
   216 	{
       
   217 	TheAlarmTest.Test().Next(_L("Test alarm data functionality"));
       
   218 
       
   219 	time.HomeTime();
       
   220 	time += TTimeIntervalMinutes(1);
       
   221 	id = AddUtcAlarm(time);
       
   222 
       
   223 	_LIT8(KAlarmData, "some 8-bit data to attach to the alarm");
       
   224 
       
   225 	ret = TheAlarmTest.Session().AlarmDataAttachL(id, KAlarmData());
       
   226 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   227 	ret = TheAlarmTest.Session().AlarmDataSize(id);
       
   228 	TheAlarmTest(ret > 0, __LINE__);
       
   229 
       
   230 	HBufC8* temp = HBufC8::NewLC(KAlarmData().Size());
       
   231 	TPtr8 pData(temp->Des());
       
   232 	ret = TheAlarmTest.Session().GetAlarmData(id, pData);
       
   233 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   234 	TheAlarmTest(pData.CompareC(KAlarmData()) == 0, __LINE__);
       
   235 	CleanupStack::PopAndDestroy(temp);
       
   236 	}
       
   237 
       
   238 
       
   239 static void TestOtherFunctionalityUtcL()
       
   240 	{
       
   241 	TheAlarmTest.Test().Next(_L("Test other functionality"));
       
   242 
       
   243 	TheAlarmTest.TestClearStoreL();
       
   244 
       
   245 	time.HomeTime();
       
   246 	time += TTimeIntervalMinutes(1);
       
   247 	id = AddUtcAlarm(time);
       
   248 	time -= TTimeIntervalSeconds(30);
       
   249 	id = AddUtcAlarm(time);
       
   250 
       
   251 	ret = TheAlarmTest.Session().SetAlarmStatusByCategory(KASCliCategoryClock, EAlarmStatusEnabled);
       
   252 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   253 
       
   254 	ret = TheAlarmTest.Session().GetAlarmCountForCategory(KASCliCategoryClock);
       
   255 	TheAlarmTest(ret == 2, __LINE__);
       
   256 
       
   257 	RArray<TAlarmCategory> categories;
       
   258 	TheAlarmTest.Session().GetAvailableCategoryListL(categories);
       
   259 	TheAlarmTest(categories.Count() == 1, __LINE__);
       
   260 	categories.Reset();
       
   261 
       
   262 	RArray<TAlarmId> ids;
       
   263 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, ids);
       
   264 	TheAlarmTest(ids.Count() == 2, __LINE__);
       
   265 	ids.Reset();
       
   266 
       
   267 	ret = TheAlarmTest.Session().AlarmCountByState(EAlarmStateQueued);
       
   268 	TheAlarmTest(ret == 2, __LINE__);
       
   269 
       
   270 	TheAlarmTest.Session().GetAlarmIdListL(ids);
       
   271 	TheAlarmTest(ids.Count() == 2, __LINE__);
       
   272 	ids.Reset();
       
   273 
       
   274 	TAlarmId nextDueId;
       
   275 	ret = TheAlarmTest.Session().GetNextDueAlarmId(nextDueId);
       
   276 	TheAlarmTest(ret == KErrNone && nextDueId == id, __LINE__);
       
   277 
       
   278 	ret = TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   279 	TheAlarmTest(ret == 2, __LINE__);
       
   280 
       
   281 	ret = TheAlarmTest.Session().AlarmDeleteAllByCategory(KASCliCategoryClock, EFalse);
       
   282 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   283 	}
       
   284 
       
   285 
       
   286 static void TestSoundControlL()
       
   287 	{
       
   288 	TheAlarmTest.Test().Next(_L("Test sound control"));
       
   289 
       
   290 	TheAlarmTest.TestClearStoreL();
       
   291 
       
   292 	ret = TheAlarmTest.Session().SetAlarmSoundState(EAlarmGlobalSoundStateOff);
       
   293 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   294 
       
   295 	TAlarmGlobalSoundState soundState;
       
   296 	ret = TheAlarmTest.Session().GetAlarmSoundState(soundState);
       
   297 	TheAlarmTest(ret == KErrNone && soundState == EAlarmGlobalSoundStateOff, __LINE__);
       
   298 
       
   299 	time.HomeTime();
       
   300 	time += TTimeIntervalMinutes(1);
       
   301 	ret = TheAlarmTest.Session().SetAlarmSoundsSilentUntil(time);
       
   302 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   303 
       
   304 	ret = TheAlarmTest.Session().CancelAlarmSilence();
       
   305 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   306 
       
   307 	ret = TheAlarmTest.Session().SetAlarmSoundsSilentFor(1);
       
   308 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   309 
       
   310 	ret = TheAlarmTest.Session().GetAlarmSoundsSilentUntil(time);
       
   311 	TheAlarmTest(ret == KErrNone, __LINE__);
       
   312 
       
   313 	if (!TheAlarmTest.Session().AlarmSoundsTemporarilySilenced())
       
   314 		{
       
   315 		User::Leave(KErrGeneral);
       
   316 		}
       
   317 	}
       
   318 
       
   319 	
       
   320 static void TestStartUpAndShutDownL()
       
   321 	{
       
   322 #ifdef _DEBUG
       
   323 #ifdef __WINS__
       
   324 	TheAlarmTest.Test().Next(_L("Test start up and shut down"));
       
   325 
       
   326 	TheAlarmTest.Session().__DbgShutDownServer();
       
   327 	TheAlarmTest.TestStartServers();
       
   328 	TheAlarmTest.TestClearStoreL();
       
   329 
       
   330 	time.HomeTime();
       
   331 	time += TTimeIntervalMinutes(5);
       
   332 	id = AddAlarm(time);
       
   333 
       
   334 	TheAlarmTest.Test().Printf(_L("\nDebug only - shutting down server\n"));
       
   335 	TheAlarmTest.Session().__DbgShutDownServer();
       
   336 	User::After(3 * 1000000);
       
   337 
       
   338 	TheAlarmTest.TestStartServers();
       
   339 
       
   340 	TAlarmId nextDueId;
       
   341 	ret = TheAlarmTest.Session().GetNextDueAlarmId(nextDueId);
       
   342 	TheAlarmTest(ret == KErrNone && nextDueId == id, __LINE__);
       
   343 
       
   344 	time -= TTimeIntervalMinutes(1);
       
   345 	id = AddAlarm(time);
       
   346 
       
   347 	TheAlarmTest.Session().__DbgShutDownServer();
       
   348 	User::After(3 * 1000000);
       
   349 
       
   350 	TheAlarmTest.TestStartServers();
       
   351 
       
   352 	ret = TheAlarmTest.Session().GetNextDueAlarmId(nextDueId);
       
   353 	TheAlarmTest(ret == KErrNone && nextDueId == id, __LINE__);
       
   354 #endif
       
   355 #endif
       
   356 	}
       
   357 
       
   358 
       
   359 static void TestStartUpAndShutDownUtcL()
       
   360 	{
       
   361 #ifdef _DEBUG
       
   362 #ifdef __WINS__
       
   363 	TheAlarmTest.Test().Next(_L("Test start up and shut down with UTC Alarms"));
       
   364 
       
   365 	TheAlarmTest.Session().__DbgShutDownServer();
       
   366 	TheAlarmTest.TestStartServers();
       
   367 	TheAlarmTest.TestClearStoreL();
       
   368 
       
   369 	time.HomeTime();
       
   370 	time += TTimeIntervalMinutes(5);
       
   371 	id = AddUtcAlarm(time);
       
   372 
       
   373 	TheAlarmTest.Test().Printf(_L("\nDebug only - shutting down server\n"));
       
   374 	TheAlarmTest.Session().__DbgShutDownServer();
       
   375 	User::After(3 * 1000000);
       
   376 
       
   377 	TheAlarmTest.TestStartServers();
       
   378 
       
   379 	TAlarmId nextDueId;
       
   380 	ret = TheAlarmTest.Session().GetNextDueAlarmId(nextDueId);
       
   381 	TheAlarmTest(ret == KErrNone && nextDueId == id, __LINE__);
       
   382 
       
   383 	time -= TTimeIntervalMinutes(1);
       
   384 	id = AddUtcAlarm(time);
       
   385 
       
   386 	TheAlarmTest.Session().__DbgShutDownServer();
       
   387 	User::After(3 * 1000000);
       
   388 
       
   389 	TheAlarmTest.TestStartServers();
       
   390 
       
   391 	ret = TheAlarmTest.Session().GetNextDueAlarmId(nextDueId);
       
   392 	TheAlarmTest(ret == KErrNone && nextDueId == id, __LINE__);
       
   393 #endif
       
   394 #endif
       
   395 	}
       
   396 
       
   397 
       
   398 static void DoTestsL()
       
   399 	{
       
   400 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
   401 	CleanupStack::PushL(scheduler);
       
   402 	CActiveScheduler::Install(scheduler);
       
   403 
       
   404 	TheAlarmTest.Next(_L("Connecting to server"));
       
   405 	const TInt r = TheAlarmTest.Session().Connect();
       
   406 	TheAlarmTest(r == KErrNone, __LINE__);
       
   407 	TheAlarmTest.TestClearStoreL();
       
   408 
       
   409 	TestAlarmFunctionalityL();
       
   410 	TestAlarmDataFunctionalityL();
       
   411 	TestOtherFunctionalityL();
       
   412 
       
   413 	TestStartUpAndShutDownL();
       
   414 
       
   415 	TheAlarmTest.TestClearStoreL();
       
   416 
       
   417 	TestDefaultSoundInfoL();
       
   418 	TestUtcAlarmFunctionalityL();
       
   419 	TestUtcAlarmDataFunctionalityL();
       
   420 	TestOtherFunctionalityUtcL();
       
   421 	TestSoundControlL();
       
   422 
       
   423 	TestStartUpAndShutDownUtcL();
       
   424 
       
   425 	CleanupStack::PopAndDestroy(scheduler);
       
   426 	}
       
   427 
       
   428 void DoOOMTestL()
       
   429 	{
       
   430 	TheAlarmTest.Test().Printf(_L("starting OOM test\n"));
       
   431 
       
   432 	//reserve space on cleanup stack
       
   433 	for(TInt i=0; i<1000; i++)
       
   434 		{
       
   435 		CleanupStack::PushL(&i);
       
   436 		}
       
   437 	CleanupStack::Pop(1000);
       
   438 
       
   439 	TInt ret=KErrNoMemory;
       
   440 	TInt failAt=0;
       
   441 	while (ret!=KErrNone)
       
   442 		{
       
   443 		failAt++;
       
   444 		__UHEAP_SETFAIL(RHeap::EDeterministic,failAt);
       
   445 		__UHEAP_MARK;
       
   446 		TRAP(ret, DoTestsL());
       
   447 		if (ret!=KErrNone)
       
   448 			{
       
   449 			__UHEAP_MARKEND;
       
   450 			}
       
   451 		__UHEAP_RESET;
       
   452 		TheAlarmTest(ret==KErrNoMemory||ret==KErrNone, __LINE__);
       
   453 		}
       
   454 	}
       
   455 
       
   456 /**
       
   457 @SYMTestCaseID PIM-TOOMTEST-0001
       
   458 */	
       
   459 GLDEF_C TInt E32Main()
       
   460 //
       
   461 // Test the alarm server.
       
   462 //
       
   463 	{
       
   464 	__UHEAP_MARK;
       
   465 	TInt ret = KErrNone;
       
   466 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   467 	if (!cleanup)
       
   468 		{
       
   469 		return KErrNoMemory;
       
   470 		}
       
   471 
       
   472 	TheAlarmTest.Start(_L("@SYMTestCaseID PIM-TOOMTEST-0001"));
       
   473 	TheAlarmTest.Title();
       
   474 	TheAlarmTest.TestStartServers();
       
   475 
       
   476 	TRAPD(error, DoOOMTestL());
       
   477 
       
   478 	TheAlarmTest(error == KErrNone, __LINE__);
       
   479 	TRAP(ret,TheAlarmTest.EndL());
       
   480     TheAlarmTest.Test().Close();
       
   481 
       
   482 	ASTstAlarmTest::Close();
       
   483 
       
   484 	delete cleanup;
       
   485 	__UHEAP_MARKEND;
       
   486 	return ret;
       
   487 	}