|
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 "t_CaseSensitiveDB.h" |
|
17 |
|
18 // System includes |
|
19 #include <e32std.h> |
|
20 #include <e32test.h> |
|
21 #include <cntdbobs.h> |
|
22 |
|
23 #include <cntitem.h> |
|
24 #include <cntfldst.h> |
|
25 |
|
26 #include "coreappstest/testserver.h" |
|
27 |
|
28 |
|
29 // Globals |
|
30 static RTest TheTest(_L("T_CASESENSITIVEDB - Test CntModel support")); |
|
31 static CTrapCleanup* TheCleanup; |
|
32 |
|
33 // Constants (literal) |
|
34 |
|
35 // Configure database location |
|
36 _LIT(KTestDatabaseFileU, "C:Contacts.cdb"); |
|
37 _LIT(KTestDatabaseFileL, "C:contacts.cdb"); |
|
38 |
|
39 // Configure .ini file location |
|
40 _LIT(KContactsModelIniFile, "C:\\private\\10003A73\\CntModel.ini"); |
|
41 |
|
42 |
|
43 _LIT(KTestSemaphoreName, "T_CASESENSITIVEDB_SEMAPHORE"); |
|
44 |
|
45 _LIT(KNewContactName,"Test Name"); |
|
46 |
|
47 class CTestActiveScheduler : public CActiveScheduler |
|
48 { |
|
49 public: |
|
50 void Error (TInt aError) const; |
|
51 }; |
|
52 |
|
53 void CTestActiveScheduler::Error(TInt aError) const |
|
54 { |
|
55 User::Panic(_L("AScheduler"),aError); |
|
56 } |
|
57 |
|
58 CTestBase::CTestBase(TInt aClientNumber, TThreadPriority aThreadPriority, TInt aPriority) |
|
59 : CActive(aPriority), |
|
60 iClientNumber(aClientNumber), |
|
61 iThreadPriority(aThreadPriority) |
|
62 { |
|
63 } |
|
64 |
|
65 CTestBase::~CTestBase() |
|
66 { |
|
67 Cancel(); |
|
68 iThread.Close(); |
|
69 } |
|
70 |
|
71 void CTestBase::ConstructL(const TDesC& aName) |
|
72 { |
|
73 User::LeaveIfError(iThread.Create(aName, ThreadFunction, KDefaultStackSize, 0x2000, 0x20000, this, EOwnerThread)); |
|
74 iThread.Resume(); |
|
75 iThread.SetPriority(iThreadPriority); |
|
76 } |
|
77 |
|
78 void CTestBase::Complete(TInt aReason) |
|
79 { |
|
80 // Complete the request with the specified reason. |
|
81 __ASSERT_ALWAYS(IsActive() && iStatus.Int() == KRequestPending, User::Invariant()); |
|
82 TRequestStatus* status = &iStatus; |
|
83 iThread.RequestComplete(status, aReason); |
|
84 } |
|
85 |
|
86 void CTestBase::RunL() |
|
87 { |
|
88 if(!iTestFinished) |
|
89 { |
|
90 if(!iWaiting) |
|
91 { |
|
92 DoTestL(); |
|
93 CActiveScheduler::Stop(); |
|
94 } |
|
95 else |
|
96 { |
|
97 //if test hasnt finished and is waiting for notification then it must have |
|
98 //failed as timer on waiting for notification has expired. |
|
99 iTest->Printf(_L("ERROR:No notification received")); |
|
100 (*iTest)(EFalse); |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 void CTestBase::DoCancel() |
|
106 { |
|
107 iWaitTimer.Cancel(); |
|
108 |
|
109 // Cancel any outstanding request |
|
110 if (!(iWaiting)) |
|
111 Complete(KErrCancel); |
|
112 } |
|
113 |
|
114 TInt CTestBase::ThreadFunction(TAny* aParam) |
|
115 { |
|
116 CTestBase* self = STATIC_CAST(CTestBase*, aParam); |
|
117 |
|
118 // Prepare the stuff required before we start the |
|
119 // active scheduler. |
|
120 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
121 if (!cleanup) |
|
122 return KErrNoMemory; |
|
123 |
|
124 // Call virtual handler which does anything that needs doing as |
|
125 // a result of the thread function from being created. |
|
126 TRAPD(err, self->RunTestsL()); |
|
127 |
|
128 delete cleanup; |
|
129 return err; |
|
130 } |
|
131 |
|
132 void CTestBase::RunTestsL() |
|
133 { |
|
134 User::LeaveIfError(iWaitTimer.CreateLocal()); |
|
135 |
|
136 TBuf<100> threadName; |
|
137 threadName.Format(_L("Test thread %d"), iClientNumber); |
|
138 iTest = new(ELeave) RTest(threadName); |
|
139 iTest->Start(threadName); |
|
140 iTest->Printf(_L("Starting Test thread %d \n"), iClientNumber); |
|
141 //create Active Schedular for this thread |
|
142 CActiveScheduler::Install(new(ELeave) CActiveScheduler); |
|
143 |
|
144 // Create contacts db session |
|
145 OpenDatabaseL(); |
|
146 |
|
147 // Add the CTestBase object |
|
148 // to it. Has to be done in this function, otherwise the AO can |
|
149 // end up being added to the wrong active scheduler |
|
150 CActiveScheduler::Add(this); |
|
151 |
|
152 //Set request status |
|
153 SetRequestStatus(); |
|
154 |
|
155 CActiveScheduler::Start(); |
|
156 |
|
157 //close thread |
|
158 iTest->Printf(_L("\tClosing thread %d \n"), iClientNumber); |
|
159 iTest->End(); |
|
160 iTest->Close(); |
|
161 |
|
162 delete iDb; |
|
163 delete CActiveScheduler::Current(); |
|
164 |
|
165 iWaitTimer.Cancel(); |
|
166 iWaitTimer.Close(); |
|
167 |
|
168 //Setting this flag should be the last thing that happens |
|
169 iTestFinished = ETrue; |
|
170 } |
|
171 |
|
172 // Requests another test be executed |
|
173 void CTestBase::SetRequestStatus() |
|
174 { |
|
175 __ASSERT_ALWAYS(!iWaiting, User::Invariant()); |
|
176 SetActive(); |
|
177 iStatus = KRequestPending; |
|
178 } |
|
179 |
|
180 void CTestBase::WaitForContactsEvent(TContactDbObserverEventType aEvent, TInt aTimeInSecondsToWaitFor) |
|
181 { |
|
182 iWaiting = ETrue; |
|
183 iEvent = aEvent; |
|
184 TTimeIntervalMicroSeconds32 time(aTimeInSecondsToWaitFor * 1000000); |
|
185 |
|
186 iWaitTimer.After(iStatus, time); |
|
187 SetActive(); |
|
188 CActiveScheduler::Start(); |
|
189 } |
|
190 |
|
191 void CTestBase::HandleDatabaseEventL(TContactDbObserverEvent aEvent) |
|
192 { |
|
193 __ASSERT_ALWAYS(iWaiting, User::Invariant()); |
|
194 |
|
195 switch(aEvent.iType) |
|
196 { |
|
197 case EContactDbObserverEventContactAdded: |
|
198 iTest->Printf(_L("Event: EContactDbObserverEventContactAdded\n")); |
|
199 break; |
|
200 default: |
|
201 iTest->Printf(_L("Unhandled Event\n")); |
|
202 } |
|
203 |
|
204 if (aEvent.iType == iEvent) |
|
205 { |
|
206 Cancel(); |
|
207 iWaiting = EFalse; |
|
208 CActiveScheduler::Stop(); |
|
209 return; |
|
210 } |
|
211 } |
|
212 |
|
213 CTestThreadOne::CTestThreadOne(TInt aClientNumber, TThreadPriority aThreadPriority, TInt aPriority) |
|
214 : CTestBase(aClientNumber, aThreadPriority, aPriority) |
|
215 { |
|
216 } |
|
217 |
|
218 CTestThreadOne* CTestThreadOne::NewL(TInt aClientNumber, const TDesC& aName, TThreadPriority aThreadPriority, TInt aPriority) |
|
219 { |
|
220 CTestThreadOne* self = new(ELeave) CTestThreadOne(aClientNumber, aThreadPriority, aPriority); |
|
221 CleanupStack::PushL(self); |
|
222 self->ConstructL(aName); |
|
223 CleanupStack::Pop(self); |
|
224 return self; |
|
225 } |
|
226 |
|
227 void CTestThreadOne::OpenDatabaseL() |
|
228 { |
|
229 TBuf<200> buf; |
|
230 buf.Format(_L("Opening database %S for thread 1 \n"), &KTestDatabaseFileU); |
|
231 iTest->Printf(buf); |
|
232 iDb = CContactDatabase::OpenL(KTestDatabaseFileU); |
|
233 } |
|
234 |
|
235 LOCAL_C void SetNameL(CContactItem& aItem,TUid aType,const TDesC& aName) |
|
236 // |
|
237 // Set the contents of a text field, creating the field if required |
|
238 // |
|
239 { |
|
240 CContactItemFieldSet& fieldSet=aItem.CardFields(); |
|
241 const TInt pos=fieldSet.Find(aType); |
|
242 if (pos!=KErrNotFound) |
|
243 fieldSet[pos].TextStorage()->SetTextL(aName); |
|
244 } |
|
245 |
|
246 void CTestThreadOne::DoTestL() |
|
247 { |
|
248 RSemaphore semaphore; |
|
249 TInt semCreateErr = semaphore.CreateGlobal(KTestSemaphoreName, 0); |
|
250 if (semCreateErr == KErrAlreadyExists) |
|
251 { |
|
252 User::LeaveIfError(semaphore.OpenGlobal(KTestSemaphoreName)); |
|
253 } |
|
254 else |
|
255 { |
|
256 User::LeaveIfError(semCreateErr); |
|
257 } |
|
258 CleanupClosePushL(semaphore); |
|
259 |
|
260 // Pause this thread while thread 2 sets up change notifier |
|
261 semaphore.Wait(); |
|
262 |
|
263 iTest->Printf(_L("Add new contact data\n")); |
|
264 |
|
265 CContactItem* item=CContactCard::NewLC(); |
|
266 SetNameL(*item,KUidContactFieldGivenName,KNewContactName); |
|
267 iDb->AddNewContactL(*item); |
|
268 |
|
269 // Pause this thread while thread 2 checks for update |
|
270 semaphore.Wait(); |
|
271 |
|
272 CleanupStack::PopAndDestroy(2,&semaphore); // item, semaphore |
|
273 } |
|
274 |
|
275 CTestThreadTwo::CTestThreadTwo(TInt aClientNumber, TThreadPriority aThreadPriority, TInt aPriority) |
|
276 : CTestBase(aClientNumber, aThreadPriority, aPriority) |
|
277 { |
|
278 } |
|
279 |
|
280 CTestThreadTwo* CTestThreadTwo::NewL(TInt aClientNumber, const TDesC& aName, TThreadPriority aThreadPriority, TInt aPriority) |
|
281 { |
|
282 CTestThreadTwo* self = new(ELeave) CTestThreadTwo(aClientNumber, aThreadPriority, aPriority); |
|
283 CleanupStack::PushL(self); |
|
284 self->ConstructL(aName); |
|
285 CleanupStack::Pop(self); |
|
286 return self; |
|
287 } |
|
288 |
|
289 void CTestThreadTwo::OpenDatabaseL() |
|
290 { |
|
291 TBuf<200> buf; |
|
292 buf.Format(_L("Opening database %S for thread 2 \n"), &KTestDatabaseFileL); |
|
293 iTest->Printf(buf); |
|
294 iDb = CContactDatabase::OpenL(KTestDatabaseFileL); |
|
295 } |
|
296 |
|
297 void CTestThreadTwo::DoTestL() |
|
298 { |
|
299 RSemaphore semaphore; |
|
300 TInt semCreateErr = semaphore.CreateGlobal(KTestSemaphoreName, 0); |
|
301 if (semCreateErr == KErrAlreadyExists) |
|
302 { |
|
303 User::LeaveIfError(semaphore.OpenGlobal(KTestSemaphoreName)); |
|
304 } |
|
305 else |
|
306 { |
|
307 User::LeaveIfError(semCreateErr); |
|
308 } |
|
309 CleanupClosePushL(semaphore); |
|
310 |
|
311 iTest->Printf(_L("Setting up thread 2 change notifier\n")); |
|
312 |
|
313 CContactChangeNotifier* changeNotifier = CContactChangeNotifier::NewL(*iDb, this); |
|
314 CleanupStack::PushL(changeNotifier); |
|
315 |
|
316 // Wait for change notifier to start |
|
317 User::After(2000000); |
|
318 |
|
319 // Signal thread 1 |
|
320 semaphore.Signal(); |
|
321 |
|
322 // This will wait for it |
|
323 iTest->Printf(_L("\tWaiting for change notification...\n")); |
|
324 WaitForContactsEvent(EContactDbObserverEventContactAdded); |
|
325 iTest->Printf(_L("\tNotification complete...\n")); |
|
326 CleanupStack::PopAndDestroy(changeNotifier); // changeNotifier |
|
327 |
|
328 // Signal thread 1 |
|
329 semaphore.Signal(); |
|
330 |
|
331 CleanupStack::PopAndDestroy(&semaphore); // semaphore |
|
332 } |
|
333 |
|
334 CTestManager::CTestManager(TInt aPriority) |
|
335 : CTimer(aPriority) |
|
336 { |
|
337 CActiveScheduler::Add(this); |
|
338 } |
|
339 |
|
340 CTestManager::~CTestManager() |
|
341 { |
|
342 Cancel(); |
|
343 delete iClient1; |
|
344 delete iClient2; |
|
345 } |
|
346 |
|
347 void CTestManager::ConstructL() |
|
348 { |
|
349 CTimer::ConstructL(); |
|
350 |
|
351 iClient2 = CTestThreadTwo::NewL(2, _L("Client-2"), EPriorityLess); // lower |
|
352 iClient1 = CTestThreadOne::NewL(1, _L("Client-1"), EPriorityMore); |
|
353 IssueTimerRequest(); |
|
354 } |
|
355 |
|
356 CTestManager* CTestManager::NewL() |
|
357 { |
|
358 CTestManager* self = NewLC(); |
|
359 CleanupStack::Pop(self); |
|
360 return self; |
|
361 } |
|
362 |
|
363 CTestManager* CTestManager::NewLC() |
|
364 { |
|
365 CTestManager* self = new (ELeave) CTestManager(); |
|
366 CleanupStack::PushL(self); |
|
367 self->ConstructL(); |
|
368 return self; |
|
369 } |
|
370 |
|
371 void CTestManager::RunL() |
|
372 { |
|
373 |
|
374 if ( iClient1->IsActive() && iClient2->IsActive() |
|
375 && !(iClient1->Waiting() || iClient2->Waiting())) |
|
376 { |
|
377 iClient1->Complete(1); |
|
378 iClient2->Complete(1); |
|
379 } |
|
380 if ( iClient1->TestFinished() && iClient2->TestFinished()) |
|
381 { |
|
382 //tests are finished so stop active schedular |
|
383 CActiveScheduler::Stop(); |
|
384 return; // prevent from starting timer again |
|
385 } |
|
386 |
|
387 // Re-issue the asynchronous request to the CTimer |
|
388 IssueTimerRequest(); |
|
389 } |
|
390 |
|
391 void CTestManager::IssueTimerRequest() |
|
392 { |
|
393 const TInt KOneSecond = 1000000; |
|
394 After(KOneSecond); |
|
395 } |
|
396 |
|
397 static void doMainL() |
|
398 { |
|
399 CTestActiveScheduler* scheduler = new (ELeave) CTestActiveScheduler; |
|
400 CleanupStack::PushL(scheduler); |
|
401 CActiveScheduler::Install(scheduler); |
|
402 |
|
403 // Replace contacts, using two styles of capitalisation |
|
404 CContactDatabase* db; |
|
405 |
|
406 db = CContactDatabase::ReplaceL(KTestDatabaseFileU); |
|
407 delete db; |
|
408 db = NULL; |
|
409 |
|
410 db = CContactDatabase::ReplaceL(KTestDatabaseFileL); |
|
411 delete db; |
|
412 db = NULL; |
|
413 |
|
414 // Secure Server waits 5s from last connection |
|
415 // to write the .ini file and close |
|
416 User::After(6000000); |
|
417 // Delete any existing ini file so that we can be sure this test is ok |
|
418 RPIMTestServer serv; |
|
419 User::LeaveIfError(serv.Connect()); |
|
420 serv.DeleteFileL(KContactsModelIniFile); |
|
421 serv.Close(); |
|
422 |
|
423 // create test manager |
|
424 CTestManager::NewLC(); |
|
425 // run tests |
|
426 CActiveScheduler::Start(); |
|
427 // clean up |
|
428 CleanupStack::PopAndDestroy(2,scheduler); // scheduler, tester |
|
429 } |
|
430 |
|
431 |
|
432 /** |
|
433 |
|
434 @SYMTestCaseID PIM-T-CASESENSITIVEDB-0001 |
|
435 |
|
436 */ |
|
437 |
|
438 GLDEF_C TInt E32Main() |
|
439 { |
|
440 __UHEAP_MARK; |
|
441 TheTest.Start(_L("@SYMTESTCaseID:PIM-T-CASESENSITIVEDB-0001 Multi session case sensitive testcode")); |
|
442 |
|
443 TheTest.Title(); |
|
444 TheCleanup = CTrapCleanup::New(); |
|
445 TRAPD(ret, doMainL()); |
|
446 TheTest(ret == KErrNone); |
|
447 delete TheCleanup; |
|
448 |
|
449 TheTest.End(); |
|
450 TheTest.Close(); |
|
451 __UHEAP_MARKEND; |
|
452 return(KErrNone); |
|
453 } |
|
454 |