|
1 // Copyright (c) 2008-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 "ASSrvDefs.h" |
|
18 #include <tz.h> |
|
19 #include <vtzrules.h> |
|
20 #include <e32math.h> |
|
21 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
22 #include <tzusernames.h> |
|
23 #include <tzuserdefineddata.h> |
|
24 #endif |
|
25 |
|
26 // |
|
27 class CUserDefinedFloatingAlarmTest:public CBase |
|
28 { |
|
29 public: |
|
30 static CUserDefinedFloatingAlarmTest* NewLC(); |
|
31 ~CUserDefinedFloatingAlarmTest(); |
|
32 void TestFloatingAlarmWithUserDefinedRulesL(); |
|
33 |
|
34 private: |
|
35 CUserDefinedFloatingAlarmTest(); |
|
36 void DeleteAllAlarmsL(); |
|
37 void TestTimesEqual(const TTime& aTime1, const TTime& aTime2); |
|
38 void ResetDataL(); |
|
39 CTzRules* CreateRulesL(TInt aOffset); |
|
40 CTzUserNames* CreateNamesL(); |
|
41 void CreateString(TDes& aString); |
|
42 |
|
43 private: |
|
44 TInt64 iSeed; |
|
45 }; |
|
46 |
|
47 CUserDefinedFloatingAlarmTest::CUserDefinedFloatingAlarmTest():iSeed(100) |
|
48 { |
|
49 } |
|
50 |
|
51 CUserDefinedFloatingAlarmTest::~CUserDefinedFloatingAlarmTest() |
|
52 { |
|
53 TRAP_IGNORE(ResetDataL()); |
|
54 } |
|
55 |
|
56 CUserDefinedFloatingAlarmTest* CUserDefinedFloatingAlarmTest::NewLC() |
|
57 { |
|
58 CUserDefinedFloatingAlarmTest* self = new(ELeave) CUserDefinedFloatingAlarmTest(); |
|
59 CleanupStack::PushL(self); |
|
60 return self; |
|
61 } |
|
62 |
|
63 void CUserDefinedFloatingAlarmTest::TestTimesEqual(const TTime& aTime1, const TTime& aTime2) |
|
64 { |
|
65 // determine the difference, in minutes |
|
66 TTimeIntervalMinutes timeDiffMinutes; |
|
67 const TInt error = aTime2.MinutesFrom(aTime1, timeDiffMinutes); |
|
68 TheAlarmTest(error == KErrNone); |
|
69 // check the times are within 1 minute |
|
70 const TTimeIntervalMinutes maxDiff(1); |
|
71 const TTimeIntervalMinutes minDiff(-1); |
|
72 TheAlarmTest(((timeDiffMinutes <= maxDiff) && (timeDiffMinutes >= minDiff))); |
|
73 } |
|
74 |
|
75 void CUserDefinedFloatingAlarmTest::DeleteAllAlarmsL() |
|
76 { |
|
77 // Delete all alarms |
|
78 RArray<TAlarmCategory> categories; |
|
79 TheAlarmTest.Session().GetAvailableCategoryListL(categories); |
|
80 TInt count = categories.Count(); |
|
81 for(TInt i=0; i<count; i++) |
|
82 { |
|
83 const TAlarmCategory category = categories[i]; |
|
84 User::LeaveIfError(TheAlarmTest.Session().AlarmDeleteAllByCategory(category, EFalse)); |
|
85 } |
|
86 categories.Close(); |
|
87 } |
|
88 |
|
89 LOCAL_D void ResetAndDestroyTzIdArray(TAny* aPtr) |
|
90 { |
|
91 RPointerArray<CTzId>* ids = static_cast<RPointerArray<CTzId>*>(aPtr); |
|
92 if (ids) |
|
93 { |
|
94 ids->ResetAndDestroy(); |
|
95 } |
|
96 } |
|
97 |
|
98 void CUserDefinedFloatingAlarmTest::ResetDataL() |
|
99 { |
|
100 RTz tz; |
|
101 User::LeaveIfError(tz.Connect()); |
|
102 CleanupClosePushL(tz); |
|
103 CTzUserData* userdata = CTzUserData::NewL(tz); |
|
104 CleanupStack::PushL(userdata); |
|
105 RPointerArray<CTzId> ids; |
|
106 CleanupStack::PushL(TCleanupItem(ResetAndDestroyTzIdArray, &ids)); |
|
107 userdata->GetTzIdsL(ids); |
|
108 TInt count = ids.Count(); |
|
109 for (TInt ii=0; ii<count; ++ii) |
|
110 { |
|
111 userdata->DeleteL(*ids[ii]); |
|
112 } |
|
113 CleanupStack::PopAndDestroy(3, &tz); |
|
114 } |
|
115 |
|
116 CTzRules* CUserDefinedFloatingAlarmTest::CreateRulesL(TInt aOffset) |
|
117 { |
|
118 //Create time zone rules |
|
119 TTzRule rule1(2007, 2009, aOffset, 0, EOctober, ETzFixedDate, 0, 0, ETzWallTimeReference, 120); |
|
120 TTzRule rule2(2007,2009,0,aOffset,EMarch,ETzFixedDate,0,0,ETzWallTimeReference,0); |
|
121 |
|
122 CTzRules* rules = CTzRules::NewL(0, 9999); |
|
123 CleanupStack::PushL(rules); |
|
124 rules->AddRuleL(rule1); |
|
125 rules->AddRuleL(rule2); |
|
126 |
|
127 return rules; |
|
128 } |
|
129 |
|
130 void CUserDefinedFloatingAlarmTest::CreateString(TDes& aString) |
|
131 { |
|
132 for (TInt i(0) ; i < 10 ; ++i) |
|
133 { |
|
134 // Add random upper and lower case letters |
|
135 aString.Append( 'A' + (Math::Rand(iSeed) % 26) ); |
|
136 } |
|
137 } |
|
138 |
|
139 CTzUserNames* CUserDefinedFloatingAlarmTest::CreateNamesL() |
|
140 { |
|
141 TBuf<10> stdName; |
|
142 CreateString(stdName); |
|
143 TBuf<10> shortStdName; |
|
144 CreateString(shortStdName); |
|
145 TBuf<10> dstName; |
|
146 CreateString(dstName); |
|
147 TBuf<10> shortDstName; |
|
148 CreateString(shortDstName); |
|
149 TBuf<10> cityName; |
|
150 CreateString(cityName); |
|
151 TBuf<10> regionName; |
|
152 CreateString(regionName); |
|
153 |
|
154 return CTzUserNames::NewLC(stdName, shortStdName, dstName, shortDstName, cityName, regionName); |
|
155 } |
|
156 |
|
157 /** Floating local time alarm using user defined time zone |
|
158 |
|
159 @SYMTestCaseID PIM-APPSRV-ALS-TZC-0001 |
|
160 @SYMTestCaseDesc The purpose of this test is to verify that |
|
161 the expiry time of a floating local time |
|
162 alarm is correct when the time zone rules |
|
163 data for the current time zone is changed. |
|
164 @SYMTestActions 1. Create a user defined time zone with a UTC |
|
165 offset of 2 hours. |
|
166 2. Set the current time zone to the user defined |
|
167 time zone. |
|
168 3. Create a floating local time alarm using the |
|
169 Alarm Server that is due to expire 2 hours + |
|
170 t minutes from the current local time. |
|
171 4. Modify the user defined time zone to have a UTC |
|
172 offset of 0 hours. |
|
173 @SYMTestExpectedResults The alarm expires no longer than t minutes from the |
|
174 current local time. |
|
175 @SYMTestType CIT |
|
176 @SYMTestPriority 1 |
|
177 |
|
178 */ |
|
179 void CUserDefinedFloatingAlarmTest::TestFloatingAlarmWithUserDefinedRulesL() |
|
180 { |
|
181 //Clear the alarmserver's queue |
|
182 DeleteAllAlarmsL(); |
|
183 //Clear the user-defined database |
|
184 ResetDataL(); |
|
185 RTz tz; |
|
186 CleanupClosePushL(tz); |
|
187 User::LeaveIfError(tz.Connect()); |
|
188 |
|
189 CTzUserData* userData = CTzUserData::NewL(tz); |
|
190 CleanupStack::PushL(userData); |
|
191 TheAlarmTest.Test().Printf(_L("@SYMTestCaseID PIM-APPSRV-ALS-TZC-0001 Add a user-defined time zone rule to the database\n")); |
|
192 |
|
193 //aOffset = 60 |
|
194 CTzRules* rules = CreateRulesL(60); |
|
195 |
|
196 CTzUserNames* names = CreateNamesL(); |
|
197 |
|
198 CTzId* id = NULL; |
|
199 id = userData->CreateL(*rules, *names); //Create user-defined rules |
|
200 |
|
201 CleanupStack::PopAndDestroy(2, rules); |
|
202 |
|
203 //Set the current time zone to the user-defined zone |
|
204 tz.SetTimeZoneL(*id); |
|
205 tz.SetHomeTime(TDateTime(2008, EJuly, 11, 7, 0, 0, 0)); |
|
206 |
|
207 TTime now; |
|
208 now.HomeTime(); |
|
209 TDateTime dt = now.DateTime(); |
|
210 TheAlarmTest.Test().Printf(_L("The current home time is hours: %d, minutes: %d, seconds:%d \r\n"), |
|
211 dt.Hour(), dt.Minute(), dt.Second()); |
|
212 |
|
213 TASShdAlarm alarm; |
|
214 alarm.Category() = KASCliCategoryClock; |
|
215 alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatOnce; |
|
216 |
|
217 TDateTime date; |
|
218 //Set the time for the floating alarm |
|
219 alarm.NextDueTime().HomeTime(); |
|
220 alarm.NextDueTime() += TTimeIntervalMinutes(61); |
|
221 //Add the floating alarm to the server's queue |
|
222 TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__); |
|
223 const TAlarmId alarmId = alarm.Id(); |
|
224 TheAlarmTest(alarmId != KNullAlarmId, __LINE__); |
|
225 |
|
226 date=alarm.NextDueTime().DateTime(); |
|
227 TheAlarmTest.Test().Printf(_L("Floating alarm with id %d has due time hours: %d,\ |
|
228 minutes: %d, seconds: %d.\r\n"), alarmId,date.Hour(), date.Minute(), date.Second()); |
|
229 |
|
230 CTzRules* newRule = CreateRulesL(120); |
|
231 |
|
232 CTzUserNames* newNames = CreateNamesL(); |
|
233 |
|
234 //Update the rule |
|
235 userData->UpdateL(*id, *newRule, *newNames); |
|
236 |
|
237 now.HomeTime(); |
|
238 dt = now.DateTime(); |
|
239 TheAlarmTest.Test().Printf(_L("The new local time after update is hours: %d, minutes: %d, seconds:%d \r\n"), |
|
240 dt.Hour(), dt.Minute(), dt.Second()); |
|
241 |
|
242 date = alarm.NextDueTime().DateTime(); |
|
243 TheAlarmTest.Test().Printf(_L("Floating alarm has a new due time : hours: %d, minutes:\ |
|
244 %d, seconds: %d.\r\n"),date.Hour(), date.Minute(), date.Second()); |
|
245 |
|
246 now.HomeTime(); |
|
247 TestTimesEqual(alarm.NextDueTime(), now); |
|
248 |
|
249 CleanupStack::PopAndDestroy(2, newRule); |
|
250 delete id; |
|
251 CleanupStack::PopAndDestroy(userData); |
|
252 CleanupStack::PopAndDestroy(&tz); |
|
253 // Clear all the alarms from the queue |
|
254 DeleteAllAlarmsL(); |
|
255 ResetDataL(); |
|
256 } |
|
257 |
|
258 // |
|
259 |
|
260 static void doTestsL() |
|
261 { |
|
262 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler; |
|
263 CleanupStack::PushL(scheduler); |
|
264 CActiveScheduler::Install(scheduler); |
|
265 |
|
266 TheAlarmTest.TestStartServers(); |
|
267 TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone, __LINE__); |
|
268 |
|
269 CUserDefinedFloatingAlarmTest* testFloatingAlarm = CUserDefinedFloatingAlarmTest::NewLC(); |
|
270 |
|
271 testFloatingAlarm->TestFloatingAlarmWithUserDefinedRulesL(); |
|
272 |
|
273 // cleanup |
|
274 CleanupStack::PopAndDestroy(testFloatingAlarm); |
|
275 CleanupStack::PopAndDestroy(scheduler); |
|
276 } |
|
277 |
|
278 // |
|
279 |
|
280 |
|
281 GLDEF_C TInt E32Main() |
|
282 { |
|
283 __UHEAP_MARK; |
|
284 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
285 if (!cleanup) |
|
286 return KErrNoMemory; |
|
287 |
|
288 TheAlarmTest.Test().Title(); |
|
289 TheAlarmTest.Test().Start(_L("Alarms with user-defined rules")); |
|
290 |
|
291 |
|
292 TRAPD(error, doTestsL()); |
|
293 |
|
294 // did any test leave? |
|
295 TheAlarmTest(error == KErrNone, __LINE__); |
|
296 |
|
297 TheAlarmTest.Test().End(); |
|
298 TheAlarmTest.Test().Close(); |
|
299 ASTstAlarmTest::Close(); |
|
300 delete cleanup; |
|
301 __UHEAP_MARKEND; |
|
302 return KErrNone; |
|
303 } |